Skip to content

rdna2spirv: add --int64-atomics option for 64-bit OpAtomicExchange on *_atomic_swap_x2 (SHADER-868) - #251

Open
kotekompotek wants to merge 1 commit into
ProjectLeonida:mainfrom
kotekompotek:work/shader-868-atomic-swap-x2
Open

kotekompotek wants to merge 1 commit into
ProjectLeonida:mainfrom
kotekompotek:work/shader-868-atomic-swap-x2

Conversation

@kotekompotek

@kotekompotek kotekompotek commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-868 (#14 (comment))

What and why

  • Addressed @ubiquitarianism's review feedback on this opt-in feature PR (ds_wrxchg_rtn_b64 is untouched).
  • Added --int64-atomics flag forwarding in make_game.py (FALLBACK_FLAGS and CLI parser) alongside tools/rdna2spirv.py and runtime/test/shaders/build.py (LEONIDA_INT64_ATOMICS=1).
  • Updated the Global atomics and buffer_atomic_{and,or,xor,swap}_x2 rows in docs/design.md to document opt-in --int64-atomics vs the default 32-bit dword-pair approximation.
  • Added GPU shader fixture runtime/test/shaders/cs_atomic_swap_x2_int64.asm, registered it in runtime/test/shaders/check.py, scripts/run_gpu_tests.py, and tests/semantic/regen_fixtures.py (tests/semantic/fixtures/disasm/cs_atomic_swap_x2_int64.s).
  • Updated in commit 14eaf6052: enabled shaderBufferInt64Atomics on VkPhysicalDeviceVulkan12Features in runtime/shims/d3d12_vulkan_device.cpp (recorded in runtime/shims/d3d12_com.h, asserted in runtime/test/d3d12_vulkan_device_test.cpp), replaced the single-thread cs_atomic_swap_x2_int64.asm fixture with a 64-thread tearing check, and added dedicated --int64-atomics runner pass in scripts/run_gpu_tests.py.

Tested

On commit 14eaf6052:

  • python -I tests/semantic/regen_fixtures.py --check (OK)
  • python -I tests/semantic/test_atomic_swap_x2_int64.py (6/6 OK via spirv_mini_interp.run_workgroup, 64 threads + 14 = OpAtomicExchange %ulong)
  • tests/semantic/test_fixtures.py & tests/semantic/test_regen_fixtures.py (OK)
  • All 12 tests/test_docs_*.py (OK)
  • compileall (OK)
  • MSVC /W4 compile-check of runtime/shims/d3d12_vulkan_device.cpp & runtime/test/d3d12_vulkan_device_test.cpp (OK)
  • Note: live RDNA2/Vulkan hardware execution of the 64-thread tearing fixture not available locally.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@Mouad-Scriptz-0

Copy link
Copy Markdown

I tested this on a GPU. The 64-bit path works, and it fixes a real tearing problem. Some suggestions to help it land are at the end.

Hardware: Intel Meteor Lake graphics (ANV, Mesa 26.2.7, Fedora 45, shaderBufferInt64Atomics = true). I used vk_compute_test from main 84347bbb6 and this branch at 06de9fea0.

1. Single thread, exact. This is a temporary variant of cs_global_mem_ext (one thread) that does global_atomic_swap_x2 … glc and buffer_atomic_swap_x2 … offen glc, then reads back the old values and the memory. main, this PR without the flag and this PR with LEONIDA_INT64_ATOMICS=1 all give the expected 0xF 0xF00000 0xAAAA0001 0xBBBB0002 0x12345678 0x9ABCDEF0 0xCCCC0003 0xDDDD0004. With the flag, spirv-dis shows OpAtomicExchange %ulong twice and OpCapability Int64Atomics, and the module passes spirv-val.

2. Contention. Here 40 groups × 64 threads each swap {x, ~x} (with x unique per thread) into the same 8 bytes using buffer_atomic_swap_x2 glc. A returned old pair is counted as torn when hi != ~lo; the 0xEEEEEEEE fill is excluded. Results over 8 runs of 2560 threads each:

translator torn old pairs per run
main 2455–2494 (~96%)
this PR, default (flag off) 2455–2490
this PR, LEONIDA_INT64_ATOMICS=1 0, 8 of 8 runs

On RDNA2 these instructions are single 64-bit atomics, so the returned old value never tears. Two 32-bit atomics per element (what main emits) tear almost every time under contention. With the flag on, this PR gets that right.

The contention fixture (hand-written, no game data). It replaces cs_global_mem_ext.asm to reuse its FSSM header and run line: vk_compute_test <spv> 40 dump
; out[0] - 0xEEEEEEEE = torn count, out[1] - 0xEEEEEEEE = threads that ran (2560), out[2..3] = final pair
v_lshl_add_u32 v1, s4, 6, v0
v_add_nc_u32_e32 v1, 1, v1
v_not_b32_e32 v2, v1
v_mov_b32_e32 v3, 8
buffer_atomic_swap_x2 v[1:2], v3, s[0:3], 0 offen glc
s_waitcnt vmcnt(0)
v_not_b32_e32 v4, v1
v_xor_b32_e32 v5, v4, v2
v_xor_b32_e32 v6, v1, v2
v_min_u32_e32 v5, 1, v5
v_min_u32_e32 v6, 1, v6
v_and_b32_e32 v5, v5, v6
v_mov_b32_e32 v7, 0
buffer_atomic_add v5, v7, s[0:3], 0 offen
v_mov_b32_e32 v8, 1
v_mov_b32_e32 v9, 4
buffer_atomic_add v8, v9, s[0:3], 0 offen
s_waitcnt vmcnt(0)
s_endpgm

Suggestions:

  • The fix never runs as wired. --int64-atomics is off by default, and only build.py turns it on, through LEONIDA_INT64_ATOMICS. make_game.py and scripts/run_gpu_tests.py never pass it, so no game shader and no CI or GPU-suite run uses the 64-bit path. One option is to turn it on by default: the module already records shaderBufferInt64Atomics in its metadata, so pipeline creation can refuse it on a device without the feature. The other is to have make_game.py pass it when the device reports the feature.
  • The title doesn't match the diff. It names ds_wrxchg_rtn_b64 (LDS), but the diff changes only global_atomic_swap_x2 and buffer_atomic_swap_x2. Retitling it, for example to "rdna2spirv: 64-bit OpAtomicExchange for global/buffer_atomic_swap_x2 (SHADER-868)", would avoid confusion. Note that ds_wrxchg_rtn_b64 would need shaderSharedInt64Atomics, which this Intel GPU reports as false.
  • Project process. CONTRIBUTING.md and AGENTS.md ask for three things this PR doesn't have yet:
    • a claim in Coordinating who works on what #14 (the commit message says SHADER-868, an ID from the old repository);
    • a row in the docs/design.md contract table saying how the rule was confirmed;
    • a GPU fixture. The contention fixture above could become one, with the stress check in scripts/run_gpu_tests.py.

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

@ubiquitarianism

Copy link
Copy Markdown
Collaborator

Not merging yet. This adds an opt-in feature rather than fixing a bug, and ds_wrxchg_rtn_b64 isn't touched. Please:

  • retitle it;
  • update the buffer_atomic_{and,or,xor,swap}_x2 row in docs/design.md, which currently calls the dword-wise tearing an accepted approximation;
  • add a GPU fixture under runtime/test/shaders, built with LEONIDA_INT64_ATOMICS=1, and say which device and driver ran it;
  • pass the flag through make_game.py; today only build.py reads it.

@kotekompotek
kotekompotek force-pushed the work/shader-868-atomic-swap-x2 branch from 06de9fe to a48dc6e Compare November 24, 2026 03:33
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested a48dc6e on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, regression check

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026); llvm-mc 23.1.6 (conda-forge), SPIRV-Tools 2026.4, Vulkan SDK 1.4.363.0. No game files. This PR adds no GPU fixture, so this checks the translator change on the GPU suite.

