Skip to content

rdna2spirv: global_atomic_add/sub_x2 and buffer_atomic_add/sub_x2 - #3065

Open
LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-add-sub-x2
Open

LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-add-sub-x2

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

global_atomic_add_x2/sub_x2 and buffer_atomic_add_x2/sub_x2 raised Unsupported. This is the global and buffer counterpart of #2738, which does the same for ds_add/sub_u64. The global and buffer _x2 paths weren't in #2738, and no one had claimed them.

How: two 32-bit atomics, exact without Int64 atomics. Gen.x2_atomic_words now holds the per-dword part shared by global_atomic_x2 and buffer_atomic_x2.

  1. An atomic add (or sub) of DATA.lo on the low dword returns its old value. From that old value this op knows its own carry: new_lo < old_lo for add, old_lo < DATA.lo for sub.
  2. The high dword then takes an atomic add (or sub) of DATA.hi + carry.

Addition commutes, and the carries over any order of the ops add up to exactly the number of times the low dword wrapped. So the memory is exact even with other lanes on the element. Only the returned high dword can be torn, since it comes from its own atomic. The and/or/xor/swap forms are unchanged.

Tests:

  • test_global_atomics_x2.py:
    • carry and borrow across the dword (0x1_FFFFFFFF + 1, ~0 + 1, 0x1_00000000 - 1, 0 - 1, …)
    • add/sub in the value grids and seeded cases (MODEL)
    • 32 lanes adding / subtracting one carrying value on one element: the interpreter runs the lanes in lockstep, so their low and high atomics interleave, and the result is m ± 32·d exactly. With the carry forced to 0 (a sabotage I ran locally), both carry tests fail.
  • test_buffer_atomics_x2.py: add/sub in its grid, including the bounds cases.
  • The "still rejected" lists in those files and in test_global_atomics.py now use smin/umax/cmpswap_x2.

python -I -m unittest discover -s tests/semantic: 4144 tests OK (4 skipped). The docs tests pass.

docs/design.md: the global and buffer atomics rows describe add/sub_x2. The global row also had a stale first "Still Unsupported: the _x2 64-bit forms …" sentence, a leftover from #1633 that contradicted the row's own _x2 text; it's removed.

Tested on CPU only; no GPU run. No game files. Claimed on #2891 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Overlap note: #2710 (SHADER-480) wraps all six atomic emitters, the _x2 ones included, in a natural-alignment guard (Gen.atomic_aligned). Whichever lands second needs a rebase; the change here is confined to the per-dword part (x2_atomic_words), so it should fit inside #2710's guard as is.

@TheMrBeez

Copy link
Copy Markdown

Tested 5aa65eb (base 16282101) on the GPU, under real contention: pass. AI-assisted run, synthetic shaders only, no game files.

The PR was CPU-only, and the interpreter runs lanes in lockstep, so I wanted the two-atomic carry scheme on hardware where the low and high atomics of different waves really interleave. I wrote a hand-written compute shader (not part of this PR), translated it with this head and ran it through vk_compute_test with 64 groups × 64 threads = 4096 threads, all hitting the same three 64-bit elements of the heap (initially 0xEEEEEEEE_EEEEEEEE):

  • A: buffer_atomic_add_x2 of 0x1_9E3779B9 (offen, glc)
  • B: buffer_atomic_sub_x2 of 0x2_7F4A7C15
  • C: global_atomic_add_x2 of 0x1_9E3779B9 (VA 0 = heap)

The checks: each element's final 64-bit value equals M ± 4096·D exactly, and the 4096 returned low dwords per element are exactly the multiset {(M ± k·D) mod 2³²} for k = 0..4095, meaning every op saw a distinct old low word. The returned high dwords aren't checked, since the PR says they can be torn.

Device A, B, C memory returned low words spirv-val
NVIDIA GeForce RTX 5080, driver 621.01 exact (0xeeef08d2668a7eee, 0xeeeec6fa472d9eee, 0xeeef08d2668a7eee) all three match OK
AMD Radeon iGPU, Ryzen 9 9950X3D (gfx1036), driver 25.10.30.02 exact, same words all three match OK

Controls:

  • main at the base refuses the shader (buffer_atomic_add_x2 Unsupported).
  • Sabotage: with the carry forced to 0 (OpSelect … 0 0 in x2_atomic_words, a local edit, since reverted), all three memory checks fail on the RTX 5080 (A = 0xeeeefeee668a7eee), so the test does see a lost carry.

Also: tests/semantic/test_global_atomics_x2.py, test_buffer_atomics_x2.py and test_global_atomics.py OK; scripts/run_gpu_tests.py with this translator: RTX 5080 143/189 (46 skipped, wave64), iGPU 174/189 (15 skipped), no failures. Windows 11 Pro 10.0.26200, Vulkan SDK 1.4.363.0, llvm-mc from rocm-sdk-core 10.0.0, MSVC test programs from this tree.

