perf(shader): fence a CAS-loop LDS atomic once #4212
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4212"
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 karanvora2599 on 2026-10-12T01:25:14Z.
What
A compare-and-swap LDS atomic now ends with one workgroup fence instead of two identical ones.
Background.
ds_rsub_u32,ds_add_f32,ds_cmpst_b32,ds_cmpst_f32,ds_mskor_b32,ds_wrap_rtn_b32,ds_min_f32andds_max_f32go throughSharedAtomicUpdate, which runsAtomicUpdate's compare-and-swap loop.AtomicUpdateTypedalready ends that loop withOpMemoryBarrier(Workgroup, AcquireRelease | LdsMemorySemantics).SharedAtomicUpdatethen emitted the same barrier again.How. The second barrier is now emitted only for GDS. There, the loop's own barrier is device-scoped on uniform memory, so it isn't the same as the workgroup one, and that path is left unchanged.
Why results don't change. Two identical barriers in a row order nothing more than one does.
Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS per instruction is from the
APS5_PIPELINE_STATSbinary size over 16 instances,FLOAT_MODE0xc0:ds_add_f32ds_cmpst_b32ds_max_f32The other LDS, buffer and scalar memory instructions in the sweep compile to the same size. The barrier is emitted the same way for every NVIDIA generation.
Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices:
GALLIUM_OVERRIDE_CPU_CAPS=avx.This round's four changes were built and tested together on
mainatbca7a29d2(cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto34ba918bc. Its two new commits touch onlyGuestMemory.cppand thePreparedShaders.cppfixture.agc_driver_lds_atomics,_integer_atomics,_float_atomic_modes,_condxchg,_mixed_width_atomics,_atomics64, plusagc_spirv_lds_atomic_lock.agc_driver_gds_append_consumepasses on the RTX 4060 and the Intel UHD, which covers the GDS path that keeps its second fence. lavapipe skips it.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 338 tests):agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way onmainatbca7a29d2(fixed since by #4177). I left out the BDA tests that lose the device on driver 591.86 (#3291):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures*,agc_driver_pixel_interlock.main:agc_driver_ds_swizzle,agc_driver_f32_dot2_roundingandagc_driver_prepared_shaders_registered_workgroup_sync. Also,agc_driver_compute_user_data_calls_registeredexited with 0xc0000374 once under the suite's load, then passed three runs on its own.agc_driver_prepared_shaders_registered_workgroup_sync, as onmain.Limits. It saves one instruction per CAS-loop LDS atomic. These instructions are a small share of real shaders.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:37:39Z.
Three small, well-scoped perf/correctness fixes from the same author — reviewed individually since one of them is a real correctness bug, not just an optimization.
#4212 (LDS atomic double-fence): Confirmed exactly as described.
AtomicUpdateTyped's compare-and-swap loop already ends its LDS path with a workgroup-scoped acquire-release barrier, andSharedAtomicUpdateemitted a byte-for-byte identical second one right after — same scope, same semantics, same state, truly redundant. The GDS path keeps its second barrier correctly, because GDS's loop barrier is device-scoped, not workgroup-scoped, so there it isn't a duplicate. The LDS/GDS split is driven by the same enum the loop itself already branches on, not a separate heuristic that could misclassify. One cosmetic nit: the body's instruction list omitsEmitSharedAtomicCmpstF32, which also routes through this same function and gets the same fix.#4211 (bindless table double-search): Also confirmed clean. The key question was whether the two searches are provably identical, not just usually identical — and they are: the runtime image-mode variants are copies of the base image that only ever mutate format/numeric-class/dimension/cube-type fields, never the four fields the search actually reads (
indirectRoot,source,indirectSearchIterations,indirectMappingOffset). Since those are untouched across every mode, the second search could never have produced a different slot. Minor nuance worth noting, not a defect: the search is now also evaluated on two arms (FMASK, and the depth-bits-fail case) that previously skipped it entirely — practically inert for non-bindless images, but worth a mention in case a future bindless+FMASK combination surfaces a different failure-message path than before.#4210 (early depth decode) — this one is a genuine correctness bug, not a perf nit, and the fix is right. The pixel-stage decoder's precheck already accepted the
DEPTH_BEFORE_SHADERbit but never actually read it, andZ_ORDER==3(EARLY_Z_THEN_RE_Z) was never treated as early depth at all — onlyZ_ORDER==1was. WithoutEarlyFragmentTestsdeclared, a Vulkan driver can't discard an occluded pixel before a shader that writes memory, so on every host GPU those shaders were running — and writing memory — for pixels the real hardware would never have reached. That's silent output corruption, not just wasted work. The fix is correct and the alpha-to-coverage exception is properly preserved. One gap worth flagging: the fix deliberately letsDEPTH_BEFORE_SHADERset early-Z even when combined with a Z-export or mask-export, which the accompanying TechnicalDebt entry only partially covers — it notes the bit-plus-Z-export combination as unmeasured but doesn't mention the mask-export case. Also, the new test can't actually exercise the alpha-to-coverage clause end-to-end, since that flag is only ever set by the driver, not by anything the decode test constructs — the "still tests late" claim rests on reading the code, not on a test proving it.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T02:16:00Z.
Thanks @Dextroid17. You're right,
ds_cmpst_f32(EmitSharedAtomicCmpstF32) goes through the same function. I've updated the description to list every caller:ds_rsub_u32,ds_add_f32,ds_cmpst_b32,ds_cmpst_f32,ds_mskor_b32,ds_wrap_rtn_b32,ds_min_f32andds_max_f32.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T03:22:44Z.
For the record, from @fma-rc's census of the shaders nine titles submit (https://github.com/fma-rc/AnyPS5/tree/evidence/shader-census: 749 shaders, 301,896 instructions; see #4061):
ds_rsub_u32,ds_add_f32,ds_cmpst_b32/_f32,ds_mskor_b32,ds_wrap_rtn_b32ords_min/max_f32.ds_add/min/max/and/or_u32,ds_add_rtn_u32andds_wrxchg_rtn_b32, and none of them goes throughSharedAtomicUpdate.So in those titles this removes a redundant fence without speeding anything up. Whether that's worth merging is your call.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T04:31:06Z.
Correction to my census note above. @fma-rc has since added Ratchet & Clank: Rift Apart and Kena (11 titles, 837 shaders, 316,352 instructions; https://github.com/boykopovar/AnyPS5/pull/4061#issuecomment-6117615215). Ratchet has 3
ds_min_f32and 3ds_max_f32, all in one wave64 shader. Both go throughSharedAtomicUpdate, so this change does apply in that title: one fence less per atomic (ds_max_f3260.5 → 59.5 SASS in the description). None of the other instructions it changes appears.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:44:03Z.
Closing: by your own count this drops one instruction per atomic in six places in one title, so there is no gain to measure, and
perf(changes go in on a measured one. Please reopen if a title shows a difference.Pull request closed