From c6bde0bfc3716b072854510a10e00fc8738a1517 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Wed, 6 Dec 2023 21:40:16 -0800 Subject: [PATCH] Fix various build issues with latest llvm18 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 --- CMakeLists.txt | 4 ++++ cmake/clang_libs.cmake | 7 +++++++ src/cc/bpf_module_rw_engine.cc | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index dfed06e5b623..18407e864523 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/cmake/clang_libs.cmake b/cmake/clang_libs.cmake index fe82f2fba94e..65696044324d 100644 --- a/cmake/clang_libs.cmake +++ b/cmake/clang_libs.cmake @@ -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}) @@ -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() diff --git a/src/cc/bpf_module_rw_engine.cc b/src/cc/bpf_module_rw_engine.cc index ec8d35d89508..4bdbf0257f37 100644 --- a/src/cc/bpf_module_rw_engine.cc +++ b/src/cc/bpf_module_rw_engine.cc @@ -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 fprintf_fn_args({B.getInt64Ty(), B.getPtrTy()}); +#else vector 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); @@ -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, @@ -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 fn_args({B.getPtrTy(), PointerType::getUnqual(type)}); +#else vector 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); @@ -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 locals{{"nread", nread}, {"sptr", sptr}}; B.CreateStore(arg_in, sptr); vector args({nullptr, nullptr}); @@ -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 fn_args({B.getPtrTy(), B.getInt64Ty(), PointerType::getUnqual(type)}); +#else vector 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); @@ -369,7 +389,11 @@ string BPFModule::make_writer(Module *mod, Type *type) { if (0) debug_printf(mod, B, "%d %p %p\n", vector({arg_len, arg_out, arg_in})); +#if LLVM_VERSION_MAJOR >= 18 + vector snprintf_fn_args({B.getPtrTy(), B.getInt64Ty(), B.getPtrTy()}); +#else vector 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)