fix(shader): match half-wave reductions over relatively addressed vector registers #4162

Merged
Celegans12 merged 1 commit from upstream-pr/4162 into main 2026-10-12 10:54:08 +00:00
Celegans12 commented 2026-10-12 00:15:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-12T00:15:50Z.


What

Since #3856, a program that uses v_movrels_b32 or v_movreld_b32 keeps its vector registers from the lowest relatively addressed one up in a per-invocation window array, so plain register accesses become LoadVectorWindow / StoreVectorWindow. The half-wave reduction matcher (#2449-#2451) only follows SSA values, so a wave64 scan, v_permlanex16_b32 join and lane 31/63 read through that array still throws that lane 63 is outside the 32-lane host subgroup.

  • The matcher now reads a window load at a constant slot as the value of the last store to that slot earlier in the same block. Each invocation owns its array, and stores to other constant slots cannot alias, so that is exactly the value the load returns.
  • A store at a runtime slot in between (a v_movreld_b32 at a runtime M0) or a store in another block stops the forwarding, and the read still throws.
  • Nothing else in the matcher changes: identity keys under the entry EXEC, the all-lanes scan and the 32-lane narrowing are as before, for every stage it already covers.

Among PPSA26344's wave64 graphics programs, 38 of 39 lane 32-63 reads already match on main; the remaining one is this shape, in a pixel program.

#4120 keeps registers in SSA only for programs without indexed writes, so it does not cover this case, and these tests use v_movreld_b32 so they keep testing the window path.

Tested

Based on main bca7a29d. Windows 11 (MinGW-w64 GCC 15.2.0) and Linux: configure -DBUILD_TESTING=ON, full build, --target libs, ctest.

  • Windows NVIDIA: 913/917 pass; log_macros, agc_write_tracking, agc_driver_prepared_shaders_registered_workgroup_sync and guest_kernel_stack_protect fail the same way on main
  • Linux (upstream's job in WSL, GCC 13.3, llvmpipe), merged with main bca7a29d: 920/923 pass; the 3 failures also fail on main 43bc17f7. agc_driver_pixel_wave_reduction also passes on Windows lavapipe

agc_shader_memory checks a UMin scan through a v_movreld_b32 window (subgroup UMin on 32 lanes, correct halves on 64) and keeps the runtime-M0 refusal; agc_driver_pixel_wave_reduction runs each of its five reductions through a window too, per wave. Both fail on main with the lane-63 refusal.

Not tested: console behaviour.

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:4823444923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4162). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-12T00:15:50Z. --- ### What Since #3856, a program that uses `v_movrels_b32` or `v_movreld_b32` keeps its vector registers from the lowest relatively addressed one up in a per-invocation window array, so plain register accesses become `LoadVectorWindow` / `StoreVectorWindow`. The half-wave reduction matcher (#2449-#2451) only follows SSA values, so a wave64 scan, `v_permlanex16_b32` join and lane 31/63 read through that array still throws that lane 63 is outside the 32-lane host subgroup. - The matcher now reads a window load at a constant slot as the value of the last store to that slot earlier in the same block. Each invocation owns its array, and stores to other constant slots cannot alias, so that is exactly the value the load returns. - A store at a runtime slot in between (a `v_movreld_b32` at a runtime M0) or a store in another block stops the forwarding, and the read still throws. - Nothing else in the matcher changes: identity keys under the entry EXEC, the all-lanes scan and the 32-lane narrowing are as before, for every stage it already covers. Among PPSA26344's wave64 graphics programs, 38 of 39 lane 32-63 reads already match on `main`; the remaining one is this shape, in a pixel program. #4120 keeps registers in SSA only for programs without indexed writes, so it does not cover this case, and these tests use `v_movreld_b32` so they keep testing the window path. ### Tested Based on `main` bca7a29d. Windows 11 (MinGW-w64 GCC 15.2.0) and Linux: configure -DBUILD_TESTING=ON, full build, --target libs, ctest. - Windows NVIDIA: 913/917 pass; `log_macros`, `agc_write_tracking`, `agc_driver_prepared_shaders_registered_workgroup_sync` and `guest_kernel_stack_protect` fail the same way on `main` - Linux (upstream's job in WSL, GCC 13.3, llvmpipe), merged with `main` bca7a29d: 920/923 pass; the 3 failures also fail on `main` 43bc17f7. `agc_driver_pixel_wave_reduction` also passes on Windows lavapipe `agc_shader_memory` checks a UMin scan through a `v_movreld_b32` window (subgroup UMin on 32 lanes, correct halves on 64) and keeps the runtime-M0 refusal; `agc_driver_pixel_wave_reduction` runs each of its five reductions through a window too, per wave. Both fail on main with the lane-63 refusal. Not tested: console behaviour. ### 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
Dextroid17 commented 2026-10-12 01:09:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:09:20Z.


