Skip to content

Commit

Permalink
fix llvm 11 compilation issues
Browse files Browse the repository at this point in the history
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<llvm::Value*, std::allocator<llvm::Value*> >&)’
     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<char>::size_type)’
   B.CreateMemCpy(ptr, 1, global, 1, n->val_.size() + 1);
                                                       ^
  ...

This patch fixed the compilation issue.
  • Loading branch information
yonghong-song committed Apr 20, 2020
1 parent d3359b2 commit 45e63f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/cc/frontends/b/codegen_llvm.cc
Expand Up @@ -112,6 +112,17 @@ void CodegenLLVM::emit(const char *s) {
//fflush(out_);
}

CallInst *CodegenLLVM::createCall(Value *callee, ArrayRef<Value *> args)
{
#if LLVM_MAJOR_VERSION >= 11
auto *calleePtrType = cast<PointerType>(callee->getType());
auto *calleeType = cast<FunctionType>(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
Expand Down Expand Up @@ -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<Value *>({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7),
createCall(store_fn, vector<Value *>({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);
Expand All @@ -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<Value *>({skb_ptr8, skb_hdr_offset,
expr_ = createCall(load_fn, vector<Value *>({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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
CallInst *pseudo_call = createCall(pseudo_fn, vector<Value *>({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<Value *>({pseudo_map_fd, key_ptr}));
expr_ = createCall(lookup_fn, vector<Value *>({pseudo_map_fd, key_ptr}));

if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED") {
if (n->args_.size() == 2) {
Expand Down Expand Up @@ -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<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
CallInst *pseudo_call = createCall(pseudo_fn, vector<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
B.getInt64(table_fd_it->second)}));
Value *pseudo_map_fd = pseudo_call;

Expand All @@ -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<Value *>({pseudo_map_fd, key_ptr, value_ptr, B.getInt64(BPF_ANY)}));
expr_ = createCall(update_fn, vector<Value *>({pseudo_map_fd, key_ptr, value_ptr, B.getInt64(BPF_ANY)}));
} else {
return mkstatus_(n, "unsupported");
}
Expand All @@ -656,15 +669,15 @@ 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<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
CallInst *pseudo_call = createCall(pseudo_fn, vector<Value *>({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());

if (table->type_id()->name_ == "FIXED_MATCH" || table->type_id()->name_ == "INDEXED") {
expr_ = B.CreateCall(update_fn, vector<Value *>({pseudo_map_fd, key_ptr}));
expr_ = createCall(update_fn, vector<Value *>({pseudo_map_fd, key_ptr}));
} else {
return mkstatus_(n, "unsupported");
}
Expand All @@ -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();
}

Expand Down Expand Up @@ -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<Value *>({skb_ptr8, offset, old_val, new_val, flags}));
expr_ = createCall(csum_fn, vector<Value *>({skb_ptr8, offset, old_val, new_val, flags}));
return StatusTuple::OK();
}

Expand Down Expand Up @@ -797,15 +810,15 @@ 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<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
CallInst *pseudo_call = createCall(pseudo_fn, vector<Value *>({B.getInt64(BPF_PSEUDO_MAP_FD),
B.getInt64(table_fd_it->second)}));
Value *pseudo_map_fd = pseudo_call;

TRY2(n->index_->accept(this));
Value *key_ptr = B.CreateBitCast(pop_expr(), B.getInt8PtrTy());

// result = lookup(key)
Value *lookup1 = B.CreateBitCast(B.CreateCall(lookup_fn, vector<Value *>({pseudo_map_fd, key_ptr})), leaf_ptype);
Value *lookup1 = B.CreateBitCast(createCall(lookup_fn, vector<Value *>({pseudo_map_fd, key_ptr})), leaf_ptype);

Value *result = nullptr;
if (n->table_->policy_id()->name_ == "AUTO") {
Expand All @@ -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<Value *>({pseudo_map_fd, key_ptr, leaf_ptr, B.getInt64(BPF_NOEXIST)}));
createCall(update_fn, vector<Value *>({pseudo_map_fd, key_ptr, leaf_ptr, B.getInt64(BPF_NOEXIST)}));

// result = lookup(key)
Value *lookup2 = B.CreateBitCast(B.CreateCall(lookup_fn, vector<Value *>({pseudo_map_fd, key_ptr})), leaf_ptype);
Value *lookup2 = B.CreateBitCast(createCall(lookup_fn, vector<Value *>({pseudo_map_fd, key_ptr})), leaf_ptype);
B.CreateBr(label_end);

B.SetInsertPoint(label_end);
Expand Down
4 changes: 4 additions & 0 deletions src/cc/frontends/b/codegen_llvm.h
Expand Up @@ -27,8 +27,10 @@

namespace llvm {
class AllocaInst;
template<typename T> class ArrayRef;
class BasicBlock;
class BranchInst;
class CallInst;
class Constant;
class Instruction;
class IRBuilderBase;
Expand Down Expand Up @@ -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<llvm::Value *> Args);

template <typename... Args> void emit(const char *fmt, Args&&... params);
void emit(const char *s);
Expand Down

0 comments on commit 45e63f2

Please sign in to comment.