Run ADCE when the printf extension is used. (#5215)
This is for
https://github.com/microsoft/DirectXShaderCompiler/issues/5136.
diff --git a/source/opt/aggressive_dead_code_elim_pass.cpp b/source/opt/aggressive_dead_code_elim_pass.cpp
index 51a6524..1645638 100644
--- a/source/opt/aggressive_dead_code_elim_pass.cpp
+++ b/source/opt/aggressive_dead_code_elim_pass.cpp
@@ -156,7 +156,8 @@
"Expecting an import of an extension's instruction set.");
const std::string extension_name = inst.GetInOperand(0).AsString();
if (spvtools::utils::starts_with(extension_name, "NonSemantic.") &&
- extension_name != "NonSemantic.Shader.DebugInfo.100") {
+ (extension_name != "NonSemantic.Shader.DebugInfo.100") &&
+ (extension_name != "NonSemantic.DebugPrintf")) {
return false;
}
}
diff --git a/test/opt/aggressive_dead_code_elim_test.cpp b/test/opt/aggressive_dead_code_elim_test.cpp
index 0d94151..83aab3c 100644
--- a/test/opt/aggressive_dead_code_elim_test.cpp
+++ b/test/opt/aggressive_dead_code_elim_test.cpp
@@ -7857,6 +7857,33 @@
SinglePassRunAndMatch<AggressiveDCEPass>(text, true, false, false);
}
+TEST_F(AggressiveDCETest, RemoveWhenUsingPrintfExtension) {
+ // Remove dead n_out output variable from module
+ const std::string text = R"(
+; CHECK: OpExtInstImport "NonSemantic.DebugPrintf"
+; CHECK-NOT: OpVariable
+ OpCapability Shader
+ %1 = OpExtInstImport "NonSemantic.DebugPrintf"
+ OpMemoryModel Logical GLSL450
+ OpEntryPoint GLCompute %main "main"
+ OpExecutionMode %main LocalSize 8 8 1
+ OpSource HLSL 660
+ OpName %main "main"
+ %uint = OpTypeInt 32 0
+ %void = OpTypeVoid
+ %5 = OpTypeFunction %void
+%_ptr_Function_uint = OpTypePointer Function %uint
+ %main = OpFunction %void None %5
+ %7 = OpLabel
+ %8 = OpVariable %_ptr_Function_uint Function
+ OpReturn
+ OpFunctionEnd
+)";
+
+ SetTargetEnv(SPV_ENV_VULKAN_1_3);
+ SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
+}
+
} // namespace
} // namespace opt
} // namespace spvtools