[llvm] [SPIRV] Emit NonSemantic DebugGlobalVariable (PR #207230)

Manuel Carrasco via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 05:44:21 PDT 2026


================
@@ -251,6 +255,27 @@ void SPIRVNonSemanticDebugHandler::beginModule(Module *M) {
     if (!SP->isDefinition())
       SubprogramDeclarations.push_back(SP);
   }
+
+  // Map DIGlobalVariable -> llvm::GlobalVariable for globals that attach !dbg.
+  // The link lives on the IR global (via DIGlobalVariableExpression); DIGV
+  // metadata has no back-reference to its @g.
+  for (const GlobalVariable &G : M->globals()) {
+    SmallVector<DIGlobalVariableExpression *, 4> GVEs;
+    G.getDebugInfo(GVEs);
+    for (DIGlobalVariableExpression *GVE : GVEs)
+      DIGVToLLVMGV.try_emplace(GVE->getVariable(), &G);
+  }
+
+  // DebugInfoFinder deduplicates expressions but not by their pointed
+  // variables. We use a SmallSetVector to deduplicate them.
----------------
mgcarrasco wrote:

Source code:

```
static union {
  int x;
  float y;
};

int use(void) {
  x = 1;
  return x;
}
```

It emits this IR (reduced for the sake of space):

```
%union.anon = type { i32 }

@_Z1x = internal global %union.anon zeroinitializer, align 4, !dbg !0, !dbg !5

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "x", linkageName: "_Z1x", scope: !2, file: !7, line: 4, type: !9, isLocal: true, isDefinition: true)
!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
!6 = distinct !DIGlobalVariable(name: "y", linkageName: "_Z1x", scope: !2, file: !7, line: 4, type: !8, isLocal: true, isDefinition: true)
```

The IR GV has multiple DIGVE (!0 and !5).

I've just added a new test where we successfully handle this scenario.

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


More information about the llvm-commits mailing list