Skip to content

Commit 05312db

Browse files
committed
[DirectX] Add /Qsource_in_debug_module to llc and clang. Replace dx.source metadata with dummy data in ILDB when needed
1 parent d8b0cd4 commit 05312db

6 files changed

Lines changed: 144 additions & 10 deletions

File tree

clang/include/clang/Options/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9666,6 +9666,11 @@ def dxc_Zss : DXCFlag<"Zss">,
96669666
HelpText<"Compute Shader Hash considering source information">;
96679667
def dxc_Zsb : DXCFlag<"Zsb">,
96689668
HelpText<"Compute Shader Hash considering only output binary">;
9669+
def dxc_source_in_debug_module
9670+
: Option<["/", "-"], "Qsource_in_debug_module", KIND_FLAG>,
9671+
Group<dxc_Group>,
9672+
Visibility<[DXCOption]>,
9673+
HelpText<"Embed source code into debug module on DirectX target">;
96699674
def dxil_validator_version : Option<["/", "-"], "validator-version", KIND_SEPARATE>,
96709675
Group<dxc_Group>, Flags<[HelpHidden]>,
96719676
Visibility<[DXCOption, ClangOption, CC1Option]>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,6 +3846,11 @@ static void RenderHLSLOptions(const Driver &D, const ArgList &Args,
38463846
}
38473847
if (Arg *A = Args.getLastArg(options::OPT_dxc_Zsb))
38483848
A->claim(); // /Zsb is the default behavior, no need to forward it to llc.
3849+
3850+
if (Args.hasArg(options::OPT_dxc_source_in_debug_module)) {
3851+
CmdArgs.push_back("-mllvm");
3852+
CmdArgs.push_back("--dx-source-in-debug-module");
3853+
}
38493854
}
38503855

