perf(recompiler): index lane-strided per-invocation LDS by constant slots #612

Merged
oneandonlydean merged 2 commits from upstream-pr/612 into main 2026-10-05 08:24:43 +00:00
oneandonlydean commented 2026-10-05 03:35:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T03:35:13Z.


What

Depends on #586; the first commit here is #586's. Review only the last commit.

Stages without workgroup memory keep LDS in a per-invocation Function array. #586 sizes that array to the addresses it can reach. PPSA21564's intro pixel shader still takes 864 bytes of local memory per thread on NVIDIA. Its accesses are ds_*_addtid at lane * 4 + {0, 256, 512}, so the array index stays dynamic and the 192-dword array lives in local memory.

FunctionLdsLaneAddresses (SpirvAnalysis) runs when #586's bound analysis succeeded. It checks whether every LDS access's address has the form lane * stride + constant, built from the lane id, constants, adds, multiplies and left shifts. All accesses must share one stride, a whole number of dwords. If so, ByteAddress emits constant + instruction offset for each access, and the array is sized to those addresses. Any other form, including mixed strides, a constant address beside lane-strided ones, selects, phis, and append/consume, leaves the map empty and the module unchanged.

Why it is exact:

  • The lane id is the same input for every access of one invocation, and the array is private to that invocation.
  • Subtracting lane * stride from every address maps the touched addresses one to one. Which accesses alias, every load result, and every access being in bounds are all kept. #586's analysis already proves that no address wraps.
  • stride % 4 == 0 keeps the byte within the dword for sub-dword accesses.
  • Reading a slot that was never written is undefined in both versions.

Tested

  • Linux (CachyOS, x86-64, GCC): full build with BUILD_TESTING=ON; ctest passes (223/223).
  • verifyFunctionLdsBound gets 6 more cases in agc_shader_memory_tests:
    • lane-strided slots lose the lane term;
    • a constant address beside lane-strided ones, two strides, a half-dword stride, and a selected address keep it;
    • two lane-id instructions with one stride share the rebase, and the array shrinks from 576 to 384 dwords.
  • New GPU execution test agc_driver_pixel_lds_lane_slots: a wave64 and a wave32 pixel shader write ds_write_addtid_b32 at offsets 0, 256 and 512, then read the banks back rotated by one. All 12,288 pixels and 36,864 LDS results match, with and without this change.
    • On current main + #586 with APS5_PIPELINE_STATS=1 (NVIDIA 610.57), its pixel shader goes from 18 registers / 768 B of local memory to 16 registers / 0 B.
    • #601's agc_driver_pixel_lds is unchanged (its addresses aren't lane-strided) and passes.
  • PPSA21564, on my integration branch, with this change and #586:
    • Offline: the intro pixel shader goes from 96 registers / 864 B to 124 registers / 0 B when its SPIR-V is indexed this way.

    • In the game, its 3 pipelines no longer show 864 B; the largest pixel-shader spill left is 96 B. That build also has the exec-select PR, which lowers register pressure, so the driver picks 96 registers there.

    • FPS is unchanged within noise. Two passing runs of each:

      intro title menu level
      before 44.7, 44.5 48.2, 48.9 46.8, 48.2 31.4, 30.6
      after (with the exec-select PR) 45.4, 44.7 47.7, 49.6 46.2, 48.0 30.4, 30.3
    • My visual regression check passes; 0 GPU faults (Xid 31/109) in the 5 runs of this build.

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: #586
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4739853550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/612). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T03:35:13Z. --- ### What Depends on #586; the first commit here is #586's. Review only the last commit. Stages without workgroup memory keep LDS in a per-invocation `Function` array. #586 sizes that array to the addresses it can reach. PPSA21564's intro pixel shader still takes 864 bytes of local memory per thread on NVIDIA. Its accesses are `ds_*_addtid` at `lane * 4 + {0, 256, 512}`, so the array index stays dynamic and the 192-dword array lives in local memory. `FunctionLdsLaneAddresses` (SpirvAnalysis) runs when #586's bound analysis succeeded. It checks whether every LDS access's address has the form `lane * stride + constant`, built from the lane id, constants, adds, multiplies and left shifts. All accesses must share one stride, a whole number of dwords. If so, `ByteAddress` emits `constant + instruction offset` for each access, and the array is sized to those addresses. Any other form, including mixed strides, a constant address beside lane-strided ones, selects, phis, and append/consume, leaves the map empty and the module unchanged. Why it is exact: - The lane id is the same input for every access of one invocation, and the array is private to that invocation. - Subtracting `lane * stride` from every address maps the touched addresses one to one. Which accesses alias, every load result, and every access being in bounds are all kept. #586's analysis already proves that no address wraps. - `stride % 4 == 0` keeps the byte within the dword for sub-dword accesses. - Reading a slot that was never written is undefined in both versions. ### Tested - Linux (CachyOS, x86-64, GCC): full build with `BUILD_TESTING=ON`; `ctest` passes (223/223). - `verifyFunctionLdsBound` gets 6 more cases in `agc_shader_memory_tests`: - lane-strided slots lose the lane term; - a constant address beside lane-strided ones, two strides, a half-dword stride, and a selected address keep it; - two lane-id instructions with one stride share the rebase, and the array shrinks from 576 to 384 dwords. - New GPU execution test `agc_driver_pixel_lds_lane_slots`: a wave64 and a wave32 pixel shader write `ds_write_addtid_b32` at offsets 0, 256 and 512, then read the banks back rotated by one. All 12,288 pixels and 36,864 LDS results match, with and without this change. - On current main + #586 with `APS5_PIPELINE_STATS=1` (NVIDIA 610.57), its pixel shader goes from 18 registers / 768 B of local memory to 16 registers / 0 B. - #601's `agc_driver_pixel_lds` is unchanged (its addresses aren't lane-strided) and passes. - PPSA21564, on my integration branch, with this change and #586: - Offline: the intro pixel shader goes from 96 registers / 864 B to 124 registers / 0 B when its SPIR-V is indexed this way. - In the game, its 3 pipelines no longer show 864 B; the largest pixel-shader spill left is 96 B. That build also has the exec-select PR, which lowers register pressure, so the driver picks 96 registers there. - FPS is unchanged within noise. Two passing runs of each: | | intro | title | menu | level | |---|---|---|---|---| | before | 44.7, 44.5 | 48.2, 48.9 | 46.8, 48.2 | 31.4, 30.6 | | after (with the exec-select PR) | 45.4, 44.7 | 47.7, 49.6 | 46.2, 48.0 | 30.4, 30.3 | - My visual regression check passes; 0 GPU faults (Xid 31/109) in the 5 runs of this build. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #586 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-05 07:30:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T07:30:34Z.


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
#448 core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp L1
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990065095 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/612#issuecomment-5990065095). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T07:30:34Z. --- <!-- 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 | |---|---|---|---| | #448 | `core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp` [L1](https://github.com/boykopovar/AnyPS5/blob/c0e8c89e2ed3515c6156d9b435edef8a49f9fa3d/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp#L1) | | |
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!612
No description provided.