fix(shader): forward vector window stores to loads within basic blocks #4213

Closed
Kikoarl wants to merge 1 commit from upstream-pr/4213 into main
Kikoarl commented 2026-10-12 01:33:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-12T01:33:21Z.


What

Forwards StoreVectorWindow values to subsequent LoadVectorWindow instructions with matching constant indices within the same basic block during shader simplification (Recompiler.cpp).

In shaders with relative vector addressing (v_movrels_b32 / v_movreld_b32), vector registers are mapped into the function-scope vector_window array. When intra-wave reduction sequences (e.g. v_max_f32 with DPP or Permlane16) write intermediate steps into windowed registers and immediately read them, the load previously remained as a separate LoadVectorWindow instruction. This broke the SSA expression tree, preventing MatchHalfWaveScan and ReadLaneEliminator from optimizing the reduction and leading to illegal lane reads outside the host subgroup (e.g. v_readlane_b32 of lane 63 on 32-lane host subgroups).

  • Stores to constant indices are recorded in an intra-block map.
  • Loads with matching constant indices are replaced directly with the stored value, preserving SSA dominance.
  • Dynamic stores with non-constant indices clear the map to prevent unsound forwarding across unknown offsets.
  • Added pinned-value unit tests in core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp (agc_vector_window_tests) verifying:
    1. Sequential constant stores and loads fold without redundant OpAccessChain instructions (shape.accessChains == 2).
    2. Dynamic stores invalidate forwarding, preserving required access chains (shape.accessChains == 4).

Tested

  • Linux x86_64, 13th Gen Intel(R) Core(TM) i7-13700T, NVIDIA GeForce RTX 5060 Ti (subgroup size 32).
  • Unit tests: agc_vector_window_tests passed (100% in ctest -R "^agc_vector_window$"). Full vector instruction suite passed (wave32_wide_subgroup, vector_min_max_med16, etc.).
  • Astro Bot (PPSA21567): Compute shader 0x500798d00 recompiled successfully into 302,178 SPIR-V words without throwing. Reached live 3D in-level gameplay past 320 seconds without crashing (1,322 frames presented, 77,600 dispatches executed).
  • Conventions: python3 tools/check_conventions.py passed with zero errors.

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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823891976 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4213). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-12T01:33:21Z. --- ### What Forwards `StoreVectorWindow` values to subsequent `LoadVectorWindow` instructions with matching constant indices within the same basic block during shader simplification (`Recompiler.cpp`). In shaders with relative vector addressing (`v_movrels_b32` / `v_movreld_b32`), vector registers are mapped into the function-scope `vector_window` array. When intra-wave reduction sequences (e.g. `v_max_f32` with DPP or Permlane16) write intermediate steps into windowed registers and immediately read them, the load previously remained as a separate `LoadVectorWindow` instruction. This broke the SSA expression tree, preventing `MatchHalfWaveScan` and `ReadLaneEliminator` from optimizing the reduction and leading to illegal lane reads outside the host subgroup (e.g. `v_readlane_b32` of lane 63 on 32-lane host subgroups). - Stores to constant indices are recorded in an intra-block map. - Loads with matching constant indices are replaced directly with the stored value, preserving SSA dominance. - Dynamic stores with non-constant indices clear the map to prevent unsound forwarding across unknown offsets. - Added pinned-value unit tests in `core/libs/prx/libSceAgcDriver/tests/VectorWindow.cpp` (`agc_vector_window_tests`) verifying: 1. Sequential constant stores and loads fold without redundant `OpAccessChain` instructions (`shape.accessChains == 2`). 2. Dynamic stores invalidate forwarding, preserving required access chains (`shape.accessChains == 4`). ### Tested - Linux x86_64, 13th Gen Intel(R) Core(TM) i7-13700T, NVIDIA GeForce RTX 5060 Ti (subgroup size 32). - Unit tests: `agc_vector_window_tests` passed (100% in `ctest -R "^agc_vector_window$"`). Full vector instruction suite passed (`wave32_wide_subgroup`, `vector_min_max_med16`, etc.). - Astro Bot (*PPSA21567*): Compute shader `0x500798d00` recompiled successfully into 302,178 SPIR-V words without throwing. Reached live 3D in-level gameplay past 320 seconds without crashing (1,322 frames presented, 77,600 dispatches executed). - Conventions: `python3 tools/check_conventions.py` passed with zero errors. ### 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] AI-assisted: yes
kr4y4 commented 2026-10-12 21:23:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-12T21:23:08Z.


