Skip to content

rdna2spirv: GFX10.1 scalar atomics (s_atomic_*, s_buffer_atomic_*), once per wave - #3676

Open
evilclanker9000 wants to merge 8 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/s-atomic
Open

evilclanker9000 wants to merge 8 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/s-atomic

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-SMEM-ATOMIC, #3574 (comment)

Depends on: #3589 (scalar stores). This branch is stacked on it, and only the last two commits are new here.

What and why

s_atomic_* and s_buffer_atomic_* raised Unsupported. Like the scalar stores, they exist on GFX10.1 only (gfx1010-gfx1013, which includes the console). Each one is a single atomic per wave, at the address an s_load / s_buffer_load of the same operands reads. With glc, SDATA gets the value from before the op.

Change (tools/rdna2spirv.py, Gen.scalar_load(..., atomic=kind) and Gen.scalar_atomic):

  • Once per wave. The invocation in hardware lane 0 performs the atomic. Scalar instructions ignore EXEC, so lane 0 always does it. Only compute and mesh shaders are translated, because there that lane is LocalInvocationIndex mod the wave size. That gives exactly one invocation, also under --wave64-emulate and when a wave64 spans two 32-wide subgroups. Other stages raise Unsupported.
  • Returned value (glc). Every lane gets lane 0's value, through OpGroupNonUniformShuffle from lane 0, or through an exchange round (x_round) under --wave64-emulate. A wave64 on 32-wide subgroups without emulation gets the value in lanes 0-31 only, the same known cross-half approximation as the other cross-lane ops.
  • Kinds.
    • The 32-bit buffer atomic kinds are translated: add, sub, swap, cmpswap ({DATA, CMP}, the old value going to SDATA[0]), the four min/max ops, and, or, xor, and inc/dec through the existing CAS loop.
    • The and/or/xor/swap _x2 forms are translated as one 32-bit atomic per dword, as buffer_atomic_*_x2 does.
    • The other _x2 forms stay Unsupported.
  • Out of range. An s_buffer_atomic whose element ends past the V# limit does nothing and returns 0, as the load reads 0 there. This is inferred.
  • Lane-mask SDATA is refused, as for the stores.
  • Known ordering limit (from rdna2spirv: GFX10.1 scalar stores (s_store_dword*, s_buffer_store_dword*) #3589): every invocation performs a scalar store, so an s_store followed by an s_atomic on the same address in one wave can see the store land after the atomic. This is documented in the scalar-store row, and the semantics are unchanged.

Not measured natively. The gfx1036 oracle has no scalar atomics (llvm-mc rejects them for gfx1030/gfx1036). An RDNA1 part (gfx1010-gfx1013) could check them natively, for example the V540 farm announced in the coordination issue.

New contract row in docs/design.md. The scalar-store row now points to it.

Tested

Windows 11, LLVM llvm-mc (-mcpu=gfx1013), VK_LOADER_LAYERS_DISABLE=~implicit~, one GPU at a time, python -I scripts/run_gpu_tests.py --filter scalar_atomic. The vk_compute_test device lines were AMD Radeon(TM) Graphics (subgroup 32..64) (with compute subgroup size: 64 for wave64) and NVIDIA GeForce RTX 5080 (subgroup 32..32).

GPU Driver cs_w32_scalar_atomic cs_w64_scalar_atomic
AMD Radeon RDNA2 iGPU, Ryzen 9 9950X3D 26.10.1 PASS 100 7 240 5 103 5 15 6 PASS native wave64 100 100 240 5 103 15 255 261
NVIDIA GeForce RTX 5080 616.92 PASS, same words PASS with LEONIDA_W64_EMULATE=1, same words (SKIP without: no 64-wide subgroup)
  • New fixtures:
    • runtime/test/shaders/cs_w32_scalar_atomic.asm: add, sub, cmpswap and inc, with s_buffer_atomic and s_atomic, returned values read in lane 31.
    • cs_w64_scalar_atomic.asm: add, cmpswap and or_x2, returned values read in lanes 0 and 63.
    • Lane 0 writes the targets before the atomics. The words are derived in the headers. Both fixtures are registered in check.py, run_gpu_tests.py and regen_fixtures.py.
  • Semantic test. ScalarAtomics in test_value_semantics.py runs every lane of one wave (wave64 on a 64-wide subgroup), checks the words against a plain-Python reference, and checks the refusals (a pixel shader, s_atomic_add_x2).
  • Before the change: on rdna2spirv: GFX10.1 scalar stores (s_store_dword*, s_buffer_store_dword*) #3589's branch it errors with Unsupported: s_atomic_add and s_buffer_atomic_add.
  • Negative control: I made every lane perform the atomic. ScalarAtomics fails, and on the Radeon both fixtures fail, with the add target at 196 (100 + 32 × 3) and 292 (100 + 64 × 3). Then I reverted it.
  • Passing: python -m unittest discover -s tests/semantic, python tests/semantic/regen_fixtures.py --check (clean), python -I tests/test_shader_checks.py, and pytest tests/test_docs_design_rows.py tests/test_run_gpu_tests.py tests/test_shader_meta.py.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

…rd*)

