Skip to content

Commit 20cb35f

Browse files
spirv-opt: Correct DebugValue placement in ADCE (#6491)
When a DebugDeclare's variable is eliminated, it can generate multiple DebugValue instructions, one for each OpStore to the eliminated variable. The current logic is not placing the DebugValues in the correct place. They are currently placed at the DebugDeclare location, and multiple DebugValues are not tested. This PR fixes the location and extends the DebugValue test such that a DebugDeclare is broken into multiple DebugValues, and shows that the placement of both DebugValues is now correct.
1 parent 49a2371 commit 20cb35f

2 files changed

Lines changed: 43 additions & 26 deletions

File tree

source/opt/aggressive_dead_code_elim_pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ Pass::Status AggressiveDCEPass::ProcessDebugInformation(
309309
// DebugDeclare Variable is not live. Find the value that was being
310310
// stored to this variable. If it's live then create a new DebugValue
311311
// with this value. Otherwise let it die in peace.
312-
get_def_use_mgr()->ForEachUser(var_id, [this, var_id,
313-
inst](Instruction* user) {
312+
get_def_use_mgr()->ForEachUser(var_id, [this,
313+
var_id](Instruction* user) {
314314
if (user->opcode() == spv::Op::OpStore) {
315315
uint32_t stored_value_id = 0;
316316
const uint32_t kStoreValueInIdx = 1;
@@ -320,10 +320,10 @@ Pass::Status AggressiveDCEPass::ProcessDebugInformation(
320320
}
321321

322322
// value being stored is still live
323-
Instruction* next_inst = inst->NextNode();
323+
Instruction* next_inst = user->NextNode();
324324
bool added =
325325
context()->get_debug_info_mgr()->AddDebugValueForVariable(
326-
user, var_id, stored_value_id, inst);
326+
user, var_id, stored_value_id, user);
327327
if (added && next_inst) {
328328
auto new_debug_value = next_inst->PreviousNode();
329329
live_insts_.Set(new_debug_value->unique_id());

test/opt/aggressive_dead_code_elim_test.cpp

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8809,57 +8809,74 @@ TEST_F(AggressiveDCETest, ConvertDebugDeclareToDebugValue) {
88098809
OpExtension "SPV_KHR_non_semantic_info"
88108810
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
88118811
OpMemoryModel Logical GLSL450
8812-
OpEntryPoint Fragment %main "main" %input %output
8812+
OpEntryPoint Fragment %main "main" %input1 %input2 %output
88138813
OpExecutionMode %main OriginUpperLeft
88148814
%5 = OpString "test.hlsl"
88158815
OpSource HLSL 600
88168816
OpName %main "main"
8817-
OpDecorate %input Location 0
8817+
OpDecorate %input1 Location 0
8818+
OpDecorate %input2 Location 1
88188819
OpDecorate %output Location 0
88198820
%void = OpTypeVoid
88208821
%float = OpTypeFloat 32
8822+
%v3float = OpTypeVector %float 3
88218823
%v4float = OpTypeVector %float 4
88228824
%uint = OpTypeInt 32 0
88238825
%uint_1 = OpConstant %uint 1
88248826
%uint_2 = OpConstant %uint 2
88258827
%uint_3 = OpConstant %uint 3
8828+
%uint_4 = OpConstant %uint 4
88268829
%uint_32 = OpConstant %uint 32
88278830
%uint_0 = OpConstant %uint 0
8831+
%float_0 = OpConstant %float 0
88288832
%float_1 = OpConstant %float 1
8829-
%_ptr_Input_v4float = OpTypePointer Input %v4float
8830-
%_ptr_Output_v4float = OpTypePointer Output %v4float
8833+
%_ptr_Input_v3float = OpTypePointer Input %v3float
8834+
%_ptr_Output_v3float = OpTypePointer Output %v3float
88318835
%_ptr_Function_v4float = OpTypePointer Function %v4float
8832-
%input = OpVariable %_ptr_Input_v4float Input
8833-
%output = OpVariable %_ptr_Output_v4float Output
8836+
%input1 = OpVariable %_ptr_Input_v3float Input
8837+
%input2 = OpVariable %_ptr_Input_v3float Input
8838+
%output = OpVariable %_ptr_Output_v3float Output
88348839
%29 = OpTypeFunction %void
8840+
; CHECK: [[initial:%\w+]] = OpConstantComposite
88358841
; CHECK: [[expr:%\w+]] = OpExtInst %void {{%\w+}} DebugExpression
8836-
; CHECK: [[source:%\w+]] = OpExtInst %void {{%\w+}} DebugSource %5
8842+
; CHECK: [[source:%\w+]] = OpExtInst %void {{%\w+}} DebugSource %6
88378843
%30 = OpExtInst %void %1 DebugSource %5
88388844
%31 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_3 %30 %uint_32
88398845
; CHECK: [[basic:%\w+]] = OpExtInst %void {{%\w+}} DebugTypeBasic
88408846
%32 = OpExtInst %void %1 DebugTypeBasic %5 %uint_32 %uint_3 %uint_0
8841-
; CHECK: [[vec_type:%\w+]] = OpExtInst %void {{%\w+}} DebugTypeVector [[basic]] %uint_3
8842-
%33 = OpExtInst %void %1 DebugTypeVector %32 %uint_3
8847+
; CHECK: [[vec_type:%\w+]] = OpExtInst %void {{%\w+}} DebugTypeVector [[basic]] %uint_4
8848+
%33 = OpExtInst %void %1 DebugTypeVector %32 %uint_4
88438849
%34 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void
88448850
%35 = OpExtInst %void %1 DebugFunction %5 %34 %30 %uint_1 %uint_0 %31 %5 %uint_0 %uint_1
8851+
; CHECK: [[local:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable %6 [[vec_type]] [[source]] %uint_1
88458852
%36 = OpExtInst %void %1 DebugLocalVariable %5 %33 %30 %uint_1 %uint_0 %35 %uint_0
8846-
; CHECK: [[local:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable %5 [[vec_type]] [[source]] %uint_2
8847-
%37 = OpExtInst %void %1 DebugLocalVariable %5 %33 %30 %uint_2 %uint_0 %35 %uint_0
88488853
%38 = OpExtInst %void %1 DebugExpression
8854+
%initial_value = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_1
88498855
%main = OpFunction %void None %29
88508856
%39 = OpLabel
8851-
%dead_var = OpVariable %_ptr_Function_v4float Function
8852-
%live_var = OpVariable %_ptr_Function_v4float Function
8853-
; CHECK: [[live:%\w+]] = OpLoad
8854-
%live_input = OpLoad %v4float %input
8855-
%dead_value = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
8856-
OpStore %dead_var %dead_value
8857+
%dead_pos_w = OpVariable %_ptr_Function_v4float Function
8858+
%live_var1 = OpVariable %_ptr_Function_v4float Function
8859+
%live_var2 = OpVariable %_ptr_Function_v4float Function
8860+
%input1_value = OpLoad %v3float %input1
8861+
%input2_value = OpLoad %v3float %input2
88578862
; CHECK-NOT: DebugDeclare
8858-
%40 = OpExtInst %void %1 DebugDeclare %36 %dead_var %38
8859-
OpStore %live_var %live_input
8860-
; CHECK: DebugValue [[local]] [[live]] [[expr]]
8861-
%41 = OpExtInst %void %1 DebugDeclare %37 %live_var %38
8862-
OpStore %output %live_input
8863+
%40 = OpExtInst %void %1 DebugDeclare %36 %dead_pos_w %38
8864+
OpStore %dead_pos_w %initial_value
8865+
; CHECK: DebugValue %30 [[initial]] %31
8866+
OpStore %live_var1 %initial_value
8867+
%computed1 = OpVectorTimesScalar %v3float %input1_value %float_1
8868+
%computed2 = OpFAdd %v3float %computed1 %input2_value
8869+
; CHECK: [[new:%\w+]] = OpCompositeConstruct
8870+
%new_pos_w = OpCompositeConstruct %v4float %computed2 %float_1
8871+
OpStore %dead_pos_w %new_pos_w
8872+
; CHECK: DebugValue %30 [[new]] %31
8873+
OpStore %live_var2 %new_pos_w
8874+
%loaded1 = OpLoad %v4float %live_var1
8875+
%loaded2 = OpLoad %v4float %live_var2
8876+
%pos_xyz1 = OpVectorShuffle %v3float %loaded1 %loaded1 0 1 2
8877+
%pos_xyz2 = OpVectorShuffle %v3float %loaded2 %loaded2 0 1 2
8878+
%mixed_result = OpFAdd %v3float %pos_xyz1 %pos_xyz2
8879+
OpStore %output %mixed_result
88638880
OpReturn
88648881
OpFunctionEnd
88658882
)";

0 commit comments

Comments
 (0)