From 0ec3061a468b4ee44bb0d09674f0dfe51a5cfc8f Mon Sep 17 00:00:00 2001 From: alfalfa-star Date: Tue, 6 Oct 2026 15:31:36 +0200 Subject: [PATCH] fix(recompiler): merge a loop selection where its other arm joins or leaves A selection inside a loop whose arm either joins the other target or continues/breaks (`if (a) { if (b) continue; x; }`) got the nearest common post-dominator as its merge. When the loop exits after the outer join, that is the outer selection's merge, so both selections shared it. splitSharedMergeBlocks then inserted a forwarding block in front of it, recomputed the same merge, and repeated until the budget ran out: PPSA14632's compute shader 0x1a80ee0000 stopped with "CFG shared merge splitting exceeded budget: originalBlocks=92 currentBlocks=460". findSelectionMerge now picks the joined target when the other arm is a conditional block with the header as its only predecessor, and every successor of that arm is the joined target or the loop's continue or merge block. The shader structures with 13 added blocks and no clones or route variables. In the result the outer selection header dominates the loop's continue target, so the test's verifyStructured counted the continue target as a selection member that branches outside (back to the loop header). SPIR-V uses structural dominance, where the loop header's edge to its continue target keeps it out of the selection, so verifyStructured now skips the continue construct of an enclosing loop. spirv-val accepts the new test's SPIR-V for both subgroup sizes. Co-Authored-By: Claude Opus 5.5 --- .../prx/libSceAgcDriver/tests/ControlFlow.cpp | 31 +++++++++++++++++++ .../ControlFlow/src/Structurizer.cpp | 13 ++++++++ 2 files changed, 44 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp b/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp index ee6cd1e82..df874dd91 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ControlFlow.cpp @@ -53,6 +53,13 @@ std::vector constructBlocks(const ControlFlowGraph& graph, std::u return blocks; } +bool inEnclosingContinueConstruct(const ControlFlowGraph& graph, std::uint32_t header, std::uint32_t member) { + return std::any_of(graph.blocks.begin(), graph.blocks.end(), [&](const BasicBlock& loop) { + const auto continueBlock = loop.terminator.continueBlock; + return loop.terminator.loopHeader && graph.Dominates(loop.id, header) && !graph.Dominates(continueBlock, header) && graph.Dominates(continueBlock, member); + }); +} + void verifyStructured(const std::string& prefix, const ControlFlowGraph& graph) { std::map mergeOwners; std::vector> loopExits; @@ -66,6 +73,7 @@ void verifyStructured(const std::string& prefix, const ControlFlowGraph& graph) const auto& terminator = block.terminator; if (terminator.loopHeader || terminator.mergeBlock == InvalidControlFlowId) continue; for (const auto member : constructBlocks(graph, block.id, terminator.mergeBlock)) { + if (inEnclosingContinueConstruct(graph, block.id, member)) continue; for (const auto successor : graph.FindBlock(member).successors) { if (successor == terminator.mergeBlock || (graph.Dominates(block.id, successor) && !graph.Dominates(terminator.mergeBlock, successor))) continue; const bool loopExit = std::any_of(loopExits.begin(), loopExits.end(), [&](const auto& exits) { return successor == exits.first || successor == exits.second; }); @@ -363,6 +371,29 @@ latch: buffer_store_dword v1, off, s[0:3], 0 s_endpgm)", Store({0xbe880380u, 0x7e020280u, 0x7d880008u, 0xbf860004u, 0x7d880088u, 0xbf870003u, 0x4a020281u, 0xbf820002u, 0x4a020282u, 0x4a020283u, 0x80089008u, 0xbf0ac008u, 0xbf85fff5u}), Split::Clone}, + {"continue beside the inner merge of a nested selection in a loop", R"( + s_mov_b32 s8, 0 + v_mov_b32 v1, 0 +loop: + v_cmp_gt_u32 vcc, s8, v0 + s_cbranch_vccz join + v_cmp_gt_u32 vcc, 8, v0 + s_cbranch_vccnz inner + v_cmp_gt_u32 vcc, 4, v0 + s_cbranch_vccnz latch +inner: + v_add_nc_u32 v1, 1, v1 +join: + v_add_nc_u32 v1, 2, v1 + s_cmp_ge_u32 s8, 64 + s_cbranch_scc1 done +latch: + s_add_u32 s8, s8, 16 + s_branch loop +done: + buffer_store_dword v1, off, s[0:3], 0 + s_endpgm)", + Store({0xbe880380u, 0x7e020280u, 0x7d880008u, 0xbf860005u, 0x7d880088u, 0xbf870002u, 0x7d880084u, 0xbf870004u, 0x4a020281u, 0x4a020282u, 0xbf09c008u, 0xbf850002u, 0x80089008u, 0xbf82fff4u}), Split::None}, {"loop exit to the end of the program beside a kill exit", R"( v_mov_b32 v1, 0 s_mov_b64 s[20:21], exec diff --git a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp index 6f6eb09be..f23bd9258 100644 --- a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp +++ b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp @@ -464,6 +464,19 @@ std::uint32_t findSelectionMerge(const ControlFlowGraph& graph, const BasicBlock if (isLoopControlGateway(graph, *loop, falseTarget) && graph.Dominates(block.id, falseTarget) && isInsideLoopConstruct(graph, *loop, trueTarget)) { return falseTarget; } + const auto joinsAt = [&](std::uint32_t join, std::uint32_t other) { + const auto& arm = graph.FindBlock(other); + const bool armLeavesOrJoins = arm.terminator.kind == TerminatorKind::ConditionalBranch && arm.predecessors.size() == 1u && std::all_of(arm.successors.begin(), arm.successors.end(), [&](std::uint32_t successor) { + return successor == join || successor == loop->continueBlock || successor == loop->mergeBlock; + }); + return armLeavesOrJoins && isInsideLoopConstruct(graph, *loop, other) && isInsideLoopConstruct(graph, *loop, join) && graph.Dominates(block.id, join); + }; + if (joinsAt(trueTarget, falseTarget)) { + return trueTarget; + } + if (joinsAt(falseTarget, trueTarget)) { + return falseTarget; + } if (globalMerge != InvalidControlFlowId) { const bool trueJoins = reachesWithinIteration(graph, *loop, trueTarget, globalMerge); const bool falseJoins = reachesWithinIteration(graph, *loop, falseTarget, globalMerge);