Skip to content

GPU test cs_w32_atomic_contend: buffer_atomic_inc/dec under contention - #4331

Open
LouisRiverstone wants to merge 6 commits into
ItsNotRexed:mainfrom
LouisRiverstone:gpu-test-atomic-contend
Open

LouisRiverstone wants to merge 6 commits into
ItsNotRexed:mainfrom
LouisRiverstone:gpu-test-atomic-contend

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

buffer_atomic_inc/dec (#2448) are OpAtomicCompareExchange loops. Their tests run them in one lane, or in the CPU interpreter, which runs lanes in lockstep. @TheMrBeez checked the loops under contention by hand on #3097, but nothing in the suite does. This GPU fixture, runtime/test/shaders/cs_w32_atomic_contend.asm, has all 32 lanes of a wave32 group run every op on the same dwords, so the loops race for real on a GPU.

The words start as the harness's 0xEE fill, F = 0xEEEEEEEE. Every lane applies the same op, so the final word doesn't depend on the order. The returned old values of one op are all different, so each lane adds 1 << (distance of its old value from F) into another word. A lost or doubled update changes that sum.

word case expected
0 32 × inc, D = ~0: F + 32 0xEEEEEF0E
1 F + Σ 1 << (old − F) over word 0's returned values (F + 0xFFFFFFFF iff each of 0…31 came back once) 0xEEEEEEED
2 32 × inc, D = 20: the first gives 0, then it wraps every 21 0xA
3 32 × dec, D = ~0: F − 32 0xEEEEEECE
4 32 × dec, D = 5: the first gives 5, then 5…0 repeats 0x4
5 umax of F and word 0's returned values: F + 31 0xEEEEEF0D
6 umin of F and word 3's returned values: F − 31 0xEEEEEECF
7 F + Σ 1 << (F − old) over word 3's returned values 0xEEEEEEED
  • Registration: like the other wave32 fixtures (WAVE32_DUMPS/CS_DUMPS, the compute list, the expected words, the wave64-exclusion tuple, regen_fixtures.py). It's not a lane-mask test, so it also runs on llvmpipe.
  • Scope: LDS is left out on purpose. The CPU interpreter gives each invocation its own LDS, so it couldn't check those words.

Tests: tests/semantic/test_atomic_contend_fixture.py runs the fixture on the CPU against 32 sequential ops. It also checks that a lost update would change the bit sum, and that both runners expect the same words. It passes 4/4, regen_fixtures.py --check is clean, and tests/test_shader_checks.py and tests/test_run_gpu_tests.py pass.

Not run on a GPU by me: CI's llvmpipe job will run it, and hardware runs are welcome, especially AMD, NVIDIA and Intel, where the loops really interleave. No game files. Claimed on #4257 before starting.

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

All 32 lanes of a wave32 group run each op on the same dwords, so the
translated CAS loops race for real on a GPU (the CPU interpreter runs lanes in
lockstep). Same op on every lane: the final words are order-independent,
including inc/dec wrapping at a small D. Each lane adds 1 << (old - fill)
into another word, so a lost or doubled update changes the sum; umax/umin
of the returned values check the extremes. Registered in check.py,
run_gpu_tests.py and regen_fixtures.py; tests/semantic/
test_atomic_contend_fixture.py runs it on the CPU against 32 sequential ops.

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

Built and ran this at 559ce671 on Windows 11, RTX 5070 Ti: run_gpu_tests.py --filter atomic_contend -> 1/1 passed; translator 1/1 spirv-val.

cs_w32_atomic_contend returned 4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141, exercising the buffer_atomic_inc/dec compare-exchange loops under real cross-lane contention rather than the lockstep CPU interpreter. No blocking issue.

@MKmajster

Copy link
Copy Markdown

Hardware run as requested: NVIDIA GeForce RTX 3070 (Ampere, wave32), driver 617.42, Windows 11 24H2. run_gpu_tests.py --filter cs_w32_atomic_contend at 559ce67, three runs: PASS each time, same words:

PASS  cs_w32_atomic_contend  4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141

So with all 32 lanes racing on the same dwords, the OpAtomicCompareExchange loops of buffer_atomic_inc/dec lose no update on NVIDIA's driver. llvm-mc 23.1.6, runtime built from main aa2988c. No game files. AI-assisted (Claude).

@akimfomin

Copy link
Copy Markdown

Independent check at 559ce671 on RTX 4070 Ti SUPER (617.14), Windows 11 28000.3501, physical subgroups of 32. The exact fixture returned all eight expected words in 3/3 dispatches.

I extended the same synthetic input to record the returned inc/dec values per invocation and ran it with 128 invocations in one workgroup (four subgroups sharing the same words). 16/16 dispatches returned the expected final words and both complete ranges of 128 previous values, with no duplicates or gaps. The 32-invocation readback variant also passed 4/4. Masking off one lane in a control changed the final words and left that lane's two output slots untouched, as expected. All four modules passed offline Vulkan 1.2 validation. I used the RDNA2 text as a listing with the repository's compute header and this head's Vulkan harness.

One runner issue at this head: python -B -I tests/semantic/test_atomic_contend_fixture.py -v exits with ModuleNotFoundError: support, while unittest discovery passes 4/4. In a scratch copy, adding the test directory to sys.path before the two helper imports (as in test_valu_permlane16.py) made both entry points pass 4/4. Synthetic inputs only. AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

PASS. The inc/dec compare-exchange math checks out by hand (F+32, F-32, D=20 -> 0xA, D=5 -> 0x4, the bit sums -> F-1, umax -> F+31, umin -> F-31); the fixture is hand-written with expected words, registration is complete, 32-lane contention is real on a GPU, and regen_fixtures --check/compileall/test_shader_checks/test_run_gpu_tests are clean.

Coordination: it inserts at the same anchors as #4337 and #4323 (check.py:88,119, run_gpu_tests.py:947,975,1063, regen_fixtures.py:24) and conflicts on top of #4337, so rebase after whichever lands first.

Approve. AI-assisted review, no game files, no GPU.

@theswiss24

Copy link
Copy Markdown

Tested 559ce671 (contains main 03b09a08) on two NVIDIA GPUs, test programs built from main 03b09a08: pass on both, with the same words.

GPU driver, OS llvm-mc run_gpu_tests.py
RTX 4070 (Ada) 621.24, Windows 11 21.1.8 196/211 passed, 15 skipped, 0 failed
RTX 2060 SUPER (Turing) 580.178.04, Ubuntu 24.04 20.1.2 196/211 passed, 15 skipped, 0 failed

Both are main's 195/210 plus the new fixture, with every other test unchanged:

PASS  cs_w32_atomic_contend  4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141

The words are identical on both cards. LEONIDA_W64_EMULATE=1 on both. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

python -I on the file failed with ModuleNotFoundError: support, as the folder
was not on sys.path (found by @justr4mzi on ItsNotRexed#4323, the same template). It is
now inserted, as test_cs_shared_memory_limit does.
@vibryxcx

Copy link
Copy Markdown

Tested f4734c0 on AMD Radeon RX 6800 (RDNA2, subgroup 32..64), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200): pass. The runs so far were NVIDIA; this is the AMD side of the contention check.

  • run_gpu_tests.py --filter cs_w32_atomic_contend --require-pass: 1/1 in four runs (three at 559ce67, one at f4734c0; the fixture is the same in both), the same words each time: 4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141, which is your table (0xEEEEEF0E 0xEEEEEEED 0xA 0xEEEEEECE 0x4 0xEEEEEF0D 0xEEEEEECF 0xEEEEEEED).
  • tests/semantic/test_atomic_contend_fixture.py at f4734c0 under python -I: 4 OK.
  • Full run_gpu_tests.py at 559ce67: 196/211 passed, 15 skipped, 0 failed; the base 03b09a0: 195/210 with the same skips. The only difference is the new fixture.
  • Full ctest: one failure, vk_pipeline_check_cli, on both the PR and the base (a timeout in its CLI checks that passes when rerun alone), so not from this change.

Built with scripts\build_runtime.ps1 (MSVC 14.44), Vulkan SDK 1.4.363, llvm-mc 23.1.6 (conda-forge), Python 3.12.10. No game files.

AI-assisted (Claude Code): it built both commits and ran the tests.

@radv-tinker

Copy link
Copy Markdown
Contributor

Tested f4734c0a on AMD Radeon RX 9070 XT (RDNA4), RADV GFX1201, Linux (Fedora 43), subgroup 32..64: pass. The earlier checks don't include RADV.

python3 -I scripts/run_gpu_tests.py --filter <test> with the Mesa shader cache off, for the 1 test(s) this PR adds. Each passed in all three configurations: Mesa 25.3.6 (Fedora package) with ACO, the same with LLVM (RADV_DEBUG=llvm), and Mesa 26.3.1 (built from source) with ACO. The three give the same words:

  • cs_w32_atomic_contend: 4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141

# Conflicts:
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1c49799 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

  • pytest tests/semantic/test_atomic_contend_fixture.py -q: 4 passed, 10 subtests passed.
  • Targeted GPU filter cs_w32_atomic_contend: 1/1 pass.
  • Full GPU suite (python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build): 213/233 passed, 16 skipped, 0 failed (expected skips; total includes the new fixture).

No runtime C++ changes in this PR; tested from the PR worktree against the existing runtime build.

AI-assisted (OpenCode).

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested 1c49799e merged onto main 561b65a8 (the head already contains it) on Apple M4, macOS 27.2.1, MoltenVK 1.4.2, Python 3.15.2 and 3.12.16: pass, with one word that should change (last bullet)

  • scripts/run_gpu_tests.py --filter cs_w32_atomic_contend: PASS in 12/12 native runs, with the same words every time (4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141), and 1/1 with LEONIDA_W64_EMULATE=1. check.py on the same output: ok.
  • Full run_gpu_tests.py: 166/233 passed, 59 skipped, 8 failed. Main 561b65a8 on the same build: 165/232, 59 skipped, the same 8 failures. The only difference is the new PASS row. The PR changes no C++.
  • test_atomic_contend_fixture.py 4/4 on both Pythons. regen_fixtures.py --check, compileall and git diff --check are clean. I re-derived the expected words independently by applying the ISA inc/dec pseudocode 32 times in order. All match.
  • Heavier contention in a scratch shader (not proposed): the same four ops with 1024 threads (32 subgroups, all on the same dwords through the Device-scope CAS loops). In 30/30 dispatches the final words were exact (F+1024, 0xF, F−1024, 0x2), and the 1024 old values returned by inc and by dec were each a permutation of the in-order ones. No hang and no lost update on MoltenVK.
  • Mutations caught:
    • one word changed in check.py and in run_gpu_tests.py: each runner reports the mismatch, and the semantic test fails;
    • CAS loop exiting without a retry (done always true): GPU FAIL (word 0 = F+1, 31 updates lost), and 8 semantic failures;
    • inc wrap >= changed to >: GPU FAIL (word 2 = 9), and the semantic test fails;
    • buffer_atomic_dec v9 (D = 5) changed to inc: FAIL (word 4 = 1);
    • buffer_atomic_inc v1 changed to buffer_atomic_add: FAIL.
  • Survived: buffer_atomic_inc v8, v20 (D = 20) changed to buffer_atomic_dec gives GPU PASS with the same eight words. With 32 ops and D = 20, inc ends at 31 mod 21 = 10 and dec ends at 20 − (31 mod 21) = 10, so word 2 cannot tell inc from dec. D = 2, 6 and 8 have the same coincidence. Only regen --check noticed the change, because the CPU test reads the .s fixture. Fix: use D = 19 (cs_w32_atomic_contend.asm:24 and the header). Inc then gives 31 mod 20 = 11 = 0xB and dec would give 8. Update word 2 in check.py and run_gpu_tests.py, sequential(inc, 19) in test_atomic_contend_fixture.py:58, and regenerate the fixture.
  • It conflicts only at registration lines (check.py, run_gpu_tests.py, regen_fixtures.py) with GPU test cs_w32_lds_add_f32: ds_add_f32 and ds_add_rtn_f32 #4323, and in the first two with rdna2spirv: ds_add_u64 and ds_sub_u64 (and _rtn_) as two 32-bit LDS atomics, GPU-checked #2738.

AI-assisted.

…give the same word

@sovljanskii on ItsNotRexed#4331: with 32 ops and D = 20, inc ends at 31 mod 21 = 10 and
dec at 20 - (31 mod 21) = 10, so swapping buffer_atomic_inc v8 for a dec
still passed on the GPU (D = 2, 6 and 8 coincide the same way). With D = 19
inc gives 31 mod 20 = 11 (0xB) and dec would give 8. Fixture, header, both
expected-word lists and the CPU reference updated; the test now also checks
that inc and dec differ at that D. llvmpipe: PASS with 11 in word 2.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @sovljanskii. Good mutation work: D = 20 really can't tell inc from dec. Fixed in 69a488b4 as you suggested.

  • The change: word 2 now uses D = 19. inc gives 31 mod 20 = 11 (0xB), and dec would give 8.
  • Updated: the fixture and its header, both expected-word lists, and sequential(inc, 19) in the CPU test, plus a regenerated fixture.
  • New check: the test asserts that inc and dec differ at that D, so a later edit can't bring back a coincidental value.
  • Results: llvmpipe passes with 11 in word 2, and the CPU test, regen --check and both registration suites are OK.

Thanks also @javdc for the Steam Deck run.

# Conflicts:
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
@nicknickalive

Copy link
Copy Markdown

Tested cef6d5a (trial-merged onto main 1cc6e3c; vk_* test programs rebuilt from 1cc6e3c) on NVIDIA GeForce RTX 5090 (610.88, subgroup 32..32), Windows 11 Pro 26200, llvm-mc 23.1.6: pass.

  • LEONIDA_VK_DEVICE=5090 python -I scripts/run_gpu_tests.py --build <build> --filter atomic_contend: 1 translated, passes spirv-val, cs_w32_atomic_contend PASS 0xEEEEEF0E 0xEEEEEEED 0xB 0xEEEEEECE 0x4 0xEEEEEF0D 0xEEEEEECF 0xEEEEEEED: the 32 contending inc/dec each returned a distinct old value, with no lost or doubled update.
  • Same on the AMD Radeon RDNA2 iGPU in a Ryzen 9 9950X3D (driver 32.0.21036.18, subgroup 32..64): PASS, same words.
  • tests/semantic/test_atomic_contend_fixture.py 4/4 OK, tests/test_shader_checks.py OK, regen_fixtures.py --check clean.
  • The previous head 1c49799 (word 2 with D = 20) also passed on both GPUs. The description's table still lists word 2 as D = 20 / 0xA; the fixture is now D = 19 / 0xB.

No game files. AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested cef6d5a083007a2f2042ca4bfd7717cd79b98d26 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

The revised D=19 case catches an increment/decrement swap on Linux. The stock fixture matched all eight expected words, including 11 at word 2. In a temporary copy I changed only the atomic increment on that word to a decrement and kept the stock expectations. The runner reported a failure with 8 at word 2.

I repeated each version in three fresh native wave32 dispatches. Only word 2 differed in each pair of full readbacks. This confirms the revised bound distinguishes the two instructions under contention; the stock result also agrees with the Windows hardware runs.

AI-assisted verification.

@Mortify7612

Copy link
Copy Markdown

Tested cef6d5a0 merged onto main 1cc6e3c1 (clean) on AMD Radeon RX 9060 XT (RDNA4, GFX1200, subgroup 32..64), RADV Mesa 25.0.7, Debian 12 Linux, llvm-mc 23.1.6: pass. I ran run_gpu_tests.py --filter <fixture> for the fixture(s) this PR adds or changes, not the full suite:

PASS cs_w32_atomic_contend 4008636174 4008636141 11 4008636110 4 4008636173 4008636111 4008636141

AI-assisted (Claude Code).

@Itschotsch

Copy link
Copy Markdown

Tested cef6d5a on NVIDIA GeForce RTX 4090, driver 595.99.02, Linux (Debian 13 userspace), llvm-mc 21.1.8: pass

Test programs built from main 1cc6e3c (this PR changes no C++). Run in an isolated container; AI-assisted (Claude Code).

# Conflicts:
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
@lukasas74

Copy link
Copy Markdown

Tested 0cda9988f on NVIDIA GeForce RTX 5080 (subgroup 32..32), NVIDIA 621.01, Vulkan 1.4.357, Windows 11 Pro 26200: pass.

  • run_gpu_tests.py --filter cs_w32_atomic_contend: PASS, PASS, PASS on 3 runs (4008636174 4008636141 11 4008636110 4 4008636173 4008636111 4008636141).
  • Full run_gpu_tests.py: 193/242 passed, 48 skipped (248 shaders: 248 translated, 248 pass spirv-val). Against main a5e3db9f7 on the same card (192/241 passed, 48 skipped): new: cs_w32_atomic_contend PASS.
  • Failing on main too: ms_wave_info_s3 (ms_wave_info_s3 fails natively on NVIDIA RTX 5080 (Windows, subgroup 32): all pixels read back -1 #8306).
  • python -I -m unittest discover -s tests/semantic -p test_atomic_contend_fixture.py: Ran 4 tests, OK

Toolchain: standalone tools (-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF) built with llvm-mingw 20261117 (clang 23.1.6) against Vulkan-Headers 1.4.369; llvm-mc 23.1.6 (conda-forge llvm-tools), SPIRV-Tools 2026.4. Automated run, AI-assisted (Claude Code).

@YordiLorenzo

Copy link
Copy Markdown

Tested 0cda998 on RTX 5090, NVIDIA 615.71.09, Ubuntu 26.04: pass

  • run_gpu_tests.py, whole suite: 193 of 242 passed, 48 skipped, 1 failed (ms_wave_info_s3, ms_wave_info_s3 fails natively on NVIDIA RTX 5080 (Windows, subgroup 32): all pixels read back -1 #8306). Main c8ef3d0 on the same card: 192 of 241, the same failure. The only difference is cs_w32_atomic_contend, which passes
  • run_gpu_tests.py --filter cs_w32_atomic_contend, 20 runs in a row: 20 passed
  • build.py + check.py: cs_w32_atomic_contend ok, 177 of 212 pass, 35 skipped, none failed
  • pytest tests/semantic/test_atomic_contend_fixture.py: 4 passed
  • ctest, Linux subset: 64 tests, 0 failed (2 skipped as on main)
  • Negative control: with the first expected word of cs_w32_atomic_contend changed by one, run_gpu_tests.py fails it

@matheusfermiano-source

Copy link
Copy Markdown

Tested 0cda9988f (the PR's diff applied on main 867d922f0), full python -I scripts/run_gpu_tests.py on both GPUs of one laptop, each compared with main on the same machine and binaries, CachyOS Linux: pass.

GPU Driver main this PR Difference
AMD Radeon 660M (RDNA2 iGPU, gfx1035, Ryzen 5 7535HS), subgroup 32..64 RADV, Mesa 26.3.1 225/241 passed, 16 skipped 226/242 passed, 16 skipped new PASS: cs_w32_atomic_contend
NVIDIA GeForce RTX 3050 Laptop (Ampere), subgroup 32 NVIDIA 620.73.04 192/241 passed, 48 skipped 193/242 passed, 48 skipped new PASS: cs_w32_atomic_contend; fails on main too: ms_wave_info_s3 (#6172)

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

@nicolasabosch

Copy link
Copy Markdown
Contributor

Tested 0cda9988f on NVIDIA GeForce RTX 4070 Laptop GPU (Ada, subgroup 32..32), driver 621.24, Windows 11 Pro build 26300, MSVC 19.51: pass. Applied on main 46492b021.

  • tests/semantic/test_atomic_contend_fixture.py: passes.
  • build.py + check.py, native: cs_w32_atomic_contend ok, 177 of 212 pass, 35 skipped.
  • run_gpu_tests.py --filter cs_w32: 18/18 passed native and with LEONIDA_W64_EMULATE=1, including cs_w32_atomic_contend.
  • cs_w32_atomic_contend repeated natively: 20/20 passed.
  • I ran the cs_w32 subset here, not the whole GPU battery with this fixture.

Overlap: this PR, #3039 and #4323 add to the same lists in check.py and run_gpu_tests.py, so they conflict with each other. Whichever lands second needs a rebase.

No game files. AI-assisted (Claude).

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.