Skip to content

rdna2spirv: ds_add_u64 and ds_sub_u64 (and _rtn_) as two 32-bit LDS atomics, GPU-checked - #2738

Open
evilclanker9000 wants to merge 4 commits into
ItsNotRexed:mainfrom
evilclanker9000:feat/ds-atomic-b64
Open

evilclanker9000 wants to merge 4 commits into
ItsNotRexed:mainfrom
evilclanker9000:feat/ds-atomic-b64

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: #2516 (comment)

What and why

ds_add_u64, ds_sub_u64 and their _rtn_ forms raised Unsupported. They now translate to two 32-bit atomics on the two dwords of the element, with no Int64 atomics on Workgroup memory (optional in Vulkan, like the existing ds_and/or/xor_b64): the low dword takes DATA.lo, and the carry (add) or borrow (sub) it made, found from the old low dword the atomic returns, goes into the high dword's DATA.hi. The number of carries over any order of the ops is the same, so the memory result is exact under any contention. The returned high dword comes from its own atomic, so another lane's op on the same element between the two can show up half done; this is documented as an approximation in the docs/design.md row.

Not done: ds_min/max_u64/i64, ds_rsub/inc/dec_u64 and ds_cmpst_b64 still raise Unsupported. A 64-bit min or max cannot be made of two independent dword atomics without sometimes giving a wrong word under contention, and an exact version needs either shaderSharedInt64Atomics plus an aliasing layout for the 32-bit LDS accesses (VK_KHR_workgroup_memory_explicit_layout; the feature would have to be enabled in every runtime and test device, as for VK_KHR_shader_fma) or a lock. Left for a separate decision. The 32-bit rsub/inc/dec forms are #2495's.

Related open PRs #2350 and #2317 add semantic tests for ds_add_u64 / ds_sub_u64; this PR does not touch them.

Tested

  • Semantic: tests/semantic/test_lds_add_sub_u64.py (carry and borrow edges, offsets, vdst aliasing the data, seeded values, the fixture's lane-0 twin). On main the value tests fail (199 errors, Unsupported); with the fix all 10 pass.
  • GPU fixture cs_w32_ds_add_sub_u64.asm (hand-derived words in the header): 32 lanes run ds_add_u64 and ds_sub_u64 on one element each (most adds carry out of the low dword), then lane 0 runs the wrapping/borrowing _rtn_ forms. main: translation fails (Unsupported) on both GPUs. With the fix, 8/8 words exact on the NVIDIA GeForce RTX 5080 (driver 616.92) and the AMD Radeon RDNA2 iGPU (driver 26.10.1), each selected with LEONIDA_VK_DEVICE.
  • Negative control (reverted): reversing the carry compare (OpULessThan dl new_lo) fails the fixture on both GPUs (out[1] 0x1fd, out[6] 0x1) and the semantic tests.
  • python -m unittest discover -s tests/semantic, regen_fixtures.py --check, tests/test_shader_checks.py: 2577 tests OK (3 skipped); the fixture check is clean and the shader checks pass.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • The new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted.

…cs with carry/borrow

The memory is exact under any contention; the returned high dword is an approximation like ds_and/or/xor_b64.
GPU fixture cs_w32_ds_add_sub_u64 on an RTX 5080 and the Radeon RDNA2 iGPU, semantic tests, design.md row.

AI-assisted.
@KiryaAfimin

Copy link
Copy Markdown
Contributor

Tested 67e6eb8 on Windows 11 with an RTX 4070 (driver 621.01). The stock cs_w32_ds_add_sub_u64 test matched all eight expected words.

I also tested the _rtn_ forms under contention. One workgroup of 128 threads (four 32-lane subgroups) added 0x1ffffffff to one shared 64-bit LDS counter and subtracted it from a second. Both counters started at zero. I checked the final values and every thread’s returned value against the expected 64-bit sequence, allowing any thread order.

Across 12 runs, both final counters matched and each operation returned all 128 expected prior values exactly once. I saw no mixed high/low return on this GPU and driver. A control with separate counters per thread also passed.

AI-assisted

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent RTX 5070 check at 67e6eb80 (Windows, driver 621.24, subgroup size 32). I extended contention to 256 × 128 lanes per configuration, using four native wave32 subgroups or two emulated RDNA wave64s per workgroup.

  • Every final accumulator matched, including a shared LDS word with mixed adds/subtracts from alternating lanes using seeded 64-bit data. Per-lane uncontended addresses also passed both aliased-destination returns and their final values (65,536 add/sub pairs per wave configuration).
  • The documented split-return approximation is readily observable. For _rtn_ I used a delta of 0x1_00000001, with no carry or borrow during the uniform-step section. The exact old value has the same rank in both dwords. One run had 20,864 add / 14,464 sub torn returns of 32,768 each in wave32, and 8,704 add / 9,120 sub in emulated wave64. Each half independently gave the expected permutation of ranks. For example, wave32 returned 0x74_00200040 starting from 0x54_00200000, which combines high rank 32 with low rank 64. Counts varied between dispatches.
  • I separately tested the native route on this GPU. With shaderSharedInt64Atomics and VK_KHR_workgroup_memory_explicit_layout enabled, two aliased std430 Workgroup blocks (uint[4] and uint64_t[2]) let me initialize and read back via dwords while performing a single native OpAtomicIAdd %ulong per operation. Subtract used the negated 64-bit delta. There was a workgroup barrier before and after the atomic phase. 65,536/65,536 supplied old values matched the legal serial sequence, and all final dword readbacks were exact. That tests the building block on this driver, independently of translator/runtime integration.

All the synthetic modules passed Vulkan 1.2 SPIR-V validation and the ten LDS semantic tests passed. The split path's post-barrier results look sound for these workloads. For a shader that uses the returned pair under contention, the mismatch counts make the native feature path worth distinguishing from the approximation. AMD RDNA 2 ISA reference · Khronos Workgroup memory aliasing extension

@padhossein

Copy link
Copy Markdown
Contributor

Independent 8-lane Vulkan check (67e6eb80). The submitted shader assembles and passes spirv-val, and its official runner correctly skips on local llvmpipe's subgroup size of 8. I ran a separate adaptation of the same program with local size 8 and recomputed the final sum/subtraction for lanes 0–7. All eight output words matched: 9bffff1c 00000022 7fffffe9 0000000f ffffffff ffffffff fffffffe 00000001. This exercises carry/borrow with eight lanes and the two return forms with lane 0 alone. The 10 semantic tests passed as well. Linux, software Vulkan Mesa 26.1.6, Khronos validation loaded with no diagnostics.

AI-assisted (Codex).

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested 67e6eb8 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 171/186 passed, 15 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base 4947579 on the same machine, every shared test has the same result, and the test this PR adds (cs_w32_ds_add_sub_u64) passes on both GPUs.
  • python -m unittest discover -s tests/semantic: 2577 OK

No game files. AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 67e6eb80be13ea37a344ad64246af2cb050b3fb3 on AMD Radeon Pro V540 (RDNA1, Navi 12 / gfx1011, PCI 1002:7360, RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan device 1.4.318, subgroup 32..64): pass. This is the first GPU result on this PR.

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --filter cs_w32_ds_add_sub_u64 --build runtime/build/linux: 1/1 passed, eight exact words (0xf00000f0 0x205 0xfffffe15 0xd 0xffffffff 0xffffffff 0xfffffffe 0x1). Same on two more pinned cards (1f:00.0, ca:00.0).
  • Command: python -I scripts/run_gpu_tests.py --build runtime/build/linux: 157/186 passed, 28 skipped, 1 failed (191 shaders translated, 191 pass spirv-val). Clean base 49475796 gave 156/185, 28 skipped, 1 failed — the only difference is the new passing fixture, so no regression. The single failure is the pre-existing cs_w32_salu64 RDNA1/RADV issue.
  • Command: pytest tests/semantic/test_lds_add_sub_u64.py -q: 10 passed.

Tools: llvm-mc 21.1.8 with AMDGPU target, SPIRV-Tools 2026.1, GCC 13.3.0, Python 3.12.3. No code was edited or pushed; only synthetic fixtures were used.

AI-assisted.

runtime/test/shaders/check.py
scripts/run_gpu_tests.py

AI-assisted.
docs/design.md
runtime/test/shaders/check.py
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py

AI-assisted.
LouisRiverstone added a commit to LouisRiverstone/Leonida that referenced this pull request Nov 23, 2026
Both 64-bit add/sub forms raised Unsupported. They are exact with 32-bit
atomics: the low dword's atomic returns its old value, which gives this
op's own carry (borrow), and the high dword then takes DATA.hi + carry.
Addition commutes and the carries over any order add up to the low
dword's wraps, so the memory is exact under contention (as ItsNotRexed#2738 does for
LDS); only the returned high dword can be torn. x2_atomic_words holds the
per-dword part for the global and buffer paths.

Tests: carry and borrow across the dword (global), add/sub in the value
grids of test_global_atomics_x2.py / test_buffer_atomics_x2.py, and 32
lanes adding / subtracting on one element (they interleave in the
interpreter) ending exact. The rejected-form lists move to min/max_x2.
docs/design.md rows updated; a stale 'the _x2 forms stay Unsupported'
sentence in the global atomics row goes.
@amarovndres

Copy link
Copy Markdown

