fix llvm compilation failure
authorYonghong Song <yhs@fb.com>
Mon, 6 Dec 2021 17:05:08 +0000 (09:05 -0800)
committeryonghong-song <ys114321@gmail.com>
Mon, 6 Dec 2021 18:05:00 +0000 (10:05 -0800)
Fix issue #3734

llvm upstream commit
  89eb85ac6eab [IRBuilder] Remove deprecated methods
deprecated some functions which are used by bcc.
Let us follow the above commit to use the underlying
implementation instead.

Note that I didn't create a common header for the newer
functions since b language support will be removed in
the near future.

Signed-off-by: Yonghong Song <yhs@fb.com>
src/cc/bpf_module_rw_engine.cc
src/cc/frontends/b/codegen_llvm.cc
src/cc/frontends/b/codegen_llvm.h

index 9890af699d0db6fe3304c852660ab37e25ebf53c..533d8a1322bcba5d6f227bd02b7abefb2353525f 100644 (file)
@@ -47,9 +47,28 @@ void BPFModule::cleanup_rw_engine() {
   rw_engine_.reset();
 }
 
+static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false)
+{
+#if LLVM_MAJOR_VERSION >= 14
+  return B.CreateLoad(addr->getType()->getPointerElementType(), addr, isVolatile);
+#else
+  return B.CreateLoad(addr, isVolatile);
+#endif
+}
+
+static Value *createInBoundsGEP(IRBuilder<> &B, Value *ptr, ArrayRef<Value *>idxlist)
+{
+#if LLVM_MAJOR_VERSION >= 14
+  return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
+                             ptr, idxlist);
+#else
+  return B.CreateInBoundsGEP(ptr, idxlist);
+#endif
+}
+
 static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) {
   GlobalVariable *fmt_gvar = B.CreateGlobalString(fmt, "fmt");
-  args.insert(args.begin(), B.CreateInBoundsGEP(fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)})));
+  args.insert(args.begin(), createInBoundsGEP(B, fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)})));
   args.insert(args.begin(), B.getInt64((uintptr_t)stderr));
   Function *fprintf_fn = mod->getFunction("fprintf");
   if (!fprintf_fn) {
@@ -76,8 +95,8 @@ static void finish_sscanf(IRBuilder<> &B, vector<Value *> *args, string *fmt,
   *fmt += "%n";
   B.CreateStore(B.getInt32(0), nread);
   GlobalVariable *fmt_gvar = B.CreateGlobalString(*fmt, "fmt");
-  (*args)[1] = B.CreateInBoundsGEP(fmt_gvar, {B.getInt64(0), B.getInt64(0)});
-  (*args)[0] = B.CreateLoad(sptr);
+  (*args)[1] = createInBoundsGEP(B, fmt_gvar, {B.getInt64(0), B.getInt64(0)});
+  (*args)[0] = createLoad(B, sptr);
   args->push_back(nread);
   CallInst *call = B.CreateCall(sscanf_fn, *args);
   call->setTailCall(true);
@@ -97,7 +116,7 @@ static void finish_sscanf(IRBuilder<> &B, vector<Value *> *args, string *fmt,
   B.SetInsertPoint(label_false);
   // s = &s[nread];
   B.CreateStore(
-      B.CreateInBoundsGEP(B.CreateLoad(sptr), B.CreateLoad(nread, true)), sptr);
+      createInBoundsGEP(B, createLoad(B, sptr), createLoad(B, nread, true)), sptr);
 
   args->resize(2);
   fmt->clear();
@@ -196,7 +215,7 @@ static void parse_type(IRBuilder<> &B, vector<Value *> *args, string *fmt,
       *fmt += "x";
     else
       *fmt += "i";
-    args->push_back(is_writer ? B.CreateLoad(out) : out);
+    args->push_back(is_writer ? createLoad(B, out) : out);
   }
 }
 
@@ -326,7 +345,7 @@ string BPFModule::make_writer(Module *mod, Type *type) {
 
   GlobalVariable *fmt_gvar = B.CreateGlobalString(fmt, "fmt");
 
-  args[2] = B.CreateInBoundsGEP(fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)}));
+  args[2] = createInBoundsGEP(B, fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)}));
 
   if (0)
     debug_printf(mod, B, "%d %p %p\n", vector<Value *>({arg_len, arg_out, arg_in}));
