feat(recompiler): structurize externally entered selection regions by cloning or route variables #134

Merged
oneandonlydean merged 1 commit from upstream-pr/134 into main 2026-10-01 07:48:36 +00:00
oneandonlydean commented 2026-09-29 00:33:21 +00:00 (Migrated from github.com)

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. splitOneSelectionMerge then threw "selection header block N has externally entered region block M; semantic block cloning is disabled". Three shapes hit this in practice:

  • pixel shaders where several selections branch to one shared discard tail (s_mov_b64 exec, 0 / exp null / s_endpgm),
  • compute shaders with short-circuit conditions that jump into a shared body,
  • ray-traced compute passes where an s_cbranch_execz skips 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. GraphBuilder gives each block an estimate of the SPIR-V words it translates to (estimatedSpirvWords, printed by --cfg):

  • The per-instruction weights come from a fit of the recompiler's per-block output (before spirv-opt, phis excluded) over 8,300 blocks of captured shaders: VALU 60, buffer and flat 120, SMEM 180, image 220, LDS 400.
  • SALU (8) and exports (20) fit near zero, so they keep a small weight.
  • Lane ops (120) and 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:

  • It copies the blocks that the outside edges reach inside the region and redirects the outside edges to the copies. Each copy keeps the same instruction range and terminator.
  • When the header's merge is a shared linear return tail, it first tries giving the header a private copy of that tail.
  • A region is cloned only while it is at most 1024 estimated words and at most an eighth of the program (at least 256), and while all clones together stay within a quarter of the program.
  • The shared discard tails (about 28 words) and short-circuit bodies still clone.