s_store_dword/x2/x4 and s_buffer_store_dword/x2/x4 exist on gfx1010-gfx1013
(the console) only. They now write SDATA as PhysicalStorageBuffer stores at
the address an s_load / s_buffer_load of the same operands reads, with the
buffer bound; a lane-mask source is refused. Scalar atomics stay
Unsupported. Not measurable on the gfx1036 oracle. GPU fixture
cs_w32_scalar_store, semantic test ScalarStores, design.md row; the old
contract test that expected Unsupported now checks the emitted stores.

AI-assisted.
s_atomic_* and s_buffer_atomic_* (the 32-bit buffer atomic kinds, inc/dec,
cmpswap, and the and/or/xor/swap _x2 forms as one 32-bit atomic per dword)
are performed by the invocation in hardware lane 0 of the wave, at the
s_load / s_buffer_load address; with glc the old value is shuffled to every
lane (an exchange round under --wave64-emulate). Compute and mesh only.
GPU fixtures cs_w32_scalar_atomic / cs_w64_scalar_atomic, semantic test
ScalarAtomics.

AI-assisted.
@leoleahy

Copy link
Copy Markdown
Contributor

Independent CPU review at 5c7c4768 on Windows (CPython 3.14.8, no GPU, no game files): pass.

  • python -I tests/semantic/test_scalar_mem_store.py: 32 OK (negative control on f847748c: 4 errors, Unsupported: s_buffer_store_dword).
  • python tests/semantic/test_value_semantics.py ScalarStores ScalarAtomics: 6 OK (negative control on f847748c: 1 failure + 4 errors).
  • Code check: scalar stores and atomics reuse scalar_load() address/V#-bound logic cleanly, refuse lane-mask sources and non-compute/mesh stages for atomics, execute atomics once on hardware lane 0, and broadcast glc return values via subgroup shuffle / wave64 exchange round.

AI-assisted.

@kacper-serewis kacper-serewis 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.

