fix(recompiler): size per-invocation LDS arrays to the addresses they reach #586

Merged
oneandonlydean merged 1 commit from upstream-pr/586 into main 2026-10-05 08:24:01 +00:00
oneandonlydean commented 2026-10-04 23:55:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T23:55:28Z.


What

Stages without workgroup memory (vertex, pixel, ...) keep LDS in a per-invocation Function array of 8192 dwords (FunctionLdsDwords in SpirvBufferAccess.cpp). Most shaders that use it touch a few lane-strided slots. On NVIDIA, the whole array goes to local memory per thread.

PPSA21564 creates a pixel shader that writes lane * 4 at LDS byte offsets 0, 256 and 512, so the highest byte it can reach is 767. Its pipeline gets 32,864 bytes of local memory per thread (pipeline executable statistics, NVIDIA 610.57). About 15 s after that pipeline is created, the GPU hangs:

  • Xid 109 (CTX SWITCH TIMEOUT) and VK_ERROR_DEVICE_LOST;
  • with VK_EXT_device_fault on, Xid 31 (FAULT_PDE VIRT_WRITE) at an address no Vulkan object ever held.

With the array cut to 256 dwords, the same runs stopped hanging.

This PR sizes the array to what the shader can reach:

  • FunctionLdsDwords(program) in SpirvAnalysis bounds each LDS access's address. It handles lane ids, constants, IAdd32, IMul32, shifts, ands, ors, xors, UMin32/UMax32, SelectU32, BitFieldUExtract and phis of those, in unsigned 64-bit arithmetic.
  • These get no bound:
    • a loop-carried or otherwise unknown address;
    • an operation that could pass 2^32;
    • an add of the instruction offset that could wrap;
    • a shared opcode whose width isn't listed (LoadShared, StoreShared, DataAppend, DataConsume, and any opcode added later).
  • The array becomes the reachable dwords rounded up to 64. If any access has no bound, it stays at 8192.
  • LdsDwordCount uses the new size for stages without workgroup memory. Compute and mesh stages are unchanged.

This is exact: no access can reach past the smaller array, so the existing bounds checks and every result are the same as with 8192 dwords. Shaders whose addresses can't be bounded produce the same module as before.

Tested

  • Linux (CachyOS, x86-64, GCC): full build with BUILD_TESTING=ON; ctest passes (213/213).
  • New verifyFunctionLdsBound in agc_shader_memory_tests:
    • lane-strided slots at offsets 0, 256 and 512: 192 dwords, also carried in AnalyzeProgramRequirements;
    • a 4-dword access at select(c, (lane & 7) * 16, 0x100): 128 dwords;
    • user-data address, a wrapping instruction offset, a loop phi, and LoadShared without a width: 8192;
    • a phi of 0x40 and 0x3fc: 256.
  • PPSA21564, on my integration branch with the same change:
    • before: the hang hit 3 of 3 runs with pinned worker threads (the setting that makes it most frequent), and also in unpinned runs;
    • after: 0 hangs in 13 pinned runs to 2 minutes past the point where it used to hang;
    • my visual regression check passes twice;
    • the shader cache holds the pixel shader with a 192-dword array.
  • Fork CI (Linux and Windows) passes.

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 (touches the same files as #573, see below)
  • AI-assisted: yes

Note for #573: its 64-bit shared atomics touch 2 dwords. This PR doesn't list them, so a shader that uses them keeps the full array until they're added to the width table.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4738562160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/586). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T23:55:28Z. --- ### What Stages without workgroup memory (vertex, pixel, ...) keep LDS in a per-invocation `Function` array of 8192 dwords (`FunctionLdsDwords` in SpirvBufferAccess.cpp). Most shaders that use it touch a few lane-strided slots. On NVIDIA, the whole array goes to local memory per thread. PPSA21564 creates a pixel shader that writes `lane * 4` at LDS byte offsets 0, 256 and 512, so the highest byte it can reach is 767. Its pipeline gets 32,864 bytes of local memory per thread (pipeline executable statistics, NVIDIA 610.57). About 15 s after that pipeline is created, the GPU hangs: - Xid 109 (CTX SWITCH TIMEOUT) and `VK_ERROR_DEVICE_LOST`; - with `VK_EXT_device_fault` on, Xid 31 (FAULT_PDE VIRT_WRITE) at an address no Vulkan object ever held. With the array cut to 256 dwords, the same runs stopped hanging. This PR sizes the array to what the shader can reach: - `FunctionLdsDwords(program)` in SpirvAnalysis bounds each LDS access's address. It handles lane ids, constants, `IAdd32`, `IMul32`, shifts, ands, ors, xors, `UMin32`/`UMax32`, `SelectU32`, `BitFieldUExtract` and phis of those, in unsigned 64-bit arithmetic. - These get no bound: - a loop-carried or otherwise unknown address; - an operation that could pass 2^32; - an add of the instruction offset that could wrap; - a shared opcode whose width isn't listed (`LoadShared`, `StoreShared`, `DataAppend`, `DataConsume`, and any opcode added later). - The array becomes the reachable dwords rounded up to 64. If any access has no bound, it stays at 8192. - `LdsDwordCount` uses the new size for stages without workgroup memory. Compute and mesh stages are unchanged. This is exact: no access can reach past the smaller array, so the existing bounds checks and every result are the same as with 8192 dwords. Shaders whose addresses can't be bounded produce the same module as before. ### Tested - Linux (CachyOS, x86-64, GCC): full build with `BUILD_TESTING=ON`; `ctest` passes (213/213). - New `verifyFunctionLdsBound` in `agc_shader_memory_tests`: - lane-strided slots at offsets 0, 256 and 512: 192 dwords, also carried in `AnalyzeProgramRequirements`; - a 4-dword access at `select(c, (lane & 7) * 16, 0x100)`: 128 dwords; - user-data address, a wrapping instruction offset, a loop phi, and `LoadShared` without a width: 8192; - a phi of 0x40 and 0x3fc: 256. - PPSA21564, on my integration branch with the same change: - before: the hang hit 3 of 3 runs with pinned worker threads (the setting that makes it most frequent), and also in unpinned runs; - after: 0 hangs in 13 pinned runs to 2 minutes past the point where it used to hang; - my visual regression check passes twice; - the shader cache holds the pixel shader with a 192-dword array. - Fork CI (Linux and Windows) passes. ### 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: none (touches the same files as #573, see below) - [x] AI-assisted: yes Note for #573: its 64-bit shared atomics touch 2 dwords. This PR doesn't list them, so a shader that uses them keeps the full array until they're added to the width table.
github-actions[bot] commented 2026-10-05 07:30:30 +00:00 (Migrated from github.com)

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


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:5990064128 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/586#issuecomment-5990064128). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T07:30:30Z. --- <!-- 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!586
No description provided.