[llvm] [Instrumentor] Ignore amdgcn intrinsics (PR #216205)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 15:48:32 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Vincent Arcila (jandrovins)

<details>
<summary>Changes</summary>

Avoid instrumenting load/store accesses whose pointer operand originates from an AMDGCN intrinsic such as `llvm.amdgcn.dispatch.ptr()`.

These pointers refer to special GPU runtime state and should not go through normal Instrumentor load/store callbacks.

Includes a lit test covering both the ignored AMDGCN case and a normal instrumented access.

---
Full diff: https://github.com/llvm/llvm-project/pull/216205.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/IPO/Instrumentor.cpp (+40) 
- (added) llvm/test/Instrumentation/Instrumentor/load_store_ignore_amdgcn_intrinsics.ll (+47) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/Instrumentor.cpp b/llvm/lib/Transforms/IPO/Instrumentor.cpp
index bfa22199ef9f6..1f30cd4c3d331 100644
--- a/llvm/lib/Transforms/IPO/Instrumentor.cpp
+++ b/llvm/lib/Transforms/IPO/Instrumentor.cpp
@@ -1208,6 +1208,36 @@ Value *AllocaIO::getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
 }
 ///}
 
+// Check if a pointer comes from an intrinsic function and is one of the AMD GCN intrinsics we avoid when instrumenting
+static bool ptrOriginatesFromAMDGCNIntrinsic(llvm::Value *PtrArg) {
+  SmallVector<const llvm::Value *, 4> Objs;
+  llvm::getUnderlyingObjects(PtrArg, Objs, nullptr, 20);
+  assert(!Objs.empty() && "How can a pointer have no underlying objects?");
+
+  auto IsAMDGCNObj = [](const llvm::Value *Obj) {
+    if (auto *II = llvm::dyn_cast<llvm::IntrinsicInst>(Obj))
+      if (llvm::Function *CalledFunc = II->getCalledFunction())
+        return CalledFunc->getName().contains("amdgcn");
+    return false;
+  };
+
+  bool SawAMDGCN = false;
+  bool SawNonAMDGCN = false;
+  for (const llvm::Value *Obj : Objs) {
+    if (IsAMDGCNObj(Obj))
+      SawAMDGCN = true;
+    else
+      SawNonAMDGCN = true;
+  }
+
+  assert(!(SawAMDGCN && SawNonAMDGCN) &&
+         "Mixed underlying objects: either all underlying objects must contain "
+         "'amdgcn' or none of them may contain it");
+
+  bool ShouldInstrument = !SawAMDGCN;
+  return ShouldInstrument;
+}
+
 void StoreIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
                    ConfigTy *UserConfig) {
   if (UserConfig)
@@ -1278,6 +1308,11 @@ void StoreIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
 
   addCommonArgs(IConf, IIRB.Ctx, Config.has(PassId));
   IConf.addChoice(*this, IIRB.Ctx);
+
+  CB = [&](Value &V) -> bool {
+    Value *Ptr = cast<StoreInst>(V).getPointerOperand();
+    return ptrOriginatesFromAMDGCNIntrinsic(Ptr);
+  };
 }
 
 Value *StoreIO::getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
@@ -1428,6 +1463,11 @@ void LoadIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
 
   addCommonArgs(IConf, IIRB.Ctx, Config.has(PassId));
   IConf.addChoice(*this, IIRB.Ctx);
+
+  CB = [&](Value &V) -> bool {
+    Value *Ptr = cast<LoadInst>(V).getPointerOperand();
+    return ptrOriginatesFromAMDGCNIntrinsic(Ptr);
+  };
 }
 
 Value *LoadIO::getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
