Skip to content

rdna2spirv: ds_cmpst_f32 and the global/buffer fcmpswap atomics - #3153

Open
LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:float-cmpswap
Open

LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:float-cmpswap

Conversation

@LouisRiverstone

@LouisRiverstone LouisRiverstone commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

ds_cmpst_f32/ds_cmpst_rtn_f32, global_atomic_fcmpswap and buffer_atomic_fcmpswap raised Unsupported. #2495 has the LDS f32 min/max but not cmpst_f32, and #2653 covers only the image form.

Semantics (RDNA2 ISA): tmp = M; M = (tmp == CMP) ? DATA : tmp, with the returning forms giving tmp. The compare is a float compare: −0 == +0 matches, and a NaN never matches, even with identical bits.

Implementation: OpAtomicCompareExchange compares bits, which differs exactly in those cases. So Gen.float_cmpswap_loop runs a compare-and-swap loop:

  • read M
  • pick DATA or M by OpFOrdEqual
  • swap that in if M still holds what was read; otherwise retry

This is exact under contention.

  • Operands: ds_cmpst_f32 takes CMP then DATA, as ds_cmpst_b32. The global and buffer forms take vdata as {DATA, CMP}, as cmpswap.
  • Inactive lanes, buffer bounds, glc: as for the integer siblings.
  • OpPhi fix: the result OpPhi in global_atomic and buffer_atomic now names the block that actually branches to the merge. For the existing ops that's the same _do block as before, so their code is unchanged. For fcmpswap it's the loop's exit block.
  • Not measured: subnormal inputs; the interpreter compares them exactly. _x2 forms stay Unsupported.

Tests: tests/semantic/test_float_cmpswap.py, for all four ops:

  • equal, unequal, −0/+0 both ways, NaN against the same NaN, NaN CMP, ∞, and a subnormal
  • no glc
  • buffer out-of-range
  • 32 lanes on one dword: every lane tries 0.0 → 1.0 with CMP −0; one wins and the rest keep 1.0
  • non-pair vdata and _x2 rejected

Without the change the file fails (30 errors); with it, 6/6 pass. test_global_atomics.py and test_lds_atomics_values.py still pass. python -I -m unittest discover -s tests/semantic: 4148 tests OK (4 skipped).

docs/design.md: new row after ds_cmpst_b32.

GPU test (added after the first review): runtime/test/shaders/cs_w32_fcmpswap.asm. Lane 0 of a wave32 group runs ds_cmpst_rtn_f32, ds_cmpst_f32 and buffer_atomic_fcmpswap (with and without glc): −0 against +0, a NaN against its own bits, and a value one ulp off. It is registered in check.py, run_gpu_tests.py and regen_fixtures.py. tests/semantic/test_fcmpswap_fixture.py runs it on the CPU and checks that both runners expect the same eight words. The global form isn't in the fixture, because it needs the heap setup from #1760.

The new GPU fixture hasn't been run on hardware by me; CI's llvmpipe job will run it. No game files. Claimed on #2891 before starting.

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

@CharlyRien

Copy link
Copy Markdown

Tested 9c70c02 (merged on current main) on RTX 3090, driver 616.56, Windows 11 23H2: pass. With this translator change, run_gpu_tests.py gives 143/189 passed, 46 skipped (wave64 on a 32-wide GPU), the same as main on this GPU. ctest matches main here (3 known failures on this machine: d3d12_shader_cache, see #2167; and d3d12_vulkan_device / d3d12_typeless_rtv, see #2498).

AI-assisted test run.

@hidrogenone

Copy link
Copy Markdown
Contributor

Tested fbe9cc11 on NVIDIA GeForce RTX 5080 (Blackwell), driver 32.0.16.2124, Windows 11, Vulkan SDK 1.4.357.0. llvm-mc LLVM 21.1.8 (WSL Ubuntu).

scripts/run_gpu_tests.py --filter fcmpswap:

cs_w32_fcmpswap   1/1 passed

Translator: 1 shader translated, spirv-val clean — the ds_cmpst_f32 / global/buffer float compare-swap path is exact on this adapter. NVIDIA Blackwell (RTX 50-series) coverage. AI-assisted.

@emirduran707

Copy link
Copy Markdown
Contributor

Tested fbe9cc1 on AMD Radeon RX 9060 XT (RDNA4), AMD driver 26.10.44 (LLPC, Vulkan driver 2.0.395), Windows 11 Pro 10.0.26300, subgroup 32..64: pass. The checks above have no AMD run from outside the author; this adds an AMD card with native 64-wide subgroups.

  • python -I scripts/run_gpu_tests.py --build <build> --filter <fixture>, one run per new fixture:
    • cs_w32_fcmpswap: PASS, 0 1065353216 2143289344 1065353216 2147483648 1077936128 1065353216 2143289344; same with LEONIDA_W64_EMULATE=1
  • Whole suite (native wave64): 195 shaders translated, 195 pass spirv-val; 176/190 passed, 14 skipped, 0 failed. The skips are the 14 fixtures the d3d12_* tests run.
  • build.py + check.py: 215 shaders: 215 translated, 215 pass spirv-val; 165 of 168 pass, 3 skipped; ok for cs_w32_fcmpswap.
  • CPU: python -I -m unittest discover -s tests/semantic -p test_fcmpswap_fixture.py 4 OK, python -I -m unittest discover -s tests/semantic -p test_float_cmpswap.py 6 OK, tests/test_shader_checks.py 69 OK, tests/test_run_gpu_tests.py 34 OK, tests/semantic/regen_fixtures.py --check clean. I did not run the whole tests/semantic suite.

Test programs: an MSVC 14.44 build of main ec8ec4c; this PR's runtime C++ and CMake are identical to that commit, and the translator, runner and check.py are this PR's. llvm-mc 21.1.8 (native), Vulkan SDK 1.4.363.0. No negative control run here. No game files. AI-assisted (Claude Code).

ds_cmpst_f32 / ds_cmpst_rtn_f32, global_atomic_fcmpswap and
buffer_atomic_fcmpswap raised Unsupported. They store DATA when M equals
CMP as binary32 (-0 == +0, NaN never matches), which is not the bit compare
OpAtomicCompareExchange does, so float_cmpswap_loop runs a CAS loop: read M,
pick DATA or M by the float compare, swap that in if M is unchanged.
Operands follow ds_cmpst_b32 / cmpswap; bounds, glc and inactive lanes
follow their integer siblings. The result OpPhi of global_atomic and
buffer_atomic now names the block that actually branches to the merge
(the loop's exit block for fcmpswap).

tests/semantic/test_float_cmpswap.py covers all four, incl. 32 lanes on one
dword. docs/design.md row added.
Lane 0 of a wave32 group checks the float compare on a GPU: -0 matches +0,
NaN never matches its own bits, one ulp off doesn't match, for
ds_cmpst_rtn_f32, ds_cmpst_f32 and buffer_atomic_fcmpswap (glc and not).
Registered in check.py, run_gpu_tests.py and regen_fixtures.py;
tests/semantic/test_fcmpswap_fixture.py runs it on the CPU and checks both
runners expect the same words.
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.

4 participants