Blame SOURCES/bpftrace-0.11.1-irbuilderbpf.cpp-bpforc.h-Fix-compilation-with-LLVM-.patch

cb3401
From 0768e5f58d39ebb60c18813ea77953be00ce5830 Mon Sep 17 00:00:00 2001
cb3401
From: Ovidiu Panait <ovidiu.panait@windriver.com>
cb3401
Date: Thu, 6 Aug 2020 10:34:23 +0300
cb3401
Subject: [PATCH] irbuilderbpf.cpp, bpforc.h: Fix compilation with LLVM 11
cb3401
MIME-Version: 1.0
cb3401
Content-Type: text/plain; charset=UTF-8
cb3401
Content-Transfer-Encoding: 8bit
cb3401
cb3401
Fixes: #1384
cb3401
cb3401
Fix the following build errors when compiling with LLVM 11:
cb3401
cb3401
 #1
cb3401
----
cb3401
/llvm/include/llvm/ExecutionEngine/Orc/Legacy.h:118:35: error: no match for call to ‘(bpftrace::BpfOrc::BpfOrc(llvm::TargetMachine*)::<lambda(const string&)>) (llvm::StringRef)’
cb3401
  118 |     if (JITSymbol Sym = FindSymbol(*S)) {
cb3401
      |                         ~~~~~~~~~~^~~~
cb3401
/llvm/include/llvm/ExecutionEngine/Orc/Legacy.h:118:35: note: candidate: ‘llvm::JITSymbol (*)(const string&)’ {aka ‘llvm::JITSymbol (*)(const std::__cxx11::basic_string<char>&)’} <conversion>
cb3401
/llvm/include/llvm/ExecutionEngine/Orc/Legacy.h:118:35: note:   candidate expects 2 arguments, 2 provided
cb3401
In file included from /work/src/github.com/iovisor/bpftrace/src/ast/codegen_llvm.cpp:5:
cb3401
/work/src/github.com/iovisor/bpftrace/src/bpforc.h:99:13: note: candidate: ‘bpftrace::BpfOrc::BpfOrc(llvm::TargetMachine*)::<lambda(const string&)>’
cb3401
   99 |             [](const std::string &Name __attribute__((unused))) -> JITSymbol {
cb3401
      |             ^
cb3401
/work/src/github.com/iovisor/bpftrace/src/bpforc.h:99:13: note:   no known conversion for argument 1 from ‘llvm::StringRef’ to ‘const string&’ {aka ‘const std::__cxx11::basic_string<char>&’}
cb3401
In file included from /llvm/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h:23,
cb3401
cb3401
 #2
cb3401
----
cb3401
| /src/ast/irbuilderbpf.cpp: In member function 'llvm::CallInst* bpftrace::ast::IRBuilderBPF::createMapLookup(int, llvm::AllocaInst*)':
cb3401
| /src/ast/irbuilderbpf.cpp:230:65: error: no matching function for call to 'bpftrace::ast::IRBuilderBPF::CreateCall(llvm::Constant*&, <brace-enclosed initializer list>, const char [12])'
cb3401
|   230 |   return CreateCall(lookup_func, { map_ptr, key }, "lookup_elem");
cb3401
|       |                                                                 ^
cb3401
| In file included from /src/ast/irbuilderbpf.h:9,
cb3401
|                  from /src/ast/async_event_types.h:3,
cb3401
|                  from /src/ast/irbuilderbpf.cpp:5:
cb3401
| /usr/include/llvm/IR/IRBuilder.h:2324:13: note: candidate: 'llvm::CallInst* llvm::IRBuilderBase::CreateCall(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, const llvm::Twine&, llvm::MDNode*)'
cb3401
|  2324 |   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
cb3401
|       |             ^~~~~~~~~~
cb3401
| /usr/include/llvm/IR/IRBuilder.h:2324:38: note:   no known conversion for argument 1 from 'llvm::Constant*' to 'llvm::FunctionType*'
cb3401
|  2324 |   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
cb3401
|       |                        ~~~~~~~~~~~~~~^~~
cb3401
cb3401
The CreateCall part is based on the llvm 11 fix from bcc:
cb3401
https://github.com/iovisor/bcc/commit/45e63f2b316cdce2d8cc925f6f14a8726ade9ff6
cb3401
cb3401
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
cb3401
---
cb3401
 src/ast/irbuilderbpf.cpp | 55 ++++++++++++++++++++++++++--------------
cb3401
 src/ast/irbuilderbpf.h   |  1 +
cb3401
 src/bpforc.h             |  6 +++++
cb3401
 3 files changed, 43 insertions(+), 19 deletions(-)
cb3401
cb3401
diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
cb3401
index 8ae055e..4498e0f 100644
cb3401
--- a/src/ast/irbuilderbpf.cpp
cb3401
+++ b/src/ast/irbuilderbpf.cpp
cb3401
@@ -201,10 +201,25 @@ llvm::Type *IRBuilderBPF::GetType(const SizedType &stype)
cb3401
   return ty;
cb3401
 }
cb3401
 
cb3401
+CallInst *IRBuilderBPF::createCall(Value *callee,
cb3401
+                                   ArrayRef<Value *> args,
cb3401
+                                   const Twine &Name)
cb3401
+{
cb3401
+#if LLVM_VERSION_MAJOR >= 11
cb3401
+  auto *calleePtrType = cast<PointerType>(callee->getType());
cb3401
+  auto *calleeType = cast<FunctionType>(calleePtrType->getElementType());
cb3401
+  return CreateCall(calleeType, callee, args, Name);
cb3401
+#else
cb3401
+  return CreateCall(callee, args, Name);
cb3401
+#endif
cb3401
+}
cb3401
+
cb3401
 CallInst *IRBuilderBPF::CreateBpfPseudoCall(int mapfd)
cb3401
 {
cb3401
   Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
cb3401
-  return CreateCall(pseudo_func, {getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapfd)}, "pseudo");
cb3401
+  return createCall(pseudo_func,
cb3401
+                    { getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapfd) },
cb3401
+                    "pseudo");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateBpfPseudoCall(Map &map)
cb3401
@@ -227,7 +242,7 @@ CallInst *IRBuilderBPF::createMapLookup(int mapfd, AllocaInst *key)
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_map_lookup_elem),
cb3401
       lookup_func_ptr_type);
