From 45e63f2b316cdce2d8cc925f6f14a8726ade9ff6 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Mon, 20 Apr 2020 11:29:18 -0700 Subject: [PATCH] fix llvm 11 compilation issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The llvm CreateCall used in bcc is deprecated in llvm 11: https://reviews.llvm.org/D76269 The llvm CreateMemCpy is changed in llvm 11 as well: https://reviews.llvm.org/D71473 This caused bcc compilation error. /home/yhs/work/bcc/src/cc/frontends/b/codegen_llvm.cc: In member function ‘ebpf::StatusTuple ebpf::cc::CodegenLLVM::emit_log(ebpf::cc::Method CallExprNode*)’: /home/yhs/work/bcc/src/cc/frontends/b/codegen_llvm.cc:691:39: error: no matching function for call to ‘llvm::IRBuilder<>::CreateCall(llvm::Value*&, std::vector >&)’ expr_ = B.CreateCall(printk_fn, args); ^ ... /home/yhs/work/bcc/src/cc/frontends/b/codegen_llvm.cc: In member function ‘virtual ebpf::StatusTuple ebpf::cc::CodegenLLVM::visit_string_exp_node(ebpf::cc::StringExprNode*)’: /home/yhs/work/bcc/src/cc/frontends/b/codegen_llvm.cc:440:55: error: no matching function for call to ‘llvm::IRBuilder<>::CreateMemCpy(llvm:Value*&, int, llvm::Value*&, int, std::__cxx11::basic_string::size_type)’ B.CreateMemCpy(ptr, 1, global, 1, n->val_.size() + 1); ^ ... This patch fixed the compilation issue. --- src/cc/frontends/b/codegen_llvm.cc | 43 +++++++++++++++++++----------- src/cc/frontends/b/codegen_llvm.h | 4 +++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/cc/frontends/b/codegen_llvm.cc b/src/cc/frontends/b/codegen_llvm.cc index fefd4d551ea8..d8c9470a73ae 100644 --- a/src/cc/frontends/b/codegen_llvm.cc +++ b/src/cc/frontends/b/codegen_llvm.cc @@ -112,6 +112,17 @@ void CodegenLLVM::emit(const char *s) { //fflush(out_); } +CallInst *CodegenLLVM::createCall(Value *callee, ArrayRef args) +{ +#if LLVM_MAJOR_VERSION >= 11 + auto *calleePtrType = cast(callee->getType()); + auto *calleeType = cast(calleePtrType->getElementType()); + return B.CreateCall(calleeType, callee, args); +#else + return B.CreateCall(callee, args); +#endif +} + StatusTuple CodegenLLVM::visit_block_stmt_node(BlockStmtNode *n) { // enter scope @@ -386,7 +397,7 @@ StatusTuple CodegenLLVM::visit_packet_expr_node(PacketExprNode *n) { LoadInst *offset_ptr = B.CreateLoad(offset_mem); Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3)); Value *rhs = B.CreateIntCast(pop_expr(), B.getInt64Ty(), false); - B.CreateCall(store_fn, vector({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7), + createCall(store_fn, vector({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7), B.getInt64(bit_width), rhs})); } else { emit("bpf_dext_pkt(pkt, %s + %zu, %zu, %zu)", n->id_->c_str(), bit_offset >> 3, bit_offset & 0x7, bit_width); @@ -396,7 +407,7 @@ StatusTuple CodegenLLVM::visit_packet_expr_node(PacketExprNode *n) { Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy()); LoadInst *offset_ptr = B.CreateLoad(offset_mem); Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3)); - expr_ = B.CreateCall(load_fn, vector({skb_ptr8, skb_hdr_offset, + expr_ = createCall(load_fn, vector({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7), B.getInt64(bit_width)})); // this generates extra trunc insns whereas the bpf.load fns already // trunc the values internally in the bpf interpreter @@ -425,7 +436,9 @@ StatusTuple CodegenLLVM::visit_string_expr_node(StringExprNode *n) { Value *global = B.CreateGlobalString(n->val_); Value *ptr = make_alloca(resolve_entry_stack(), B.getInt8Ty(), "", B.getInt64(n->val_.size() + 1)); -#if LLVM_MAJOR_VERSION >= 7 +#if LLVM_MAJOR_VERSION >= 11 + B.CreateMemCpy(ptr, Align(1), global, Align(1), n->val_.size() + 1); +#elif LLVM_MAJOR_VERSION >= 7 B.CreateMemCpy(ptr, 1, global, 1, n->val_.size() + 1); #else B.CreateMemCpy(ptr, global, n->val_.size() + 1, 1); @@ -585,14 +598,14 @@ StatusTuple CodegenLLVM::emit_table_lookup(MethodCallExprNode *n) { Function *lookup_fn = mod_->getFunction("bpf_map_lookup_elem_"); if (!lookup_fn) return mkstatus_(n, "bpf_map_lookup_elem_ undefined"); - CallInst *pseudo_call = B.CreateCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), + CallInst *pseudo_call = createCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), B.getInt64(table_fd_it->second)})); Value *pseudo_map_fd = pseudo_call; TRY2(arg0->accept(this)); Value *key_ptr = B.CreateBitCast(pop_expr(), B.getInt8PtrTy()); - expr_ = B.CreateCall(lookup_fn, vector({pseudo_map_fd, key_ptr})); + expr_ = createCall(lookup_fn, vector({pseudo_map_fd, key_ptr})); if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED") { if (n->args_.size() == 2) { @@ -626,7 +639,7 @@ StatusTuple CodegenLLVM::emit_table_update(MethodCallExprNode *n) { Function *update_fn = mod_->getFunction("bpf_map_update_elem_"); if (!update_fn) return mkstatus_(n, "bpf_map_update_elem_ undefined"); - CallInst *pseudo_call = B.CreateCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), + CallInst *pseudo_call = createCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), B.getInt64(table_fd_it->second)})); Value *pseudo_map_fd = pseudo_call; @@ -637,7 +650,7 @@ StatusTuple CodegenLLVM::emit_table_update(MethodCallExprNode *n) { TRY2(arg1->accept(this)); Value *value_ptr = B.CreateBitCast(pop_expr(), B.getInt8PtrTy()); - expr_ = B.CreateCall(update_fn, vector({pseudo_map_fd, key_ptr, value_ptr, B.getInt64(BPF_ANY)})); + expr_ = createCall(update_fn, vector({pseudo_map_fd, key_ptr, value_ptr, B.getInt64(BPF_ANY)})); } else { return mkstatus_(n, "unsupported"); } @@ -656,7 +669,7 @@ StatusTuple CodegenLLVM::emit_table_delete(MethodCallExprNode *n) { Function *update_fn = mod_->getFunction("bpf_map_update_elem_"); if (!update_fn) return mkstatus_(n, "bpf_map_update_elem_ undefined"); - CallInst *pseudo_call = B.CreateCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), + CallInst *pseudo_call = createCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), B.getInt64(table_fd_it->second)})); Value *pseudo_map_fd = pseudo_call; @@ -664,7 +677,7 @@ StatusTuple CodegenLLVM::emit_table_delete(MethodCallExprNode *n) { Value *key_ptr = B.CreateBitCast(pop_expr(), B.getInt8PtrTy()); if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED") { - expr_ = B.CreateCall(update_fn, vector({pseudo_map_fd, key_ptr})); + expr_ = createCall(update_fn, vector({pseudo_map_fd, key_ptr})); } else { return mkstatus_(n, "unsupported"); } @@ -688,7 +701,7 @@ StatusTuple CodegenLLVM::emit_log(MethodCallExprNode *n) { Value *printk_fn = B.CreateIntToPtr(B.getInt64(BPF_FUNC_trace_printk), PointerType::getUnqual(printk_fn_type)); - expr_ = B.CreateCall(printk_fn, args); + expr_ = createCall(printk_fn, args); return StatusTuple::OK(); } @@ -742,7 +755,7 @@ StatusTuple CodegenLLVM::emit_incr_cksum(MethodCallExprNode *n, size_t sz) { LoadInst *skb_ptr = B.CreateLoad(skb_mem); Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy()); - expr_ = B.CreateCall(csum_fn, vector({skb_ptr8, offset, old_val, new_val, flags})); + expr_ = createCall(csum_fn, vector({skb_ptr8, offset, old_val, new_val, flags})); return StatusTuple::OK(); } @@ -797,7 +810,7 @@ StatusTuple CodegenLLVM::visit_table_index_expr_node(TableIndexExprNode *n) { TRY2(lookup_struct_type(n->table_->leaf_type_, &leaf_type)); PointerType *leaf_ptype = PointerType::getUnqual(leaf_type); - CallInst *pseudo_call = B.CreateCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), + CallInst *pseudo_call = createCall(pseudo_fn, vector({B.getInt64(BPF_PSEUDO_MAP_FD), B.getInt64(table_fd_it->second)})); Value *pseudo_map_fd = pseudo_call; @@ -805,7 +818,7 @@ StatusTuple CodegenLLVM::visit_table_index_expr_node(TableIndexExprNode *n) { Value *key_ptr = B.CreateBitCast(pop_expr(), B.getInt8PtrTy()); // result = lookup(key) - Value *lookup1 = B.CreateBitCast(B.CreateCall(lookup_fn, vector({pseudo_map_fd, key_ptr})), leaf_ptype); + Value *lookup1 = B.CreateBitCast(createCall(lookup_fn, vector({pseudo_map_fd, key_ptr})), leaf_ptype); Value *result = nullptr; if (n->table_->policy_id()->name_ == "AUTO") { @@ -827,10 +840,10 @@ StatusTuple CodegenLLVM::visit_table_index_expr_node(TableIndexExprNode *n) { B.CreateMemSet(leaf_ptr, B.getInt8(0), B.getInt64(n->table_->leaf_id()->bit_width_ >> 3), 1); #endif // update(key, leaf) - B.CreateCall(update_fn, vector({pseudo_map_fd, key_ptr, leaf_ptr, B.getInt64(BPF_NOEXIST)})); + createCall(update_fn, vector({pseudo_map_fd, key_ptr, leaf_ptr, B.getInt64(BPF_NOEXIST)})); // result = lookup(key) - Value *lookup2 = B.CreateBitCast(B.CreateCall(lookup_fn, vector({pseudo_map_fd, key_ptr})), leaf_ptype); + Value *lookup2 = B.CreateBitCast(createCall(lookup_fn, vector({pseudo_map_fd, key_ptr})), leaf_ptype); B.CreateBr(label_end); B.SetInsertPoint(label_end); diff --git a/src/cc/frontends/b/codegen_llvm.h b/src/cc/frontends/b/codegen_llvm.h index c2947f74ebae..d77c9de82f6e 100644 --- a/src/cc/frontends/b/codegen_llvm.h +++ b/src/cc/frontends/b/codegen_llvm.h @@ -27,8 +27,10 @@ namespace llvm { class AllocaInst; +template class ArrayRef; class BasicBlock; class BranchInst; +class CallInst; class Constant; class Instruction; class IRBuilderBase; @@ -104,6 +106,8 @@ class CodegenLLVM : public Visitor { StatusTuple lookup_struct_type(StructDeclStmtNode *decl, llvm::StructType **stype) const; StatusTuple lookup_struct_type(VariableDeclStmtNode *n, llvm::StructType **stype, StructDeclStmtNode **decl = nullptr) const; + llvm::CallInst *createCall(llvm::Value *Callee, + llvm::ArrayRef Args); template void emit(const char *fmt, Args&&... params); void emit(const char *s);