perf(shader): fence a CAS-loop LDS atomic once #4212

Closed
karanvora2599 wants to merge 1 commit from upstream-pr/4212 into main
karanvora2599 commented 2026-10-12 01:25:14 +00:00 (Migrated from github.com)

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_f32 and ds_max_f32 go through SharedAtomicUpdate, which runs AtomicUpdate's compare-and-swap loop.
  • For LDS, AtomicUpdateTyped already ends that loop with OpMemoryBarrier(Workgroup, AcquireRelease | LdsMemorySemantics).
  • SharedAtomicUpdate then 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_STATS binary size over 16 instances, FLOAT_MODE 0xc0:

Instruction SASS before SASS after
ds_add_f32 17.5 16.5
ds_cmpst_b32 19.5 18.5
ds_max_f32 60.5 59.5

The 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:

  • RTX 4060 Laptop (Ada, driver 591.86)
  • Intel UHD Graphics (Raptor Lake, driver 101.6790)
  • lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with GALLIUM_OVERRIDE_CPU_CAPS=avx.

This round's four changes were built and tested together on main at bca7a29d2 (cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto 34ba918bc. Its two new commits touch only GuestMemory.cpp and the PreparedShaders.cpp fixture.

  • The LDS atomic tests pass on all three devices: agc_driver_lds_atomics, _integer_atomics, _float_atomic_modes, _condxchg, _mixed_width_atomics, _atomics64, plus agc_spirv_lds_atomic_lock.
  • agc_driver_gds_append_consume passes on the RTX 4060 and the Intel UHD, which covers the GDS path that keeps its second fence. lavapipe skips it.
  • Shader and driver tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 338 tests):
    • RTX 4060: everything passes except agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way on main at bca7a29d2 (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.
    • Intel UHD: everything passes or is skipped for missing features, except three tests that fail the same way on main: agc_driver_ds_swizzle, agc_driver_f32_dot2_rounding and agc_driver_prepared_shaders_registered_workgroup_sync. Also, agc_driver_compute_user_data_calls_registered exited with 0xc0000374 once under the suite's load, then passed three runs on its own.
    • lavapipe 25.2.8: everything passes or is skipped for missing features, except agc_driver_prepared_shaders_registered_workgroup_sync, as on main.

Limits. It saves one instruction per CAS-loop LDS atomic. These instructions are a small share of real shaders.

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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823839333 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212). Originally posted by [karanvora2599](https://github.com/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_f32` and `ds_max_f32` go through `SharedAtomicUpdate`, which runs `AtomicUpdate`'s compare-and-swap loop. - For LDS, `AtomicUpdateTyped` already ends that loop with `OpMemoryBarrier(Workgroup, AcquireRelease | LdsMemorySemantics)`. - `SharedAtomicUpdate` then 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_STATS` binary size over 16 instances, `FLOAT_MODE` 0xc0: | Instruction | SASS before | SASS after | |---|---|---| | `ds_add_f32` | 17.5 | 16.5 | | `ds_cmpst_b32` | 19.5 | 18.5 | | `ds_max_f32` | 60.5 | 59.5 | The 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: - RTX 4060 Laptop (Ada, driver 591.86) - Intel UHD Graphics (Raptor Lake, driver 101.6790) - lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with `GALLIUM_OVERRIDE_CPU_CAPS=avx`. This round's four changes were built and tested together on `main` at bca7a29d2 (cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto 34ba918bc. Its two new commits touch only `GuestMemory.cpp` and the `PreparedShaders.cpp` fixture. - The LDS atomic tests pass on all three devices: `agc_driver_lds_atomics`, `_integer_atomics`, `_float_atomic_modes`, `_condxchg`, `_mixed_width_atomics`, `_atomics64`, plus `agc_spirv_lds_atomic_lock`. - `agc_driver_gds_append_consume` passes on the RTX 4060 and the Intel UHD, which covers the GDS path that keeps its second fence. lavapipe skips it. - Shader and driver tests (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 338 tests): - RTX 4060: everything passes except `agc_driver_prepared_shaders_registered_workgroup_sync`, which fails the same way on `main` at bca7a29d2 (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`. - Intel UHD: everything passes or is skipped for missing features, except three tests that fail the same way on `main`: `agc_driver_ds_swizzle`, `agc_driver_f32_dot2_rounding` and `agc_driver_prepared_shaders_registered_workgroup_sync`. Also, `agc_driver_compute_user_data_calls_registered` exited with 0xc0000374 once under the suite's load, then passed three runs on its own. - lavapipe 25.2.8: everything passes or is skipped for missing features, except `agc_driver_prepared_shaders_registered_workgroup_sync`, as on `main`. **Limits.** It saves one instruction per CAS-loop LDS atomic. These instructions are a small share of real shaders. ### Checklist - [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 - [x] AI-assisted: yes
Dextroid17 commented 2026-10-12 01:37:39 +00:00 (Migrated from github.com)

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, and SharedAtomicUpdate emitted 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 omits EmitSharedAtomicCmpstF32, 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_SHADER bit but never actually read it, and Z_ORDER==3 (EARLY_Z_THEN_RE_Z) was never treated as early depth at all — only Z_ORDER==1 was. Without EarlyFragmentTests declared, 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 lets DEPTH_BEFORE_SHADER set 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116213702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212#issuecomment-6116213702). Originally posted by [Dextroid17](https://github.com/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, and `SharedAtomicUpdate` emitted 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 omits `EmitSharedAtomicCmpstF32`, 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_SHADER` bit but never actually read it, and `Z_ORDER==3` (`EARLY_Z_THEN_RE_Z`) was never treated as early depth at all — only `Z_ORDER==1` was. Without `EarlyFragmentTests` declared, 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 lets `DEPTH_BEFORE_SHADER` set 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.
karanvora2599 commented 2026-10-12 02:16:00 +00:00 (Migrated from github.com)

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_f32 and ds_max_f32.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116555308 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212#issuecomment-6116555308). Originally posted by [karanvora2599](https://github.com/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_f32` and `ds_max_f32`.
karanvora2599 commented 2026-10-12 03:22:44 +00:00 (Migrated from github.com)

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):

  • None of the instructions this changes appears: no ds_rsub_u32, ds_add_f32, ds_cmpst_b32/_f32, ds_mskor_b32, ds_wrap_rtn_b32 or ds_min/max_f32.
  • The only LDS atomics there are ds_add/min/max/and/or_u32, ds_add_rtn_u32 and ds_wrxchg_rtn_b32, and none of them goes through SharedAtomicUpdate.

