perf(recompiler): back compute scratch with a device buffer slot pool #12764
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12764"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Pipodi on 2026-10-29T08:33:26Z.
What
Every compute FLAT load, store and atomic in a program with scratch tests its address against the shared and the private aperture (
c35f29b6be). The private path reaches the per-lane scratch, held in Functionscratch_dwordsarrays indexed dynamically at every FLAT site. On NVIDIA that indexing is what makes large compute programs slow to compile: the SPIR-V front end (nvoglv64) spends about a third of the compile on it.This PR keeps the FLAT routing exactly as on
main, for every access, and changes only where scratch lives. A compute program that uses scratch (scratch_*, or a FLAT access routed to the private aperture) now keeps it in a storage buffer, so no Function array is declared or indexed:APS5_SCRATCH_POOL_MIB(default 64, never below one slot), bymaxStorageBufferRangeand by 2 GiB: on the RTX 4090 a larger buffer returned wrong values (2049 MiB fails, 2047 MiB passes). All pools together stay underAPS5_SCRATCH_POOL_TOTAL_MIB(default 256); a new pool evicts the least recently used ones first. The shader takes the slot count from the array length.tests/GuardedMemoryLoads.cppchanges too: that fixture runs the emitted code in its interpreter, which now skips the slot claim and offsets scratch indexes by the 2-dword slot header. It also gains checks for the new layout.The buffer is decorated
Coherent. Without it, on an RTX 4090, an unaligned 32-bit FLAT store to scratch (emitted as four byte read-modify-writes) loses the first byte of each pair sharing a dword:flat_apertures_2,flat_apertures_5andflat_aperture_sourcesfail, always with the same value. It is not lane overlap (4× lane spacing gives the same result), not spirv-opt (same with it off) andAliaseddoesn't help. The SPIR-V is a plain load, mask and store in program order, so this looks like a driver ordering issue, but I haven't read the GPU assembly to prove it. The debt entry indocs/dev/debt/shader.mdsays so.On the review of the first version: the provenance rule,
formsPrivateAperture,APS5_FLAT_PRIVATE_ALWAYSand the edit toFlatApertures.cppare gone.main'sFlatApertures.cppis unchanged and passes. Callees reached throughs_swappc_b64are linked into the caller's graph (GraphBuilderLinkCalls), so they are translated with it and use the same buffer; the new test covers that case.On the second review (discarded batch): the slot headers were zeroed inside the first dispatch's batch and the pool was marked zeroed before submit, so a discarded batch left a cached pool marked zeroed with garbage headers. Now the headers are zeroed once when the pool is created, in a batch submitted and waited on under the GPU lock; the
zeroedflag and the in-batch fill are gone, and the pool lookup runs before the recorder opens the dispatch's commands. The debt entry "Compute scratch pool header clear" indocs/dev/debt/libSceAgcDriver.mdcovers the blocking submit.On the AMD report: the test seam
Recorder::FailNextSubmitsForTestthrew beforevkEndCommandBuffer, so the discarded batch went back to the spare list while still recording, and the nextvkBeginCommandBufferon it failed on RDNA 4 (VUID-vkBeginCommandBuffer-commandBuffer-00049, result -13). The injected failure now comes after the end and stands for a failedvkQueueSubmit.The PR has six commits, meant to be read in order (squash on merge is fine):
a46f050af8perf(recompiler): back compute scratch with a device buffer slot pool — the change described above.f369273365fix(agc): zero the scratch pool header when the pool is created — the fix for the discarded-batch review.d9668fdbeefix(agc): end the command buffer before the injected submit failure — the fix for the AMD report; it touches only the test seam.cc61c3ae31perf(agc): hold all compute scratch pools under one memory budget —APS5_SCRATCH_POOL_TOTAL_MIBwith LRU eviction (review point 3).ce38162aa1fix(agc): hold one compute scratch pool under 2 GiB and the total budget.28a30ed35dperf(shader): read a scratch pool slot lock before trying to take it (review point 4).Why the dynamic indexing is the cost (offline replay, cold driver cache, one compile thread, PPSA26344 program 0x1994ed, 1.52M SPIR-V words)
mainWorkgroup memory can't hold it: 56 dwords per lane × 512 lanes is 114 KB.
The runtime side (third review) has a cost. A local bench, not in this PR: one compute program with 448 scratch dwords per lane, median of repeated dispatches, RTX 4090, driver 617.42. It is listed in the debt file as "Compute scratch runtime cost".
Dispatch time of a scratch-heavy program, `main` against this PR
main(Function arrays)CoherentdroppedAbout 2.4× slower at the default size;
Coherentis not the cost. With fewer slots than resident groups the groups wait for a slot: reading the lock before the compare-exchange gains about 5 % at 9 slots and nothing at 580.Tested
Windows 11, MinGW GCC 15.2, RTX 4090 (driver 617.42), i9-13900K.
agc_driver_scratch_pool(tests/execution/ScratchPool.cpp), wave32 and wave64:scratch_*;s_swappc_b64;ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: 45/45 pass (FLAT apertures, LDS, scratch, guarded memory loads), noVUID-. Rerun of the 16 FLAT aperture and scratch pool tests withVK_LOADER_DEBUG=layerto confirm the layer loads: 16/16, noVUID-.agc_driver_scratch_poolfails (isolation: "round 0 lane 0 dword 0 read 0x00002200, expected 0x00000000"; divergent exit: "round 0 lane 160 dword 0 read 0x00003180, expected 0x00000280").RunDiscardedInitializationinagc_driver_scratch_pool(test seamRecorder::FailNextSubmitsForTest): discards the first dispatch's batch, then dispatches again on the cached pool. Fails before the header fix ("header word 0 reads 0xffffffff"), passes after.mainf7f03daf52: 3107/3107. The focused run above, repeated on the rebased branch: 43/43 FLAT aperture, scratch pool, LDS and guarded memory load tests, noVUID-. The rebase conflicted only wheremain'smeshWindowBufferfield (#12233) sits next toscratchPoolSlotDwords(kept both, same order in the disk cache writer and reader) and in the debt file.mainf2d874c1fd: full ctest 3286/3286; focused run with the same environment 56/56 (FLAT apertures, scratch pool, LDS, guarded memory loads, recorder),agc_driver_scratch_poolincluded, noVUID-. The rebase conflicted only wheremain's compute pipeline binary key (0c572f1c25) is built: the key's first word now also sets bit 1 when the scratch pool set is bound, so keys of pipelines without scratch are unchanged.mainbe24202363: full ctest 3283/3283. Focused run with the same environment (flat|scratch|lds|guarded|recorder): 64/64 on the PR, 63/63 onmain, noVUID-in either.mainaaccd0ba39and the first commit on top of it.PPSA26344 (Ghost of Yōtei 01.008.000), 3840x2160, fresh save, shader cache warmed by a previous run of the same build, frames 250-280 (loading).
mainaaccd0ba39and this PR alternate, three runs each. The PR build compiled about 13 compute programs with the slot pool before frame 350 (26 shader cache entries bind descriptor set 2; none onmain); Those 13 are compiled, not dispatched: a local build that logs every pool creation, run warm withAPS5_PROFILE_GPU=1for 1500 s, created no pool up to frame 517 (main) and 530 (PR). So no scratch program dispatches in the window, and the trace pair below can only show compile-side effects. GPU time over frames 250-280 was 2.08 s onmainand 2.20 s on the PR (noise; 50.9 s against 52.5 s over the whole run).main:This PR:
main(window, frame, GPU per present)The spread between runs of the same build (14.5-17.0 s) is larger than any difference between the two; no frame cost from the device-memory scratch shows in this scene.
APS5_NO_SNAPSHOT_CHECKreads1APS5_SPANNED_WAVE=1because of a missing comma in my launcher, soAPS5_SPANNED_WAVEis off in all six runs, as onmainby default.trace_digest, `main` (last pair)
frames 250:30 from present 250: 30 presents, frame p50 308.6 / p90 1276.6 / p99 1890.4 / max 1890.4 ms, GPU busy 111.8 ms per present
NVIDIA GeForce RTX 4090, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit
aaccd0ba39, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 1550.5, Driver.WorkerWait.idle_and_completion_poll 1550.5, Driver.Packet 1407.2, Driver.Packet.memory_wait 757.1, Driver.Packet.dispatch 421.3, Driver.Dispatch 405.4, GpuMutex.wait.other 160.2, Vulkan.Dispatch 143.7
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest, this PR (last pair)
frames 250:30 from present 250: 30 presents, frame p50 263.9 / p90 1099.0 / p99 1768.4 / max 1768.4 ms, GPU busy 102.8 ms per present
NVIDIA GeForce RTX 4090, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit 659d07bbc8c0, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 1263.4, Driver.WorkerWait 1211.0, Driver.WorkerWait.idle_and_completion_poll 1211.0, Driver.Packet.memory_wait 702.5, Driver.Packet.dispatch 376.2, Driver.Dispatch 360.8, Vulkan.Dispatch 131.1, Vulkan.Dispatch.shader_resources 118.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Downstream only (Celegans12's yotei/best plus local changes, not main): PPSA26344 all cold to frame 1200
Same downstream build and environment in each row; only the scratch handling differs. Cold shader cache, NVIDIA driver cache moved aside, fresh save.
main)run570 also lacks one other local change (#6867), so its frame times are not a pure A/B; the compile times of the four programs are. Offline, from captures of the same programs (one compile thread, cold driver cache): 1.69M and 1.57M words compile in 100 s each with this PR, against 135-137 s for 1.45-1.52M words with the arrays.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:50:59Z.
Review: COMMENTED
The compile-time measurement is solid (141-145 s down to about 100 s on program 0x1994ed, nvoglv64 share 36% to 1.4%). My questions are about the runtime side, which the evidence does not cover.
Coherentin global memory. Scratch used to be Function-scope memory the driver can keep close to the SIMD; it is now a device-scope coherent storage buffer, and everyscratch_*and private-aperture FLAT access, including the four byte read-modify-writes for an unaligned 32-bit store, becomes a coherent global access. The PR says the cost of theCoherentdecoration is unexplained ("looks like a driver ordering issue"). Please show the dispatch time of a scratch-heavy compute program before and after, not only compile time.trace_digest.pypair therefore shows no runtime change by construction. Please state which of the 13 pool programs dispatch in the window, or pick a scene where they run every frame.APS5_SCRATCH_POOL_MIB(default 64) each, never evicted. 13 pool programs by frame 350 is up to 832 MiB of VRAM held for the life of the process, per title, to save compile time. A shared pool keyed by slot size, or a pool released with the pipeline, would bound it.EmitScratchPoolAcquireruns aCompareExchangeloop fromlinear % slotswith no back-off, so when the dispatch has more resident workgroups than slots the waiting workgroups burn atomics on the same cache lines the holders are using. With 114 KB slots (56 dwords x 512 lanes) 64 MiB gives about 580 slots; what does a 4K dispatch with thousands of workgroups do? A test withslots < resident groupsand a timing would answer it;agc_driver_scratch_poolisolates 64 workgroups over one or two slots for correctness only.