fix(recompiler): give a loop's exit-tail branch a selection merge #275

Merged
oneandonlydean merged 1 commit from upstream-pr/275 into main 2026-10-02 19:44:47 +00:00
oneandonlydean commented 2026-10-01 21:36:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-01T21:36:33Z.


What

A conditional inside a loop with one target in the loop body and the other outside it is 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. The tail is then an if (c) { tail; break; }, which the loop already allows. exitTailSelectionMerge finds these in the pass over loop-control conditionals, and their merges are reserved after every other construct's, so a merge block already taken leaves the branch as before.

This builds on the structurizer from @boykopovar (af2ff821) and its loop-control handling; nothing outside Structurizer.cpp changes.

Measured in PPSA21564 on my integration branch: all 13 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.

Tested

  • Linux (CachyOS, RTX 4090). agc_structurizer_tests (already in ctest as agc_structurizer) now also 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 innermost loop's merge or continue. With the test change alone on current main it fails with "a loop exit through a tail block: block 3 branches to 4/5 without a merge, and neither is its loop's merge or continue"; with the fix it passes.
  • ctest --test-dir build --timeout 120: 96/96 pass on current main with this branch. agc_driver_recorder_tests, agc_driver_graphics_tests and agc_structurizer_tests pass when run directly.
  • Exercised in PPSA21564 on my integration branch (numbers above).

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:4709923863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/275). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T21:36:33Z. --- ### What A conditional inside a loop with one target in the loop body and the other outside it is 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. The tail is then an `if (c) { tail; break; }`, which the loop already allows. `exitTailSelectionMerge` finds these in the pass over loop-control conditionals, and their merges are reserved after every other construct's, so a merge block already taken leaves the branch as before. This builds on the structurizer from @boykopovar (af2ff821) and its loop-control handling; nothing outside `Structurizer.cpp` changes. Measured in PPSA21564 on my integration branch: all 13 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. ### Tested - Linux (CachyOS, RTX 4090). `agc_structurizer_tests` (already in ctest as `agc_structurizer`) now also 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 innermost loop's merge or continue. With the test change alone on current main it fails with "a loop exit through a tail block: block 3 branches to 4/5 without a merge, and neither is its loop's merge or continue"; with the fix it passes. - `ctest --test-dir build --timeout 120`: 96/96 pass on current main with this branch. `agc_driver_recorder_tests`, `agc_driver_graphics_tests` and `agc_structurizer_tests` pass when run directly. - Exercised in PPSA21564 on my integration branch (numbers above). ### 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
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!275
No description provided.