fix(shader): keep the back-edge of a loop whose continue block is dead #4600

Merged
coltken95 merged 1 commit from upstream-pr/4600 into main 2026-10-13 03:52:25 +00:00
coltken95 commented 2026-10-12 16:36:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-12T16:36:10Z.


What

Pipeline specialization (SpirvSpecialization.cpp, prune) replaced every unreachable block with OpUnreachable. When a loop's continue target was unreachable, that removed the header's only back-edge, and validation rejects the module: "Loop header is targeted by 0 back-edge blocks but the standard requires exactly one". This happens when the loop condition compares two constants, e.g. s_cmp_lt_u32 with an inline constant and a literal: either the emitter already exits the latch unconditionally, or specialization folds the latch's branch. Without SPIRV-Tools (the default build) the invalid module is handed to the driver.

An unreachable continue target of a live loop header now keeps an OpBranch to its header, as SPIRV-Tools' dead branch elimination does. The header's phis keep an operand for that predecessor, naming the phi itself: the edge is never taken and the continue block's values are gone, and the validator requires an operand for every predecessor, reachable or not.

The phi rewrite now compares contents rather than sizes, since replacing that operand keeps the size.

Independent of #4580 and compatible with #4431 (its cleanup removes only unreferenced OpUnreachable blocks; the kept continue block is referenced by OpLoopMerge); both merge cleanly with this branch.

Tested

Linux (Ubuntu 24.04, GCC 13.3), homebrew compute shaders, no title.

  • New case in agc_spirv_optimizer_tests (testSpecializedDeadContinue): a loop whose latch exits on OpConstantFalse, and one whose continue target is already unreachable, both with a header phi fed by the continue block. Both fail validation after specialization without the change and pass with it; a second specialization leaves the result unchanged.
  • Recompiling 20000 mutated compute shaders with validation on: 59 were rejected for this rule before, none after.
  • All 448 agc_* ctest entries pass in a RelWithDebInfo build with ANYPS5_ENABLE_SPIRV_TOOLS=ON (GPU tests skip without a device).
  • tools/check_conventions.py passes.
  • Windows not built locally.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832297729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4600). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-12T16:36:10Z. --- ### What Pipeline specialization (`SpirvSpecialization.cpp`, `prune`) replaced every unreachable block with `OpUnreachable`. When a loop's continue target was unreachable, that removed the header's only back-edge, and validation rejects the module: "Loop header is targeted by 0 back-edge blocks but the standard requires exactly one". This happens when the loop condition compares two constants, e.g. `s_cmp_lt_u32` with an inline constant and a literal: either the emitter already exits the latch unconditionally, or specialization folds the latch's branch. Without SPIRV-Tools (the default build) the invalid module is handed to the driver. An unreachable continue target of a live loop header now keeps an `OpBranch` to its header, as SPIRV-Tools' dead branch elimination does. The header's phis keep an operand for that predecessor, naming the phi itself: the edge is never taken and the continue block's values are gone, and the validator requires an operand for every predecessor, reachable or not. The phi rewrite now compares contents rather than sizes, since replacing that operand keeps the size. Independent of #4580 and compatible with #4431 (its cleanup removes only unreferenced `OpUnreachable` blocks; the kept continue block is referenced by `OpLoopMerge`); both merge cleanly with this branch. ### Tested Linux (Ubuntu 24.04, GCC 13.3), homebrew compute shaders, no title. - New case in `agc_spirv_optimizer_tests` (`testSpecializedDeadContinue`): a loop whose latch exits on `OpConstantFalse`, and one whose continue target is already unreachable, both with a header phi fed by the continue block. Both fail validation after specialization without the change and pass with it; a second specialization leaves the result unchanged. - Recompiling 20000 mutated compute shaders with validation on: 59 were rejected for this rule before, none after. - All 448 `agc_*` ctest entries pass in a RelWithDebInfo build with `ANYPS5_ENABLE_SPIRV_TOOLS=ON` (GPU tests skip without a device). - `tools/check_conventions.py` passes. - Windows not built locally. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb
github-actions[bot] commented 2026-10-12 17:51:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T17:51:18Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4288 core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp L243
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129613497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4600#issuecomment-6129613497). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T17:51:18Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4288 | `core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp` [L243](https://github.com/boykopovar/AnyPS5/blob/6124bf063f6348e4451ad0b9c67f82acec5bc1c2/core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp#L243) | | |
kr4y4 commented 2026-10-13 01:46:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T01:46:35Z.


A Windows run of the new case with ANYPS5_ENABLE_SPIRV_TOOLS=ON, since the CI build jobs were skipped for this PR.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head 52b679c55 merged into main 284a1c334.

  • With SPIRV-Tools: agc_spirv_optimizer, agc_spirv_optimizer_full and agc_spirv_optimizer_none pass with --repeat until-fail:3. With SpirvSpecialization.cpp as on main, both new cases fail in all three: Loop header '12[%12]' is targeted by 0 back-edge blocks but the standard requires exactly one.
  • Default build, without SPIRV-Tools: the full suite (ctest --timeout 120 --parallel 4) passes 1020 of 1020 on both GPUs.
  • Together with #4580: the branches merge cleanly in either order, and with both merged, the three optimizer tests and agc_spirv_branch_targets pass with --repeat until-fail:3.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136278849 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4600#issuecomment-6136278849). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-13T01:46:35Z. --- A Windows run of the new case with `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, since the CI build jobs were skipped for this PR. Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head 52b679c55 merged into `main` 284a1c334. - With SPIRV-Tools: `agc_spirv_optimizer`, `agc_spirv_optimizer_full` and `agc_spirv_optimizer_none` pass with `--repeat until-fail:3`. With `SpirvSpecialization.cpp` as on `main`, both new cases fail in all three: `Loop header '12[%12]' is targeted by 0 back-edge blocks but the standard requires exactly one`. - Default build, without SPIRV-Tools: the full suite (`ctest --timeout 120 --parallel 4`) passes 1020 of 1020 on both GPUs. - Together with #4580: the branches merge cleanly in either order, and with both merged, the three optimizer tests and `agc_spirv_branch_targets` pass with `--repeat until-fail:3`. Written with AI assistance.
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!4600
No description provided.