[llvm] b2ccaab - [TypeSanitizer] Properly add ext attributes on arguments where needed. (#224366)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 01:26:11 PDT 2026


Author: Jonas Paulsson
Date: 2026-09-22T10:26:06+02:00
New Revision: b2ccaab8ea618082b73d23201963dce0a1c03937

URL: https://github.com/llvm/llvm-project/commit/b2ccaab8ea618082b73d23201963dce0a1c03937
DIFF: https://github.com/llvm/llvm-project/commit/b2ccaab8ea618082b73d23201963dce0a1c03937.diff

LOG: [TypeSanitizer] Properly add ext attributes on arguments where needed. (#224366)

The TypeSanitizer should add extension attributes to arguments when inserting
instrumented calls.

This also adds getExtAttrForBoolParam() (and initBoolExtensionsForTriple()) in
TargetLibraryInfo.

Added: 
    llvm/test/Instrumentation/TypeSanitizer/instrumented-args-exts.ll

Modified: 
    llvm/include/llvm/Analysis/TargetLibraryInfo.h
    llvm/include/llvm/IR/Attributes.h
    llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.h b/llvm/include/llvm/Analysis/TargetLibraryInfo.h
index abbf2db6e27cb..19c251f592608 100644
--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.h
+++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.h
@@ -464,6 +464,17 @@ class TargetLibraryInfo {
     return Impl->CustomNames.find(F)->second;
   }
 
+  static void initBoolExtensionsForTriple(bool &ShouldZExtBoolParam,
+                                          bool &ShouldZExtBoolReturn,
+                                          const Triple &T) {
+    ShouldZExtBoolParam = ShouldZExtBoolReturn = false;
+
+    if (!T.isAArch64() && !T.isOSDarwin()) {
+      ShouldZExtBoolParam = true;
+      ShouldZExtBoolReturn = true;
+    }
+  }
+
   static void initExtensionsForTriple(bool &ShouldExtI32Param,
                                       bool &ShouldExtI32Return,
                                       bool &ShouldSignExtI32Param,
@@ -506,6 +517,12 @@ class TargetLibraryInfo {
   }
 
 public:
+  static Attribute::AttrKind getExtAttrForBoolParam(const Triple &T) {
+    bool ShouldZExtBoolParam, ShouldZExtBoolReturn;
+    initBoolExtensionsForTriple(ShouldZExtBoolParam, ShouldZExtBoolReturn, T);
+    return ShouldZExtBoolParam ? Attribute::ZExt : Attribute::None;
+  }
+
   static Attribute::AttrKind getExtAttrForI32Param(const Triple &T,
                                                    bool Signed = true) {
     bool ShouldExtI32Param, ShouldExtI32Return;

diff  --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index d9ab56cdcc1f6..7ff7def7525fa 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -721,6 +721,16 @@ class AttributeList {
     return addAttributesAtIndex(C, ArgNo + FirstArgIndex, B);
   }
 
+  /// Add an argument attribute to the list. Returns a new list because
+  /// attribute lists are immutable.
+  [[nodiscard]] AttributeList
+  maybeAddParamAttribute(LLVMContext &C, unsigned ArgNo,
+                         Attribute::AttrKind Kind) const {
+    if (Kind != Attribute::AttrKind::None)
+      return addParamAttribute(C, ArgNo, Kind);
+    return *this;
+  }
+
   /// Remove the specified attribute at the specified index from this
   /// attribute list. Returns a new list because attribute lists are immutable.
   [[nodiscard]] LLVM_ABI AttributeList removeAttributeAtIndex(

diff  --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
index cea6e9e316c1d..c6436617d69e6 100644
--- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
@@ -144,16 +144,25 @@ TypeSanitizer::TypeSanitizer(Module &M)
 }
 
 void TypeSanitizer::initializeCallbacks(Module &M) {
-  IRBuilder<> IRB(M.getContext());
+  LLVMContext &C = M.getContext();
+  IRBuilder<> IRB(C);
   OrdTy = IRB.getInt32Ty();
   U64Ty = IRB.getInt64Ty();
   Type *BoolType = IRB.getInt1Ty();
 
   AttributeList Attr;
-  Attr = Attr.addFnAttribute(M.getContext(), Attribute::NoUnwind);
-  // Initialize the callbacks.
+  Attr = Attr.addFnAttribute(C, Attribute::NoUnwind);
+  Attribute::AttrKind SExtAttr =
+      TargetLibraryInfo::getExtAttrForI32Param(TargetTriple, /*Signed=*/true);
+  Attribute::AttrKind BoolExtAttr =
+      TargetLibraryInfo::getExtAttrForBoolParam(TargetTriple);
+
+  // Initialize the callbacks.  TODO: use TLI/emitLibFunc() for these functions.
   TysanCheck =
-      M.getOrInsertFunction(kTysanCheckName, Attr, IRB.getVoidTy(),
+      M.getOrInsertFunction(kTysanCheckName,
+                            Attr.maybeAddParamAttribute(C, 1, SExtAttr)
+                                .maybeAddParamAttribute(C, 3, SExtAttr),
+                            IRB.getVoidTy(),
                             IRB.getPtrTy(), // Pointer to data to be read.
                             OrdTy,          // Size of the data in bytes.
                             IRB.getPtrTy(), // Pointer to type descriptor.
@@ -164,21 +173,25 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
       M.getOrInsertFunction(kTysanModuleCtorName, Attr, IRB.getVoidTy());
 
   TysanIntrumentMemInst = M.getOrInsertFunction(
-      "__tysan_instrument_mem_inst", Attr, IRB.getVoidTy(),
+      "__tysan_instrument_mem_inst",
+      Attr.maybeAddParamAttribute(C, 3, BoolExtAttr), IRB.getVoidTy(),
       IRB.getPtrTy(), // Pointer of data to be written to
       IRB.getPtrTy(), // Pointer of data to write
       U64Ty,          // Size of the data in bytes
       BoolType        // Do we need to call memmove
   );
 
-  TysanInstrumentWithShadowUpdate = M.getOrInsertFunction(
-      "__tysan_instrument_with_shadow_update", Attr, IRB.getVoidTy(),
-      IRB.getPtrTy(), // Pointer to data to be read
-      IRB.getPtrTy(), // Pointer to type descriptor
-      BoolType,       // Do we need to type check this
-      U64Ty,          // Size of data we access in bytes
-      OrdTy           // Flags
-  );
+  TysanInstrumentWithShadowUpdate =
+      M.getOrInsertFunction("__tysan_instrument_with_shadow_update",
+                            Attr.maybeAddParamAttribute(C, 2, BoolExtAttr)
+                                .maybeAddParamAttribute(C, 4, SExtAttr),
+                            IRB.getVoidTy(),
+                            IRB.getPtrTy(), // Pointer to data to be read
+                            IRB.getPtrTy(), // Pointer to type descriptor
+                            BoolType,       // Do we need to type check this
+                            U64Ty,          // Size of data we access in bytes
+                            OrdTy           // Flags
+      );
 
   TysanSetShadowType = M.getOrInsertFunction(
       "__tysan_set_shadow_type", Attr, IRB.getVoidTy(),

diff  --git a/llvm/test/Instrumentation/TypeSanitizer/instrumented-args-exts.ll b/llvm/test/Instrumentation/TypeSanitizer/instrumented-args-exts.ll
new file mode 100644
index 0000000000000..94e799889d90f
--- /dev/null
+++ b/llvm/test/Instrumentation/TypeSanitizer/instrumented-args-exts.ll
@@ -0,0 +1,11 @@
+; Test extensions of arguments to instrumented functions.
+;
+; RUN: opt -passes='tysan' -mtriple=s390x-unknown-linux -S %s | FileCheck %s
+
+define void @fun() {
+  ret void
+}
+
+; CHECK: declare void @__tysan_check(ptr, i32 signext, ptr, i32 signext)
+; CHECK: declare void @__tysan_instrument_mem_inst(ptr, ptr, i64, i1 zeroext)
+; CHECK: declare void @__tysan_instrument_with_shadow_update(ptr, ptr, i1 zeroext, i64, i32 signext)


        


More information about the llvm-commits mailing list