Skip to content

Commit fbe4f3a

Browse files
authored
[OPT] Process debug scope instructions of debug value instructions (#6510)
When we create a debug value from a debug scope instruction in ADCE, we add the new instruction directly to the live set. This by passes the code that processes the operands of the new instructions to make sure they are all live. We do the same thing when a debug value is modify to have an undef. In that case we have bespoke code to process some of this accociated code, but it misses the debug scope. I replace the bespoke processing by adding it to the worklist. Fixes #6508
1 parent 20cb35f commit fbe4f3a

2 files changed

Lines changed: 144 additions & 33 deletions

File tree

source/opt/aggressive_dead_code_elim_pass.cpp

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ constexpr uint32_t kExtInstOpInIdx = 1;
4545
constexpr uint32_t kInterpolantInIdx = 2;
4646
constexpr uint32_t kCooperativeMatrixLoadSourceAddrInIdx = 0;
4747
constexpr uint32_t kDebugDeclareVariableInIdx = 3;
48-
constexpr uint32_t kDebugValueLocalVariableInIdx = 2;
4948
constexpr uint32_t kDebugValueValueInIdx = 3;
50-
constexpr uint32_t kDebugValueExpressionInIdx = 4;
5149

5250
// Sorting functor to present annotation instructions in an easy-to-process
5351
// order. The functor orders by opcode first and falls back on unique id
@@ -326,7 +324,7 @@ Pass::Status AggressiveDCEPass::ProcessDebugInformation(
326324
user, var_id, stored_value_id, user);
327325
if (added && next_inst) {
328326
auto new_debug_value = next_inst->PreviousNode();
329-
live_insts_.Set(new_debug_value->unique_id());
327+
AddToWorklist(new_debug_value);
330328
}
331329
}
332330
return true;
@@ -344,42 +342,13 @@ Pass::Status AggressiveDCEPass::ProcessDebugInformation(
344342

345343
// Value operand of DebugValue is not live
346344
// Set Value to Undef of appropriate type
347-
live_insts_.Set(inst->unique_id());
348-
349345
uint32_t type_id = def->type_id();
350-
auto type_def = get_def_use_mgr()->GetDef(type_id);
351-
AddToWorklist(type_def);
352-
353346
uint32_t undef_id = Type2Undef(type_id);
354347
if (undef_id == 0) return false;
355348

356-
auto undef_inst = get_def_use_mgr()->GetDef(undef_id);
357-
live_insts_.Set(undef_inst->unique_id());
358349
inst->SetInOperand(var_operand_idx, {undef_id});
359350
context()->get_def_use_mgr()->AnalyzeInstUse(inst);
360-
361-
id = inst->GetSingleWordInOperand(kDebugValueLocalVariableInIdx);
362-
auto localVar = get_def_use_mgr()->GetDef(id);
363-
AddToWorklist(localVar);
364-
365-
uint32_t expr_idx = kDebugValueExpressionInIdx;
366-
id = inst->GetSingleWordInOperand(expr_idx);
367-
auto expression = get_def_use_mgr()->GetDef(id);
368-
AddToWorklist(expression);
369-
370-
for (uint32_t i = expr_idx + 1; i < inst->NumInOperands(); ++i) {
371-
id = inst->GetSingleWordInOperand(i);
372-
auto index_def = get_def_use_mgr()->GetDef(id);
373-
if (index_def) {
374-
AddToWorklist(index_def);
375-
}
376-
}
377-
378-
for (auto& line_inst : inst->dbg_line_insts()) {
379-
if (line_inst.IsDebugLineInst()) {
380-
AddToWorklist(&line_inst);
381-
}
382-
}
351+
AddToWorklist(inst);
383352
}
384353
return true;
385354
});

test/opt/aggressive_dead_code_elim_test.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8883,6 +8883,148 @@ OpFunctionEnd
88838883