cb3401
-  return CreateCall(lookup_func, { map_ptr, key }, "lookup_elem");
cb3401
+  return createCall(lookup_func, { map_ptr, key }, "lookup_elem");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetJoinMap(Value *ctx, const location &loc)
cb3401
@@ -325,7 +340,7 @@ void IRBuilderBPF::CreateMapUpdateElem(Value *ctx,
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_map_update_elem),
cb3401
       update_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(update_func,
cb3401
+  CallInst *call = createCall(update_func,
cb3401
                               { map_ptr, key, val, flags },
cb3401
                               "update_elem");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_update_elem, loc);
cb3401
@@ -349,7 +364,7 @@ void IRBuilderBPF::CreateMapDeleteElem(Value *ctx,
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_map_delete_elem),
cb3401
       delete_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(delete_func, { map_ptr, key }, "delete_elem");
cb3401
+  CallInst *call = createCall(delete_func, { map_ptr, key }, "delete_elem");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_delete_elem, loc);
cb3401
 }
cb3401
 
cb3401
@@ -378,7 +393,7 @@ void IRBuilderBPF::CreateProbeRead(Value *ctx,
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_probe_read),
cb3401
       proberead_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(proberead_func, { dst, size, src }, "probe_read");
cb3401
+  CallInst *call = createCall(proberead_func, { dst, size, src }, "probe_read");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_probe_read, loc);
cb3401
 }
cb3401
 
cb3401
@@ -413,7 +428,7 @@ CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
cb3401
 {
cb3401
   assert(ctx && ctx->getType() == getInt8PtrTy());
cb3401
   Constant *fn = createProbeReadStrFn(dst->getType(), src->getType());
cb3401
-  CallInst *call = CreateCall(fn,
cb3401
+  CallInst *call = createCall(fn,
cb3401
                               { dst, getInt32(size), src },
cb3401
                               "probe_read_str");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_probe_read_str, loc);
cb3401
@@ -434,7 +449,7 @@ CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
cb3401
   auto *size_i32 = CreateIntCast(size, getInt32Ty(), false);
cb3401
 
cb3401
   Constant *fn = createProbeReadStrFn(dst->getType(), src->getType());
cb3401
-  CallInst *call = CreateCall(fn, { dst, size_i32, src }, "probe_read_str");
cb3401
+  CallInst *call = createCall(fn, { dst, size_i32, src }, "probe_read_str");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_probe_read_str, loc);
cb3401
   return call;
cb3401
 }
cb3401
@@ -717,7 +732,7 @@ CallInst *IRBuilderBPF::CreateGetNs()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_ktime_get_ns),
cb3401
       gettime_func_ptr_type);
cb3401
-  return CreateCall(gettime_func, {}, "get_ns");
cb3401
+  return createCall(gettime_func, {}, "get_ns");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetPidTgid()
cb3401
@@ -730,7 +745,7 @@ CallInst *IRBuilderBPF::CreateGetPidTgid()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_current_pid_tgid),
cb3401
       getpidtgid_func_ptr_type);
cb3401
-  return CreateCall(getpidtgid_func, {}, "get_pid_tgid");
cb3401
+  return createCall(getpidtgid_func, {}, "get_pid_tgid");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetCurrentCgroupId()
cb3401
@@ -744,7 +759,7 @@ CallInst *IRBuilderBPF::CreateGetCurrentCgroupId()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_current_cgroup_id),
cb3401
       getcgroupid_func_ptr_type);
cb3401
-  return CreateCall(getcgroupid_func, {}, "get_cgroup_id");
cb3401
+  return createCall(getcgroupid_func, {}, "get_cgroup_id");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetUidGid()
cb3401
@@ -757,7 +772,7 @@ CallInst *IRBuilderBPF::CreateGetUidGid()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_current_uid_gid),
cb3401
       getuidgid_func_ptr_type);
cb3401
-  return CreateCall(getuidgid_func, {}, "get_uid_gid");
cb3401
+  return createCall(getuidgid_func, {}, "get_uid_gid");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetCpuId()
cb3401
@@ -770,7 +785,7 @@ CallInst *IRBuilderBPF::CreateGetCpuId()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_smp_processor_id),
cb3401
       getcpuid_func_ptr_type);
cb3401
-  return CreateCall(getcpuid_func, {}, "get_cpu_id");
cb3401
+  return createCall(getcpuid_func, {}, "get_cpu_id");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetCurrentTask()
cb3401
@@ -783,7 +798,7 @@ CallInst *IRBuilderBPF::CreateGetCurrentTask()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_current_task),
cb3401
       getcurtask_func_ptr_type);
cb3401
-  return CreateCall(getcurtask_func, {}, "get_cur_task");
cb3401
+  return createCall(getcurtask_func, {}, "get_cur_task");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetRandom()
cb3401
@@ -796,7 +811,7 @@ CallInst *IRBuilderBPF::CreateGetRandom()
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_prandom_u32),
cb3401
       getrandom_func_ptr_type);
cb3401
-  return CreateCall(getrandom_func, {}, "get_random");
cb3401
+  return createCall(getrandom_func, {}, "get_random");
cb3401
 }
cb3401
 
cb3401
 CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx,
cb3401
@@ -826,7 +841,7 @@ CallInst *IRBuilderBPF::CreateGetStackId(Value *ctx,
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_stackid),
cb3401
       getstackid_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(getstackid_func,
cb3401
+  CallInst *call = createCall(getstackid_func,
cb3401
                               { ctx, map_ptr, flags_val },
cb3401
                               "get_stackid");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_get_stackid, loc);
cb3401
@@ -852,7 +867,7 @@ void IRBuilderBPF::CreateGetCurrentComm(Value *ctx,
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_get_current_comm),
cb3401
       getcomm_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(getcomm_func,
cb3401
+  CallInst *call = createCall(getcomm_func,
cb3401
                               { buf, getInt64(size) },
cb3401
                               "get_comm");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_get_current_comm, loc);
cb3401
@@ -883,7 +898,9 @@ void IRBuilderBPF::CreatePerfEventOutput(Value *ctx, Value *data, size_t size)
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_perf_event_output),
cb3401
       perfoutput_func_ptr_type);
cb3401
-  CreateCall(perfoutput_func, {ctx, map_ptr, flags_val, data, size_val}, "perf_event_output");
cb3401
+  createCall(perfoutput_func,
cb3401
+             { ctx, map_ptr, flags_val, data, size_val },
cb3401
+             "perf_event_output");
cb3401
 }
cb3401
 
cb3401
 void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
cb3401
@@ -899,7 +916,7 @@ void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
cb3401
       Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_send_signal),
cb3401
       signal_func_ptr_type);
cb3401
-  CallInst *call = CreateCall(signal_func, { sig }, "signal");
cb3401
+  CallInst *call = createCall(signal_func, { sig }, "signal");
cb3401
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_send_signal, loc);
cb3401
 }
cb3401
 
cb3401
@@ -913,7 +930,7 @@ void IRBuilderBPF::CreateOverrideReturn(Value *ctx, Value *rc)
cb3401
   Constant *override_func = ConstantExpr::getCast(Instruction::IntToPtr,
cb3401
       getInt64(libbpf::BPF_FUNC_override_return),
cb3401
       override_func_ptr_type);
cb3401
-  CreateCall(override_func, { ctx, rc }, "override");
cb3401
+  createCall(override_func, { ctx, rc }, "override");
cb3401
 }
cb3401
 
cb3401
 Value *IRBuilderBPF::CreatKFuncArg(Value *ctx,
cb3401
diff --git a/src/ast/irbuilderbpf.h b/src/ast/irbuilderbpf.h
cb3401
index d4361a8..3111507 100644
cb3401
--- a/src/ast/irbuilderbpf.h
cb3401
+++ b/src/ast/irbuilderbpf.h
cb3401
@@ -80,6 +80,7 @@ class IRBuilderBPF : public IRBuilder<>
cb3401
   CallInst   *CreateGetRandom();
cb3401
   CallInst   *CreateGetStackId(Value *ctx, bool ustack, StackType stack_type, const location& loc);
cb3401
   CallInst   *CreateGetJoinMap(Value *ctx, const location& loc);
cb3401
+  CallInst   *createCall(Value *callee, ArrayRef<Value *> args, const Twine &Name);
cb3401
   void        CreateGetCurrentComm(Value *ctx, AllocaInst *buf, size_t size, const location& loc);
cb3401
   void        CreatePerfEventOutput(Value *ctx, Value *data, size_t size);
cb3401
   void        CreateSignal(Value *ctx, Value *sig, const location &loc;;
cb3401
diff --git a/src/bpforc.h b/src/bpforc.h
cb3401
index a42e031..295f703 100644
cb3401
--- a/src/bpforc.h
cb3401
+++ b/src/bpforc.h
cb3401
@@ -96,9 +96,15 @@ class BpfOrc
cb3401
       : TM(TM_),
cb3401
         Resolver(createLegacyLookupResolver(
cb3401
             ES,
cb3401
+#if LLVM_VERSION_MAJOR >= 11
cb3401
+            [](llvm::StringRef Name __attribute__((unused))) -> JITSymbol {
cb3401
+              return nullptr;
cb3401
+            },
cb3401
+#else
cb3401
             [](const std::string &Name __attribute__((unused))) -> JITSymbol {
cb3401
               return nullptr;
cb3401
             },
cb3401
+#endif
cb3401
             [](Error Err) { cantFail(std::move(Err), "lookup failed"); })),
cb3401
 #if LLVM_VERSION_MAJOR > 8
cb3401
         ObjectLayer(AcknowledgeORCv1Deprecation,
cb3401
-- 
cb3401
2.25.4
cb3401