Skip to content

Commit deca4f6

Browse files
committed
[OPT] Process debug scope instructions of debug value instructions
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 49a2371 commit deca4f6

File tree

2 files changed

+144
-33
lines changed

2 files changed

+144
-33
lines changed

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, inst);
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
@@ -8866,6 +8866,148 @@ OpFunctionEnd
88668866

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

0 commit comments

Comments
 (0)