Skip to content

rdna2spirv: global_atomic_csub and buffer_atomic_csub - #3193

Merged
shayderrr merged 3 commits into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-csub
Nov 23, 2026
Merged

shayderrr merged 3 commits 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.

@evilclanker9000

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.

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

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

Copy link
Copy Markdown
Contributor

Tested 9437193 on Windows 11, Python 3.12.10 (CPU/semantic check only, no GPU, no game files): pass.

  • Trial-merged onto origin/main (98fa503): merges cleanly; git diff --check clean.
  • python -m unittest discover -s tests/semantic -p test_atomic_csub.py: 6/6 OK.
  • python -I tests/test_docs_design_rows.py: 4/4 OK.
  • Scope: CPU semantic tests and doc-row validation only (no GPU hardware run here). No blocking issues.

AI-assisted review (Gemini).

@raxjinn

raxjinn commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9437193e (no merge with main) on Windows 11 Pro 10.0.26200, Python 3.14.4 (CPU only; this PR has no GPU part): pass.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @DenRakEiw and @raxjinn, including the red control (111 errors with the translator change reverted). Both ran the current head 9437193e, so nothing is pending.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 9437193 merged with main d974c88 on native Windows (Python 3.13.1, llvm-mc 23.1.6) and with main 98fa503 on Linux (Python 3.12.16): pass. tests/semantic/test_atomic_csub.py, test_value_semantics.py and the rdna2spirv and shader_checks tests: Windows 8/8, Linux 12/12. Negative control with main's tools/rdna2spirv.py: test_atomic_csub fails (111 errors).

@DavidM1911

Copy link
Copy Markdown

Tested 3a36b59 (head includes the latest main merge) on two Vulkan devices, Windows 11, Vulkan SDK 1.4.363, llvm-mc 23.1.6 (AMDGPU, LLVM-MinGW build):

  • tests/semantic/test_atomic_csub.py: 6/6 OK.
  • runtime/test/shaders/build.py: 261/261 translated, all pass spirv-val.
  • check.py, AMD Radeon(TM) Graphics iGPU (Ryzen 9 7900X, gfx1036, driver 24.10.24): 115/203 pass, 84 skipped (mostly the 144-byte push-constant SKIP set). The only failures — cs_bvh, cs_bvh32, cs_heap_init, ps_array_resinfo — reproduce identically with shaders built from main on the same binaries and on the NVIDIA run below, so they are not from this change.
  • check.py, NVIDIA GeForce RTX 3060 Ti (driver 610.88): 164/203 pass, 35 skipped, same four pre-existing failures.

AI-assisted. No game files.

# Conflicts:
#	tools/rdna2spirv.py
@tylerstraub

Copy link
Copy Markdown
Contributor

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

Verified both global_atomic_csub and buffer_atomic_csub with glc on an RTX 5090 at head dd12775fc5be1a03bd71b2479f7d859dcd901173. All 16 synthetic Vulkan dispatches passed with subgroup size 32. Each shader passed Vulkan 1.2 SPIR-V validation.

Sixteen odd lanes contended on one word. For each dispatch I compared their returned old values as a multiset against 16 serial unsigned saturating subtractions. I also checked the final word, inactive lane outputs and surrounding guard words. With operand 3, initial values 100, 48, 47 and 0 finished at 52, 0, 0 and 0 respectively. Both forms also passed with operands 0x80000000 and zero from initial values 0xffffffff and 0x80000000.

AI-assisted verification.

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

Summary

rdna2spirv: global_atomic_csub and buffer_atomic_csub. Changes docs/design.md and tests/semantic/test_atomic_csub.py (plus 1 more). globalatomiccsub and bufferatomiccsub (GFX10.3) raised Unsupported.
New tests: test_reference, test_global_values, test_global_without_glc_keeps_vdst, test_buffer_values_and_bounds.

Why merge

Addresses a concrete failure mode with a focused, scoped change and regression coverage. The patch stays within the affected code path, so it is low risk and prevents bad output or failures in real use.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

rdna2spirv: global_atomic_csub and buffer_atomic_csub. Changes docs/design.md and tests/semantic/test_atomic_csub.py (plus 1 more). globalatomiccsub and bufferatomiccsub (GFX10.3) raised Unsupported.
New tests: test_reference, test_global_values, test_global_without_glc_keeps_vdst, test_buffer_values_and_bounds.

Why merge

Addresses a concrete failure mode with a focused, scoped change and regression coverage. The patch stays within the affected code path, so it is low risk and prevents bad output or failures in real use.

Verdict: Approve

@shayderrr
shayderrr merged commit 5dc7bb3 into ItsNotRexed:main Nov 23, 2026
0 of 5 checks passed
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.

9 participants