Tested 1b3c2057 on AMD Radeon RX 9060 XT (RDNA4, discrete, subgroup 32..64), AMD 26.10.1 (32.0.32015.7029, LLPC, Vulkan 1.4.359), Windows 11 Pro (build 26200): pass.

  • scripts/run_gpu_tests.py (full suite; test programs built from this head with llvm-mingw, llvm-mc 23.1.6): 197/211 passed, 14 skipped, 0 failed. main d0835dd9 on the same machine: 196/210 passed, 14 skipped.
    • PASS cs_w32_ds_add_sub_u64 0xf00000f0 0x205 0xfffffe15 0xd 0xffffffff 0xffffffff 0xfffffffe 0x1
  • tests/semantic/test_lds_add_sub_u64.py: 10 tests, OK

AI-assisted (Claude Code). Synthetic shaders only, no game files.

@Kev-Ball

Copy link
Copy Markdown

Tested 1b3c2057 on an NVIDIA GeForce RTX 3080 Ti (32-lane subgroups), driver 620.73.04, CachyOS Linux 7.3.5-2-cachyos. I rebuilt the Vulkan harnesses from this revision and selected the NVIDIA device explicitly.

The new cs_w32_ds_add_sub_u64 fixture translated, passed Vulkan SPIR-V validation, and passed GPU readback with scripts/run_gpu_tests.py --filter cs_w32_ds_add_sub_u64 --require-pass. The eight words were f00000f0 00000205 fffffe15 0000000d ffffffff ffffffff fffffffe 00000001.

AI-assisted verification with synthetic inputs.

@thesubtleties

Copy link
Copy Markdown

Tested 1b3c2057 on NVIDIA GeForce RTX 3080 (Ampere, subgroup 32..32), driver 620.70.06, CachyOS Linux 7.3.4, LLVM 23.1.5: pass.

LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux: 215/215 translated and pass spirv-val; 196/211 GPU tests passed, 0 failed, 15 skipped (14 run by other tests, 1 needs a 64-wide fragment subgroup). cs_w32_ds_add_sub_u64 PASS (8/8 words). tests/semantic/test_lds_add_sub_u64.py: 10/10 OK.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 1b3c2057 merged with main 3eb4410b on Windows 11, RTX 4080 SUPER + Intel UHD 770 (MSVC 14.51): pass.

  • scripts/run_gpu_tests.py --filter cs_w32_ds_add_sub_u64: PASS on the RTX 4080 SUPER and on the Intel UHD 770 (1/1 each).
  • tests/semantic/test_lds_add_sub_u64.py: 10 tests OK.
  • Negative control (tools/rdna2spirv.py from main, everything else from the PR): the fixture fails on both GPUs with "missing .spv (translation failed)" (ds_add_u64 is untranslatable there) and the unit test gives 200 errors in 10 tests.

The GPU test programs are the runtime build of main 7b7d73f8 (the same exes for every run); the PR's tools/, scripts/ and runtime/test/shaders come from the merge.

@lunarminus

Copy link
Copy Markdown

Tested 1b3c2057 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • scripts/run_gpu_tests.py (full suite, test programs from the d0835dd9 Linux build, since the PR changes no C++/CMake):
    • Native: 197/211 passed, 14 skipped, 0 failed. 215/215 shaders translated and pass spirv-val.
    • LEONIDA_W64_EMULATE=1: 197/211 passed, 14 skipped, 0 failed. 215/215 translated and valid.
    • d0835dd9 without the PR: 196/210 in both modes. I compared every test's PASS/FAIL/SKIP between the two runs and none changed in either mode. The only new line is the added fixture, in both modes: PASS cs_w32_ds_add_sub_u64 0xf00000f0 0x205 0xfffffe15 0xd 0xffffffff 0xffffffff 0xfffffffe 0x1
  • --filter cs_w32_ds_add_sub_u64 --require-pass: 1/1 passed in both modes, same eight words.
  • tests/semantic/test_lds_add_sub_u64.py: 10 tests OK. tests/semantic/regen_fixtures.py --check: clean.
  • Full python -m unittest discover -s tests/semantic: 7466 tests, with 7 failures and 2 errors in test_loop_ballot_barrier (emulate cases), test_cmp_class_f64 (test_rejected_forms) and test_mutations (v_mul_legacy_f32). d0835dd9 without the PR gives the same 9 on its own, so they come from main and not from this PR.

I did not run the contention or torn-return experiments from the earlier comments. Synthetic shaders only, no game files.

AI-assisted (Claude Code).

docs/design.md
runtime/test/shaders/check.py
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py

AI-assisted.
@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 99215f9 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_lds_add_sub_u64.py -q: 10 passed, 198 subtests passed.
  • Targeted GPU filter cs_w32_ds_add_sub_u64: 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 main runtime build.

