Skip to content

Commit

Permalink
Use LLVM_VERSION_MAJOR from llvm-config.h instead of inferred LLVM_MA…
Browse files Browse the repository at this point in the history
…JOR_VERSION

Instead of inferring this from within the CMakeList.txt file, we can pull it
directly from llvm-config.h.
  • Loading branch information
chantra committed Sep 13, 2023
1 parent ec49363 commit 27c1361
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 40 deletions.
3 changes: 0 additions & 3 deletions src/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ if (NOT HAVE_REALLOCARRAY_SUPPORT)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY")
endif()

string(REGEX MATCH "^([0-9]+).*" _ ${LLVM_PACKAGE_VERSION})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLLVM_MAJOR_VERSION=${CMAKE_MATCH_1}")

include(static_libstdc++)

if(LIBBPF_INCLUDE_DIR)
Expand Down
20 changes: 10 additions & 10 deletions src/cc/bcc_debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <tuple>
#include <vector>

#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
#include <llvm/DebugInfo/DWARF/DWARFCompileUnit.h>
#endif
#include <llvm/DebugInfo/DWARF/DWARFContext.h>
Expand All @@ -32,10 +32,10 @@
#include <llvm/MC/MCInstrInfo.h>
#include <llvm/MC/MCObjectFileInfo.h>
#include <llvm/MC/MCRegisterInfo.h>
#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
#include <llvm/MC/MCSubtargetInfo.h>
#endif
#if LLVM_MAJOR_VERSION >= 14
#if LLVM_VERSION_MAJOR >= 14
#include <llvm/MC/TargetRegistry.h>
#else
#include <llvm/Support/TargetRegistry.h>
Expand Down Expand Up @@ -127,7 +127,7 @@ void SourceDebugger::dump() {
errs() << "Debug Error: cannot get register info\n";
return;
}
#if LLVM_MAJOR_VERSION >= 10
#if LLVM_VERSION_MAJOR >= 10
MCTargetOptions MCOptions;
std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TripleStr, MCOptions));
#else
Expand All @@ -141,7 +141,7 @@ void SourceDebugger::dump() {
std::unique_ptr<MCSubtargetInfo> STI(
T->createMCSubtargetInfo(TripleStr, "", ""));
MCObjectFileInfo MOFI;
#if LLVM_MAJOR_VERSION >= 13
#if LLVM_VERSION_MAJOR >= 13
MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), nullptr);
Ctx.setObjectFileInfo(&MOFI);
MOFI.initMCObjectFileInfo(Ctx, false, false);
Expand Down Expand Up @@ -176,7 +176,7 @@ void SourceDebugger::dump() {

// bcc has only one compilation unit
// getCompileUnitAtIndex() was gone in llvm 8.0 (https://reviews.llvm.org/D49741)
#if LLVM_MAJOR_VERSION >= 8
#if LLVM_VERSION_MAJOR >= 8
DWARFCompileUnit *CU = cast<DWARFCompileUnit>(DwarfCtx->getUnitAtIndex(0));
#else
DWARFCompileUnit *CU = DwarfCtx->getCompileUnitAtIndex(0);
Expand All @@ -202,7 +202,7 @@ void SourceDebugger::dump() {
uint64_t Size;
uint8_t *FuncStart = info.start_;
uint64_t FuncSize = info.size_;
#if LLVM_MAJOR_VERSION >= 9
#if LLVM_VERSION_MAJOR >= 9
auto section = sections_.find(info.section_);
if (section == sections_.end()) {
errs() << "Debug Error: no section entry for section " << info.section_
Expand All @@ -219,7 +219,7 @@ void SourceDebugger::dump() {
string src_dbg_str;
llvm::raw_string_ostream os(src_dbg_str);
for (uint64_t Index = 0; Index < FuncSize; Index += Size) {
#if LLVM_MAJOR_VERSION >= 10
#if LLVM_VERSION_MAJOR >= 10
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
#else
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index, nulls(),
Expand All @@ -232,7 +232,7 @@ void SourceDebugger::dump() {
DILineInfo LineInfo;

LineTable->getFileLineInfoForAddress(
#if LLVM_MAJOR_VERSION >= 9
#if LLVM_VERSION_MAJOR >= 9
{(uint64_t)FuncStart + Index, SectionID},
#else
(uint64_t)FuncStart + Index,
Expand All @@ -245,7 +245,7 @@ void SourceDebugger::dump() {
os);
os << format("%4" PRIu64 ":", Index >> 3) << '\t';
dumpBytes(Data.slice(Index, Size), os);
#if LLVM_MAJOR_VERSION >= 10
#if LLVM_VERSION_MAJOR >= 10
IP->printInst(&Inst, 0, "", *STI, os);
#else
IP->printInst(&Inst, os, "", *STI);
Expand Down
2 changes: 1 addition & 1 deletion src/cc/bcc_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SourceDebugger {
// on a set of in-memory sections with slightly different interfaces.
// FIXME: possibly to support 5.x
//
#if LLVM_MAJOR_VERSION >= 6
#if LLVM_VERSION_MAJOR >= 6
void dump();

private:
Expand Down
20 changes: 10 additions & 10 deletions src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

#include <fcntl.h>
#include <linux/bpf.h>
#if LLVM_MAJOR_VERSION <= 16
#if LLVM_VERSION_MAJOR <= 16
#include <llvm-c/Transforms/IPO.h>
#endif
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#if LLVM_MAJOR_VERSION >= 16
#if LLVM_VERSION_MAJOR >= 16
#include <llvm/IRPrinter/IRPrintingPasses.h>
#else
#include <llvm/IR/IRPrintingPasses.h>
#endif
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>

#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
#include <llvm/Pass.h>
#include <llvm/IR/PassManager.h>
#include <llvm/Passes/PassBuilder.h>
Expand All @@ -45,7 +45,7 @@
#include <llvm/Object/SymbolSize.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Transforms/IPO.h>
#if LLVM_MAJOR_VERSION <= 16
#if LLVM_VERSION_MAJOR <= 16
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#endif
#include <net/if.h>
Expand Down Expand Up @@ -122,7 +122,7 @@ class MyMemoryManager : public SectionMemoryManager {
if (!section)
continue;

#if LLVM_MAJOR_VERSION >= 10
#if LLVM_VERSION_MAJOR >= 10
auto sec_name = section.get()->getName();
if (!sec_name)
continue;
Expand Down Expand Up @@ -159,7 +159,7 @@ BPFModule::BPFModule(unsigned flags, TableStorage *ts, bool rw_engine_enabled,
LLVMInitializeBPFTargetMC();
LLVMInitializeBPFTargetInfo();
LLVMInitializeBPFAsmPrinter();
#if LLVM_MAJOR_VERSION >= 6
#if LLVM_VERSION_MAJOR >= 6
LLVMInitializeBPFAsmParser();
if (flags & DEBUG_SOURCE)
LLVMInitializeBPFDisassembler();
Expand Down Expand Up @@ -254,7 +254,7 @@ void BPFModule::annotate_light() {
}

void BPFModule::dump_ir(Module &mod) {
#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
// Create the analysis managers
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
Expand Down Expand Up @@ -287,7 +287,7 @@ int BPFModule::run_pass_manager(Module &mod) {
return -1;
}

#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
// Create the analysis managers
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
Expand Down Expand Up @@ -552,7 +552,7 @@ int BPFModule::finalize() {
*sections_p;

mod->setTargetTriple("bpf-pc-linux");
#if LLVM_MAJOR_VERSION >= 11
#if LLVM_VERSION_MAJOR >= 11
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
mod->setDataLayout("e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
#else
Expand All @@ -573,7 +573,7 @@ int BPFModule::finalize() {
builder.setMCJITMemoryManager(
ebpf::make_unique<MyMemoryManager>(sections_p, &*prog_func_info_));
builder.setMArch("bpf");
#if LLVM_MAJOR_VERSION <= 11
#if LLVM_VERSION_MAJOR <= 11
builder.setUseOrcMCJITReplacement(false);
#endif
engine_ = unique_ptr<ExecutionEngine>(builder.create());
Expand Down
2 changes: 2 additions & 0 deletions src/cc/bpf_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <string>
#include <vector>

#include <llvm/Config/llvm-config.h>

#include "bcc_exception.h"
#include "table_storage.h"

Expand Down
12 changes: 6 additions & 6 deletions src/cc/bpf_module_rw_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ void BPFModule::cleanup_rw_engine() {

static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false)
{
#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
if (isa<AllocaInst>(addr))
return B.CreateLoad(dyn_cast<AllocaInst>(addr)->getAllocatedType(), addr, isVolatile);
else
return B.CreateLoad(addr->getType(), addr, isVolatile);
#elif LLVM_MAJOR_VERSION >= 13
#elif LLVM_VERSION_MAJOR >= 13
return B.CreateLoad(addr->getType()->getPointerElementType(), addr, isVolatile);
#else
return B.CreateLoad(addr, isVolatile);
Expand All @@ -63,12 +63,12 @@ static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false

static Value *createInBoundsGEP(IRBuilder<> &B, Value *ptr, ArrayRef<Value *>idxlist)
{
#if LLVM_MAJOR_VERSION >= 15
#if LLVM_VERSION_MAJOR >= 15
if (isa<GlobalValue>(ptr))
return B.CreateInBoundsGEP(dyn_cast<GlobalValue>(ptr)->getValueType(), ptr, idxlist);
else
return B.CreateInBoundsGEP(ptr->getType(), ptr, idxlist);
#elif LLVM_MAJOR_VERSION >= 13
#elif LLVM_VERSION_MAJOR >= 13
return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
ptr, idxlist);
#else
Expand Down Expand Up @@ -385,7 +385,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
string err;
EngineBuilder builder(move(m));
builder.setErrorStr(&err);
#if LLVM_MAJOR_VERSION <= 11
#if LLVM_VERSION_MAJOR <= 11
builder.setUseOrcMCJITReplacement(false);
#endif
auto engine = unique_ptr<ExecutionEngine>(builder.create());
Expand All @@ -410,7 +410,7 @@ int BPFModule::annotate() {
table_names_[table.name] = id++;
GlobalValue *gvar = mod_->getNamedValue(table.name);
if (!gvar) continue;
#if LLVM_MAJOR_VERSION >= 14
#if LLVM_VERSION_MAJOR >= 14
{
Type *t = gvar->getValueType();
StructType *st = dyn_cast<StructType>(t);
Expand Down
10 changes: 5 additions & 5 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ bool ProbeVisitor::IsContextMemberExpr(Expr *E) {

SourceRange
ProbeVisitor::expansionRange(SourceRange range) {
#if LLVM_MAJOR_VERSION >= 7
#if LLVM_VERSION_MAJOR >= 7
return rewriter_.getSourceMgr().getExpansionRange(range).getAsRange();
#else
return rewriter_.getSourceMgr().getExpansionRange(range);
Expand Down Expand Up @@ -1387,7 +1387,7 @@ bool BTypeVisitor::VisitImplicitCastExpr(ImplicitCastExpr *E) {

SourceRange
BTypeVisitor::expansionRange(SourceRange range) {
#if LLVM_MAJOR_VERSION >= 7
#if LLVM_VERSION_MAJOR >= 7
return rewriter_.getSourceMgr().getExpansionRange(range).getAsRange();
#else
return rewriter_.getSourceMgr().getExpansionRange(range);
Expand All @@ -1410,7 +1410,7 @@ int64_t BTypeVisitor::getFieldValue(VarDecl *Decl, FieldDecl *FDecl, int64_t Ori
unsigned idx = FDecl->getFieldIndex();

if (auto I = dyn_cast_or_null<InitListExpr>(Decl->getInit())) {
#if LLVM_MAJOR_VERSION >= 8
#if LLVM_VERSION_MAJOR >= 8
Expr::EvalResult res;
if (I->getInit(idx)->EvaluateAsInt(res, C)) {
return res.Val.getInt().getExtValue();
Expand Down Expand Up @@ -1809,7 +1809,7 @@ void BFrontendAction::DoMiscWorkAround() {
false);

rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).InsertTextAfter(
#if LLVM_MAJOR_VERSION >= 12
#if LLVM_VERSION_MAJOR >= 12
rewriter_->getSourceMgr().getBufferOrFake(rewriter_->getSourceMgr().getMainFileID()).getBufferSize(),
#else
rewriter_->getSourceMgr().getBuffer(rewriter_->getSourceMgr().getMainFileID())->getBufferSize(),
Expand All @@ -1823,7 +1823,7 @@ void BFrontendAction::EndSourceFileAction() {

if (flags_ & DEBUG_PREPROCESSOR)
rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(llvm::errs());
#if LLVM_MAJOR_VERSION >= 9
#if LLVM_VERSION_MAJOR >= 9
llvm::raw_string_ostream tmp_os(mod_src_);
rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID())
.write(tmp_os);
Expand Down
4 changes: 3 additions & 1 deletion src/cc/frontends/clang/frontend_action_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if LLVM_MAJOR_VERSION >= 8
#include <llvm/Config/llvm-config.h>

#if LLVM_VERSION_MAJOR >= 8
#define GET_BEGINLOC(E) ((E)->getBeginLoc())
#define GET_ENDLOC(E) ((E)->getEndLoc())
#else
Expand Down
6 changes: 3 additions & 3 deletions src/cc/frontends/clang/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static int CreateFromArgs(clang::CompilerInvocation &invocation,
const llvm::opt::ArgStringList &ccargs,
clang::DiagnosticsEngine &diags)
{
#if LLVM_MAJOR_VERSION >= 10
#if LLVM_VERSION_MAJOR >= 10
return clang::CompilerInvocation::CreateFromArgs(invocation, ccargs, diags);
#else
return clang::CompilerInvocation::CreateFromArgs(
Expand Down Expand Up @@ -284,7 +284,7 @@ int ClangLoader::parse(
vector<string> kflags;
if (kbuild_helper.get_flags(un.machine, &kflags))
return -1;
#if LLVM_MAJOR_VERSION >= 9
#if LLVM_VERSION_MAJOR >= 9
flags_cstr.push_back("-g");
flags_cstr.push_back("-gdwarf-4");
#else
Expand Down Expand Up @@ -412,7 +412,7 @@ int ClangLoader::do_compile(
string target_triple = get_clang_target();
driver::Driver drv("", target_triple, diags);

#if LLVM_MAJOR_VERSION >= 4
#if LLVM_VERSION_MAJOR >= 4
if (target_triple == "x86_64-unknown-linux-gnu" || target_triple == "aarch64-unknown-linux-gnu")
flags_cstr.push_back("-fno-jump-tables");
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/cc/json_map_decl_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <clang/AST/RecordLayout.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/Config/llvm-config.h>
#include "common.h"
#include "table_desc.h"

Expand Down Expand Up @@ -80,7 +81,7 @@ void BMapDeclVisitor::genJSONForField(FieldDecl *F) {
result_ += "[";
TraverseDecl(F);
if (const ConstantArrayType *T = dyn_cast<ConstantArrayType>(F->getType()))
#if LLVM_MAJOR_VERSION >= 13
#if LLVM_VERSION_MAJOR >= 13
result_ += ", [" + toString(T->getSize(), 10, false) + "]";
#else
result_ += ", [" + T->getSize().toString(10, false) + "]";
Expand Down

0 comments on commit 27c1361

Please sign in to comment.