@kotekompotek
kotekompotek force-pushed the work/shader-868-atomic-swap-x2 branch from a48dc6e to 8435a80 Compare November 24, 2026 15:13
@kotekompotek kotekompotek changed the title rdna2spirv: fix ds_wrxchg_rtn_b64 / atomic_swap_x2 64-bit address and data handling rdna2spirv: add --int64-atomics option for 64-bit OpAtomicExchange on *_atomic_swap_x2 (SHADER-868) Nov 24, 2026
@kotekompotek

Copy link
Copy Markdown
Contributor Author

Updated per feedback and rebased cleanly onto current main as a single commit (8435a8069):

  1. Retitled to reflect opt-in --int64-atomics on buffer_atomic_swap_x2 / global_atomic_swap_x2 (ds_wrxchg_rtn_b64 untouched).
  2. Updated the Global atomics and buffer_atomic_{and,or,xor,swap}_x2 rows in docs/design.md.
  3. Added runtime/test/shaders/cs_atomic_swap_x2_int64.asm (wired into build.py via LEONIDA_INT64_ATOMICS=1, check.py, run_gpu_tests.py, and tests/semantic/regen_fixtures.py). Note: llvm-mc/spirv-as are not installed locally, so physical GPU execution could not be run on this host; CPU verification via spirv_mini_interp and all translator/doc/CI tests pass.
  4. Forwarded --int64-atomics through make_game.py (FALLBACK_FLAGS and CLI parser, tested in test_atomic_swap_x2_int64.py and test_docs_cli_flags.py).

