[llvm] [SPIRV] Emit intrinsics for globals only in function that references them (PR #178143)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 27 07:23:57 PST 2026


================
@@ -2117,13 +2117,49 @@ Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
   return &I;
 }
 
-void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
-                                             IRBuilder<> &B) {
+static bool shouldEmitIntrinsicsForGlobalValue(const GlobalVariable &GV,
+                                               const Function *CurrF) {
   // Skip special artificial variables.
   static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
                                              "llvm.compiler.used"};
 
   if (ArtificialGlobals.contains(GV.getName()))
+    return false;
+
+  SmallPtrSet<const Value *, 8> Visited;
+  SmallVector<const Value *> Worklist = {&GV};
+  bool ReferencedByAnotherFunction = false;
+  while (!Worklist.empty()) {
+    const Value *V = Worklist.pop_back_val();
+    if (!Visited.insert(V).second)
+      continue;
+
+    if (const Instruction *I = dyn_cast<Instruction>(V)) {
+      if (I->getFunction() == CurrF)
+        return true;
+      ReferencedByAnotherFunction = true;
+      continue;
+    }
+
+    if (const Constant *C = dyn_cast<Constant>(V))
+      Worklist.append(C->user_begin(), C->user_end());
+  }
----------------
jmmartinez wrote:

Updated in https://github.com/llvm/llvm-project/pull/178143/changes/e2e50f687996a710b2fb32a3dbd481b94a8cafee

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


More information about the llvm-commits mailing list