AI-assisted (OpenCode).

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested 99215f93 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

  • How the carry works (tools/rdna2spirv.py:4526-4551): the low dword is an OpAtomicIAdd/ISub. Its returned old value gives the carry (old + DATA.lo < DATA.lo) or the borrow (old < DATA.lo). Each lane then does its own atomic on the high dword with DATA.hi + carry. The total carry count is (lo0 + ΣDATA.lo) div 2^32 in any order (with subtractions mixed in, carries minus borrows), so the final memory is exact under contention, as the row at docs/design.md:390 claims. The _rtn_ high dword is read by a separate atomic, which makes the returned pair an approximation. The row says so.
  • scripts/run_gpu_tests.py --filter cs_w32_ds_add_sub_u64: the fixture is contended, with all 32 lanes on one element for the add and on another for the sub, and 0x14 carries. PASS in 12/12 native runs and 1/1 with LEONIDA_W64_EMULATE=1, every run 0xf00000f0 0x205 0xfffffe15 0xd 0xffffffff 0xffffffff 0xfffffffe 0x1. check.py on the same output: ok.
  • Heavier contention in a scratch shader (not proposed): 1024 threads in 32 subgroups, on an add element, a sub element and a mixed element (even lanes add, odd lanes subtract), with carries. In 30/30 dispatches all final 64-bit values were exact, and the returned low dwords were exact (a permutation of the in-order values). Returned pairs tear as documented. With 1024 ds_add_rtn_u64 of 0x1_00000001 from 0, 13,728 of 30,720 returned pairs (44.7%) had high ≠ low. With a carry crossing (0xFFFFFE00 + 1 each), 800 of 30,720 were torn.
  • 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++.
  • Semantic suite: Ran 6160, OK (skipped=1, expected failures=1). Main: 6150, the same result, so the difference is the 10 new tests and there are no new failures. Corpus slice --shard 0/64: 415/415 translated and 415 spirv-val-valid, as on main, and 0 of 415 .spv hashes differ. None of the 25,734 corpus listings uses ds_add/sub_u64 or the _rtn_ forms.
  • test_lds_add_sub_u64.py 10/10, test_lds_bitwise_b64.py and test_lds_atomics_values.py pass on both Pythons. regen_fixtures.py --check, compileall and git diff --check are clean. I re-derived the eight expected words with plain 64-bit integer arithmetic. All match.
  • Mutations:
    • one word changed in check.py and in run_gpu_tests.py: each runner reports the mismatch, and the semantic test fails;
    • ds_sub_u64 changed to ds_add_u64 in the .asm: GPU FAIL, and regen --check fails;
    • carry dropped: GPU FAIL (high word 0x1f1), and 72 semantic failures;
    • carry taken from a plain atomic load before the low-dword atomic (right for one lane, wrong under contention): the CPU tests pass, and only the contended GPU fixture catches it (0x211, and 0xfffffff0 for the sub high word);
    • _rtn_ high returning the low word: GPU FAIL, and 158 semantic failures;
    • borrow < changed to <=: 27 semantic failures; the GPU fixture survives because no lane hits old == DATA.lo.
  • Doc nit (docs/design.md:390): "exact under any contention" holds among ds_add/sub_u64 ops on the element. A concurrent ds_write_b64, ds_wrxchg*_b64 or ds_and/or/xor_b64 on the same element can leave a value that no order of 64-bit ops gives. A few words like "among add/sub on the element" would say that. Also, "32 lanes add and subtract on one element each" would be clearer as "all 32 lanes add into one element and subtract from another".
  • It conflicts only at registration lines (check.py, run_gpu_tests.py) with GPU test cs_w32_lds_add_f32: ds_add_f32 and ds_add_rtn_f32 #4323 and GPU test cs_w32_atomic_contend: buffer_atomic_inc/dec under contention #4331.

AI-assisted.

@DavidM1911

Copy link
Copy Markdown

Tested 99215f9 on two Vulkan devices, Windows 11, Vulkan SDK 1.4.363, MSVC 14.50, llvm-mc 23.1.6 (AMDGPU, LLVM-MinGW build):

  • tests/semantic/test_lds_add_sub_u64.py: 10/10 OK: OK.
  • runtime/test/shaders/build.py: 262/262 translated, all pass spirv-val.
  • check.py, NVIDIA GeForce RTX 3060 Ti (driver 610.88): cs_w32_ds_add_sub_u64 ok; 169/204 pass, 35 skipped, 0 failed.
  • check.py, AMD Radeon(TM) Graphics iGPU (Ryzen 9 7900X, gfx1036, driver 24.10.24, 128-byte push constants): cs_w32_ds_add_sub_u64 ok; 104/204 pass, 97 skipped, 0 failed.

AI-assisted. No game files.

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.