feat(recompiler): structurize externally entered selection regions by cloning or route variables #134
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/134"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T00:33:21Z.
What
A selection region is the set of blocks reachable from the selection's arms before its merge. When that region is also entered from outside the header, it cannot be expressed as a SPIR-V selection construct.
splitOneSelectionMergethen threw "selection header block N has externally entered region block M; semantic block cloning is disabled". Three shapes hit this in practice:s_mov_b64 exec, 0/exp null/s_endpgm),s_cbranch_execzskips past a join into the middle of a selection region that holds most of the shader.The structurizer now makes such a region single-entry in one of two ways, chosen per region by its estimated translated size.
Size estimate.
GraphBuildergives each block an estimate of the SPIR-V words it translates to (estimatedSpirvWords, printed by--cfg):image_bvh_intersect_ray(2000) were too rare there to fit. They are weighted for the wave and traversal expansions they need.Cloning (node splitting), for small regions:
Route variables, for larger regions:
EmitBranchConditionuses it directly, like other goto-variable conditions.Merges beside private returns. A selection whose arms meet again only after private return tails had no post-dominator merge. When the existing rules find none, the merge is looked up with edges into private return tails ignored, or it is taken as the other arm when one arm only returns. The candidate must still exit the construct only through its merge.
Other changes:
applyBlockOrdernow remaps indirect branch targets as well.agc_shader_replay --cfgprints a request's control flow graph before and after structurization, and fails the request if structurization throws.It still throws, naming the header, the entered block and the reason, when:
Tested
agc_shader_control_flow_tests, run under ctest, builds 13 hand-assembled gfx1030 programs:s_cbranch_execzinto an expensive region with a loop, an entered region inside a loop, nested entered regions, a shared body reached after SCC is overwritten, early returns beside an entered region, and an arm that only returns.execzprogram must stay within 10% of the SPIR-V of the same program without the entering branch. It measures 1,094 words against 1,028..reqpaths, the test checks those graphs instead. It passes on all 310 loadable captured PPSA21564 requests.WORD_0source selector onv_bfrev_b32(relaxed locally),APS5_RUNTIME_DESCRIPTORS=1on the integration branch).shader_50059bc00, a 7-block, 111-instruction region, went from 19,304 to 12,876 words.ctest: 62/62 pass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-29T18:54:35Z.
Tested this on a retail title's ray tracing compute shader (wave64,
image_bvh_intersect_ray). Before this PR it fails with:The shape is an
s_cbranch_execzskip that jumps past a join into the middle of a selection region.With this PR applied, the shader structurizes and the title gets past it. Cloning makes the output much larger than a comparable shader, though:
OpSwitch, tried earlier)NVIDIA's compiler (nvgpucomp64, RTX PRO 6000 Blackwell) spent about 9 minutes in pipeline creation for it. The game is frozen for that whole time. The cloned region contains the BVH intersection code, which expands into a lot of SPIR-V. So a budget counted in guest instructions (2× the program) lets through a copy that is cheap in GCN terms but very expensive after translation. I could not check whether the cloned shader gives correct results: the next dispatch aborted on an unrelated unsupported feature (
SCRATCH_EN).For graphs like this one, a goto-elimination pass that doesn't duplicate code might be the better fit: structured guard variables in the style of shadPS4's GotoPass or KytyPS5's structurizer. Cloning could then stay for small tails like the shared discard ones. If it helps, I can share the request dump (
.reqforagc_shader_replay).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T19:27:21Z.
Thanks for testing this and for the numbers. You're right: the budget counts guest instructions, which doesn't bound the translated size, and a 9-minute pipeline build isn't acceptable. I'll change the PR to keep cloning only for small regions (budgeted by estimated SPIR-V size) and add guard-variable restructuring for larger entered regions, so shapes like yours don't duplicate the BVH code. The
.reqwould be very helpful — could you attach it here? I'll add it to the control-flow tests as a size regression.Imported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-29T19:37:19Z.
Thanks, that plan sounds right to me.
I've attached the
.req. It's for local checks only and won't be merged into the repository. It contains a retail title's shader binary and snapshots of its guest memory, so it can't live in the tree as a fixture. For the committed regression test, please build a synthetic CFG with the same shape: ans_cbranch_execzthat skips past a join into the middle of a selection region, with loops inside the entered part. The test could assert that no instruction range is duplicated, or that the cloned instruction count stays under a small bound. The.reqthen works as the end-to-end check:agc_shader_replay --spvon it should succeed, and the SPIR-V size shows whether the BVH code still gets duplicated.On guard variables, some data from our side. We ran KytyPS5's goto-elimination structurizer on this shader's CFG as a standalone probe. It is in their
mainas part of the "UFC 5 in-game" squash, and the original commit is "shader: simplify shared-exit control flow". It follows the outward/inward/lift scheme of shadPS4's GotoPass, with Boolean route variables. The algorithm was not changed.So every block is emitted exactly once, and there were no failures. This is a CFG-level result only: we haven't translated it to SPIR-V or measured pipeline build time yet. We're porting it now as a fallback for graphs our structurizer rejects, and I'll post those numbers when we have them.
Two things that bit or nearly bit in that design and may apply to guard-variable restructuring in general:
SiblingSharedExitKeepsCapturedConditionscovers exactly this: SCC is overwritten after the branch decision, before a shared exit.Keeping cloning for small tails, like the shared discard or return ones, makes sense. That is also where route variables would add the most overhead for the least benefit.
shader_25b1e8600.req.txt
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T20:27:50Z.
@STL1te pushed, thanks again for the
.reqand the notes. Cloning now only takes regions that are small in estimated SPIR-V words (per-instruction weights fitted on the recompiler's own output), and larger entered regions are routed instead: every edge into the entered part goes through one junction that tests Boolean route variables, so the region is emitted once. Each native condition is still evaluated only at the end of its original block and the route variable just records which edge was taken, so a later SCC/VCC/EXEC write can't change the route, and on two-lane wave64 the branch is combined across both halves where it sits.On your shader (replayed locally only, not added to the tree):
It structures with 0 cloned instructions and 2 route variables. The remaining 163 s isn't from routing: the same program without the skipping branch is only 145 words smaller.
The committed regression test is the synthetic shape you described (an
s_cbranch_execzskipping past a join into an expensive region with a loop), plus a shared body reached after SCC is overwritten, in the spirit of KytyPS5's captured-condition test. The checker follows route variables along every path and requires every original instruction range to run under the same branch condition with no duplication. Discard and return tails still clone.Replaying your
.reqon main also needs a newer request serializer, the VOP1 SDWAWORD_0selector onv_bfrev_b32and runtime scalar-buffer V#s, which aren't upstream yet, so I measured on our integration build.Imported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-30T07:12:18Z.
Tested the update (64eb5b4) on our integration branch (upstream
mainplus our recompiler commits) with the.reqI attached:agc_shader_control_flow_testspass, and on the.reqit reports 48 added blocks, 0 cloned instructions, 2 route variables.spirv-valclean. The first version of this PR gave 564,944 on the same base.On the remaining pipeline time: on an RTX PRO 6000 Blackwell with the driver's disk cache defeated, this SPIR-V takes 103.8 s to create. We profiled that last night, and it is not the routing. The module's main function has ~59k instructions plus 496 calls to the BDA lookup functions (
get_bda_pointeris 612 instructions with a binary-search loop,probe_bda_pointer128 calls); ~88% of them come from scalar loads through GPU-selected V#s, which cost one probe plus four byte lookups per dword, times two wave64 halves. NVIDIA's compiler inlines all of them (+250k instructions, +496 loops) and its compile time tracks the number ofOpConvertUToPtrsites and loops.The fix is one line:
FunctionControlDontInlineMaskon the lookup functions inSpirvBdaLookup.cpp. With it, the same 278,626-word module from this PR creates in 4.84 s, and our goto build goes from 104 s to 4.8 s as well. Same-size shaders without BDA reads are unaffected. Happy to put that up as a separate PR if you want it.Not run in game yet with this version; the first version got the title past this shader, and the output here is the same shape and size as our goto build that did.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-30T07:29:08Z.
Thanks for testing it, and for comparing it against your KytyPS5 port. Good to see the two land within a percent of each other. That's a great find on the BDA lookups. Please do put the DontInline change up as its own PR; it's a clean fix and we'd pick it up straight away for PPSA21564, which uses the same lookup path. Glad the routing itself isn't the cost.
Imported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-30T07:34:28Z.
@oneandonlydean Done: the DontInline change is up as #174, one line on top of current main, with the pipeline timings in the description. For the shader from this thread it is 104 s → 4.8 s on our goto build and 103.8 s → 4.84 s on the SPIR-V from this PR, so it applies on top of either.
On the structurizer itself: since the two come out within a percent of each other and yours is the one going upstream with tests and a corpus, we're going to drop our KytyPS5 port from our integration branch and carry this PR's patch instead until it lands. We'll report if the game ever hits a shape it refuses (back-edge entries, wrap-around regions).