Skip to content

Commit 978a6d3

Browse files
[SPIRV] Emit DebugScope in wrapper (microsoft#7341) (microsoft#7529)
Legalization and optimization will produce inaccurate NS100 debug info if there is no DebugScope emitted in the wrapper function. This PR corrects this oversight and renames the wrapper to "__dxc_setup". This may cause a stack frame named __dxc_setup to appear in an HLSL debugger, however, users should be familiar with this type of thing. A C debugger might show crt0, or a debugger can filter this frame out of the user's view. This PR addresses [7341](https://github.com/microsoft/DirectXShaderCompiler/issues//7341)
1 parent 8a77b0c commit 978a6d3

5 files changed

Lines changed: 17 additions & 12 deletions

File tree

tools/clang/lib/SPIRV/EmitVisitor.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,12 +1635,6 @@ bool EmitVisitor::visit(SpirvDebugLexicalBlock *inst) {
16351635
}
16361636

16371637
bool EmitVisitor::visit(SpirvDebugScope *inst) {
1638-
// Technically entry function wrappers do not exist in HLSL. They
1639-
// are just created by DXC. We do not want to emit DebugScope for
1640-
// it.
1641-
if (inEntryFunctionWrapper)
1642-
return true;
1643-
16441638
initInstruction(inst);
16451639
curInst.push_back(inst->getResultTypeId());
16461640
curInst.push_back(getOrAssignResultId<SpirvInstruction>(inst));

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14050,8 +14050,8 @@ bool SpirvEmitter::processTessellationShaderAttributes(
1405014050
}
1405114051

1405214052
bool SpirvEmitter::emitEntryFunctionWrapperForRayTracing(
14053-
const FunctionDecl *decl, SpirvDebugFunction *debugFunction,
14054-
SpirvFunction *entryFuncInstr) {
14053+
const FunctionDecl *decl, RichDebugInfo **info,
14054+
SpirvDebugFunction *debugFunction, SpirvFunction *entryFuncInstr) {
1405514055
// The entry basic block.
1405614056
auto *entryLabel = spvBuilder.createBasicBlock();
1405714057
spvBuilder.setInsertPoint(entryLabel);
@@ -14160,6 +14160,10 @@ bool SpirvEmitter::emitEntryFunctionWrapperForRayTracing(
1416014160
spvBuilder.createReturn(decl->getBody()->getLocEnd());
1416114161
spvBuilder.endFunction();
1416214162

14163+
if (spirvOptions.debugInfoRich && decl->hasBody()) {
14164+
spvContext.popDebugLexicalScope(*info);
14165+
}
14166+
1416314167
return true;
1416414168
}
1416514169

@@ -14374,7 +14378,9 @@ SpirvFunction *SpirvEmitter::emitEntryFunctionWrapper(
1437414378
astContext.VoidTy, decl->getLocStart(), decl->getName());
1437514379

1437614380
if (spirvOptions.debugInfoRich && decl->hasBody()) {
14377-
*debugFunction = emitDebugFunction(decl, entryFunction, info, "wrapper");
14381+
*debugFunction =
14382+
emitDebugFunction(decl, entryFunction, info, "__dxc_setup");
14383+
spvContext.pushDebugLexicalScope(*info, *debugFunction);
1437814384
}
1437914385

1438014386
// Specify that entryFunction is an entry function wrapper.
@@ -14391,7 +14397,7 @@ SpirvFunction *SpirvEmitter::emitEntryFunctionWrapper(
1439114397
entryInfo->entryFunction = entryFunction;
1439214398

1439314399
if (spvContext.isRay()) {
14394-
return emitEntryFunctionWrapperForRayTracing(decl, *debugFunction,
14400+
return emitEntryFunctionWrapperForRayTracing(decl, info, *debugFunction,
1439514401
entryFuncInstr)
1439614402
? entryFunction
1439714403
: nullptr;
@@ -14632,6 +14638,10 @@ SpirvFunction *SpirvEmitter::emitEntryFunctionWrapper(
1463214638
if (spvContext.isHS())
1463314639
doDecl(patchConstFunc);
1463414640

14641+
if (spirvOptions.debugInfoRich && decl->hasBody()) {
14642+
spvContext.popDebugLexicalScope(*info);
14643+
}
14644+
1463514645
return entryFunction;
1463614646
}
1463714647

tools/clang/lib/SPIRV/SpirvEmitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ class SpirvEmitter : public ASTConsumer {
958958
/// The wrapper function is also responsible for initializing global static
959959
/// variables for some cases.
960960
bool emitEntryFunctionWrapperForRayTracing(const FunctionDecl *entryFunction,
961+
RichDebugInfo **info,
961962
SpirvDebugFunction *debugFunction,
962963
SpirvFunction *entryFuncId);
963964

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// CHECK: [[x:%[0-9]+]] = OpString "x"
1010
// CHECK: [[srcMainName:%[0-9]+]] = OpString "main"
1111
// CHECK: [[color:%[0-9]+]] = OpString "color"
12-
// CHECK: [[mainName:%[0-9]+]] = OpString "wrapper"
12+
// CHECK: [[mainName:%[0-9]+]] = OpString "__dxc_setup"
1313

1414
// CHECK: [[int:%[0-9]+]] = OpExtInst %void [[set]] DebugTypeBasic {{%[0-9]+}} %uint_32 Signed
1515
// CHECK: [[float:%[0-9]+]] = OpExtInst %void [[set]] DebugTypeBasic {{%[0-9]+}} %uint_32 Float

tools/clang/test/CodeGenSPIRV/shader.debug.function.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// CHECK: [[fooName:%[0-9]+]] = OpString "foo"
77
// CHECK: [[emptyStr:%[0-9]+]] = OpString ""
88
// CHECK: [[srcMainName:%[0-9]+]] = OpString "main"
9-
// CHECK: [[mainName:%[0-9]+]] = OpString "wrapper"
9+
// CHECK: [[mainName:%[0-9]+]] = OpString "__dxc_setup"
1010
// CHECK: [[clOpts:%[0-9]+]] = OpString " -E main -T ps_6_0 -spirv -fcgl -fspv-debug=vulkan
1111

1212
// CHECK: [[int:%[0-9]+]] = OpExtInst %void [[set]] DebugTypeBasic {{%[0-9]+}} %uint_32 %uint_4 %uint_0

0 commit comments

Comments
 (0)