Skip to content

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

Open
Vonsek wants to merge 6 commits into
ItsNotRexed:mainfrom
Vonsek:claude/s-atomic
Open

Vonsek wants to merge 6 commits into
ItsNotRexed:mainfrom
Vonsek:claude/s-atomic

Conversation

@Vonsek

@Vonsek Vonsek 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.
@Vonsek
Vonsek marked this pull request as ready for review November 22, 2026 21:30
@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.

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