88848884
SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
88858885
}
8886+
8887+
TEST_F(AggressiveDCETest, DebugDeclareConvertedToDebugValueKeepsDebugScope) {
8888+
const std::string spirv = R"(
8889+
; CHECK: [[ext:%\w+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8890+
; CHECK-DAG: [[inlined:%\w+]] = OpExtInst %void [[ext]] DebugInlinedAt
8891+
; CHECK-DAG: [[UniformVar:%\w+]] = OpVariable %_ptr_Uniform_float Uniform
8892+
; CHECK: [[scope:%\w+]] = OpExtInst %void [[ext]] DebugScope {{%\w+}} [[inlined]]
8893+
; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue {{%\w+}} {{%\w+}} {{%\w+}}
8894+
; CHECK-NEXT: DebugScope
8895+
; CHECK-NEXT: DebugLine
8896+
; CHECK-NEXT: OpStore [[UniformVar]] %float_0
8897+
; CHECK-NEXT: OpReturn
8898+
OpCapability Shader
8899+
OpExtension "SPV_KHR_non_semantic_info"
8900+
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8901+
OpMemoryModel Logical GLSL450
8902+
OpEntryPoint GLCompute %2 "main"
8903+
OpExecutionMode %2 LocalSize 1 1 1
8904+
%3 = OpString ""
8905+
%void = OpTypeVoid
8906+
%uint = OpTypeInt 32 0
8907+
%6 = OpTypeFunction %void
8908+
%float = OpTypeFloat 32
8909+
%uint_3 = OpConstant %uint 3
8910+
%uint_0 = OpConstant %uint 0
8911+
%_ptr_Function_float = OpTypePointer Function %float
8912+
%_ptr_Uniform_float = OpTypePointer Uniform %float
8913+
%float_0 = OpConstant %float 0
8914+
%13 = OpVariable %_ptr_Uniform_float Uniform
8915+
%uint_57 = OpConstant %uint 57
8916+
%15 = OpExtInst %void %1 DebugExpression
8917+
%16 = OpExtInst %void %1 DebugSource %3 %3
8918+
%17 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %16 %uint_0
8919+
%18 = OpExtInst %void %1 DebugTypeBasic %3 %uint_0 %uint_0 %uint_0
8920+
%19 = OpExtInst %void %1 DebugTypeVector %18 %uint_3
8921+
%20 = OpExtInst %void %1 DebugTypePointer %19 %uint_0 %uint_0
8922+
%21 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void %20
8923+
%22 = OpExtInst %void %1 DebugFunction %3 %21 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0
8924+
%23 = OpExtInst %void %1 DebugTypeBasic %3 %uint_0 %uint_3 %uint_0
8925+
%24 = OpExtInst %void %1 DebugTypeMember %3 %23 %16 %uint_0 %uint_0 %uint_0 %uint_0 %uint_0
8926+
%25 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0 %24
8927+
%26 = OpExtInst %void %1 DebugTypeFunction %uint_0 %23 %25 %23
8928+
%27 = OpExtInst %void %1 DebugFunction %3 %26 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0
8929+
%28 = OpExtInst %void %1 DebugLocalVariable %3 %23 %16 %uint_0 %uint_0 %27 %uint_0 %uint_0
8930+
%29 = OpExtInst %void %1 DebugInlinedAt %uint_0 %22
8931+
%2 = OpFunction %void None %6
8932+
%30 = OpLabel
8933+
%31 = OpVariable %_ptr_Function_float Function
8934+
%32 = OpExtInst %void %1 DebugScope %27 %29
8935+
%33 = OpExtInst %void %1 DebugNoLine
8936+
%34 = OpExtInst %void %1 DebugDeclare %28 %31 %15
8937+
OpStore %31 %float_0
8938+
%35 = OpExtInst %void %1 DebugScope %22
8939+
%36 = OpExtInst %void %1 DebugLine %16 %uint_0 %uint_0 %uint_0 %uint_0
8940+
OpStore %13 %float_0
8941+
OpReturn
8942+
%37 = OpExtInst %void %1 DebugNoScope
8943+
OpFunctionEnd
8944+
)";
8945+
8946+
SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
8947+
}
8948+
8949+
TEST_F(AggressiveDCETest, DebugValueWithDeadOperandKeepsDebugScope) {
8950+
const std::string spirv = R"(
8951+
; CHECK: [[ext:%\w+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8952+
; CHECK-DAG: [[inlined:%\w+]] = OpExtInst %void [[ext]] DebugInlinedAt
8953+
; CHECK-DAG: [[undef:%\w+]] = OpUndef %float
8954+
; CHECK: [[scope:%\w+]] = OpExtInst %void [[ext]] DebugScope {{%\w+}} [[inlined]]
8955+
; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue {{%\w+}} [[undef]] {{%\w+}}
8956+
OpCapability Shader
8957+
OpExtension "SPV_KHR_non_semantic_info"
8958+
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8959+
OpMemoryModel Logical GLSL450
8960+
OpEntryPoint GLCompute %2 "main"
8961+
OpExecutionMode %2 LocalSize 1 1 1
8962+
%4 = OpString ""
8963+
%5 = OpString "file.slang"
8964+
%6 = OpString "uint"
8965+
%7 = OpString "computeMain"
8966+
%8 = OpString "float"
8967+
%9 = OpString "x"
8968+
%10 = OpString "A"
8969+
%11 = OpString "test"
8970+
%12 = OpString "v"
8971+
%void = OpTypeVoid
8972+
%uint = OpTypeInt 32 0
8973+
%uint_11 = OpConstant %uint 11
8974+
%uint_5 = OpConstant %uint 5
8975+
%uint_100 = OpConstant %uint 100
8976+
%21 = OpTypeFunction %void
8977+
%float = OpTypeFloat 32
8978+
%uint_32 = OpConstant %uint 32
8979+
%uint_6 = OpConstant %uint 6
8980+
%uint_131072 = OpConstant %uint 131072
8981+
%uint_3 = OpConstant %uint 3
8982+
%uint_7 = OpConstant %uint 7
8983+
%uint_0 = OpConstant %uint 0
8984+
%uint_46 = OpConstant %uint 46
8985+
%uint_1 = OpConstant %uint 1
8986+
%uint_8 = OpConstant %uint 8
8987+
%_ptr_Function_float = OpTypePointer Function %float
8988+
%int = OpTypeInt 32 1
8989+
%int_0 = OpConstant %int 0
8990+
%float_0 = OpConstant %float 0
8991+
%uint_50 = OpConstant %uint 50
8992+
%uint_34 = OpConstant %uint 34
8993+
%uint_2 = OpConstant %uint 2
8994+
%uint_56 = OpConstant %uint 56
8995+
%_runtimearr_float = OpTypeRuntimeArray %float
8996+
%uint_57 = OpConstant %uint 57
8997+
%42 = OpExtInst %void %1 DebugExpression
8998+
%43 = OpExtInst %void %1 DebugSource %5 %4
8999+
%44 = OpExtInst %void %1 DebugCompilationUnit %uint_100 %uint_5 %43 %uint_11
9000+
%45 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 %uint_6 %uint_131072
9001+
%46 = OpExtInst %void %1 DebugTypeVector %45 %uint_3
9002+
%47 = OpExtInst %void %1 DebugTypePointer %46 %uint_7 %uint_131072
9003+
%48 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void %47
9004+
%49 = OpExtInst %void %1 DebugFunction %7 %48 %43 %uint_46 %uint_6 %44 %7 %uint_0 %uint_46
9005+
%50 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 %uint_3 %uint_131072
9006+
%51 = OpExtInst %void %1 DebugTypeMember %9 %50 %43 %uint_8 %uint_11 %uint_0 %uint_32 %uint_0
9007+
%52 = OpExtInst %void %1 DebugTypeComposite %10 %uint_1 %43 %uint_6 %uint_8 %44 %10 %uint_32 %uint_131072 %51
9008+
%53 = OpExtInst %void %1 DebugTypeFunction %uint_0 %50 %52 %50
9009+
%54 = OpExtInst %void %1 DebugFunction %11 %53 %43 %uint_34 %uint_7 %44 %11 %uint_0 %uint_34
9010+
%55 = OpExtInst %void %1 DebugLocalVariable %12 %50 %43 %uint_34 %uint_7 %54 %uint_0 %uint_2
9011+
%56 = OpExtInst %void %1 DebugInlinedAt %uint_50 %49
9012+
%2 = OpFunction %void None %21
9013+
%57 = OpLabel
9014+
%59 = OpExtInst %void %1 DebugScope %54 %56
9015+
%60 = OpExtInst %void %1 DebugNoLine
9016+
%val = OpFAdd %float %float_0 %float_0
9017+
%61 = OpExtInst %void %1 DebugValue %55 %val %42
9018+
%62 = OpExtInst %void %1 DebugScope %49
9019+
%63 = OpExtInst %void %1 DebugLine %43 %uint_56 %uint_56 %uint_5 %uint_6
9020+
OpReturn
9021+
%66 = OpExtInst %void %1 DebugNoScope
9022+
OpFunctionEnd
9023+
)";
9024+
9025+
SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
9026+
}
9027+
88869028
} // namespace
88879029
} // namespace opt
88889030
} // namespace spvtools

0 commit comments

Comments
 (0)