feat(shader): implement the float and 64-bit inc/dec buffer atomics #332

Merged
adriadam10 merged 2 commits from upstream-pr/332 into main 2026-10-04 22:40:04 +00:00
adriadam10 commented 2026-10-02 06:47:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T06:47:05Z.


What

This implements the six buffer atomics still missing. It also fixes the existing 32-bit buffer_atomic_fmin/fmax.

Opcode Semantics (measured on an RDNA2 GPU)
buffer_atomic_fcmpswap (0x3e) / _x2 (0x5e) mem = (mem == cmp) ? src : mem, compared as floats: −0 equals +0, NaN equals nothing, denormals compare exactly
buffer_atomic_fmin_x2 (0x5f) / fmax_x2 (0x60) IEEE minNum/maxNum on f64: −0 < +0. A signalling NaN operand gives that NaN quieted (src first). A quiet NaN loses to a number. Two quiet NaNs give src
buffer_atomic_inc_x2 (0x5c) / dec_x2 (0x5d) as the 32-bit inc/dec from #321, on u64

Bug: the existing 32-bit buffer_atomic_fmin/fmax (EmitFloatAtomicReplacement) kept the old value whenever either operand was a NaN or both were zeros. The hardware follows the same rule as the f64 forms above:

  • fmin(qNaN, 1.0) = 1.0
  • fmin(+0, −0) = −0
  • fmin(1.0, sNaN 0x7fa00000) = 0x7fe00000

2048/2048 vectors on hardware. The helper is now unused and is removed.

Implementation:

  • AtomicUpdate's compare-exchange loop is generalized to a type (AtomicUpdateTyped), and BufferAtomic64Update runs it on the u64 storage-buffer view that #321 added.
  • AtomicFloatBits does the float compare and minNum/maxNum on the raw bits, for 32 and 64 bits, so the driver's denormal/NaN float controls don't matter.

Measuring: float atomics are not performed on fine-grained system memory. The first capture returned zeros, while the integer atomics were fine. The vectors were captured with the buffers in the GPU's coarse-grained pool.

Tests, all values captured on the RDNA2 GPU (ROCm HSA, raw kernels):

  • agc_driver_buffer_atomics_float: fcmpswap (32/64) returned and stored values, fmin/fmax 32 and 64 across ±0, ±inf, quiet and signalling NaNs with payloads, and denormals.
  • agc_driver_buffer_atomics_inc_dec64: the wrap edges (0, limit, limit ± 1, 2^32, 2^64−1).

Tested

Linux, full ctest (101) passes.

  • RADV (Radeon 660M) and lavapipe: both tests pass.
  • With the old 32-bit replacement: fmin_mem0 is 0x7fa00000, expected 0x7fe00000. Without the sNaN quieting: fmin_x2_mem0 is 0xbf800000, expected 0x00000001.

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: #251 (test harness), #321 (64-bit buffer atomics); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4713547846 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T06:47:05Z. --- ### What This implements the six buffer atomics still missing. It also fixes the existing 32-bit `buffer_atomic_fmin/fmax`. | Opcode | Semantics (measured on an RDNA2 GPU) | |---|---| | `buffer_atomic_fcmpswap` (0x3e) / `_x2` (0x5e) | `mem = (mem == cmp) ? src : mem`, compared **as floats**: −0 equals +0, NaN equals nothing, denormals compare exactly | | `buffer_atomic_fmin_x2` (0x5f) / `fmax_x2` (0x60) | IEEE minNum/maxNum on f64: −0 < +0. A signalling NaN operand gives that NaN quieted (src first). A quiet NaN loses to a number. Two quiet NaNs give src | | `buffer_atomic_inc_x2` (0x5c) / `dec_x2` (0x5d) | as the 32-bit `inc`/`dec` from #321, on u64 | **Bug:** the existing 32-bit `buffer_atomic_fmin/fmax` (`EmitFloatAtomicReplacement`) kept the old value whenever either operand was a NaN or both were zeros. The hardware follows the same rule as the f64 forms above: - `fmin(qNaN, 1.0)` = 1.0 - `fmin(+0, −0)` = −0 - `fmin(1.0, sNaN 0x7fa00000)` = `0x7fe00000` 2048/2048 vectors on hardware. The helper is now unused and is removed. Implementation: - `AtomicUpdate`'s compare-exchange loop is generalized to a type (`AtomicUpdateTyped`), and `BufferAtomic64Update` runs it on the u64 storage-buffer view that #321 added. - `AtomicFloatBits` does the float compare and minNum/maxNum on the raw bits, for 32 and 64 bits, so the driver's denormal/NaN float controls don't matter. Measuring: float atomics are not performed on fine-grained system memory. The first capture returned zeros, while the integer atomics were fine. The vectors were captured with the buffers in the GPU's coarse-grained pool. Tests, all values **captured on the RDNA2 GPU** (ROCm HSA, raw kernels): - `agc_driver_buffer_atomics_float`: fcmpswap (32/64) returned and stored values, fmin/fmax 32 and 64 across ±0, ±inf, quiet and signalling NaNs with payloads, and denormals. - `agc_driver_buffer_atomics_inc_dec64`: the wrap edges (0, limit, limit ± 1, 2^32, 2^64−1). ### Tested Linux, full ctest (101) passes. - RADV (Radeon 660M) and lavapipe: both tests pass. - With the old 32-bit replacement: `fmin_mem0 is 0x7fa00000, expected 0x7fe00000`. Without the sNaN quieting: `fmin_x2_mem0 is 0xbf800000, expected 0x00000001`. ### 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: #251 (test harness), #321 (64-bit buffer atomics); review only the last commit - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 06:47:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T06:47:29Z.


