Skip to content

rdna2spirv: global_atomic_csub and buffer_atomic_csub - #3193

Open
LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-csub
Open

LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-csub

Conversation

@LouisRiverstone

@LouisRiverstone LouisRiverstone commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

global_atomic_csub and buffer_atomic_csub (GFX10.3) raised Unsupported.

Semantics: tmp = M; M = (DATA > tmp) ? 0 : tmp - DATA. It's an unsigned subtract clamped at 0 that never wraps. With glc, the old M comes back. @Vonsek measured this on gfx1036 (comment below): 162/162 (memory, data) pairs over both forms.

Implementation: a csub case in main's Gen.atomic_inc_dec_cas (from #2448), plus csub in the is_inc_dec routing of global_atomic and buffer_atomic. That's 10 lines in tools/rdna2spirv.py.

  • Exact under contention: every update is one successful exchange of the value it read.
  • Operands, inactive lanes, buffer bounds, glc: as for inc/dec.

Tests: tests/semantic/test_atomic_csub.py:

  • a 9×9 value grid, including DATA = M, DATA > M, and the 0/~0 edges
  • no glc (vdst kept)
  • buffer values and an out-of-range element
  • 32 lanes contending on one dword, which must equal 32 sequential subtracts, including cases that hit the clamp partway
  • 20 seeded cases

Without the change the file fails (111 errors); with it, 6/6 pass.

python -I -m unittest discover -s tests/semantic: 10 failures, the same ones main has without this change (test_loop_ballot_barrier, test_vcmp16_opsel, test_cmp_class_f64, test_mutations). None are in the atomics tests.

docs/design.md: new row after "Global atomics".

Previously stacked on my #3097, which I closed as a duplicate of #2448. Now rebuilt on main.

The translation is tested on CPU only; no GPU run. No game files. Claimed on #3047 before starting.

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

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Converted to draft: #3097, which this was stacked on, is closed as a duplicate of the earlier #2448. Once #2448 merges, I'll rebase this onto main and reuse its atomic_inc_dec_cas loop, so the diff is only the csub formula, its routing, the test and the design row.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE; integer atomics): buffer_atomic_csub v1, off, s[4:7], 0 offset:16 glc (a raw V# over the output buffer) and global_atomic_csub v1, v0, v6, s[2:3] offset:16 glc, through raw hw_oracle kernels, one lane, the dword seeded with a global store, on every (memory, data) pair of {0, 1, 2, 5, 0x7fffffff, 0x80000000, 0xfffffffe, 0xffffffff, 0x12345678}: 81 pairs per form, 162 in all. The returned register is the old word and the word afterwards is (DATA > M) ? 0 : M - DATA in all 162, so the clamped unsigned subtract in the PR description is what the hardware does for both forms. (The assembler spells these buffer_atomic_csub / global_atomic_csub; the _u32 spelling is rejected for gfx1036.) Contention between lanes, inactive lanes and out-of-range buffer elements were not part of this run, and neither was this PR's translation. The script is local, not pushed.
AI-assisted.

Both raised Unsupported. csub (GFX10.3) stores (DATA > M) ? 0 : M - DATA,
an unsigned subtract clamped at 0 (measured on gfx1036 by Vonsek in ItsNotRexed#3193);
it goes through main's inc/dec CAS loop (atomic_inc_dec_cas), exact under
contention.

tests/semantic/test_atomic_csub.py: value grid, no glc, buffer bounds,
32 lanes on one dword, seeded values. docs/design.md row added.
@tylerstraub

Copy link
Copy Markdown
Contributor

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

Translated global_atomic_csub and buffer_atomic_csub passed a 32-lane contention check on Linux, RTX 5090, driver 595.99.02, at 068d5b04. In each synthetic dispatch, all 32 lanes subtracted from the same dword with glc and stored their returned words at separate offsets. I compared the final dword and the full multiset of returned old values with 32 sequential unsigned subtracts clamped at zero. Lane order was unconstrained.

All 14 dispatches matched (448 returned words), and the 95 guard dwords in each readback stayed intact. Starting at 100 and subtracting 3 finished at 4; starting at 50 reached zero partway through. The cases also included zero data, an initially zero dword, equality at 0x80000000, and subtracting 0x80000000 from 0xffffffff. All six translated compute modules passed Vulkan 1.2 SPIR-V validation. This covers in-range accesses with all lanes active.

AI-assisted verification.

@ttd0x

ttd0x commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 068d5b04 on NVIDIA GeForce RTX 2080 Ti (Turing, subgroup 32..32, driver 610.88), Windows 11, on top of current main aa2988cc: pass, no regression.

  • tests/semantic/test_atomic_csub.py: 6 tests OK.
  • scripts/run_gpu_tests.py (default, no emulation): 214 shaders translated, all pass spirv-val, 164/210 passed, 46 skipped (native wave64, expected on a 32-wide device), 0 failed. Identical to main aa2988cc.
  • Limit: no GPU fixture in the repo exercises global_atomic_csub / buffer_atomic_csub, so this shows only that nothing else broke (the contention check by tylerstraub above covers the instruction itself).

Synthetic fixtures only, no game data. AI-assisted (Claude Code).

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