From 66ddf59c224f85d5d6082b3e9782e0daa359808d Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Fri, 2 Oct 2026 10:23:02 +1300 Subject: [PATCH] fix(recompiler): give a loop's exit-tail branch a selection merge A conditional inside a loop with one target in the loop body and the other outside it was taken for a break and emitted without a merge. When the outside target is not the loop's merge but a tail of blocks that reaches it, SPIR-V has no construct for that branch: spirv-val rejects the module ("Selection must be structured") and the driver is free to compile it into anything. Such a branch now merges at its in-body target when the tail is private to it (every tail block is dominated by the tail's first block, stays in the loop construct and leaves only for the loop's merge), so the tail is an "if (c) { tail; break; }" the loop already allows. Merges are given to these after every other construct's, and one already taken leaves the branch as before. All 13 PPSA21564 programs whose V#s are read from GPU memory (the deferred lighting and ray-traced passes) had two such branches in their traversal loops. With this change none of the 74 dumped title programs has one, and the 209 modules of a title-to-cinematic run validate apart from three unrelated image-offset and entry-point errors. Tests: agc_structurizer_tests structures a loop that exits through one tail block and a loop with two exit tails, and requires every merge-less conditional to target its loop's merge or continue. --- .../libSceAgcDriver/tests/Structurizer.cpp | 58 ++++++++++++++++--- .../ControlFlow/src/Structurizer.cpp | 52 +++++++++++++++++ 2 files changed, 103 insertions(+), 7 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp b/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp index 197224a25..cc527eefa 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp @@ -1,13 +1,17 @@ #include "ControlFlow/Structurizer.hpp" +#include #include #include +#include +#include #include #include using namespace ShaderRecompiler; -int main() { - const std::vector> successors{{1}, {2}, {5, 3}, {5, 4}, {7}, {6, 7}, {}, {1}}; +namespace { + +ControlFlowGraph makeGraph(const std::vector>& successors) { ControlFlowGraph graph; graph.entryBlock = 0; for (std::uint32_t id = 0; id < successors.size(); ++id) { @@ -35,15 +39,55 @@ int main() { for (const auto& block : graph.blocks) { for (const auto successor : block.successors) graph.blocks[successor].predecessors.push_back(block.id); } + return graph; +} + +const BasicBlock* innermostLoopHeader(const ControlFlowGraph& graph, std::uint32_t blockId) { + const BasicBlock* innermost = nullptr; + std::size_t innermostSize = 0; + for (const auto& header : graph.blocks) { + if (!header.terminator.loopHeader || !graph.Dominates(header.id, blockId) || graph.Dominates(header.terminator.mergeBlock, blockId) || blockId == header.terminator.mergeBlock) continue; + const auto size = static_cast(std::count_if(graph.blocks.begin(), graph.blocks.end(), [&](const BasicBlock& block) { return graph.Dominates(header.id, block.id) && !graph.Dominates(header.terminator.mergeBlock, block.id); })); + if (innermost == nullptr || size < innermostSize) { + innermost = &header; + innermostSize = size; + } + } + return innermost; +} + +void requireStructuredBranches(const ControlFlowGraph& graph, const char* name) { + for (const auto& block : graph.blocks) { + const auto& terminator = block.terminator; + if (terminator.kind != TerminatorKind::ConditionalBranch || terminator.loopHeader || terminator.mergeBlock != InvalidControlFlowId || terminator.trueBlock == terminator.falseBlock) continue; + const auto* loop = innermostLoopHeader(graph, block.id); + const auto exits = [&](std::uint32_t target) { return loop != nullptr && (target == loop->terminator.mergeBlock || target == loop->terminator.continueBlock); }; + if (!exits(terminator.trueBlock) && !exits(terminator.falseBlock)) { + throw std::runtime_error(std::string(name) + ": block " + std::to_string(block.id) + " branches to " + std::to_string(terminator.trueBlock) + "/" + std::to_string(terminator.falseBlock) + " without a merge, and neither is its loop's merge or continue"); + } + } +} + +} + +int main() { try { - Structurizer{}.Structurize(graph); + auto nested = makeGraph({{1}, {2}, {5, 3}, {5, 4}, {7}, {6, 7}, {}, {1}}); + Structurizer{}.Structurize(nested); + if (nested.FindBlock(2).terminator.mergeBlock == nested.FindBlock(3).terminator.mergeBlock) { + std::fprintf(stderr, "the nested selections share merge block %u\n", nested.FindBlock(2).terminator.mergeBlock); + return 1; + } + requireStructuredBranches(nested, "nested selections"); + auto exitTail = makeGraph({{1}, {2}, {3, 4}, {6}, {6, 5}, {1}, {}}); + Structurizer{}.Structurize(exitTail); + requireStructuredBranches(exitTail, "a loop exit through a tail block"); + auto exitTails = makeGraph({{1}, {2}, {3, 4}, {7}, {5, 6}, {7, 1}, {7}, {}}); + Structurizer{}.Structurize(exitTails); + requireStructuredBranches(exitTails, "a loop with two exit tails"); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } - if (graph.FindBlock(2).terminator.mergeBlock == graph.FindBlock(3).terminator.mergeBlock) { - std::fprintf(stderr, "the nested selections share merge block %u\n", graph.FindBlock(2).terminator.mergeBlock); - return 1; - } return 0; } diff --git a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp index 983df6988..f20825afa 100644 --- a/core/shader/recompiler/ControlFlow/src/Structurizer.cpp +++ b/core/shader/recompiler/ControlFlow/src/Structurizer.cpp @@ -501,6 +501,47 @@ bool isInnermostLoopControlConditional(const ControlFlowGraph& graph, const Basi return (isControlTarget(trueTarget) && (isControlTarget(falseTarget) || isInsideLoopConstruct(graph, *loop, falseTarget))) || (isControlTarget(falseTarget) && isInsideLoopConstruct(graph, *loop, trueTarget)); } +std::uint32_t exitTailSelectionMerge(const ControlFlowGraph& graph, const BasicBlock& block) { + if (block.terminator.kind != TerminatorKind::ConditionalBranch) { + return InvalidControlFlowId; + } + const auto* loop = findInnermostContainingLoop(graph, block.id); + if (loop == nullptr || loop->mergeBlock == InvalidControlFlowId || loop->continueBlock == InvalidControlFlowId || block.id == loop->continueBlock) { + return InvalidControlFlowId; + } + const auto trueTarget = block.terminator.trueBlock; + const auto falseTarget = block.terminator.falseBlock; + const bool trueInBody = contains(loop->blocks, trueTarget); + if (trueInBody == contains(loop->blocks, falseTarget)) { + return InvalidControlFlowId; + } + const auto body = trueInBody ? trueTarget : falseTarget; + const auto tail = trueInBody ? falseTarget : trueTarget; + if (tail == loop->mergeBlock || tail == loop->continueBlock || !isInsideLoopConstruct(graph, *loop, tail) || !graph.Dominates(block.id, body) || !graph.Dominates(block.id, tail)) { + return InvalidControlFlowId; + } + std::vector pending{tail}; + std::vector seen; + while (!pending.empty()) { + const auto blockId = pending.back(); + pending.pop_back(); + if (contains(seen, blockId)) { + continue; + } + seen.push_back(blockId); + for (const auto successor : graph.FindBlock(blockId).successors) { + if (successor == loop->mergeBlock) { + continue; + } + if (contains(loop->blocks, successor) || !graph.Dominates(tail, successor) || !isInsideLoopConstruct(graph, *loop, successor)) { + return InvalidControlFlowId; + } + pending.push_back(successor); + } + } + return body; +} + bool mergeLeavesContainingLoop(const ControlFlowGraph& graph, std::uint32_t header, std::uint32_t merge) { for (const auto& loop : graph.naturalLoops) { if (loop.headerBlock != header && isInsideLoopConstruct(graph, loop, header) && !isInsideLoopConstruct(graph, loop, merge)) { @@ -1116,11 +1157,15 @@ void Structurizer::Structurize(ControlFlowGraph& graph) const { header.terminator.continueBlock = loop.continueBlock; } + std::vector> exitTails; for (auto& block : graph.blocks) { if (block.terminator.kind != TerminatorKind::ConditionalBranch || block.terminator.loopHeader) { continue; } if (isInnermostLoopControlConditional(graph, block)) { + if (const auto merge = exitTailSelectionMerge(graph, block); merge != InvalidControlFlowId) { + exitTails.emplace_back(block.id, merge); + } continue; } @@ -1132,6 +1177,13 @@ void Structurizer::Structurize(ControlFlowGraph& graph) const { reserveMergeBlock(block.id, merge); block.terminator.mergeBlock = merge; } + for (const auto& [header, merge] : exitTails) { + if (mergeHeaders.contains(merge)) { + continue; + } + reserveMergeBlock(header, merge); + graph.FindBlock(header).terminator.mergeBlock = merge; + } } void Structurizer::computeDominatorTree(ControlFlowGraph& graph) const {