Progress report (2cf901b → 7f291b7)

📈 GPU shader instructions: 81.73% (+1.46%, +17 implemented instructions)

✅ 17 implemented
Encoding Name
MUBUF BUFFER_ATOMIC_ADD_X2
MUBUF BUFFER_ATOMIC_AND_X2
MUBUF BUFFER_ATOMIC_CMPSWAP_X2
MUBUF BUFFER_ATOMIC_DEC
MUBUF BUFFER_ATOMIC_DEC_X2
MUBUF BUFFER_ATOMIC_FCMPSWAP
MUBUF BUFFER_ATOMIC_FCMPSWAP_X2
MUBUF BUFFER_ATOMIC_FMAX_X2
MUBUF BUFFER_ATOMIC_FMIN_X2
MUBUF BUFFER_ATOMIC_INC
MUBUF BUFFER_ATOMIC_INC_X2
MUBUF BUFFER_ATOMIC_SMAX_X2
MUBUF BUFFER_ATOMIC_SMIN_X2
MUBUF BUFFER_ATOMIC_SUB_X2
MUBUF BUFFER_ATOMIC_UMAX_X2
MUBUF BUFFER_ATOMIC_UMIN_X2
MUBUF BUFFER_ATOMIC_XOR_X2
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5946923738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332#issuecomment-5946923738). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T06:47:29Z. --- <!-- progress-report --> ### Progress report (2cf901b → 7f291b7) 📈 **GPU shader instructions**: 81.73% (+1.46%, +17 implemented instructions) <details> <summary>✅ 17 implemented</summary> | Encoding | Name | | - | - | | MUBUF | `BUFFER_ATOMIC_ADD_X2` | | MUBUF | `BUFFER_ATOMIC_AND_X2` | | MUBUF | `BUFFER_ATOMIC_CMPSWAP_X2` | | MUBUF | `BUFFER_ATOMIC_DEC` | | MUBUF | `BUFFER_ATOMIC_DEC_X2` | | MUBUF | `BUFFER_ATOMIC_FCMPSWAP` | | MUBUF | `BUFFER_ATOMIC_FCMPSWAP_X2` | | MUBUF | `BUFFER_ATOMIC_FMAX_X2` | | MUBUF | `BUFFER_ATOMIC_FMIN_X2` | | MUBUF | `BUFFER_ATOMIC_INC` | | MUBUF | `BUFFER_ATOMIC_INC_X2` | | MUBUF | `BUFFER_ATOMIC_SMAX_X2` | | MUBUF | `BUFFER_ATOMIC_SMIN_X2` | | MUBUF | `BUFFER_ATOMIC_SUB_X2` | | MUBUF | `BUFFER_ATOMIC_UMAX_X2` | | MUBUF | `BUFFER_ATOMIC_UMIN_X2` | | MUBUF | `BUFFER_ATOMIC_XOR_X2` | </details>
adriadam10 commented 2026-10-03 09:44:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T09:44:40Z.


Rebased on main. main now has SharedAtomicUpdate for the LDS atomics, so this PR no longer carries its own SharedFloatAtomic: the shared min/max path is main's, unchanged. The buffer side (AtomicFloatBits, BufferAtomic64Update) is as before. Still stacked on #321; review only the last commit.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5967900752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332#issuecomment-5967900752). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T09:44:40Z. --- Rebased on main. main now has `SharedAtomicUpdate` for the LDS atomics, so this PR no longer carries its own `SharedFloatAtomic`: the shared min/max path is main's, unchanged. The buffer side (`AtomicFloatBits`, `BufferAtomic64Update`) is as before. Still stacked on #321; review only the last commit.
oneandonlydean commented 2026-10-04 09:57:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T09:57:20Z.


This passed my PPSA21564 check too; it needs a rebase once #321 is rebased, since #319 to #331 are now on main. Then it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978752131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332#issuecomment-5978752131). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T09:57:20Z. --- This passed my PPSA21564 check too; it needs a rebase once #321 is rebased, since #319 to #331 are now on main. Then it's ready to merge from my side.
adriadam10 commented 2026-10-04 10:01:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:01:59Z.


Rebased on the rebased #321 and current main (same MUBUF table conflict, both sides kept). BufferAtomicsFloat, BufferAtomicsIncDec64 and BufferAtomics pass on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978802006 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332#issuecomment-5978802006). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:01:59Z. --- Rebased on the rebased #321 and current main (same MUBUF table conflict, both sides kept). BufferAtomicsFloat, BufferAtomicsIncDec64 and BufferAtomics pass on RADV and lavapipe.
oneandonlydean commented 2026-10-04 22:37:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:51Z.


Thanks for the rebase. Both commits have exactly the content I ran through my PPSA21564 check (the first is #321's head), and agc_driver_buffer_atomics_float, agc_driver_buffer_atomics_inc_dec64 and agc_driver_buffer_atomics pass here on current main. Ready to merge right after #321.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985217961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/332#issuecomment-5985217961). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:37:51Z. --- Thanks for the rebase. Both commits have exactly the content I ran through my PPSA21564 check (the first is #321's head), and `agc_driver_buffer_atomics_float`, `agc_driver_buffer_atomics_inc_dec64` and `agc_driver_buffer_atomics` pass here on current main. Ready to merge right after #321.
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!332
No description provided.