Windows run, to go with the Linux one in the description.

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 26d03715c merged into main 42496c5e5.

  • agc_vector_window and agc_vector_window_stage_inputs pass with --repeat until-fail:3, and the full suite (ctest --timeout 120 --parallel 4) passes 1007 of 1007, on both GPUs.
  • The two new checks also pass with main's Recompiler.cpp: the build recompiled it and relinked agc_vector_window_tests, and both cases still see one array of length 2 with 2 and 4 access chains. So on main these two programs already compile to the shapes the checks expect, and the tests don't fail without the change. A program that main gets wrong, such as the v_readlane_b32 of lane 63 on a 32-lane subgroup from the description, would pin it.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132859687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4213#issuecomment-6132859687). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-12T21:23:08Z. --- Windows run, to go with the Linux one in the description. 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 26d03715c merged into `main` 42496c5e5. - `agc_vector_window` and `agc_vector_window_stage_inputs` pass with `--repeat until-fail:3`, and the full suite (`ctest --timeout 120 --parallel 4`) passes 1007 of 1007, on both GPUs. - The two new checks also pass with `main`'s `Recompiler.cpp`: the build recompiled it and relinked `agc_vector_window_tests`, and both cases still see one array of length 2 with 2 and 4 access chains. So on `main` these two programs already compile to the shapes the checks expect, and the tests don't fail without the change. A program that `main` gets wrong, such as the `v_readlane_b32` of lane 63 on a 32-lane subgroup from the description, would pin it. Written with AI assistance.
oneandonlydean commented 2026-10-13 00:19:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:19:04Z.


Does compute shader 0x500798d00 still stop on main since #4162? That one follows a constant-slot window load to its last store when matching the half-wave reduction, which is the case described here. If it still stops, please quote the log line and add a test that fails on main: kr4y4's run shows both new checks pass with main's Recompiler.cpp.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135162483 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4213#issuecomment-6135162483). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:19:04Z. --- Does compute shader `0x500798d00` still stop on main since #4162? That one follows a constant-slot window load to its last store when matching the half-wave reduction, which is the case described here. If it still stops, please quote the log line and add a test that fails on main: kr4y4's run shows both new checks pass with main's `Recompiler.cpp`.
Kikoarl commented 2026-10-13 06:25:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-13T06:25:38Z.


Thanks @oneandonlydean and @kr4y4 for the verification and review. Indeed, with #4162 merged into main, the half-wave reduction matcher in SpirvAluEmitterMath.cpp (StoredWindowValue) already tracks constant-slot window loads to their last store directly. On current main, compute shader 0x500798d00 compiles and runs without throwing, and agc_vector_window_tests passes on main without this patch. Since #4162 resolves the underlying half-wave reduction issue cleanly and there is no differential failure on main, closing this PR to keep the tree clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139643677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4213#issuecomment-6139643677). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-13T06:25:38Z. --- Thanks @oneandonlydean and @kr4y4 for the verification and review. Indeed, with #4162 merged into `main`, the half-wave reduction matcher in `SpirvAluEmitterMath.cpp` (`StoredWindowValue`) already tracks constant-slot window loads to their last store directly. On current `main`, compute shader `0x500798d00` compiles and runs without throwing, and `agc_vector_window_tests` passes on `main` without this patch. Since #4162 resolves the underlying half-wave reduction issue cleanly and there is no differential failure on `main`, closing this PR to keep the tree clean.

Pull request closed

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!4213
No description provided.