So in those titles this removes a redundant fence without speeding anything up. Whether that's worth merging is your call.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117158145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212#issuecomment-6117158145). Originally posted by [karanvora2599](https://github.com/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): - None of the instructions this changes appears: no `ds_rsub_u32`, `ds_add_f32`, `ds_cmpst_b32`/`_f32`, `ds_mskor_b32`, `ds_wrap_rtn_b32` or `ds_min/max_f32`. - The only LDS atomics there are `ds_add/min/max/and/or_u32`, `ds_add_rtn_u32` and `ds_wrxchg_rtn_b32`, and none of them goes through `SharedAtomicUpdate`. So in those titles this removes a redundant fence without speeding anything up. Whether that's worth merging is your call.
karanvora2599 commented 2026-10-12 04:31:06 +00:00 (Migrated from github.com)

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_f32 and 3 ds_max_f32, all in one wave64 shader. Both go through SharedAtomicUpdate, so this change does apply in that title: one fence less per atomic (ds_max_f32 60.5 → 59.5 SASS in the description). None of the other instructions it changes appears.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117781300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212#issuecomment-6117781300). Originally posted by [karanvora2599](https://github.com/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_f32` and 3 `ds_max_f32`, all in one wave64 shader. Both go through `SharedAtomicUpdate`, so this change does apply in that title: one fence less per atomic (`ds_max_f32` 60.5 → 59.5 SASS in the description). None of the other instructions it changes appears.
oneandonlydean commented 2026-10-13 02:44:03 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137050667 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4212#issuecomment-6137050667). Originally posted by [oneandonlydean](https://github.com/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

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!4212
No description provided.