CPU review at 5c7c476; 88ef3e8 changes only docs/design.md relative to the tested tree. With two logical wave32s in one synthetic 64-wide subgroup, two adds of 3 correctly move 100 to 106 but both waves receive old value 100. The return shuffle at rdna2spirv.py:8974 reads lane 0 for both; the second logical wave needs lane 32. With 32-wide subgroups the returns are 100 and 103. Separately the _x2 lowering near line 8944 uses independent 32-bit atomics, allowing a torn pair under contention (source finding). Preserve 64-bit atomicity or explicitly reject unsupported forms. Three focused CPU tests passed. AI-assisted; no GPU execution.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 88ef3e8 merged into main f847748c (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New tests pass natively: cs_w32_scalar_atomic (100 7 240 5 103 5 15 6); cs_w32_scalar_store (286331153 572662306 858993459 1145324612 1431655765 1717986918 2004318071 2290649224); cs_w64_scalar_atomic (100 100 240 5 103 15 255 261).
  • Whole run_gpu_tests.py: 188/202 native, 188/202 with LEONIDA_W64_EMULATE=1 (skips: the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 41/41.
  • tests/semantic/test_scalar_mem_store.py, tests/semantic/test_value_semantics.py: 72 passed, 296 subtests passed.

No game files. AI-assisted (Claude Code).

runtime/test/shaders/check.py
scripts/run_gpu_tests.py
tools/rdna2spirv.py

AI-assisted.
@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4458ea1 on NVIDIA RTX 4080, driver 610.88, Windows 11: pass

This head is the merge of main (aa2988cc); the earlier independent checks were at 5c7c4768 (CPU) and 88ef3e8e (AMD, Linux). Ran (MSVC 19.44 / VS 2022 BuildTools, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, native llvm-mc 23.1.6):

  • python -I scripts/run_gpu_tests.py --llvm-mc <llvm-mc> --build <build>: 166/213 passed, 47 skipped, 0 failed.
    • cs_w32_scalar_atomic PASS 100 7 240 5 103 5 15 6, cs_w32_scalar_store PASS.
    • cs_w64_scalar_atomic SKIP: no 64-wide subgroup here.
  • The same with LEONIDA_W64_EMULATE=1: 198/213 passed, 15 skipped, 0 failed. All three PASS, cs_w64_scalar_atomic 100 100 240 5 103 15 255 261, the same words as in the description.
  • tests/semantic/test_scalar_mem_store.py: OK. test_value_semantics (run from tests/semantic): 45 tests OK, including ScalarAtomics and ScalarStores (6/6).

Not run here: native GFX10.1 hardware. The description notes the gfx1036 oracle can't assemble scalar atomics.

Review: no new findings. The s_store-then-s_atomic ordering limit is documented in the contract rows, as the description says.

AI-assisted.

Conflicts: docs/design.md, tools/rdna2spirv.py

AI-assisted.
@KkelebekK

Copy link
Copy Markdown

Tested b9e2804e (clean merge into main 3faac557; this branch also carries the scalar-store commits it is stacked on) on AMD Radeon 780M (RADV PHOENIX, gfx1103 / RDNA3, Mesa 26.2.6, subgroup 32..64) and NVIDIA RTX 4070 Laptop (620.70.06, subgroup 32..32), CachyOS, Linux 7.3.4, llvm-mc 23.1.4, Python 3.14.10: pass, with one concern (the open wave32-on-64-wide-subgroup return value; see below).

scripts/run_gpu_tests.py, one GPU at a time (VK_LOADER_DRIVERS_SELECT), each run inside a 4 GB scope. The vk_* test programs come from an existing Linux build of an earlier main; git diff of runtime/ between that build's commit and this head touches no vk_* program (only xgameruntime files, CMakeLists and check.py), so no C++ was rebuilt.

main 3faac557 (my run) this head
780M native 202/217, 15 skipped, 0 failed 205/220, 15 skipped, 0 failed
4070 native 170/217, 47 skipped, 0 failed 172/220, 48 skipped, 0 failed
4070 LEONIDA_W64_EMULATE=1 201/217, 16 skipped, 0 failed 204/220, 16 skipped, 0 failed

Per-fixture verdicts differ from main only by the three new fixtures:

  • cs_w32_scalar_atomic PASS 100 7 240 5 103 5 15 6 and cs_w32_scalar_store PASS (286331153 ... 2290649224) on all three configurations.
  • cs_w64_scalar_atomic PASS 100 100 240 5 103 15 255 261 on the 780M with a real 64-wide subgroup (compute subgroup size: 64, --require-wave64) and on the 4070 with emulation; SKIP on the 4070 without it (no 64-wide subgroup), as the PR says.

Python: tests/semantic/test_scalar_mem_store.py + test_value_semantics.py 77 passed (326 subtests); with test_scalar_mem_smem, test_w64x_scalar_load_order, test_docs_design_rows, test_run_gpu_tests*, test_shader_meta, test_shader_checks: 293 passed (709 subtests). regen_fixtures.py --check exit 0. Whole python -I -m unittest discover -s tests/semantic: 6251 tests, 3 failures and 2 errors, all in test_loop_ballot_barrier.py (...emulate_and_native_cs x3, test_nested_divergent_loops_emulate, test_bug4b_loop_carried_split_sgpr_pair_ballot); the same 5 fail on main 3faac557 here, so they are not from this PR.

Before the change: the same tests on this head with tools/rdna2spirv.py restored to 3faac557: 9 failed, 76 passed (Unsupported: s_buffer_atomic_add, s_atomic_add, and the scalar-store tests).

Circularity: the fixtures' words come from the same reading of the ISA as the translator, so I checked them separately. A plain-Python model of add/sub/inc/cmpswap/or_x2 written from the instruction descriptions gives exactly the words of both fixtures. llvm-mc -mcpu=gfx1013 -show-inst -show-encoding decodes the fixtures' scalar-memory lines to the operands written (e.g. s_buffer_atomic_cmpswap s[22:23], s[0:3], 0x18 glc = S_BUFFER_ATOMIC_CMPSWAP_IMM_RTN_gfx10). The {DATA, CMP} order of cmpswap and inc's wrap rule are the PR's reading of the ISA text; nothing here can measure them.

Hardware: scalar stores and atomics are GFX10.1 only; llvm-mc rejects them for gfx1036 and for this 780M's gfx1103, so neither of my GPUs can run the native instruction. The 780M is RDNA3, not RDNA2. These runs check the translation on RADV/NVIDIA, not scalar-atomic hardware behaviour.

Concern (the open review finding, reproduced on the 780M): for a wave32 shader whose workgroup of 64 lands in one 64-wide subgroup (VKC_SUBGROUP_SIZE=64), two logical wave32s each do their atomic (target 100 -> 106), but both get lane 0's returned value. I ran a variant of cs_w32_scalar_atomic with group: 64,1,1 that also stores the returns seen by invocation 63 (not in the PR): at 64-wide, invocations 31 and 63 both see 100 7 240 5; at 32-wide they see 103 5 15 6 and 100 7 240 5, so with two separate subgroups the waves get distinct returns (one sees the other's add), while at 64-wide the second wave gets 100 although the target ended at 106. The PR's fixture reads only lane 31, so it does not reach this. Separately, as documented, a wave64 on 32-wide subgroups without emulation returns only to lanes 0-31 (cs_w64_scalar_atomic at VKC_SUBGROUP_SIZE=32 gives 100 0 0 0 103 15 255 261). I did not test the _x2 torn-pair case (it needs contention).

Synthetic inputs only, no game data. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested b9e2804 on Intel UHD 630 (CFL GT2, subgroup 8..32), Mesa 25.2.8 ANV, Ubuntu 24.04, LEONIDA_VK_DEVICE=Intel: pass.

  • scripts/run_gpu_tests.py --filter scalar_atomic after a full build: cs_w32_scalar_atomic PASS, cs_w64_scalar_atomic SKIP (no 64-wide compute subgroup on this device); 1/2 passed, 1 skipped. LEONIDA_VK_DEVICE=NoSuchDevice fails both with "no Vulkan physical device matches", so the Intel device ran the passing one.
  • Full scripts/run_gpu_tests.py: 156/220 passed, 62 skipped, 2 FAIL (cs_fmt_int_narrow and cs_signed_zero_nan), the same two FAILs main 3faac55 gives on this machine (154/217, 61 skipped, earlier run today).
  • tests/test_shader_checks.py: OK. regen_fixtures.py --check: no diff. pytest test_docs_design_rows, test_run_gpu_tests and test_shader_meta: 64 passed. tests/semantic ScalarAtomics plus test_scalar_mem_store: Ran 35 tests, OK.

The wave64 scalar atomic fixture cannot run on this GPU, so the 64-wide claim is not tested here.

AI-assisted.

@cxchhh

cxchhh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b9e2804 on Tesla V100 (Volta) + RTX 4090 (Ada), Linux: pass

Machines: Tesla V100-SXM2-32GB (Volta, 32-wide, NVIDIA 580.95.05, Ubuntu 24.04) and RTX 4090 (Ada, 32-wide, NVIDIA 570.153.02 user-space Vulkan driver in a Linux container, Vulkan 1.4.303). The 4090 has mesh shaders and fragment barycentrics, so it runs the mesh/barycentric tests the V100 skips. Each run is the PR diff applied to main 3faac557 (clean apply) and compared with unchanged main on the same machine. GPU suite: scripts/run_gpu_tests.py native, and with LEONIDA_W64_EMULATE=1 (V100 also LEONIDA_BARY_FALLBACK=1). Baseline main: V100 161/217 native, 194/217 emulated; 4090 167/217 native, 198/217 emulated; 0 failures anywhere. tests/semantic full suite: main has 6245 tests with the 5 known test_loop_ballot_barrier emulated cases failing (plus one test_vcmp16_opsel spirv-val error from my PATH, identical on every tree).

  • GPU suite: V100 163/217 native, 197/217 emulated; 4090 169/217 native, 201/217 emulated; 0 failures. The only differences from main are the three new fixtures:
    • cs_w32_scalar_atomic and cs_w32_scalar_store: PASS native and emulated on both GPUs, same words on both.
    • cs_w64_scalar_atomic: PASS emulated on both; skipped native, since there is no 64-wide subgroup.
  • tests/semantic: 6251 tests; the failure list is identical to main's.
  • test_value_semantics.py: 45 OK. test_scalar_mem_store.py: 32 OK. Run against main's translator, the PR's versions of these give 1 failure + 4 errors and 4 errors.

Not covered here: AMD hardware (native wave64), Windows.

AI-assisted (Claude Code).

docs/design.md
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py

AI-assisted.
@AwkGrep256

Copy link
Copy Markdown
Contributor

Tested 0900164 on CachyOS, RADV/Mesa 26.3.1, LLVM 23.1.5: pass.

  • GPU, scripts/run_gpu_tests.py --filter scalar_atomic: cs_w32_scalar_atomic and cs_w64_scalar_atomic PASS on an AMD Radeon RX 9070 XT (RADV GFX1201) and on AMD Radeon Graphics (Ryzen 7 9800X3D iGPU, RADV gfx1036), same words on both.
  • Negative control: with main's (561b65a8) tools/rdna2spirv.py both fixtures fail to translate (0/2).
  • CPU: tests/semantic/test_scalar_mem_store.py and test_value_semantics.py OK.

AI-assisted.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review

Reviewed change in PR #3676: rdna2spirv: GFX10.1 scalar atomics (s_atomic_, s_buffer_atomic_), once per wave.

Scope: docs/design.md, runtime/test/shaders/check.py, runtime/test/shaders/cs_w32_scalar_atomic.asm, +10 more
Diff: +449 / -22 across 13 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @evilclanker9000 - I went through rdna2spirv: GFX10.1 scalar atomics (s_atomic_, s_buffer_atomic_), once per wave and the diff around docs/design.md. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

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.

10 participants