Merge pull request #413 from oneandonlydean/fix/recompiler-early-return-merge

fix(recompiler): give an early-return selection that joins its parent's merge a merge of its own
This commit is contained in:
Dean
2026-10-04 11:30:06 +13:00
committed by GitHub
3 changed files with 57 additions and 2 deletions
@@ -22,6 +22,7 @@ namespace {
using namespace ShaderRecompiler;
enum class Split {
None,
Clone,
Route
};
@@ -192,7 +193,9 @@ std::size_t recompile(const std::string& name, std::span<const std::uint32_t> co
void verifyProgram(const Program& program) {
const std::string name(program.name);
const auto result = verifyGraph(name, program.code);
if (program.split == Split::Clone) {
if (program.split == Split::None) {
require(result.clonedInstructions == 0 && result.routeVariables == 0, name + ": expected no clone or routing, got " + std::to_string(result.clonedInstructions) + " cloned instructions and " + std::to_string(result.routeVariables) + " route variables");
} else if (program.split == Split::Clone) {
require(result.clonedInstructions != 0 && result.routeVariables == 0, name + ": expected a clone, got " + std::to_string(result.clonedInstructions) + " cloned instructions and " + std::to_string(result.routeVariables) + " route variables");
} else {
require(result.clonedInstructions <= program.clonedLimit && result.routeVariables != 0, name + ": expected routing with at most " + std::to_string(program.clonedLimit) + " cloned instructions, got " + std::to_string(result.clonedInstructions) + " cloned instructions and " + std::to_string(result.routeVariables) + " route variables");
@@ -594,6 +597,26 @@ early_exit:
0xbf86000au, 0xe0300000u, 0x80000200u, 0x10060302u, 0x06060503u, 0x3a020303u, 0x7e160501u, 0x4a02020bu, 0xe0700000u, 0x80000100u,
0xbf810000u, 0xbefe0480u, 0xbf810000u},
Split::Clone},
{"early exit arm beside a branch to the parent's merge", R"(
v_mov_b32 v1, 0
s_cmp_eq_u32 s2, 64
s_cbranch_scc1 join
v_add_nc_u32 v1, 1, v0
s_cmp_eq_u32 s3, 0
s_cbranch_scc1 early_exit
join:
v_cmp_gt_u32 vcc, 8, v0
s_cbranch_vccnz store
v_add_nc_u32 v1, 2, v1
store:
buffer_store_dword v1, off, s[0:3], 0
s_endpgm
early_exit:
s_mov_b64 exec, 0
s_endpgm)",
{0x7e020280u, 0xbf06c002u, 0xbf850003u, 0x4a020081u, 0xbf068003u, 0xbf850006u, 0x7d880088u, 0xbf870001u, 0x4a020282u, 0xe0700000u,
0x80000100u, 0xbf810000u, 0xbefe0480u, 0xbf810000u},
Split::None},
};
int failures = 0;
for (const auto& program : programs) {
@@ -85,6 +85,16 @@ int main() {
auto exitTails = makeGraph({{1}, {2}, {3, 4}, {7}, {5, 6}, {7, 1}, {7}, {}});
Structurizer{}.Structurize(exitTails);
requireStructuredBranches(exitTails, "a loop with two exit tails");
auto earlyReturn = makeGraph({{2, 1}, {4, 2}, {3, 5}, {5}, {}, {}});
Structurizer{}.Structurize(earlyReturn);
requireStructuredBranches(earlyReturn, "an early return inside a selection that joins its parent's merge");
for (const auto& block : earlyReturn.blocks) {
const auto merge = block.terminator.mergeBlock;
if (merge != InvalidControlFlowId && !earlyReturn.Dominates(block.id, merge)) {
std::fprintf(stderr, "an early return inside a selection: header %u does not dominate its merge %u\n", block.id, merge);
return 1;
}
}
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
@@ -593,6 +593,28 @@ bool splitSharedMergeBlock(ControlFlowGraph& graph, std::uint32_t merge, const s
return true;
}
bool splitOneReturnJoin(ControlFlowGraph& graph) {
for (const auto& block : graph.blocks) {
if (block.terminator.kind != TerminatorKind::ConditionalBranch || findInnermostContainingLoop(graph, block.id) != nullptr || findSelectionMerge(graph, block) != InvalidControlFlowId) {
continue;
}
const auto returns = [&](std::uint32_t target) { return graph.Dominates(block.id, target) && hasLinearPathToTerminal(graph, target); };
const auto joinsEnclosing = [&](std::uint32_t target) {
const auto& predecessors = graph.FindBlock(target).predecessors;
return !graph.Dominates(block.id, target) && std::all_of(predecessors.begin(), predecessors.end(), [&](std::uint32_t predecessor) {
return graph.Dominates(block.id, predecessor) || graph.Dominates(predecessor, block.id);
});
};
const auto trueTarget = block.terminator.trueBlock;
const auto falseTarget = block.terminator.falseBlock;
const auto join = returns(trueTarget) && joinsEnclosing(falseTarget) ? falseTarget : returns(falseTarget) && joinsEnclosing(trueTarget) ? trueTarget : InvalidControlFlowId;
if (join != InvalidControlFlowId && splitSharedMergeBlock(graph, join, dominatedBlocks(graph, block.id, join))) {
return true;
}
}
return false;
}
bool splitOneLoopMerge(ControlFlowGraph& graph) {
for (const auto& loop : graph.naturalLoops) {
const auto constructBlocks = dominatedBlocks(graph, loop.headerBlock, loop.mergeBlock);
@@ -1401,7 +1423,7 @@ void Structurizer::splitSharedMergeBlocks(ControlFlowGraph& graph) const {
}
SplitBudget budget{programWords, std::max(CloneWordLimit, programWords / CloneBudgetDivisor), std::max(CloneWordLimit, programWords), nextGotoVariable};
for (std::uint32_t splits = 0; splits < splitBudget; ++splits) {
if (!splitOneLoopMerge(graph) && !splitOneSelectionMerge(graph, budget, [this](ControlFlowGraph& candidate) { recomputeAnalyses(candidate); })) {
if (!splitOneLoopMerge(graph) && !splitOneSelectionMerge(graph, budget, [this](ControlFlowGraph& candidate) { recomputeAnalyses(candidate); }) && !splitOneReturnJoin(graph)) {
return;
}
rebuildPredecessors(graph);