Route variables, for larger regions:

  • Every edge into the entered part (from outside, and from the header's own arms) goes through a new empty junction block. So does every edge from the arms to the merge.
  • On the way, each rerouted edge passes empty blocks that assign Boolean route variables (the IR's goto variables, which SSA construction turns into phis of constants). The junction tests them in order to reach the edge's original target.
  • The entered part then follows the junction and is emitted once. With the junction as the header's merge, the header's construct no longer contains it.
  • Branch conditions are still evaluated only at the end of their original blocks. A route variable records the edge that a block already took, so an SCC, VCC or EXEC write after the decision cannot change the route. Nothing re-reads a native condition later.
  • In the two-lane wave64 layout (wave64 on a 32-wide host), the source branch is combined across both halves where it is, as before. The route variable then holds the same constant in both halves, so EmitBranchCondition uses it directly, like other goto-variable conditions.
  • In the single-lane layout, each invocation follows the route of the edge it took. The junction also reconverges invocations before the shared region, as the unsplit program would. Separate clones would split them.
  • Routing is refused when:
    • a rerouted edge is a back edge or leaves an indirect branch,
    • the region wraps around to a dominating block,
    • routing would change the natural loops or make the graph irreducible.
  • After a refusal, it falls back to cloning, bounded by the program's estimated size. Otherwise it throws with the header, the entered block and both reasons.

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:

  • applyBlockOrder now remaps indirect branch targets as well.
  • agc_shader_replay --cfg prints 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:

  • a region block is entered by a back edge,
  • neither cloning (a partial loop copy, a wrap-around region, an indirect or unsupported branch, or the budget) nor routing applies.

Tested

  • Linux (CachyOS, GCC): agc_shader_control_flow_tests, run under ctest, builds 13 hand-assembled gfx1030 programs:
    • Cloned: shared tail, shared tail with a selection, shared tail in a loop, shared early exit, short-circuit condition, return tail past an entered region, and shared early exit beside an expensive region.
    • Routed: s_cbranch_execz into 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.
  • For each program the test checks that:
    • every selection construct exits only through its merge,
    • the structured graph runs the same instruction ranges under the same branch conditions as the original. It follows the route variables along each path to check this.
    • the cloned instructions stay within the program's bound (0 for most routed programs),
    • the program recompiles, with SPIR-V validation, for 64- and 32-lane subgroups.
  • The routed execz program must stay within 10% of the SPIR-V of the same program without the entering branch. It measures 1,094 words against 1,028.
  • Given .req paths, the test checks those graphs instead. It passes on all 310 loadable captured PPSA21564 requests.
  • The reviewer's ray-tracing compute shader (3,200 code words, 228 blocks, 6 loops, wave64), replayed locally and not added to the repository:
    • Structured with 48 added blocks, 0 cloned instructions and 2 route variables.
    • SPIR-V: 408,081 words with the previous cloning (our integration build), 200,141 words now, and 199,996 for the same shader with the skipping branch removed.
    • Pipeline creation on an RTX 4090 (driver 610.57.04, disk cache off): 581 s before and 163 s now.
    • Replaying it needs three things this PR does not include:
      • request version 3,
      • the VOP1 SDWA WORD_0 source selector on v_bfrev_b32 (relaxed locally),
      • runtime scalar-buffer V#s (APS5_RUNTIME_DESCRIPTORS=1 on the integration branch).
  • Captured PPSA21564 requests (283 that recompile):
    • One changed from cloning to routing: shader_50059bc00, a 7-block, 111-instruction region, went from 19,304 to 12,876 words.
    • The 3-instruction discard tails still clone.
    • Every other request produces the same word count.
  • 1,600 randomly generated goto-heavy programs:
    • 529 structure and verify now, against 434 with the previous cloning. None that structured before fails now.
    • All 529 recompile with validation.
  • ctest: 62/62 pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4670964149 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134). Originally posted by [oneandonlydean](https://github.com/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. `splitOneSelectionMerge` then threw "selection header block N has externally entered region block M; semantic block cloning is disabled". Three shapes hit this in practice: - pixel shaders where several selections branch to one shared discard tail (`s_mov_b64 exec, 0` / `exp null` / `s_endpgm`), - compute shaders with short-circuit conditions that jump into a shared body, - ray-traced compute passes where an `s_cbranch_execz` skips 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.** `GraphBuilder` gives each block an estimate of the SPIR-V words it translates to (`estimatedSpirvWords`, printed by `--cfg`): - The per-instruction weights come from a fit of the recompiler's per-block output (before spirv-opt, phis excluded) over 8,300 blocks of captured shaders: VALU 60, buffer and flat 120, SMEM 180, image 220, LDS 400. - SALU (8) and exports (20) fit near zero, so they keep a small weight. - Lane ops (120) and `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: - It copies the blocks that the outside edges reach inside the region and redirects the outside edges to the copies. Each copy keeps the same instruction range and terminator. - When the header's merge is a shared linear return tail, it first tries giving the header a private copy of that tail. - A region is cloned only while it is at most 1024 estimated words and at most an eighth of the program (at least 256), and while all clones together stay within a quarter of the program. - The shared discard tails (about 28 words) and short-circuit bodies still clone. **Route variables**, for larger regions: - Every edge into the entered part (from outside, and from the header's own arms) goes through a new empty junction block. So does every edge from the arms to the merge. - On the way, each rerouted edge passes empty blocks that assign Boolean route variables (the IR's goto variables, which SSA construction turns into phis of constants). The junction tests them in order to reach the edge's original target. - The entered part then follows the junction and is emitted once. With the junction as the header's merge, the header's construct no longer contains it. - Branch conditions are still evaluated only at the end of their original blocks. A route variable records the edge that a block already took, so an SCC, VCC or EXEC write after the decision cannot change the route. Nothing re-reads a native condition later. - In the two-lane wave64 layout (wave64 on a 32-wide host), the source branch is combined across both halves where it is, as before. The route variable then holds the same constant in both halves, so `EmitBranchCondition` uses it directly, like other goto-variable conditions. - In the single-lane layout, each invocation follows the route of the edge it took. The junction also reconverges invocations before the shared region, as the unsplit program would. Separate clones would split them. - Routing is refused when: - a rerouted edge is a back edge or leaves an indirect branch, - the region wraps around to a dominating block, - routing would change the natural loops or make the graph irreducible. - After a refusal, it falls back to cloning, bounded by the program's estimated size. Otherwise it throws with the header, the entered block and both reasons. **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:** - `applyBlockOrder` now remaps indirect branch targets as well. - `agc_shader_replay --cfg` prints 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: - a region block is entered by a back edge, - neither cloning (a partial loop copy, a wrap-around region, an indirect or unsupported branch, or the budget) nor routing applies. ### Tested - Linux (CachyOS, GCC): `agc_shader_control_flow_tests`, run under ctest, builds 13 hand-assembled gfx1030 programs: - Cloned: shared tail, shared tail with a selection, shared tail in a loop, shared early exit, short-circuit condition, return tail past an entered region, and shared early exit beside an expensive region. - Routed: `s_cbranch_execz` into 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. - For each program the test checks that: - every selection construct exits only through its merge, - the structured graph runs the same instruction ranges under the same branch conditions as the original. It follows the route variables along each path to check this. - the cloned instructions stay within the program's bound (0 for most routed programs), - the program recompiles, with SPIR-V validation, for 64- and 32-lane subgroups. - The routed `execz` program must stay within 10% of the SPIR-V of the same program without the entering branch. It measures 1,094 words against 1,028. - Given `.req` paths, the test checks those graphs instead. It passes on all 310 loadable captured PPSA21564 requests. - The reviewer's ray-tracing compute shader (3,200 code words, 228 blocks, 6 loops, wave64), replayed locally and not added to the repository: - Structured with 48 added blocks, 0 cloned instructions and 2 route variables. - SPIR-V: 408,081 words with the previous cloning (our integration build), 200,141 words now, and 199,996 for the same shader with the skipping branch removed. - Pipeline creation on an RTX 4090 (driver 610.57.04, disk cache off): 581 s before and 163 s now. - Replaying it needs three things this PR does not include: - request version 3, - the VOP1 SDWA `WORD_0` source selector on `v_bfrev_b32` (relaxed locally), - runtime scalar-buffer V#s (`APS5_RUNTIME_DESCRIPTORS=1` on the integration branch). - Captured PPSA21564 requests (283 that recompile): - One changed from cloning to routing: `shader_50059bc00`, a 7-block, 111-instruction region, went from 19,304 to 12,876 words. - The 3-instruction discard tails still clone. - Every other request produces the same word count. - 1,600 randomly generated goto-heavy programs: - 529 structure and verify now, against 434 with the previous cloning. None that structured before fails now. - All 529 recompile with validation. - `ctest`: 62/62 pass. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
STL1te commented 2026-09-29 18:54:35 +00:00 (Migrated from github.com)

Imported 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:

selection header block 33 has externally entered region block 53; semantic block cloning is disabled

The shape is an s_cbranch_execz skip 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:

Shader Code words SPIR-V words
This shader, with this PR 3200 564,944
Another compute shader of the same title, no cloning needed 3124 150,018
This shader, block dispatcher fallback (loop + OpSwitch, tried earlier) 3200 317,570

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 (.req for agc_shader_replay).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5896616491 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5896616491). Originally posted by [STL1te](https://github.com/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: ``` selection header block 33 has externally entered region block 53; semantic block cloning is disabled ``` The shape is an `s_cbranch_execz` skip 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: | Shader | Code words | SPIR-V words | |---|---|---| | This shader, with this PR | 3200 | 564,944 | | Another compute shader of the same title, no cloning needed | 3124 | 150,018 | | This shader, block dispatcher fallback (loop + `OpSwitch`, tried earlier) | 3200 | 317,570 | 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 (`.req` for `agc_shader_replay`).
oneandonlydean commented 2026-09-29 19:27:21 +00:00 (Migrated from github.com)

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 .req would be very helpful — could you attach it here? I'll add it to the control-flow tests as a size regression.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5897143139 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5897143139). Originally posted by [oneandonlydean](https://github.com/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 `.req` would be very helpful — could you attach it here? I'll add it to the control-flow tests as a size regression.
STL1te commented 2026-09-29 19:37:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-29T19:37:19Z.


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 .req would be very helpful — could you attach it here? I'll add it to the control-flow tests as a size regression.


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: an s_cbranch_execz that 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 .req then works as the end-to-end check: agc_shader_replay --spv on 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 main as 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.

Input Output
Blocks 228 261
Loops 6 6
Selection constructs 110
Boolean variables 142
Guest instructions 2171 2171

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:

  • Capture branch conditions at the source block. Once gotos move across other code, SCC, VCC or EXEC may already be overwritten when the branch finally executes. KytyPS5 snapshots the native condition into a variable at the end of the original block and branches on the variable later. Their compute test SiblingSharedExitKeepsCapturedConditions covers exactly this: SCC is overwritten after the branch decision, before a shared exit.
  • Emulated wave64 on a 32-wide host. The captured scalar condition has to be combined across both halves at the capture point, not at the final branch. Otherwise the two halves can diverge on what should be one wave-wide decision.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5897296054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5897296054). Originally posted by [STL1te](https://github.com/STL1te) on 2026-09-29T19:37:19Z. --- > 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 `.req` would be very helpful — could you attach it here? I'll add it to the control-flow tests as a size regression. --- 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: an `s_cbranch_execz` that 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 `.req` then works as the end-to-end check: `agc_shader_replay --spv` on 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 `main` as 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. | | Input | Output | |---|---|---| | Blocks | 228 | 261 | | Loops | 6 | 6 | | Selection constructs | | 110 | | Boolean variables | | 142 | | Guest instructions | 2171 | 2171 | 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: - **Capture branch conditions at the source block.** Once gotos move across other code, SCC, VCC or EXEC may already be overwritten when the branch finally executes. KytyPS5 snapshots the native condition into a variable at the end of the original block and branches on the variable later. Their compute test `SiblingSharedExitKeepsCapturedConditions` covers exactly this: SCC is overwritten after the branch decision, before a shared exit. - **Emulated wave64 on a 32-wide host.** The captured scalar condition has to be combined across both halves at the capture point, not at the final branch. Otherwise the two halves can diverge on what should be one wave-wide decision. 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](https://github.com/user-attachments/files/32818657/shader_25b1e8600.req.txt)
oneandonlydean commented 2026-09-29 20:27:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T20:27:50Z.


@STL1te pushed, thanks again for the .req and 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):

Old cloning Routed Skip branch removed
SPIR-V words 408,081 200,141 199,996
Pipeline creation (RTX 4090) 581 s 163 s

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_execz skipping 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 .req on main also needs a newer request serializer, the VOP1 SDWA WORD_0 selector on v_bfrev_b32 and runtime scalar-buffer V#s, which aren't upstream yet, so I measured on our integration build.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5898181885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5898181885). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T20:27:50Z. --- @STL1te pushed, thanks again for the `.req` and 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): | | Old cloning | Routed | Skip branch removed | |---|---|---|---| | SPIR-V words | 408,081 | 200,141 | 199,996 | | Pipeline creation (RTX 4090) | 581 s | 163 s | | 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_execz` skipping 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 `.req` on main also needs a newer request serializer, the VOP1 SDWA `WORD_0` selector on `v_bfrev_b32` and runtime scalar-buffer V#s, which aren't upstream yet, so I measured on our integration build.
STL1te commented 2026-09-30 07:12:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by STL1te on 2026-09-30T07:12:18Z.


Tested the update (64eb5b4) on our integration branch (upstream main plus our recompiler commits) with the .req I attached:

  • agc_shader_control_flow_tests pass, and on the .req it reports 48 added blocks, 0 cloned instructions, 2 route variables.
  • SPIR-V: 278,626 words, spirv-val clean. The first version of this PR gave 564,944 on the same base.
  • For comparison, our own port of KytyPS5's goto elimination structures the same shader to 280,168 words on the same base, so the two approaches come out equivalent here. Nice.

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_pointer is 612 instructions with a binary-search loop, probe_bda_pointer 128 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 of OpConvertUToPtr sites and loops.

The fix is one line: FunctionControlDontInlineMask on the lookup functions in SpirvBdaLookup.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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5906108941 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5906108941). Originally posted by [STL1te](https://github.com/STL1te) on 2026-09-30T07:12:18Z. --- Tested the update (64eb5b4) on our integration branch (upstream `main` plus our recompiler commits) with the `.req` I attached: - `agc_shader_control_flow_tests` pass, and on the `.req` it reports 48 added blocks, 0 cloned instructions, 2 route variables. - SPIR-V: 278,626 words, `spirv-val` clean. The first version of this PR gave 564,944 on the same base. - For comparison, our own port of KytyPS5's goto elimination structures the same shader to 280,168 words on the same base, so the two approaches come out equivalent here. Nice. 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_pointer` is 612 instructions with a binary-search loop, `probe_bda_pointer` 128 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 of `OpConvertUToPtr` sites and loops. The fix is one line: `FunctionControlDontInlineMask` on the lookup functions in `SpirvBdaLookup.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.
oneandonlydean commented 2026-09-30 07:29:08 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5906359755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5906359755). Originally posted by [oneandonlydean](https://github.com/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.
STL1te commented 2026-09-30 07:34:28 +00:00 (Migrated from github.com)

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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5906438792 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/134#issuecomment-5906438792). Originally posted by [STL1te](https://github.com/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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!134
No description provided.