@aslan2142

Copy link
Copy Markdown
Collaborator

I ran 8435a8069 merged onto main a28927849 on two AMD GPUs, Mesa 26.3.1 RADV on Linux, with the validation layer on. Both report shaderBufferInt64Atomics = true:

  • AMD Ryzen 9 7950X3D iGPU (RDNA2, gfx1036);
  • AMD Radeon RX 7900 XTX (RDNA3).

The 64-bit path does fix tearing. I used Mouad-Scriptz-0's contention test: 2,560 threads swapping {x, ~x} into one element with glc, 8 runs.

GPU default, buffer / global --int64-atomics, buffer / global
7950X3D iGPU 8 / 8 torn per run 0 / 0
RX 7900 XTX 235–423 / 303–436 torn per run 0 / 0

No regressions.

  • Full run_gpu_tests.py: main 231/249, this PR 232/250 (+ the new fixture) on both GPUs.
  • Semantic suite: 0 failed.
  • A 2,000-shader corpus slice is byte-identical with and without the flag. No game shader uses *_atomic_swap_x2.

Still open from Mouad-Scriptz-0's review: the claim in #14, run_gpu_tests.py passing the flag, and a multi-thread fixture. The new fixture passes with and without the flag here, because a single thread can't tear.

Two more points:

  • The game runtime doesn't enable shaderBufferInt64Atomics on its device (runtime/shims/d3d12_vulkan_device.cpp:9565-9593). make_game.py --int64-atomics output would use a feature the device wasn't created with.
  • docs/design.md:289 and :497 still end with "Not GPU-verified", and the Global atomics row keeps the old unconditional tearing sentence. The docstrings at tools/rdna2spirv.py:3011 and :3156 describe the old behaviour.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142 aslan2142 added the missing claim Needs a claim in #14 label Nov 24, 2026
@kotekompotek
kotekompotek force-pushed the work/shader-868-atomic-swap-x2 branch from 8435a80 to 14eaf60 Compare November 24, 2026 18:21
@kotekompotek

Copy link
Copy Markdown
Contributor Author

@aslan2142 @Mouad-Scriptz-0 Updated in commit 14eaf6052:

  1. Enabled shaderBufferInt64Atomics on VkPhysicalDeviceVulkan12Features in runtime/shims/d3d12_vulkan_device.cpp, recorded shader_buffer_int64_atomics in runtime/shims/d3d12_com.h, and asserted it in runtime/test/d3d12_vulkan_device_test.cpp.
  2. Replaced the single-thread cs_atomic_swap_x2_int64.asm fixture (and regenerated .s via regen_fixtures.py) with a 64-thread (2 wave32 waves, 4 rounds = 256 buffer_atomic_swap_x2 + 256 global_atomic_swap_x2 via s_barrier and Device-scope atomics) tearing check (EXPECTED_FIXTURE = [0, 0, 256, 256, 1, 1, 0xDEAD0001, 0x9ABCDEF0]).
  3. Updated runtime/test/shaders/build.py, runtime/test/shaders/check.py, scripts/run_gpu_tests.py (int64_atomics() helper + dedicated --int64-atomics pass merging its index.json entry without clobbering 32-bit defaults), tests/semantic/test_atomic_swap_x2_int64.py, tools/rdna2spirv.py docstrings, and docs/design.md.
  4. Verified locally on 14eaf6052: regen_fixtures.py --check OK, test_atomic_swap_x2_int64.py (6/6 OK), test_fixtures.py & test_regen_fixtures.py OK, all 12 test_docs_*.py OK, compileall OK, MSVC /W4 compile-check of d3d12_vulkan_device.cpp & d3d12_vulkan_device_test.cpp OK. (llvm-mc, CTest Vulkan run, and RDNA2 hardware are not present on this Windows GTX 1050 host, so CI / hardware GPU run on the updated fixture is pending.)

@aslan2142 aslan2142 removed the missing claim Needs a claim in #14 label Nov 24, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@kotekompotek
kotekompotek force-pushed the work/shader-868-atomic-swap-x2 branch from 14eaf60 to 2d0a082 Compare November 25, 2026 07:29
@kotekompotek

Copy link
Copy Markdown
Contributor Author

Rebased on current ubi/main (3f81d46) and pushed. Conflicts resolved against the newer main changes; test suite for the touched area passes locally (xs_proxy_mock_test PASS on Windows; the int64-atomics semantic tests 6/6 and test_rdna2_decode 21/21 where applicable).

AI-assisted: yes

@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants