[compiler-rt] [llvm] [nsan] Emit calls to optimized functions (PR #98900)

Alexander Shaposhnikov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 23:11:35 PDT 2024


================
@@ -493,6 +493,58 @@ class ValueToShadowMap {
   DenseMap<Value *, Value *> Map;
 };
 
+// First parameter is the number of functions
+// Second parameter is the number of fallback function arguments
+template <size_t N> class NsanMemOpFn {
+public:
+  NsanMemOpFn(Module &M, ArrayRef<StringRef> Sized, StringRef Fallback,
+              int NFallbackArgs);
+  // Number of parameters can be extracted from FunctionCallee
+  FunctionCallee getFunctionFor(uint64_t MemOpSize) const;
+  FunctionCallee getFallback() const;
+
+private:
+  std::array<FunctionCallee, N> Funcs;
+};
+
+template <size_t N>
+NsanMemOpFn<N>::NsanMemOpFn(Module &M, ArrayRef<StringRef> Sized,
+                            StringRef Fallback, int NFallbackArgs) {
+  LLVMContext &Ctx = M.getContext();
+  AttributeList Attr;
+  Attr = Attr.addFnAttribute(Ctx, Attribute::NoUnwind);
+  Type *PtrTy = PointerType::getUnqual(Ctx);
+  Type *VoidTy = Type::getVoidTy(Ctx);
+  IntegerType *IntptrTy = M.getDataLayout().getIntPtrType(Ctx);
+
+  for (size_t i = 0; i < N - 1; ++i) {
+    if (NFallbackArgs == 3)
+      Funcs[i] = M.getOrInsertFunction(Sized[i], Attr, VoidTy, PtrTy, PtrTy);
----------------
alexander-shaposhnikov wrote:

this can be simplified further - getOrInsertFunction has other overloads that take function type (can be computed once based on the number of parameters)

https://github.com/llvm/llvm-project/pull/98900


More information about the llvm-commits mailing list