fix(shader): let the SRT walker decide a branch through an invariant phi, and resolve control-flow identities after every fold #23343

Merged
chitters2004 merged 2 commits from upstream-pr/23343 into main 2026-11-27 02:48:26 +00:00
chitters2004 commented 2026-11-18 22:08:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-18T22:08:19Z.


What

Two small fixes that run for every shader. Neither needs a switch.

1. SRT walker (SrtEvaluator::MayDecide). It decided a LogicalAnd/LogicalOr when one side was decidable but stopped at a Phi. When a block is split and joined again (the constant-buffer NaN guard does this today), a branch condition computed before the split reaches the old terminator through a phi whose incoming values are copies of each other. The plan then dropped the condition, the walker visited both successors, and descriptor sources that the branch rules out failed to evaluate. That aborted the recompile.
MayDecide now resolves an invariant phi and decides its value. It seeds its memo before recursing, so a phi that reaches itself is undecided.
Test: verifyMayDecideSharedConditions (in agc_shader_memory_coherent_atomic_loads) gains phi-of-copies, lanes and looped cases. It fails without the fix ("MayDecide did not decide a branch on a phi of two copies of a condition with a uniform conjunct").

2. ConstantFolder::Fold(IrProgram&) resolves control-flow identities itself. ResolveControlFlowIdentities was called by hand after four of the five folds in PrepareResourceProgram/prepareInterface and after two in Deserialize. The fold after the vector window index narrower had no call, so a later pass could read a branch condition that the fold had replaced. Fold now ends with the call and the explicit ones are gone.
Test: agc_constant_phi folds a branch condition LogicalAnd(compare, true) and checks the metadata names the compare. It fails without the change.

Effect on every shader. Both changes can alter what a shader compiles to (item 1 only where a branch is decided through a phi; item 2 only where a stale condition was read). On the 319 replay requests (316 compile; 3 fail on main for an unrelated reason) the agc_shader_replay --spv output is byte-identical to main's with default settings.

Disk cache: entries from before this change are invalidated, as for any recompiler source change.

Tested

  • agc_shader_memory_coherent_atomic_loads, agc_constant_phi: both fail first.
  • Default-setting replay over the 319 requests: SPIR-V identical to main.
  • Fork CI on 11a60f8aa5 (linux-gate and windows, ^agc_constant_phi|^agc_shader_memory): run 44394091057, green. Linux and Windows ran agc_constant_phi, agc_shader_memory and agc_shader_memory_coherent_atomic_loads, all passing, with 0 validation-layer VUIDs and nothing failing on the head that passes on main.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR
  • No comments in code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: the fork CI Linux and Windows runners above; Colewiz7 ran the merged head on RADV (RX 7900 XT, 7800X3D iGPU) in the comments
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5384918605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23343). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-18T22:08:19Z. --- ### What Two small fixes that run for every shader. Neither needs a switch. **1. SRT walker (`SrtEvaluator::MayDecide`).** It decided a `LogicalAnd`/`LogicalOr` when one side was decidable but stopped at a `Phi`. When a block is split and joined again (the constant-buffer NaN guard does this today), a branch condition computed before the split reaches the old terminator through a phi whose incoming values are copies of each other. The plan then dropped the condition, the walker visited both successors, and descriptor sources that the branch rules out failed to evaluate. That aborted the recompile. `MayDecide` now resolves an invariant phi and decides its value. It seeds its memo before recursing, so a phi that reaches itself is undecided. Test: `verifyMayDecideSharedConditions` (in `agc_shader_memory_coherent_atomic_loads`) gains phi-of-copies, lanes and looped cases. It fails without the fix ("MayDecide did not decide a branch on a phi of two copies of a condition with a uniform conjunct"). **2. `ConstantFolder::Fold(IrProgram&)` resolves control-flow identities itself.** `ResolveControlFlowIdentities` was called by hand after four of the five folds in `PrepareResourceProgram`/`prepareInterface` and after two in `Deserialize`. The fold after the vector window index narrower had no call, so a later pass could read a branch condition that the fold had replaced. `Fold` now ends with the call and the explicit ones are gone. Test: `agc_constant_phi` folds a branch condition `LogicalAnd(compare, true)` and checks the metadata names the compare. It fails without the change. **Effect on every shader.** Both changes can alter what a shader compiles to (item 1 only where a branch is decided through a phi; item 2 only where a stale condition was read). On the 319 replay requests (316 compile; 3 fail on main for an unrelated reason) the `agc_shader_replay --spv` output is byte-identical to main's with default settings. Disk cache: entries from before this change are invalidated, as for any recompiler source change. ### Tested - `agc_shader_memory_coherent_atomic_loads`, `agc_constant_phi`: both fail first. - Default-setting replay over the 319 requests: SPIR-V identical to main. - Fork CI on `11a60f8aa5` (linux-gate and windows, `^agc_constant_phi|^agc_shader_memory`): run 44394091057, green. Linux and Windows ran `agc_constant_phi`, `agc_shader_memory` and `agc_shader_memory_coherent_atomic_loads`, all passing, with 0 validation-layer VUIDs and nothing failing on the head that passes on main. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR - [x] No comments in code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: the fork CI Linux and Windows runners above; Colewiz7 ran the merged head on RADV (RX 7900 XT, 7800X3D iGPU) in the comments - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-19 05:37:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T05:37:05Z.

Review: COMMENTED


Reviewed — no blocking findings

The review found no verified blocking defect; ShaderMemory.cpp was not read in full, so the evidence is incomplete and does not support approval.

What I ran

Reviewed head 11a60f8aa5160fa61421f269e8804f17dbde3718 using the prepared diff and pinned repository reader. No tests or builds were run.

Results

Check Result Note
Diff and targeted source review passed Five changed files read; one changed file not read in full
Tests and build not run Author-reported CI and replay results were not independently verified

Findings

Blocking

None verified in the code read.

Non-blocking

None.

Sources

Reviewed the prepared diff and targeted source at the pinned head, including the changes to MayDecide and ConstantFolder::Fold.

Not verified

core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp was excluded from the review folder and not read through the repository reader. Tests, builds, replay comparisons, and author-reported CI or GPU results were not independently run or confirmed. The other changed files were read; Deserialize.cpp and Recompiler.cpp were read in the changed regions.

To get this to a good state

Nothing is required. The optional point is to inspect ShaderMemory.cpp before treating this review as complete.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5969845408 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23343#pullrequestreview-5969845408). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T05:37:05Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The review found no verified blocking defect; `ShaderMemory.cpp` was not read in full, so the evidence is incomplete and does not support approval. ## What I ran Reviewed head `11a60f8aa5160fa61421f269e8804f17dbde3718` using the prepared diff and pinned repository reader. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Five changed files read; one changed file not read in full | | Tests and build | not run | Author-reported CI and replay results were not independently verified | ## Findings ### Blocking None verified in the code read. ### Non-blocking None. ## Sources Reviewed the prepared diff and targeted source at the pinned head, including the changes to `MayDecide` and `ConstantFolder::Fold`. ## Not verified `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp` was excluded from the review folder and not read through the repository reader. Tests, builds, replay comparisons, and author-reported CI or GPU results were not independently run or confirmed. The other changed files were read; `Deserialize.cpp` and `Recompiler.cpp` were read in the changed regions. ## To get this to a good state Nothing is required. The optional point is to inspect `ShaderMemory.cpp` before treating this review as complete.
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!23343
No description provided.