The body's own claim that this doesn't conflict with #4120 holds, and it's worth stating why precisely: #4120 only changes which registers route through the window on the read side (GetVectorReg); every windowed register still always gets a StoreVectorWindow on write regardless of #4120's flag. Since this PR's StoredWindowValue only ever looks at StoreVectorWindow/LoadVectorWindow instructions and never touches #4120's flag, the merge order between the two genuinely doesn't matter — unlike #4057, which edits the same function #4120 does and predates its change.

The backward-walk terminates safely: each step in Resolved()'s new loop moves to a value defined strictly earlier in program order (same block, lower position, or a dominating block), and builder IR can't hold the self-referential store that would be needed to cycle, so there's no infinite-loop risk despite no explicit visited set.

One wording note for the body, not a functional issue: it describes 0x7e008514 (v_movreld_b32) as the thing that stores to the constant slot the matcher reads — it isn't; that instruction's own destination index is a computed (runtime) value, and it's only in the test to make the whole register file windowed. The actual constant-slot stores the matcher forwards through come from ordinary writes to windowed registers elsewhere in the fixture. And 0x7ef80500 (v_readfirstlane_b32 m0, v0) is correctly load-bearing for the "runtime M0 stops forwarding" case — without it, M0 would resolve to an immediate 0 and the walk would fold straight through instead of refusing.

Worth naming as a known scope limit, not a defect: the forwarding only looks within the load's own basic block, so a store in a predecessor block still isn't found and the refusal persists there. The PR's "the remaining one is this shape" claim quietly assumes that pixel program's scan and read live in one block — true for the fixture tested, but not something this diff can confirm in general.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115967586 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4162#issuecomment-6115967586). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:09:20Z. --- The body's own claim that this doesn't conflict with #4120 holds, and it's worth stating why precisely: #4120 only changes which registers route through the window on the *read* side (`GetVectorReg`); every windowed register still always gets a `StoreVectorWindow` on write regardless of #4120's flag. Since this PR's `StoredWindowValue` only ever looks at `StoreVectorWindow`/`LoadVectorWindow` instructions and never touches #4120's flag, the merge order between the two genuinely doesn't matter — unlike #4057, which edits the same function #4120 does and predates its change. The backward-walk terminates safely: each step in `Resolved()`'s new loop moves to a value defined strictly earlier in program order (same block, lower position, or a dominating block), and builder IR can't hold the self-referential store that would be needed to cycle, so there's no infinite-loop risk despite no explicit visited set. One wording note for the body, not a functional issue: it describes `0x7e008514` (`v_movreld_b32`) as the thing that stores to the constant slot the matcher reads — it isn't; that instruction's own destination index is a computed (runtime) value, and it's only in the test to make the whole register file windowed. The actual constant-slot stores the matcher forwards through come from ordinary writes to windowed registers elsewhere in the fixture. And `0x7ef80500` (`v_readfirstlane_b32 m0, v0`) is correctly load-bearing for the "runtime M0 stops forwarding" case — without it, `M0` would resolve to an immediate 0 and the walk would fold straight through instead of refusing. Worth naming as a known scope limit, not a defect: the forwarding only looks within the load's own basic block, so a store in a predecessor block still isn't found and the refusal persists there. The PR's "the remaining one is this shape" claim quietly assumes that pixel program's scan and read live in one block — true for the fixture tested, but not something this diff can confirm in general. Written with AI assistance.
fma-rc commented 2026-10-12 02:08:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-12T02:08:20Z.


A second title for this: Astro's Playroom (PPSA01325) stops 10 s after start on main d371ed307 (Linux, RTX 5080), in a wave64 stage-2 program:

ShaderRecompiler::Recompile: SPIR-V emission failed: hash=0x21475664128 stage=2 opcode=ReadLane reason=v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation

With this PR merged onto that main the line is gone. On a local branch that also has the other open PRs the title needs it gets back into gameplay in its first level, as it did before the window change. On main plus only this PR one run then segfaulted in the title at 158 s, before its title screen; I have not looked into that and can't say it is related. I kept the dumped request (shader_5000ca100.req) if it is useful for a test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116485867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4162#issuecomment-6116485867). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-12T02:08:20Z. --- A second title for this: Astro's Playroom (PPSA01325) stops 10 s after start on `main` d371ed307 (Linux, RTX 5080), in a wave64 stage-2 program: ``` ShaderRecompiler::Recompile: SPIR-V emission failed: hash=0x21475664128 stage=2 opcode=ReadLane reason=v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation ``` With this PR merged onto that `main` the line is gone. On a local branch that also has the other open PRs the title needs it gets back into gameplay in its first level, as it did before the window change. On `main` plus only this PR one run then segfaulted in the title at 158 s, before its title screen; I have not looked into that and can't say it is related. I kept the dumped request (`shader_5000ca100.req`) if it is useful for a test.
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!4162
No description provided.