Skip to content

Commit b1ad37b

Browse files
opt: Mark InterpolateAt* argument as live for DCE (#5824)
The GLSL 450 InterpolateAt* instructions should be treated as a load by dead code elimination. This is part of microsoft/DirectXShaderCompiler#3649.
1 parent 5b38abc commit b1ad37b

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

source/opt/aggressive_dead_code_elim_pass.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ constexpr uint32_t kCopyMemorySourceAddrInIdx = 1;
4040
constexpr uint32_t kLoadSourceAddrInIdx = 0;
4141
constexpr uint32_t kDebugDeclareOperandVariableIndex = 5;
4242
constexpr uint32_t kGlobalVariableVariableIndex = 12;
43+
constexpr uint32_t kExtInstSetInIdx = 0;
44+
constexpr uint32_t kExtInstOpInIdx = 1;
45+
constexpr uint32_t kInterpolantInIdx = 2;
4346

4447
// Sorting functor to present annotation instructions in an easy-to-process
4548
// order. The functor orders by opcode first and falls back on unique id
@@ -422,6 +425,19 @@ uint32_t AggressiveDCEPass::GetLoadedVariableFromNonFunctionCalls(
422425
case spv::Op::OpCopyMemorySized:
423426
return GetVariableId(
424427
inst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx));
428+
case spv::Op::OpExtInst: {
429+
if (inst->GetSingleWordInOperand(kExtInstSetInIdx) ==
430+
context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450()) {
431+
auto ext_inst = inst->GetSingleWordInOperand(kExtInstOpInIdx);
432+
switch (ext_inst) {
433+
case GLSLstd450InterpolateAtCentroid:
434+
case GLSLstd450InterpolateAtOffset:
435+
case GLSLstd450InterpolateAtSample:
436+
return inst->GetSingleWordInOperand(kInterpolantInIdx);
437+
}
438+
}
439+
break;
440+
}
425441
default:
426442
break;
427443
}

test/opt/aggressive_dead_code_elim_test.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8106,6 +8106,133 @@ OpFunctionEnd
81068106
SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
81078107
}
81088108

