diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp index f3ac19227a5fd..4680ba1630734 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 0fa81186000f9..0c589bc45e2fe 100644 --- a/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp +++ b/llvm/lib/Target/DirectX/DirectXIRPasses/DXILDebugInfo.cpp @@ -173,6 +173,23 @@ DXILDebugInfoMap DXILDebugInfoPass::run(Module &M) { Res.MDExtra.insert({NewCU, SubprogramsMD}); } + for (const GlobalVariable &GV : M.globals()) { + SmallVector 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(&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(T)) { DIType *BT = SR->getBaseType(); diff --git a/llvm/test/CodeGen/DirectX/DebugInfo/di-globalvariable.ll b/llvm/test/CodeGen/DirectX/DebugInfo/di-globalvariable.ll new file mode 100644 index 0000000000000..978c6f6cfb221 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/DebugInfo/di-globalvariable.ll @@ -0,0 +1,39 @@ +; RUN: llc %s -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. + +@x = global i32 0, align 4, !dbg !0 +@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: ![[FILE:[0-9]+]], producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: ![[GLOBALS:[0-9]+]], splitDebugInlining: false, nameTableKind: Apple, sysroot: "/") +; CHECK-DAG: ![[GLOBALS]] = !{![[GVEX:[0-9]+]], ![[GVEY:[0-9]+]]} +; CHECK-DAG: DXIL: ![[GVEX]]: to be replaced by: ![[GVX:[0-9]+]] +; CHECK-DAG: ![[GVX]] = !DIGlobalVariable(name: "x", scope: ![[CU]], file: ![[FILE]], line: 1, type: ![[TYPE:[0-9]+]], isLocal: false, isDefinition: true) +; CHECK-DAG: DXIL: ![[GVEY]]: to be replaced by: ![[GVY:[0-9]+]] +; CHECK-DAG: ![[GVY]] = !DIGlobalVariable(name: "y", scope: ![[CU]], file: ![[FILE]], line: 1, type: ![[TYPE:[0-9]+]], isLocal: false, isDefinition: true) +; CHECK-DAG: DXIL: ![[GVY]]: additional data: ptr @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} 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. + +@x = global i32 0, align 4, !dbg !0 +@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}