Skip to content

Commit 2d38cb6

Browse files
committed
[SPIRV] Emit DebugLexicalScope for forloop controls (#8514)
1 parent 517dd5e commit 2d38cb6

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,20 @@ void SpirvEmitter::doForStmt(const ForStmt *forStmt,
27192719
attrs.empty() ? spv::LoopControlMask::MaskNone
27202720
: translateLoopAttribute(forStmt, *attrs.front());
27212721

2722+
RichDebugInfo *info = nullptr;
2723+
if (spirvOptions.debugInfoRich) {
2724+
const auto &sm = astContext.getSourceManager();
2725+
auto loc = forStmt->getForLoc();
2726+
const uint32_t line = sm.getPresumedLineNumber(loc);
2727+
const uint32_t column = sm.getPresumedColumnNumber(loc);
2728+
info = getOrCreateRichDebugInfo(loc);
2729+
auto *debugLexicalBlock = spvBuilder.createDebugLexicalBlock(
2730+
info->source, line, column, info->scopeStack.back());
2731+
2732+
// Add this lexical block to the stack of lexical scopes.
2733+
spvContext.pushDebugLexicalScope(info, debugLexicalBlock);
2734+
}
2735+
27222736
const Stmt *initStmt = forStmt->getInit();
27232737
const Stmt *body = forStmt->getBody();
27242738
const Expr *check = forStmt->getCond();
@@ -2842,6 +2856,12 @@ void SpirvEmitter::doForStmt(const ForStmt *forStmt,
28422856
// Done with the current scope's continue block and merge block.
28432857
continueStack.pop();
28442858
breakStack.pop();
2859+
2860+
if (spirvOptions.debugInfoRich) {
2861+
// We are done with processing this compound statement. Remove its
2862+
// lexical block from the stack of lexical scopes.
2863+
spvContext.popDebugLexicalScope(info);
2864+
}
28452865
}
28462866

28472867
void SpirvEmitter::doIfStmt(const IfStmt *ifStmt,

tools/clang/test/CodeGenSPIRV/rich.debug.debuglexicalblock.hlsl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// CHECK: [[debugSet:%[0-9]+]] = OpExtInstImport "OpenCL.DebugInfo.100"
44
// CHECK: [[debugSource:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugSource
55
// CHECK: [[main:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugFunction
6-
// CHECK: [[mainFnLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 13 1 [[main]]
7-
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 21 3 [[mainFnLexBlock]]
8-
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 26 20 [[whileLoopLexBlock]]
9-
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 28 7 [[ifStmtLexBlock]]
10-
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 15 12 [[mainFnLexBlock]]
6+
// CHECK: [[mainFnLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 14 1 [[main]]
7+
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 22 3 [[mainFnLexBlock]]
8+
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 27 20 [[whileLoopLexBlock]]
9+
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 29 7 [[ifStmtLexBlock]]
10+
// CHECK: [[forLoopParensLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 16 3 [[mainFnLexBlock]]
11+
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] 16 12 [[forLoopParensLexBlock]]
1112

1213
float4 main(float4 color : COLOR) : SV_TARGET
1314
{

tools/clang/test/CodeGenSPIRV/rich.debug.debugscope.hlsl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// CHECK: [[set:%[0-9]+]] = OpExtInstImport "OpenCL.DebugInfo.100"
44
// CHECK: [[compUnit:%[0-9]+]] = OpExtInst %void [[set]] DebugCompilationUnit
55
// CHECK: [[srcMain:%[0-9]+]] = OpExtInst %void [[set]] DebugFunction
6-
// CHECK: [[srcMainFnLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 17 1 [[srcMain]]
7-
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 37 3 [[srcMainFnLexBlock]]
8-
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 44 20 [[whileLoopLexBlock]]
9-
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 49 7 [[ifStmtLexBlock]]
10-
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 22 12 [[srcMainFnLexBlock]]
6+
// CHECK: [[srcMainFnLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 18 1 [[srcMain]]
7+
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 40 3 [[srcMainFnLexBlock]]
8+
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 47 20 [[whileLoopLexBlock]]
9+
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 52 7 [[ifStmtLexBlock]]
10+
// CHECK: [[forLoopParensLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 23 3 [[srcMainFnLexBlock]]
11+
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[set]] DebugLexicalBlock {{%[0-9]+}} 23 12 [[forLoopParensLexBlock]]
1112
// CHECK: [[main:%[0-9]+]] = OpExtInst %void [[set]] DebugFunction
1213

1314
float4 main(float4 color : COLOR) : SV_TARGET
@@ -20,16 +21,18 @@ float4 main(float4 color : COLOR) : SV_TARGET
2021

2122
float4 c = 0.xxxx;
2223
for (;;) {
24+
// CHECK: %for_check = OpLabel
25+
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[forLoopParensLexBlock]]
2326
// CHECK: %for_body = OpLabel
2427
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[forLoopLexBlock]]
2528
float4 a = 0.xxxx;
2629
float4 b = 1.xxxx;
2730
c = c + a + b;
2831
// CHECK: %for_continue = OpLabel
29-
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[srcMainFnLexBlock]]
32+
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[forLoopParensLexBlock]]
3033
}
3134
// CHECK: %for_merge = OpLabel
32-
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[srcMainFnLexBlock]]
35+
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[forLoopParensLexBlock]]
3336

3437
// CHECK: %while_check = OpLabel
3538
// CHECK-NEXT: {{%[0-9]+}} = OpExtInst %void [[set]] DebugScope [[srcMainFnLexBlock]]

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// CHECK: [[debugSet:%[0-9]+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
44
// CHECK: [[debugSource:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugSource
55
// CHECK: [[main:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugFunction
6-
// CHECK: [[mainFnLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_13 %uint_1 [[main]]
7-
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_21 %uint_3 [[mainFnLexBlock]]
8-
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_26 %uint_20 [[whileLoopLexBlock]]
9-
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_28 %uint_7 [[ifStmtLexBlock]]
10-
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_15 %uint_12 [[mainFnLexBlock]]
6+
// CHECK: [[mainFnLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_14 %uint_1 [[main]]
7+
// CHECK: [[whileLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_22 %uint_3 [[mainFnLexBlock]]
8+
// CHECK: [[ifStmtLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_27 %uint_20 [[whileLoopLexBlock]]
9+
// CHECK: [[tempLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_29 %uint_7 [[ifStmtLexBlock]]
10+
// CHECK: [[forLoopParensLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_16 %uint_3 [[mainFnLexBlock]]
11+
// CHECK: [[forLoopLexBlock:%[0-9]+]] = OpExtInst %void [[debugSet]] DebugLexicalBlock [[debugSource]] %uint_16 %uint_12 [[forLoopParensLexBlock]]
1112

1213
float4 main(float4 color : COLOR) : SV_TARGET
1314
{

0 commit comments

Comments
 (0)