8109+
TEST_F(AggressiveDCETest, MarkCentroidInterpolantLive) {
8110+
const std::string spirv =
8111+
R"(OpCapability InterpolationFunction
8112+
OpCapability Shader
8113+
%1 = OpExtInstImport "GLSL.std.450"
8114+
OpMemoryModel Logical GLSL450
8115+
OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8116+
OpExecutionMode %main OriginUpperLeft
8117+
OpSource HLSL 680
8118+
OpName %in_var_COLOR "in.var.COLOR"
8119+
OpName %out_var_SV_Target "out.var.SV_Target"
8120+
OpName %main "main"
8121+
OpName %param_var_p1 "param.var.p1"
8122+
OpDecorate %in_var_COLOR Location 0
8123+
OpDecorate %out_var_SV_Target Location 0
8124+
%float = OpTypeFloat 32
8125+
%v4float = OpTypeVector %float 4
8126+
%_ptr_Input_v4float = OpTypePointer Input %v4float
8127+
%_ptr_Output_v4float = OpTypePointer Output %v4float
8128+
%void = OpTypeVoid
8129+
%11 = OpTypeFunction %void
8130+
%_ptr_Function_v4float = OpTypePointer Function %v4float
8131+
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8132+
%out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8133+
%main = OpFunction %void None %11
8134+
%13 = OpLabel
8135+
%14 = OpVariable %_ptr_Function_v4float Function
8136+
%param_var_p1 = OpVariable %_ptr_Function_v4float Function
8137+
%15 = OpLoad %v4float %in_var_COLOR
8138+
OpStore %param_var_p1 %15
8139+
%16 = OpExtInst %v4float %1 InterpolateAtCentroid %param_var_p1
8140+
OpStore %14 %16
8141+
%17 = OpLoad %v4float %14
8142+
OpStore %out_var_SV_Target %17
8143+
OpReturn
8144+
OpFunctionEnd
8145+
)";
8146+
8147+
SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8148+
}
8149+
8150+
TEST_F(AggressiveDCETest, MarkSampleInterpolantLive) {
8151+
const std::string spirv =
8152+
R"(OpCapability InterpolationFunction
8153+
OpCapability Shader
8154+
%1 = OpExtInstImport "GLSL.std.450"
8155+
OpMemoryModel Logical GLSL450
8156+
OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8157+
OpExecutionMode %main OriginUpperLeft
8158+
OpSource HLSL 680
8159+
OpName %in_var_COLOR "in.var.COLOR"
8160+
OpName %out_var_SV_Target "out.var.SV_Target"
8161+
OpName %main "main"
8162+
OpName %param_var_p1 "param.var.p1"
8163+
OpDecorate %in_var_COLOR Location 0
8164+
OpDecorate %out_var_SV_Target Location 0
8165+
%float = OpTypeFloat 32
8166+
%int = OpTypeInt 32 1
8167+
%v4float = OpTypeVector %float 4
8168+
%_ptr_Input_v4float = OpTypePointer Input %v4float
8169+
%_ptr_Output_v4float = OpTypePointer Output %v4float
8170+
%void = OpTypeVoid
8171+
%12 = OpTypeFunction %void
8172+
%_ptr_Function_v4float = OpTypePointer Function %v4float
8173+
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8174+
%out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8175+
%int_123 = OpConstant %int 123
8176+
%main = OpFunction %void None %12
8177+
%15 = OpLabel
8178+
%16 = OpVariable %_ptr_Function_v4float Function
8179+
%param_var_p1 = OpVariable %_ptr_Function_v4float Function
8180+
%17 = OpLoad %v4float %in_var_COLOR
8181+
OpStore %param_var_p1 %17
8182+
%18 = OpExtInst %v4float %1 InterpolateAtSample %param_var_p1 %int_123
8183+
OpStore %16 %18
8184+
%19 = OpLoad %v4float %16
8185+
OpStore %out_var_SV_Target %19
8186+
OpReturn
8187+
OpFunctionEnd
8188+
)";
8189+
8190+
SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8191+
}
8192+
8193+
TEST_F(AggressiveDCETest, MarkOffsetInterpolantLive) {
8194+
const std::string spirv =
8195+
R"(OpCapability InterpolationFunction
8196+
OpCapability Shader
8197+
%1 = OpExtInstImport "GLSL.std.450"
8198+
OpMemoryModel Logical GLSL450
8199+
OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8200+
OpExecutionMode %main OriginUpperLeft
8201+
OpSource HLSL 680
8202+
OpName %in_var_COLOR "in.var.COLOR"
8203+
OpName %out_var_SV_Target "out.var.SV_Target"
8204+
OpName %main "main"
8205+
OpName %param_var_p1 "param.var.p1"
8206+
OpDecorate %in_var_COLOR Location 0
8207+
OpDecorate %out_var_SV_Target Location 0
8208+
%float = OpTypeFloat 32
8209+
%int = OpTypeInt 32 1
8210+
%v4float = OpTypeVector %float 4
8211+
%_ptr_Input_v4float = OpTypePointer Input %v4float
8212+
%_ptr_Output_v4float = OpTypePointer Output %v4float
8213+
%void = OpTypeVoid
8214+
%12 = OpTypeFunction %void
8215+
%_ptr_Function_v4float = OpTypePointer Function %v4float
8216+
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8217+
%out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8218+
%int_123 = OpConstant %int 123
8219+
%main = OpFunction %void None %12
8220+
%15 = OpLabel
8221+
%16 = OpVariable %_ptr_Function_v4float Function
8222+
%param_var_p1 = OpVariable %_ptr_Function_v4float Function
8223+
%17 = OpLoad %v4float %in_var_COLOR
8224+
OpStore %param_var_p1 %17
8225+
%18 = OpExtInst %v4float %1 InterpolateAtOffset %param_var_p1 %int_123
8226+
OpStore %16 %18
8227+
%19 = OpLoad %v4float %16
8228+
OpStore %out_var_SV_Target %19
8229+
OpReturn
8230+
OpFunctionEnd
8231+
)";
8232+
8233+
SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8234+
}
8235+
81098236
} // namespace
81108237
} // namespace opt
81118238
} // namespace spvtools

0 commit comments

Comments
 (0)