fix(shader): preserve structured exits during SPIR-V specialization

This commit is contained in:
boykopovar
2026-10-08 19:19:50 +03:00
parent 2767da6a8a
commit 199eeb1805
2 changed files with 85 additions and 4 deletions
@@ -1,4 +1,5 @@
#include "SpirvBackend/SpirvOptimizer.hpp"
#include "SpirvBackend/SpirvSpecialization.hpp"
#include <spirv-tools/libspirv.hpp>
#include <spirv/unified1/spirv.hpp>
#include <array>
@@ -188,6 +189,58 @@ void expectRejected(const std::vector<std::uint32_t>& words, std::uint32_t vulka
}
}
void testSpecializedSelectionExit() {
for (const bool useSwitch : {false, true}) {
const std::string source = std::string(R"(OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main" %index
OpExecutionMode %main LocalSize 8 1 1
OpDecorate %index BuiltIn LocalInvocationIndex
%void = OpTypeVoid
%bool = OpTypeBool
%uint = OpTypeInt 32 0
%input = OpTypePointer Input %uint
%private = OpTypePointer Private %uint
%function = OpTypeFunction %void
%zero = OpConstant %uint 0
%true = OpConstantTrue %bool
%index = OpVariable %input Input
%output = OpVariable %private Private
%main = OpFunction %void None %function
%entry = OpLabel
%lane = OpLoad %uint %index
%condition = OpIEqual %bool %lane %zero
OpSelectionMerge %exit None
)") + (useSwitch ? "OpSwitch %zero %body 1 %dead\n" : "OpBranchConditional %true %body %dead\n") + R"(%body = OpLabel
OpBranchConditional %condition %exit %nested
%nested = OpLabel
OpSelectionMerge %join None
OpBranchConditional %condition %write %join
%write = OpLabel
OpStore %output %lane
OpBranch %join
%join = OpLabel
OpBranch %exit
%dead = OpLabel
OpStore %output %zero
OpBranch %exit
%exit = OpLabel
OpReturn
OpFunctionEnd
)";
try {
const auto words = assemble(source);
static_cast<void>(ShaderRecompiler::ValidateAndOptimizeSpirv(words, Vulkan11, Spirv13, false, false));
const auto specialized = ShaderRecompiler::SpecializeSpirv(words);
static_cast<void>(ShaderRecompiler::ValidateAndOptimizeSpirv(specialized, Vulkan11, Spirv13, false, false));
check(opcodeCount(specialized, spv::OpStore) == 1u, "specialization retained the dead selection arm");
check(ShaderRecompiler::SpecializeSpirv(specialized) == specialized, "selection exit specialization is not stable");
} catch (const std::exception& error) {
check(false, std::string("specialized selection exit: ") + error.what());
}
}
}
void testInvalidInputIsRejected() {
const auto valid = assemble(moduleSource(Types.front(), false, ArithmeticSupport::StorageOnly));
expectRejected(valid, 0x00400000u, Spirv13, "SPIR-V 1.3 with Vulkan 1.0");
@@ -231,6 +284,7 @@ int main() {
const bool optimizationEnabled = mode == nullptr || std::string(mode) != "none";
testNarrowConversions(optimizationEnabled);
testInvalidInputIsRejected();
testSpecializedSelectionExit();
if (failures != 0) {
std::fprintf(stderr, "%d optimizer check(s) failed\n", failures);
return 1;
@@ -399,6 +399,23 @@ private:
if (block != 0u) blocks[block].push_back(index);
}
bool changed = false;
const auto linearSelection = [&](std::uint32_t target, std::uint32_t merge) {
std::set<std::uint32_t> visited;
while (target != merge) {
if (!visited.insert(target).second) return false;
const auto& indices = blocks.at(target);
for (const auto index : indices) {
const auto op = Opcode(instructions[index]);
if (op == spv::OpSelectionMerge || op == spv::OpLoopMerge) return false;
}
const auto& terminal = instructions[indices.back()];
const auto op = Opcode(terminal);
if (op == spv::OpBranchConditional || op == spv::OpSwitch) return false;
if (op != spv::OpBranch) return true;
target = terminal.at(1);
}
return true;
};
std::map<std::uint32_t, std::vector<std::uint32_t>> edges;
for (const auto& [label, indices] : blocks) {
auto& terminal = instructions[indices.back()];
@@ -416,10 +433,20 @@ private:
}
}
if (target != 0u) {
for (const auto index : indices) if (Opcode(instructions[index]) == spv::OpSelectionMerge) instructions[index].clear();
terminal = Make(spv::OpBranch, {target});
op = spv::OpBranch;
changed = true;
bool retainMerge = false;
for (const auto index : indices) {
auto& instruction = instructions[index];
if (Opcode(instruction) != spv::OpSelectionMerge) continue;
retainMerge = !linearSelection(target, instruction.at(1));
if (!retainMerge) instruction.clear();
}
const auto replacement = !retainMerge ? Make(spv::OpBranch, {target}) : op == spv::OpSwitch ?
Make(spv::OpSwitch, {terminal.at(1), target}) : Make(spv::OpBranchConditional, {terminal.at(1), target, target});
if (terminal != replacement) {
terminal = replacement;
changed = true;
}
op = Opcode(terminal);
}
auto& successors = edges[label];
if (op == spv::OpBranch) successors.push_back(terminal.at(1));