Skip to content

Commit

Permalink
Fix various build issues with latest llvm18
Browse files Browse the repository at this point in the history
Latest llvm18 cannot build with bcc now. There are 3 issues:
  - In IRBuilder, getInt8PtrTy() renamed to getPtrTy().
  - Newly introdeuced clang library libclangAPINotes.a.
  - Newly introduced llvm library libLLVMFrontendDriver.a

This patch fixed the above three issues so bcc can be built
with llvm18 successfully. I then tried a few bcc tools and
they all works fine.

Signed-off-by: Yonghong Song <[email protected]>
  • Loading branch information
Yonghong Song authored and yonghong-song committed Dec 7, 2023
1 parent 0ae3b12 commit c6bde0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ if(NOT PYTHON_ONLY)
find_library(libclangSupport NAMES clangSupport clang-cpp HINTS ${CLANG_SEARCH})
endif()

if(${LLVM_PACKAGE_VERSION} VERSION_EQUAL 18 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 18)
find_library(libclangAPINotes NAMES clangAPINotes clang-cpp HINTS ${CLANG_SEARCH})
endif()

find_library(libclang-shared libclang-cpp.so HINTS ${CLANG_SEARCH})

if(libclangBasic STREQUAL "libclangBasic-NOTFOUND")
Expand Down
7 changes: 7 additions & 0 deletions cmake/clang_libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ endif()
if (${LLVM_PACKAGE_VERSION} VERSION_EQUAL 16 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 16)
list(APPEND llvm_raw_libs frontendhlsl)
endif()
if (${LLVM_PACKAGE_VERSION} VERSION_EQUAL 18 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 18)
list(APPEND llvm_raw_libs frontenddriver)
endif()

llvm_map_components_to_libnames(_llvm_libs ${llvm_raw_libs})
llvm_expand_dependencies(llvm_libs ${_llvm_libs})
Expand Down Expand Up @@ -60,6 +63,10 @@ list(APPEND clang_libs
list(APPEND clang_libs ${libclangSupport})
# endif()

if (${LLVM_PACKAGE_VERSION} VERSION_EQUAL 18 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 18)
list(APPEND clang_libs ${libclangAPINotes})
endif()

list(APPEND clang_libs
${libclangBasic})
endif()
Expand Down
24 changes: 24 additions & 0 deletions src/cc/bpf_module_rw_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<
args.insert(args.begin(), B.getInt64((uintptr_t)stderr));
Function *fprintf_fn = mod->getFunction("fprintf");
if (!fprintf_fn) {
#if LLVM_VERSION_MAJOR >= 18
vector<Type *> fprintf_fn_args({B.getInt64Ty(), B.getPtrTy()});
#else
vector<Type *> fprintf_fn_args({B.getInt64Ty(), B.getInt8PtrTy()});
#endif
FunctionType *fprintf_fn_type = FunctionType::get(B.getInt32Ty(), fprintf_fn_args, /*isvarArg=*/true);
fprintf_fn = Function::Create(fprintf_fn_type, GlobalValue::ExternalLinkage, "fprintf", mod);
fprintf_fn->setCallingConv(CallingConv::C);
Expand Down Expand Up @@ -267,7 +271,11 @@ string BPFModule::make_reader(Module *mod, Type *type) {
IRBuilder<> B(*ctx_);

FunctionType *sscanf_fn_type = FunctionType::get(
#if LLVM_VERSION_MAJOR >= 18
B.getInt32Ty(), {B.getPtrTy(), B.getPtrTy()}, /*isVarArg=*/true);
#else
B.getInt32Ty(), {B.getInt8PtrTy(), B.getInt8PtrTy()}, /*isVarArg=*/true);
#endif
Function *sscanf_fn = mod->getFunction("sscanf");
if (!sscanf_fn) {
sscanf_fn = Function::Create(sscanf_fn_type, GlobalValue::ExternalLinkage,
Expand All @@ -277,7 +285,11 @@ string BPFModule::make_reader(Module *mod, Type *type) {
}

string name = "reader" + std::to_string(readers_.size());
#if LLVM_VERSION_MAJOR >= 18
vector<Type *> fn_args({B.getPtrTy(), PointerType::getUnqual(type)});
#else
vector<Type *> fn_args({B.getInt8PtrTy(), PointerType::getUnqual(type)});
#endif
FunctionType *fn_type = FunctionType::get(B.getInt32Ty(), fn_args, /*isVarArg=*/false);
Function *fn =
Function::Create(fn_type, GlobalValue::ExternalLinkage, name, mod);
Expand All @@ -293,7 +305,11 @@ string BPFModule::make_reader(Module *mod, Type *type) {
B.SetInsertPoint(label_entry);

Value *nread = B.CreateAlloca(B.getInt32Ty());
#if LLVM_VERSION_MAJOR >= 18
Value *sptr = B.CreateAlloca(B.getPtrTy());
#else
Value *sptr = B.CreateAlloca(B.getInt8PtrTy());
#endif
map<string, Value *> locals{{"nread", nread}, {"sptr", sptr}};
B.CreateStore(arg_in, sptr);
vector<Value *> args({nullptr, nullptr});
Expand Down Expand Up @@ -337,7 +353,11 @@ string BPFModule::make_writer(Module *mod, Type *type) {
IRBuilder<> B(*ctx_);

string name = "writer" + std::to_string(writers_.size());
#if LLVM_VERSION_MAJOR >= 18
vector<Type *> fn_args({B.getPtrTy(), B.getInt64Ty(), PointerType::getUnqual(type)});
#else
vector<Type *> fn_args({B.getInt8PtrTy(), B.getInt64Ty(), PointerType::getUnqual(type)});
#endif
FunctionType *fn_type = FunctionType::get(B.getInt32Ty(), fn_args, /*isVarArg=*/false);
Function *fn =
Function::Create(fn_type, GlobalValue::ExternalLinkage, name, mod);
Expand Down Expand Up @@ -369,7 +389,11 @@ string BPFModule::make_writer(Module *mod, Type *type) {
if (0)
debug_printf(mod, B, "%d %p %p\n", vector<Value *>({arg_len, arg_out, arg_in}));

#if LLVM_VERSION_MAJOR >= 18
vector<Type *> snprintf_fn_args({B.getPtrTy(), B.getInt64Ty(), B.getPtrTy()});
#else
vector<Type *> snprintf_fn_args({B.getInt8PtrTy(), B.getInt64Ty(), B.getInt8PtrTy()});
#endif
FunctionType *snprintf_fn_type = FunctionType::get(B.getInt32Ty(), snprintf_fn_args, /*isVarArg=*/true);
Function *snprintf_fn = mod->getFunction("snprintf");
if (!snprintf_fn)
Expand Down

0 comments on commit c6bde0b

Please sign in to comment.