index 22991fa2d1a53d9f9f91c19521b41b442f3383d5..359303c417515a3f812e5398e166a76da13a80b5 100644 (file)
@@ -123,6 +123,25 @@ CallInst *CodegenLLVM::createCall(Value *callee, ArrayRef<Value *> args)
 #endif
 }
 
+LoadInst *CodegenLLVM::createLoad(Value *addr)
+{
+#if LLVM_MAJOR_VERSION >= 14
+  return B.CreateLoad(addr->getType()->getPointerElementType(), addr);
+#else
+  return B.CreateLoad(addr);
+#endif
+}
+
+Value *CodegenLLVM::createInBoundsGEP(Value *ptr, ArrayRef<Value *>idxlist)
+{
+#if LLVM_MAJOR_VERSION >= 14
+  return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
+                             ptr, idxlist);
+#else
+  return B.CreateInBoundsGEP(ptr, idxlist);
+#endif
+}
+
 StatusTuple CodegenLLVM::visit_block_stmt_node(BlockStmtNode *n) {
 
   // enter scope
@@ -278,17 +297,17 @@ StatusTuple CodegenLLVM::visit_ident_expr_node(IdentExprNode *n) {
           emit("%s%s->%s", n->decl_->scope_id(), n->c_str(), n->sub_name_.c_str());
           auto it = vars_.find(n->decl_);
           if (it == vars_.end()) return mkstatus_(n, "Cannot locate variable %s in vars_ table", n->c_str());
-          LoadInst *load_1 = B.CreateLoad(it->second);
+          LoadInst *load_1 = createLoad(it->second);
           vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
-          expr_ = B.CreateInBoundsGEP(load_1, indices);
+          expr_ = createInBoundsGEP(load_1, indices);
           if (!n->is_lhs())
-            expr_ = B.CreateLoad(pop_expr());
+            expr_ = createLoad(pop_expr());
         }
       }
     } else {
       auto it = vars_.find(n->decl_);
       if (it == vars_.end()) return mkstatus_(n, "Cannot locate variable %s in vars_ table", n->c_str());
-      expr_ = n->is_lhs() ? it->second : (Value *)B.CreateLoad(it->second);
+      expr_ = n->is_lhs() ? it->second : (Value *)createLoad(it->second);
     }
   } else {
     if (n->sub_name_.size()) {
@@ -298,7 +317,7 @@ StatusTuple CodegenLLVM::visit_ident_expr_node(IdentExprNode *n) {
       vector<Value *> indices({const_int(0), const_int(n->sub_decl_->slot_, 32)});
       expr_ = B.CreateGEP(nullptr, it->second, indices);
       if (!n->is_lhs())
-        expr_ = B.CreateLoad(pop_expr());
+        expr_ = createLoad(pop_expr());
     } else {
       if (n->bitop_) {
         // ident is holding a host endian number, don't use dext
@@ -315,7 +334,7 @@ StatusTuple CodegenLLVM::visit_ident_expr_node(IdentExprNode *n) {
         if (n->is_lhs() || n->decl_->is_struct())
           expr_ = it->second;
         else
-          expr_ = B.CreateLoad(it->second);
+          expr_ = createLoad(it->second);
       }
     }
   }
@@ -385,16 +404,16 @@ StatusTuple CodegenLLVM::visit_packet_expr_node(PacketExprNode *n) {
       }
       if (n->is_ref()) {
         // e.g.: @ip.hchecksum, return offset of the header within packet
-        LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+        LoadInst *offset_ptr = createLoad(offset_mem);
         Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3));
         expr_ = B.CreateIntCast(skb_hdr_offset, B.getInt64Ty(), false);
       } else if (n->is_lhs()) {
         emit("bpf_dins_pkt(pkt, %s + %zu, %zu, %zu, ", n->id_->c_str(), bit_offset >> 3, bit_offset & 0x7, bit_width);
         Function *store_fn = mod_->getFunction("bpf_dins_pkt");
         if (!store_fn) return mkstatus_(n, "unable to find function bpf_dins_pkt");
-        LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+        LoadInst *skb_ptr = createLoad(skb_mem);
         Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
-        LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+        LoadInst *offset_ptr = 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);
         createCall(store_fn, vector<Value *>({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7),
@@ -403,9 +422,9 @@ StatusTuple CodegenLLVM::visit_packet_expr_node(PacketExprNode *n) {
         emit("bpf_dext_pkt(pkt, %s + %zu, %zu, %zu)", n->id_->c_str(), bit_offset >> 3, bit_offset & 0x7, bit_width);
         Function *load_fn = mod_->getFunction("bpf_dext_pkt");
         if (!load_fn) return mkstatus_(n, "unable to find function bpf_dext_pkt");
-        LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+        LoadInst *skb_ptr = createLoad(skb_mem);
         Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
-        LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+        LoadInst *offset_ptr = createLoad(offset_mem);
         Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3));
         expr_ = createCall(load_fn, vector<Value *>({skb_ptr8, skb_hdr_offset,
                                                       B.getInt64(bit_offset & 0x7), B.getInt64(bit_width)}));
@@ -758,7 +777,7 @@ StatusTuple CodegenLLVM::emit_incr_cksum(MethodCallExprNode *n, size_t sz) {
   VariableDeclStmtNode *skb_decl;
   Value *skb_mem;
   TRY2(lookup_var(n, "skb", scopes_->current_var(), &skb_decl, &skb_mem));
-  LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+  LoadInst *skb_ptr = createLoad(skb_mem);
   Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
 
   expr_ = createCall(csum_fn, vector<Value *>({skb_ptr8, offset, old_val, new_val, flags}));
@@ -882,13 +901,13 @@ StatusTuple CodegenLLVM::visit_table_index_expr_node(TableIndexExprNode *n) {
 
         B.SetInsertPoint(label_end);
         vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
-        expr_ = B.CreateInBoundsGEP(result, indices);
+        expr_ = createInBoundsGEP(result, indices);
       } else {
         B.CreateCondBr(B.CreateIsNotNull(result), label_then, label_end);
 
         B.SetInsertPoint(label_then);
         vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
-        Value *field = B.CreateInBoundsGEP(result, indices);
+        Value *field = createInBoundsGEP(result, indices);
         B.CreateBr(label_end);
 
         B.SetInsertPoint(label_end);
@@ -920,7 +939,7 @@ StatusTuple CodegenLLVM::visit_match_decl_stmt_node(MatchDeclStmtNode *n) {
   if (result == vars_.end()) return mkstatus_(n, "unable to find memory for _result built-in");
   vars_[leaf_n] = result->second;
 
-  Value *load_1 = B.CreateLoad(result->second);
+  Value *load_1 = createLoad(result->second);
   Value *is_null = B.CreateIsNotNull(load_1);
 
   Function *parent = B.GetInsertBlock()->getParent();
@@ -948,7 +967,7 @@ StatusTuple CodegenLLVM::visit_miss_decl_stmt_node(MissDeclStmtNode *n) {
   auto result = vars_.find(result_decl);
   if (result == vars_.end()) return mkstatus_(n, "unable to find memory for _result built-in");
 
-  Value *load_1 = B.CreateLoad(result->second);
+  Value *load_1 = createLoad(result->second);
   Value *is_null = B.CreateIsNull(load_1);
 
   Function *parent = B.GetInsertBlock()->getParent();
@@ -1259,7 +1278,7 @@ StatusTuple CodegenLLVM::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
 
     // always return something
     B.SetInsertPoint(label_return);
-    B.CreateRet(B.CreateLoad(retval_));
+    B.CreateRet(createLoad(retval_));
   }
 
   return StatusTuple::OK();
index d77c9de82f6e6e4ec79e9a2f11e09778d6c96741..4998526ef448228898b8c8c965170b270b77ed62 100644 (file)
@@ -35,6 +35,7 @@ class Constant;
 class Instruction;
 class IRBuilderBase;
 class LLVMContext;
+class LoadInst;
 class Module;
 class StructType;
 class SwitchInst;
@@ -108,6 +109,8 @@ class CodegenLLVM : public Visitor {
                                  StructDeclStmtNode **decl = nullptr) const;
   llvm::CallInst *createCall(llvm::Value *Callee,
                              llvm::ArrayRef<llvm::Value *> Args);
+  llvm::LoadInst *createLoad(llvm::Value *Addr);
+  llvm::Value *createInBoundsGEP(llvm::Value *Ptr, llvm::ArrayRef<llvm::Value *> IdxList);
 
   template <typename... Args> void emit(const char *fmt, Args&&... params);
   void emit(const char *s);