38513856
static void RenderOpenACCOptions(const Driver &D, const ArgList &Args,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_dxc -Tlib_6_7 -### /Zi /Qsource_in_debug_module %s 2>&1 | FileCheck %s
2+
// RUN: %clang_dxc -Tlib_6_7 -### /Zi -Qsource_in_debug_module %s 2>&1 | FileCheck %s
3+
// RUN: %clang_dxc -Tlib_6_7 -### /Zi %s 2>&1 | FileCheck %s --check-prefix=NOFLAG
4+
5+
// CHECK: "-mllvm" "--dx-source-in-debug-module"
6+
// NOFLAG-NOT: --dx-source-in-debug-module

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static cl::opt<std::string> PdbOutputDir(
5050
cl::value_desc("directory"));
5151
static cl::opt<bool> ShaderHashDependsOnSource(
5252
"dx-Zss", cl::desc("Compute Shader Hash considering source information"));
53+
extern cl::opt<bool> SourceInDebugModule;
5354

5455
namespace {
5556
class DXContainerGlobals : public llvm::ModulePass {
@@ -409,7 +410,7 @@ void DXContainerGlobals::addSourceInfo(Module &M,
409410
dxil::ModuleMetadataInfo &MMI =
410411
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
411412

412-
if (!MMI.SourceInfo)
413+
if (!MMI.SourceInfo || SourceInDebugModule)
413414
return;
414415

415416
MMI.SourceInfo->computeEntries();

llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929
#include "llvm/InitializePasses.h"
3030
#include "llvm/Pass.h"
3131
#include "llvm/Support/Alignment.h"
32+
#include "llvm/Support/CommandLine.h"
3233
#include "llvm/Transforms/Utils/Cloning.h"
3334
#include "llvm/Transforms/Utils/ModuleUtils.h"
3435

3536
using namespace llvm;
3637
using namespace llvm::dxil;
3738

39+
// NOLINTNEXTLINE(misc-use-internal-linkage)
40+
cl::opt<bool> SourceInDebugModule(
41+
"dx-source-in-debug-module",
42+
cl::desc("Embed source code into debug module on DirectX target"),
43+
cl::init(false));
44+
3845
namespace {
3946
class WriteDXILPass : public llvm::ModulePass {
4047
raw_ostream &OS; // raw_ostream to print on
@@ -157,15 +164,17 @@ class EmbedDXILPass : public llvm::ModulePass {
157164

158165
if (HasDebugInfo) {
159166
if (WriteDebug) {
160-
// Replace dx.source metadata nodes with stubs.
161-
// TODO: Add /Qsource_in_debug_module flag to enable/disable this.
162-
LLVMContext &Ctx = M.getContext();
163-
MDString *EmptyString = MDString::get(Ctx, "");
164-
replaceNamedMetadataArray(M, "dx.source.contents",
165-
{EmptyString, EmptyString});
166-
replaceNamedMetadataArray(M, "dx.source.defines", {});
167-
replaceNamedMetadataArray(M, "dx.source.mainFileName", {EmptyString});
168-
replaceNamedMetadataArray(M, "dx.source.args", {});
167+
if (!SourceInDebugModule) {
168+
// Replace dx.source metadata nodes with stubs.
169+
// TODO: Add /Qsource_in_debug_module flag to enable/disable this.
170+
LLVMContext &Ctx = M.getContext();
171+
MDString *EmptyString = MDString::get(Ctx, "");
172+
replaceNamedMetadataArray(M, "dx.source.contents",
173+
{EmptyString, EmptyString});
174+
replaceNamedMetadataArray(M, "dx.source.defines", {});
175+
replaceNamedMetadataArray(M, "dx.source.mainFileName", {EmptyString});
176+
replaceNamedMetadataArray(M, "dx.source.args", {});
177+
}
169178
} else {
170179
// If we have an ILDB part, strip DXIL from all debug info.
171180
StripDebugInfo(M);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
; Compare source info emission with and without --dx-source-in-debug-module flag.
2+
3+
; RUN: llc %s --filetype=obj -o %t.dxbc
4+
; RUN: obj2yaml %t.dxbc | FileCheck %s --check-prefix=DXC
5+
; RUN: llvm-objcopy --dump-section=DXIL=%t.dxil.bc %t.dxbc
6+
; RUN: llvm-objcopy --dump-section=ILDB=%t.ildb.bc %t.dxbc
7+
; RUN: llvm-dis %t.dxil.bc -o - | FileCheck %s --check-prefix=DXIL-DIS
8+
; RUN: llvm-dis %t.ildb.bc -o - | FileCheck %s --check-prefix=ILDB-DIS
9+
10+
; RUN: llc %s --filetype=obj -o %t.dxbc --dx-source-in-debug-module
11+
; RUN: obj2yaml %t.dxbc | FileCheck %s --check-prefix=DXC-SOURCE
12+
; RUN: llvm-objcopy --dump-section=DXIL=%t.dxil.bc %t.dxbc
13+
; RUN: llvm-objcopy --dump-section=ILDB=%t.ildb.bc %t.dxbc
14+
; RUN: llvm-dis %t.dxil.bc -o - | FileCheck %s --check-prefix=DXIL-SOURCE-DIS
15+
; RUN: llvm-dis %t.ildb.bc -o - | FileCheck %s --check-prefix=ILDB-SOURCE-DIS
16+
17+
; Without the flag, dx.source should be stripped away from DXIL, and replaced
18+
; with dummy metadata in ILDB.
19+
; DXIL-DIS-NOT: dx.source
20+
; ILDB-DIS: !dx.source.contents = !{![[CONTENTS:[0-9]+]]}
21+
; ILDB-DIS: !dx.source.defines = !{![[EMPTY_ARR:[0-9]+]]}
22+
; ILDB-DIS: !dx.source.mainFileName = !{![[MAIN:[0-9]+]]}
23+
; ILDB-DIS: !dx.source.args = !{![[EMPTY_ARR]]}
24+
; ILDB-DIS: ![[CONTENTS]] = !{!"", !""}
25+
; ILDB-DIS: ![[EMPTY_ARR]] = !{}
26+
; ILDB-DIS: ![[MAIN]] = !{!""}
27+
28+
; Without the flag, SRCI should be emitted.
29+
; DXC: - Name: SRCI
30+
; DXC-NEXT: Size: 348
31+
; DXC-NEXT: SourceInfo:
32+
; DXC-NEXT: Header:
33+
; DXC: SectionCount: 3
34+
; DXC-NEXT: Names:
35+
; DXC-NEXT: SectionHeader:
36+
; DXC: Type: SourceNames
37+
; DXC-NEXT: Header:
38+
; DXC: Count: 3
39+
; DXC: Entries:
40+
; DXC: FileName: 'C:\dx-source-metadata.hlsl'
41+
; DXC: FileName: 'C:\a.hlsl'
42+
; DXC: FileName: 'C:\b.hlsl'
43+
; DXC-NEXT: Contents:
44+
; DXC-NEXT: SectionHeader:
45+
; DXC: Type: SourceContents
46+
; DXC-NEXT: Header:
47+
; DXC: Count: 3
48+
; DXC: Entries:
49+
; DXC: FileContent: "#include \"a.hlsl\"\n#include \"b.hlsl\"\n\nfloat foo(float a, float b) {\n return a + b;\n}\n"
50+
; DXC: FileContent: "#include \"b.hlsl\"\n"
51+
; DXC: FileContent: "#include <c.hlsl>\n"
52+
; DXC: Args:
53+
; DXC: SectionHeader:
54+
; DXC: Header:
55+
; DXC: Count: 5
56+
; DXC: Args:
57+
; DXC-NEXT: - Arg: '-g'
58+
; DXC-NEXT: Value: ''
59+
; DXC-NEXT: - Arg: '-Tlib_6_3'
60+
; DXC-NEXT: Value: ''
61+
; DXC-NEXT: - Arg: '-DUSER_DEF0=42'
62+
; DXC-NEXT: Value: ''
63+
; DXC-NEXT: - Arg: '-DUSER_DEF1=43'
64+
; DXC-NEXT: Value: ''
65+
; DXC-NEXT: - Arg: 'C:\\dx-source-metadata.hlsl'
66+
; DXC-NEXT: Value: ''
67+
68+
; With the flag, dx.source should be stripped away from DXIL, and kept untouched in ILDB.
69+
; DXIL-SOURCE-DIS-NOT: dx.source
70+
; ILDB-SOURCE-DIS: !dx.source.args = !{![[ARGS:[0-9]+]]}
71+
; ILDB-SOURCE-DIS: !dx.source.contents = !{![[FILE1:[0-9]+]], ![[FILE2:[0-9]+]], ![[FILE3:[0-9]+]]}
72+
; ILDB-SOURCE-DIS: !dx.source.mainFileName = !{![[MAIN:[0-9]+]]}
73+
; ILDB-SOURCE-DIS: !dx.source.defines = !{![[DEFINES:[0-9]+]]}
74+
; ILDB-SOURCE-DIS: ![[FILE1]] = !{!"C:\\dx-source-metadata.hlsl",
75+
; ILDB-SOURCE-DIS: ![[FILE2]] = !{!"C:\\a.hlsl"
76+
; ILDB-SOURCE-DIS: ![[FILE3]] = !{!"C:\\b.hlsl"
77+
; ILDB-SOURCE-DIS: ![[MAIN]] = !{!"C:\\dx-source-metadata.hlsl"}
78+
; ILDB-SOURCE-DIS: ![[DEFINES]] = !{!"USER_DEF0=42", !"USER_DEF1=43"}
79+
80+
; With the flag, SRCI should not be emitted.
81+
; DXC-SOURCE-NOT: - Name: SRCI
82+
83+
target triple = "dxilv1.3-pc-shadermodel6.3-library"
84+
85+
define float @_Z3fooff(float %a, float %b) {
86+
entry:
87+
%add = fadd float %a, %b
88+
ret float %add
89+
}
90+
91+
!llvm.dbg.cu = !{!4}
92+
!llvm.module.flags = !{!6, !7}
93+
94+
!dx.source.args = !{!0}
95+
!dx.source.contents = !{!1, !2, !3}
96+
!dx.source.mainFileName = !{!8}
97+
!dx.source.defines = !{!9}
98+
99+
!0 = !{!"-g", !"-Tlib_6_3", !"-DUSER_DEF0=42", !"-DUSER_DEF1=43", !"C:\\\\dx-source-metadata.hlsl"}
100+
!1 = !{!"C:\\dx-source-metadata.hlsl", !"#include \22a.hlsl\22\0A#include \22b.hlsl\22\0A\0Afloat foo(float a, float b) {\0A return a + b;\0A}\0A"}
101+
!2 = !{!"C:\\a.hlsl", !"#include \22b.hlsl\22\0A"}
102+
!3 = !{!"C:\\b.hlsl", !"#include <c.hlsl>\0A"}
103+
!4 = distinct !DICompileUnit(language: DW_LANG_C99, file: !5, emissionKind: FullDebug)
104+
!5 = !DIFile(filename: "dx-source-metadata.hlsl", directory: "C:\\")
105+
!6 = !{i32 2, !"Dwarf Version", i32 4}
106+
!7 = !{i32 2, !"Debug Info Version", i32 3}
107+
!8 = !{!"C:\\dx-source-metadata.hlsl"}
108+
!9 = !{!"USER_DEF0=42", !"USER_DEF1=43"}

0 commit comments

Comments
 (0)