diff --git a/llvm/test/Instrumentation/Instrumentor/load_store_ignore_amdgcn_intrinsics.ll b/llvm/test/Instrumentation/Instrumentor/load_store_ignore_amdgcn_intrinsics.ll
new file mode 100644
index 0000000000000..c974fdcde902a
--- /dev/null
+++ b/llvm/test/Instrumentation/Instrumentor/load_store_ignore_amdgcn_intrinsics.ll
@@ -0,0 +1,47 @@
+; REQUIRES: amdgpu-registered-target
+; RUN: opt < %s -passes=instrumentor -instrumentor-read-config-files=%S/load_store_config.json -S | FileCheck %s
+
+target datalayout = "e-m:e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
+target triple = "amdgcn-amd-amdhsa"
+
+declare noundef nonnull align 4 dereferenceable(64) ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()
+
+define void @instrument_regular_pointer(ptr %dst, ptr %src) {
+; CHECK-LABEL: define void @instrument_regular_pointer(
+; CHECK-SAME: ptr [[DST:%.*]], ptr [[SRC:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[SRC_PRE:%.*]] = call ptr @__instrumentor_pre_load(ptr [[SRC]], i32 0, i64 4, i64 4, i32 12, i32 0, i8 1, i8 0)
+; CHECK-NEXT: [[SRC_VAL:%.*]] = load i32, ptr [[SRC_PRE]], align 4
+; CHECK-NEXT: [[SRC_VAL_ZEXT:%.*]] = zext i32 [[SRC_VAL]] to i64
+; CHECK-NEXT: [[SRC_POST:%.*]] = call i64 @__instrumentor_post_load(ptr [[SRC]], i32 0, i64 [[SRC_VAL_ZEXT]], i64 4, i64 4, i32 12, i32 0, i8 1, i8 0)
+; CHECK-NEXT: [[STORE_VAL:%.*]] = trunc i64 [[SRC_POST]] to i32
+; CHECK-NEXT: [[STORE_VAL_ZEXT:%.*]] = zext i32 [[STORE_VAL]] to i64
+; CHECK-NEXT: [[DST_PRE:%.*]] = call ptr @__instrumentor_pre_store(ptr [[DST]], i32 0, i64 [[STORE_VAL_ZEXT]], i64 4, i64 4, i32 12, i32 0, i8 1, i8 0)
+; CHECK-NEXT: store i32 [[STORE_VAL]], ptr [[DST_PRE]], align 4
+; CHECK-NEXT: call void @__instrumentor_post_store(ptr [[DST]], i32 0, i64 [[STORE_VAL_ZEXT]], i64 4, i64 4, i32 12, i32 0, i8 1, i8 0)
+; CHECK-NEXT: ret void
+entry:
+  %value = load i32, ptr %src, align 4
+  store i32 %value, ptr %dst, align 4
+  ret void
+}
+
+define void @ignore_intrinsic_pointer() {
+; CHECK-LABEL: define void @ignore_intrinsic_pointer() {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[DISPATCH:%.*]] = call ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()
+; CHECK-NEXT: [[GENERIC:%.*]] = addrspacecast ptr addrspace(4) [[DISPATCH]] to ptr
+; CHECK-NEXT: [[SRC:%.*]] = getelementptr inbounds i8, ptr [[GENERIC]], i64 16
+; CHECK-NEXT: [[VALUE:%.*]] = load i32, ptr [[SRC]], align 4
+; CHECK-NEXT: [[DST:%.*]] = getelementptr inbounds i8, ptr [[GENERIC]], i64 20
+; CHECK-NEXT: store i32 [[VALUE]], ptr [[DST]], align 4
+; CHECK-NEXT: ret void
+entry:
+  %dispatch = call ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()
+  %generic = addrspacecast ptr addrspace(4) %dispatch to ptr
+  %src = getelementptr inbounds i8, ptr %generic, i64 16
+  %value = load i32, ptr %src, align 4
+  %dst = getelementptr inbounds i8, ptr %generic, i64 20
+  store i32 %value, ptr %dst, align 4
+  ret void
+}

``````````

</details>


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


More information about the llvm-commits mailing list