[llvm] [DirectX] Rewrite DIGlobalVariableExpression to DXDIGlobalVariable (PR #199791)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 26 15:48:21 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Harald van Dijk (hvdijk)

<details>
<summary>Changes</summary>

DIGlobalVariable used to have an `expr` operand. Starting from bceaaa964360 `expr` is now an operand of DIGlobalVariableExpression, which references both the variable and the expression.

Before:
    !0 = !DIGlobalVariable(expr: !1)
    !1 = !DIExpression(DW_OP_plus, 4)

Now:
    !2 = !DIGlobalVariableExpression(var: !3, expr: !4)
    !3 = distinct !DIGlobalVariable(name: "a", scope: null, isLocal: false, isDefinition: true, type: !6)
    !4 = !DIExpression(DW_OP_plus, 4)

DXIL does not support DIGlobalVariableExpression, so we have to fold `expr` back to DIGlobalVariable.

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


3 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp (+1-1) 
- (modified) llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp (+17) 
- (added) llvm/test/tools/dxil-dis/di-globalvariable.ll (+36) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index 691f84aaf2d6f..b9cba0278ec06 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -1659,7 +1659,7 @@ void DXILBitcodeWriter::writeDIGlobalVariable(const DIGlobalVariable *N,
   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   Record.push_back(N->isLocalToUnit());
   Record.push_back(N->isDefinition());
-  Record.push_back(/* N->getRawVariable() */ 0);
+  Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
 
   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
diff --git a/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp b/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp
index 0395170bc783c..5dc614bf71b3d 100644
--- a/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp
+++ b/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp
@@ -98,6 +98,23 @@ DXILDebugInfoMap DXILDebugInfoPass::run(Module &M) {
     Res.MDExtra.insert({NewCU, SubprogramsMD});
   }
 
+  for (const GlobalVariable &GV : M.globals()) {
+    SmallVector<DIGlobalVariableExpression *, 4> GVEs;
+    GV.getDebugInfo(GVEs);
+    for (DIGlobalVariableExpression *GVE : GVEs) {
+      if (GVE->getExpression()->getNumElements())
+        continue;
+      auto [It, Inserted] = Res.MDExtra.insert(
+          {GVE->getVariable(),
+           ValueAsMetadata::get(const_cast<GlobalVariable *>(&GV))});
+      if (!Inserted)
+        It->second = nullptr;
+    }
+  }
+
+  for (DIGlobalVariableExpression *GVE : DIF.global_variables())
+    Res.MDReplace.insert({GVE, GVE->getVariable()});
+
   for (DIType *T : DIF.types()) {
     if (auto *SR = dyn_cast<DISubrangeType>(T)) {
       DIType *BT = SR->getBaseType();
diff --git a/llvm/test/tools/dxil-dis/di-globalvariable.ll b/llvm/test/tools/dxil-dis/di-globalvariable.ll
new file mode 100644
index 0000000000000..effa606b81283
--- /dev/null
+++ b/llvm/test/tools/dxil-dis/di-globalvariable.ll
@@ -0,0 +1,36 @@
+; RUN: llc --filetype=obj %s -o - | dxil-dis -o - | FileCheck %s
+
+target triple = "dxil-unknown-shadermodel6.3-library"
+
+;; @x is optimized away. DIGLobalVariable stays, but its variable operand is null (not printed).
+;; @y is not optimized away.
+
+ at x = global i32 0, align 4, !dbg !0
+ at y = global i32 1, align 4, !dbg !2
+
+define void @foo() {
+  %y = load i32, ptr @y
+  ret void
+}
+
+; CHECK-DAG: !llvm.dbg.cu = !{![[CU:[0-9]+]]}
+; CHECK-DAG: ![[CU]] = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: 1, globals: ![[GLOBALS:[0-9]+]])
+; CHECK-DAG: ![[GLOBALS]] = !{![[GVX:[0-9]+]], ![[GVY:[0-9]+]]}
+; CHECK-DAG: ![[GVX]] = !DIGlobalVariable(name: "x", scope: ![[CU]], file: ![[FILE:[0-9]+]], line: 1, type: ![[TYPE:[0-9]+]], isLocal: false, isDefinition: true)
+; CHECK-DAG: ![[GVY]] = !DIGlobalVariable(name: "y", scope: ![[CU]], file: ![[FILE:[0-9]+]], line: 1, type: ![[TYPE:[0-9]+]], isLocal: false, isDefinition: true, variable: i32* @y)
+; CHECK-DAG: ![[FILE]] = !DIFile(filename: "cu.cpp", directory: "/tmp")
+; CHECK-DAG: ![[TYPE]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+
+!llvm.dbg.cu = !{!4}
+!llvm.module.flags = !{!8, !9}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = !DIGlobalVariable(name: "x", scope: !4, file: !5, line: 1, type: !7, isLocal: false, isDefinition: true)
+!2 = !DIGlobalVariableExpression(var: !3, expr: !DIExpression())
+!3 = !DIGlobalVariable(name: "y", scope: !4, file: !5, line: 1, type: !7, isLocal: false, isDefinition: true)
+!4 = distinct !DICompileUnit(language: DW_LANG_C, file: !5, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !6, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
+!5 = !DIFile(filename: "cu.cpp", directory: "/tmp")
+!6 = !{!0, !2}
+!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!8 = !{i32 7, !"Dwarf Version", i32 5}
+!9 = !{i32 2, !"Debug Info Version", i32 3}

``````````

</details>


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


More information about the llvm-commits mailing list