Only wave32 was run (the translator gives this shader subgroup size 32).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on the PR head: test_global_atomics_x2.py 6 OK, test_buffer_atomics_x2.py 5 OK, test_global_atomics.py 12 OK, full semantic 4,144 OK. On current main the two x2 suites fail 43 and 37 errors. The semantics are correct: x2_atomic_words gets (lo,hi) pointer/value pairs, callers build two AccessChains and read vdata+n, global_address handles scalar + 32-bit or 64-bit vaddr plus off, and the buffer form uses the 8-byte element (fmt 62). Mutations are non-vacuous (carry forced to 0: 21 global / 15 buffer failures; swapped words: 69 / 73). The PR's independent 4096-thread GPU contention pass is noted but not reproduced here. No game data or absolute paths. Approve.

Docs nit: docs/design.md and the test docstring still say "four _x2 forms" / "carrying forms stay Unsupported"; update those to the new count/behavior.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 5aa65eb4c4a177a9c4061743143f08d43d9e4eab on AMD Radeon Pro V540 (RDNA1, Navi 12 / gfx1011, PCI 1002:7360, RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan device 1.4.318, subgroup 32..64): pass, no regression. This adds the missing RDNA1/RADV data point to the NVIDIA and RDNA2 results in the comments.

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --build runtime/build/linux: 159/189 passed, 28 skipped, 2 failed (194 shaders translated, 194 pass spirv-val), identical to clean base 16282101 (159/189, 28 skipped, 2 failed). The same on 1f:00.0.
  • The two failures (cs_signed_zero_nan, cs_w32_salu64) are pre-existing with identical words on base. This PR adds semantic tests only, so I had no new GPU fixture to run; the existing suite shows no regression from the translator change on RDNA1.
  • Command: pytest tests/semantic/test_buffer_atomics_x2.py tests/semantic/test_global_atomics.py tests/semantic/test_global_atomics_x2.py -q: 23 passed.

Tools: llvm-mc 21.1.8 with AMDGPU target, SPIRV-Tools 2026.1, GCC 13.3.0, Python 3.12.3. No code was edited or pushed; only synthetic fixtures were used.

AI-assisted.

@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested 5aa65eb4, applied alone on main 16282101: pass.
CPU only: Windows 11 (26200), Python 3.14, llvm-mc 21.1.8, spirv-val from Vulkan SDK 1.4.363.0.

  • Tests only (the three changed test files, main's translator): test_global_atomics_x2.py 43 errors and test_buffer_atomics_x2.py 37 errors, all Unsupported: global_atomic_add_x2 / sub_x2 / buffer_atomic_add_x2 / sub_x2. test_global_atomics.py passes either way (its change only swaps the rejected example).
  • Whole PR, python -m unittest discover -s tests/semantic -p <file>: test_global_atomics_x2.py 6 OK, test_buffer_atomics_x2.py 5 OK, test_global_atomics.py 12 OK.
  • The 82 distinct modules those two _x2 files translate (43 + 39) all pass spirv-val --target-env vulkan1.2.
  • pytest tests/test_translate.py: 541 passed, 4 skipped (same as main). python -I tests/test_shader_checks.py: 69 OK. tests/test_docs_design_rows.py: OK.
  • rdna2spirv: ds_cmpst_f32 and the global/buffer fcmpswap atomics #3153 changes the same lines of Gen.global_atomic / Gen.buffer_atomic: its tools/rdna2spirv.py diff does not apply on top of this one (patch failed: tools/rdna2spirv.py:1779), so the second of the two to land needs a rebase.

Not checked: a GPU run (no GPU fixture uses these instructions) and the complete tests/semantic suite. main moved to f847748c after this run and GitHub now reports the PR as conflicting with it; a rebase was not tested.

Both 64-bit add/sub forms raised Unsupported. They are exact with 32-bit
atomics: the low dword's atomic returns its old value, which gives this
op's own carry (borrow), and the high dword then takes DATA.hi + carry.
Addition commutes and the carries over any order add up to the low
dword's wraps, so the memory is exact under contention (as ItsNotRexed#2738 does for
LDS); only the returned high dword can be torn. x2_atomic_words holds the
per-dword part for the global and buffer paths.

Tests: carry and borrow across the dword (global), add/sub in the value
grids of test_global_atomics_x2.py / test_buffer_atomics_x2.py, and 32
lanes adding / subtracting on one element (they interleave in the
interpreter) ending exact. The rejected-form lists move to min/max_x2.
docs/design.md rows updated; a stale 'the _x2 forms stay Unsupported'
sentence in the global atomics row goes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants