Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/recompiler-lds-array-bound
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/recompiler-lds-array-bound

Conversation

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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 feat(shader): implement the 64-bit LDS atomics #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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@oneandonlydean
oneandonlydean force-pushed the fix/recompiler-lds-array-bound branch from 05c293b to 3abf107 Compare October 5, 2026 00:34
@github-actions github-actions Bot added shader core/shader agc libSceAgc* and removed needs rebase Conflicts with main, rebase needed labels Oct 5, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
… reach

Stages without workgroup memory (vertex, pixel, ...) keep LDS in a
per-invocation Function array of 8192 dwords. PPSA21564 creates a pixel
shader that keeps three lane-strided slots there (lane * 4 at byte
offsets 0, 256 and 512). On NVIDIA (driver 610.57) the array becomes
32864 bytes of local memory per thread, and about 15 s after the
pipeline is created the GPU hangs: Xid 109 (CTX SWITCH TIMEOUT), or
Xid 31 (FAULT_PDE VIRT_WRITE) with VK_EXT_device_fault enabled, at an
address no Vulkan object ever held.

The analysis now bounds each LDS access's address over lane ids,
constants, adds, multiplies, shifts, ands, ors, xors, min/max, selects,
bitfield extracts and phis of those. A loop-carried or otherwise
unknown address, an add that could wrap with the instruction offset,
or a shared access whose width is not listed has no bound. The array
becomes the dwords the accesses can reach, rounded up to 64, and stays
at 8192 when any access is unbounded. No access can reach past the
smaller array, so the bounds checks and results are unchanged. The
pixel shader above now takes 192 dwords.
@oneandonlydean
oneandonlydean force-pushed the fix/recompiler-lds-array-bound branch from 3abf107 to bdf3984 Compare October 5, 2026 07:29
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
…lots

Stages without workgroup memory keep LDS in a per-invocation Function
array. With the array sized to the addresses it reaches (boykopovar#586),
PPSA21564's intro pixel shader still takes 864 bytes of local memory
per thread on NVIDIA: its accesses are lane * 4 + {0, 256, 512}, so
the index stays dynamic and the 192-dword array lives in local memory.

When every LDS access's address is lane * stride + constant (lane id,
constants, adds, multiplies and left shifts), with one stride, a whole
number of dwords, for all of them and no wrap (the bound analysis
already proves that), the emitter now addresses each access by its
constant plus its instruction offset. The lane id is the same for
every access of an invocation, so taking lane * stride out maps the
addresses one to one, keeps which accesses alias and keeps every
access in bounds; the array sees only constant indices and the driver
keeps it in registers. Shaders with any other address form are
emitted as before.
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

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

@oneandonlydean
oneandonlydean merged commit cf716ff into boykopovar:main Oct 5, 2026
3 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
Takes upstream's merged versions of our boykopovar#586 boykopovar#611 boykopovar#612 boykopovar#505 boykopovar#490 boykopovar#619
boykopovar#503 boykopovar#627 boykopovar#628 boykopovar#618 and drops astrobot's local copies, plus gate-batch-3
(boykopovar#469 boykopovar#500 boykopovar#504 boykopovar#509 boykopovar#510 boykopovar#514 boykopovar#525 boykopovar#526 boykopovar#528), round o/p (boykopovar#617 boykopovar#620
boykopovar#621 boykopovar#622 boykopovar#623 boykopovar#624 boykopovar#501 boykopovar#508 boykopovar#629 boykopovar#630 boykopovar#631).

Conflicts:
- SpirvAnalysis.hpp/.cpp, SpirvBufferAccess.cpp: upstream's boykopovar#586/boykopovar#612.
  The 64-bit LDS atomics keep the full array (f27d6e5 no longer needed).
- SpirvMemoryInstructions.cpp: upstream's constant slot plus offset;
  astrobot's split-wave append/consume kept.
- GuestTextureResource.cpp, TextureTiling.cpp and their tests: upstream's
  boykopovar#490/boykopovar#503 (astrobot had no other change there).
- MaskedSelect.cpp test, ShaderMemory.cpp LDS test: upstream's.
- State.cpp: upstream's boykopovar#505 export count and boykopovar#469 null pixel program;
  astrobot's depth export rejection and depth target model kept.
- Pipeline.cpp: upstream's pipeline key; astrobot's depth attachment and
  blend check (a trailing unused attachment stays valid, as
  colorGapPipelineTests expects).
- Draw.cpp, Recorder.cpp, ShaderResources.cpp: astrobot's (renderPassKey
  with depth, moved sample functions, residency maintenance), which
  already hold the merged boykopovar#505/boykopovar#628/boykopovar#490 changes.
- ShaderInputState.cpp: both register sets.
- State.hpp: the duplicate exportIndex member from the auto-merge removed.
- CMakeLists.txt: astrobot's test list (a superset of upstream's).
- tests/Graphics.cpp: upstream's CompactedExportTests replaces astrobot's
  inline copy; tests/Recorder.cpp: upstream's viewPastLastMipTests added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant