Skip to content

Commit 34f8fb0

Browse files
[SPIRV] Create only 1 DebugCompilationUnit per spirv module (#7669)
Currently, dxc emits multiple DebugCompilationUnits for one spirv module. This is incorrect, as acknowledged in [this comment:](#7569 (comment)). This PR corrects the problem.
1 parent 4c8d21a commit 34f8fb0

File tree

9 files changed

+38
-17
lines changed

9 files changed

+38
-17
lines changed

tools/clang/include/clang/SPIRV/SpirvModule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class SpirvModule {
165165
return debugInstructions;
166166
}
167167

168+
// Access the one DebugCompilationUnit per module
169+
SpirvDebugCompilationUnit *getDebugCompilationUnit();
170+
void setDebugCompilationUnit(SpirvDebugCompilationUnit *unit);
171+
168172
// Adds the given OpModuleProcessed to the module.
169173
void addModuleProcessed(SpirvModuleProcessed *);
170174

@@ -224,6 +228,10 @@ class SpirvModule {
224228

225229
// Keep all rich DebugInfo instructions.
226230
llvm::SmallVector<SpirvDebugInstruction *, 32> debugInstructions;
231+
232+
// There is one debugCompilationUnit per module
233+
SpirvDebugCompilationUnit *debugCompilationUnit;
234+
227235
// Whether current module is in pervertex interpolation mode.
228236
bool perVertexInterp;
229237
};

tools/clang/lib/SPIRV/DebugTypeVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SpirvDebugTypeComposite *DebugTypeVisitor::createDebugTypeComposite(
6262
} else {
6363
auto *dbgSrc = spvBuilder.createDebugSource(file);
6464
setDefaultDebugInfo(dbgSrc);
65-
auto dbgCompUnit = spvBuilder.createDebugCompilationUnit(dbgSrc);
65+
auto dbgCompUnit = spvBuilder.getModule()->getDebugCompilationUnit();
6666
setDefaultDebugInfo(dbgCompUnit);
6767
debugInfo =
6868
&debugInfoMap.insert({file, RichDebugInfo(dbgSrc, dbgCompUnit)})

tools/clang/lib/SPIRV/SpirvBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ SpirvBuilder::createDebugCompilationUnit(SpirvDebugSource *source) {
12141214
auto *inst = new (context) SpirvDebugCompilationUnit(
12151215
/*version*/ 1, /*DWARF version*/ 4, source);
12161216
mod->addDebugInfo(inst);
1217+
mod->setDebugCompilationUnit(inst);
12171218
return inst;
12181219
}
12191220

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,10 @@ SpirvEmitter::getOrCreateRichDebugInfoImpl(llvm::StringRef file) {
10731073
// returns {{string key, RichDebugInfo}, true /*Success*/}.
10741074
// debugInfo.insert().first->second is a RichDebugInfo.
10751075
return &debugInfo
1076-
.insert({file, RichDebugInfo(
1077-
dbgSrc, spvBuilder.createDebugCompilationUnit(
1078-
dbgSrc))})
1076+
.insert({file,
1077+
RichDebugInfo(
1078+
dbgSrc,
1079+
spvBuilder.getModule()->getDebugCompilationUnit())})
10791080
.first->second;
10801081
}
10811082

tools/clang/lib/SPIRV/SpirvModule.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,16 @@ void SpirvModule::addModuleProcessed(SpirvModuleProcessed *p) {
382382
moduleProcesses.push_back(p);
383383
}
384384

385+
SpirvDebugCompilationUnit *SpirvModule::getDebugCompilationUnit() {
386+
SpirvDebugCompilationUnit *unit = debugCompilationUnit;
387+
assert(unit && "null DebugCompilationUnit");
388+
return unit;
389+
}
390+
391+
void SpirvModule::setDebugCompilationUnit(SpirvDebugCompilationUnit *unit) {
392+
assert(unit);
393+
debugCompilationUnit = unit;
394+
}
395+
385396
} // end namespace spirv
386397
} // end namespace clang

tools/clang/test/CodeGenSPIRV/rich.debug.debugsource.multiple.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
// CHECK: spirv.debug.opline.include-file-1.hlsli
77
// CHECK: spirv.debug.opline.include-file-2.hlsli
88
// CHECK: spirv.debug.opline.include-file-3.hlsli
9+
// CHECK: [[main_code:%[0-9]+]] = OpString "// RUN: %dxc -T ps_6_0 -E main -fspv-debug=rich-with-source -fcgl %s -spirv | FileCheck %s
910
// CHECK: [[file3_code:%[0-9]+]] = OpString "int b;
1011
// CHECK: [[file2_code:%[0-9]+]] = OpString "static int a;
1112
// CHECK: [[file1_code:%[0-9]+]] = OpString "int function1() {
12-
// CHECK: [[main_code:%[0-9]+]] = OpString "// RUN: %dxc -T ps_6_0 -E main -fspv-debug=rich-with-source -fcgl %s -spirv | FileCheck %s
1313

14+
15+
// CHECK: {{%[0-9]+}} = OpExtInst %void [[debugSet]] DebugSource {{%[0-9]+}} [[main_code]]
1416
// CHECK: {{%[0-9]+}} = OpExtInst %void [[debugSet]] DebugSource {{%[0-9]+}} [[file3_code]]
1517
// CHECK: {{%[0-9]+}} = OpExtInst %void [[debugSet]] DebugSource {{%[0-9]+}} [[file2_code]]
1618
// CHECK: {{%[0-9]+}} = OpExtInst %void [[debugSet]] DebugSource {{%[0-9]+}} [[file1_code]]
17-
// CHECK: {{%[0-9]+}} = OpExtInst %void [[debugSet]] DebugSource {{%[0-9]+}} [[main_code]]
1819

1920
#include "spirv.debug.opline.include-file-1.hlsli"
2021

tools/clang/test/CodeGenSPIRV/rich.debug.function.parent.hlsl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@ int callFunction3() {
2929
CALL_FUNCTION_3;
3030
}
3131

32+
33+
// CHECK: [[main:%[0-9]+]] = OpExtInst %void [[set]] DebugSource
34+
// CHECK: [[c3:%[0-9]+]] = OpExtInst %void [[set]] DebugCompilationUnit 1 4 [[main]] HLSL
35+
// CHECK-NOT: DebugCompilationUnit
3236
// CHECK: [[s3:%[0-9]+]] = OpExtInst %void [[set]] DebugSource
33-
// CHECK: [[c3:%[0-9]+]] = OpExtInst %void [[set]] DebugCompilationUnit 1 4 [[s3]] HLSL
3437
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugFunction [[f3]] {{%[0-9]+}} [[s3]] 3 1 [[c3]]
3538

3639
// CHECK: [[s2:%[0-9]+]] = OpExtInst %void [[set]] DebugSource
37-
// CHECK: [[c2:%[0-9]+]] = OpExtInst %void [[set]] DebugCompilationUnit 1 4 [[s2]] HLSL
38-
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugFunction [[f2]] {{%[0-9]+}} [[s2]] 2 1 [[c2]]
40+
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugFunction [[f2]] {{%[0-9]+}} [[s2]] 2 1 [[c3]]
3941

4042
// CHECK: [[s1:%[0-9]+]] = OpExtInst %void [[set]] DebugSource
41-
// CHECK: [[c1:%[0-9]+]] = OpExtInst %void [[set]] DebugCompilationUnit 1 4 [[s1]] HLSL
42-
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugFunction [[f1]] {{%[0-9]+}} [[s1]] 1 1 [[c1]]
43-
44-
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugSource
43+
// CHECK: {{%[0-9]+}} = OpExtInst %void [[set]] DebugFunction [[f1]] {{%[0-9]+}} [[s1]] 1 1 [[c3]]
4544

4645
void main() {
4746
callFunction1();

tools/clang/test/CodeGenSPIRV/shader.debug.line.include.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// CHECK: OpString
1313
// CHECK: [[file1:%[0-9]+]] = OpString
1414
// CHECK-SAME: spirv.debug.opline.include-file-1.hlsli
15+
// CHECK: [[src0:%[0-9]+]] = OpExtInst %void %1 DebugSource [[main]]
1516
// CHECK: [[src3:%[0-9]+]] = OpExtInst %void %1 DebugSource [[file3]]
1617
// CHECK: [[src2:%[0-9]+]] = OpExtInst %void %1 DebugSource [[file2]]
1718
// CHECK: [[src1:%[0-9]+]] = OpExtInst %void %1 DebugSource [[file1]]
18-
// CHECK: [[src0:%[0-9]+]] = OpExtInst %void %1 DebugSource [[main]]
1919

2020
// DebugLine cannot preceed OpFunction
2121
// CHECK: %src_main = OpFunction %void None

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ float4 main(VertexOutput v) : SV_Position
5858
}
5959
)";
6060
std::string spirv = compileCodeAndGetSpirvAsm(code);
61-
EXPECT_THAT(spirv, ContainsRegex("%50 = OpString \"// RUN: %dxc -T vs_6_0 -E "
61+
EXPECT_THAT(spirv, ContainsRegex("%23 = OpString \"// RUN: %dxc -T vs_6_0 -E "
6262
"main -fspv-debug=vulkan-with-source"));
63-
EXPECT_THAT(spirv, ContainsRegex("DebugSource %23\n"));
64-
EXPECT_THAT(spirv, ContainsRegex("DebugSource %5 %50\n"));
63+
EXPECT_THAT(spirv, ContainsRegex("DebugSource %5 %23\n"));
64+
EXPECT_THAT(spirv, ContainsRegex("DebugSource %28\n"));
6565
}
6666
} // namespace

0 commit comments

Comments
 (0)