Skip to content

rdna2spirv: implement image_atomic_swap, image_atomic_inc and - #3548

Open
s0las16 wants to merge 2 commits into
ItsNotRexed:mainfrom
s0las16:devin/1795379284-img-atomic-swap-inc-dec
Open

s0las16 wants to merge 2 commits into
ItsNotRexed:mainfrom
s0las16:devin/1795379284-img-atomic-swap-inc-dec

Conversation

@s0las16

@s0las16 s0las16 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new item, implement image_atomic_swap, image_atomic_inc and image_atomic_dec in tools/rdna2spirv.py (the open atomic PRs #3153 and #3193 cover global/buffer only, no existing tracker item)

What and why

The RDNA2 MIMG encoding has three integer image atomics upstream main rejected as Unsupported:

  • image_atomic_swap: an exchange (OpAtomicExchange on the texel pointer)
  • image_atomic_inc: M = (M >= DATA) ? 0 : M + 1
  • image_atomic_dec: M = (M == 0 || M > DATA) ? DATA : M - 1

inc/dec are the same unsigned wrapping forms as ds_inc_u32/ds_dec_u32 and global_atomic_{inc,dec} (both already use this CAS loop), lowered to a structured OpAtomicCompareExchange loop on the image texel pointer so no extra capability is needed. None of the three has an operand that leaves memory unchanged, so like image_atomic_cmpswap they run only in lanes EXEC leaves on; with glc the texel's pre-op value lands in vdata. Works on 2D and 3D storage images, including a16 packed coordinates.

docs/design.md "Image atomics, size and LOD queries" row updated with the new rule.

Tested

  • tests/semantic/test_image_atomic_cas.py: 8 new CPU tests, all fail on main (Unsupported: image_atomic_*), pass here.
  • Every emitted module assembles with spirv-as and validates with spirv-val, native and --wave64-emulate (24 modules).
  • Semantic suite for image/LDS/buffer/atomic/smem paths: 1195 tests green.
  • No GPU run (CPU-checked only, no game files).

Checklist

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

AI-assisted (Devin).

…tomic_dec

The RDNA2 MIMG set has three integer image atomics the translator rejected as
Unsupported: swap (an exchange), and the unsigned wrapping inc/dec (inc: M =
(M >= DATA) ? 0 : M + 1; dec: M = (M == 0 || M > DATA) ? DATA : M - 1), the same
rules as ds_inc_u32/ds_dec_u32 and global_atomic_{inc,dec}. None has an operand
that leaves memory unchanged, so like cmpswap they run only under EXEC; with
glc the texel's pre-op value lands in vdata. 2D and 3D storage images, a16
packed coordinates included.

CPU-checked by tests/semantic/test_image_atomic_cas.py (all 8 tests fail on
main with Unsupported); every emitted module assembles and validates with
spirv-as/spirv-val, native and --wave64-emulate. Not GPU-verified.
AI-assisted (Devin).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE; integer atomics): image_atomic_inc, image_atomic_dec and image_atomic_swap with glc on a 1x1 32_UINT linear 2D image (T# built as tools/hw_oracle/tsharp.py does, texel in the kernel's code memory), through raw hw_oracle kernels, one lane, on every (memory, data) pair of {0, 1, 5, 0x7fffffff, 0x80000000, 0xfffffffe, 0xffffffff}: 49 pairs per op, 147 in all. The returned register is the old texel and the texel afterwards is inc: (M >= D) ? 0 : M + 1, dec: (M == 0 || M > D) ? D : M - 1, swap: D in all 147, so the rules in the PR description are what the hardware does (unsigned, wrapping, no operand that leaves memory unchanged). 2D only: 3D images, a16 and the EXEC-masked lane behaviour were not part of this run, and neither was this PR's translation. The script is local, not pushed. AI-assisted.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested 3f1e223 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

  • python3 -m pytest tests/semantic/test_image_atomic_cas.py -q -> 8 passed
  • python3 -I scripts/run_gpu_tests.py --build <build> -> 184/199 passed, 14 skipped, 0 failed (expected D3D12-owned skips)

No runtime C++ changes; GPU suite run from the PR worktree against the existing main runtime build.

AI-assisted (OpenCode).

@MKmajster

Copy link
Copy Markdown

Tested 3f1e223 on a GPU: NVIDIA GeForce RTX 3070 (Ampere), driver 617.42, Windows 11 24H2 (26100). The translated image_atomic_inc, image_atomic_dec and image_atomic_swap give the expected texels and data registers, 128/128 words, in three runs. The PR and the comments so far checked them on the CPU and on hw_oracle only, not as translated SPIR-V on a driver.

How: a throwaway fixture in the style of cs_imgatomic.asm, run through vk_store_test ... words. That gives the same 8x8 r32 uint image filled with 100 + i, the sint image with -2i, and the same T#s. It was translated with this branch's rdna2spirv.py, and the result passes spirv-val. Expected words come from the rules in the description (inc: M >= D ? 0 : M + 1, dec: M == 0 || M > D ? D : M - 1, swap: D), which @Vonsek measured on gfx1036:

  • Thread 0 only (v_cmpx_eq_u32 0, v0), with glc, data register stored to the sint image: inc with D > M, D == M (wraps to 0), D < M, D = 0xffffffff; dec with D > M, D < M (gives D), D == M - 1, D == M; swap. Edge cases: dec on M = 0 (gives D = 77), dec on M = 0 with D = 0, and inc on M = 0xffffffff with D = 0xffffffff (gives 0). The last three have their texel set first with image_store.
  • Without glc: swap and inc leave the data register alone and still change the texel.
  • All 64 threads on one texel, no glc. The result doesn't depend on lane order, so this exercises the CAS loop under contention: inc D = 0xffffffff from 113 gives 0xb1; dec D = 1000 from 114 gives 0x32; inc D = 150 from 115 wraps once and gives 0x1c; dec D = 30 from 116 goes to D, down to 0, back to D twice and gives 0x1d.

uint words 64/64 and sint words 64/64 exact in each of 3 runs. As a control, the same program translated by main (f847748) is rejected (Unsupported: image_atomic_inc). vk_store_test has no Vulkan-validation hook, so validation wasn't run on this path.

Not covered: 3D images and a16 coordinates (the PR's CPU tests have those), and wave64.

No game files. AI-assisted (Claude).

The fixture (generated)
; cs_imgatomic_incdec: image_atomic_inc/dec/swap on the 8x8 r32 uint image (vk_store_test words)
s_mov_b32 s16, 0x2000
s_mov_b32 s17, 0x1400000
s_mov_b32 s18, 0x22
s_mov_b32 s19, 0x22
s_mov_b32 s20, 0x22
s_mov_b32 s21, 0x22
s_mov_b32 s22, 0x22
s_mov_b32 s23, 0x22
s_mov_b32 s24, 0x3000
s_mov_b32 s25, 0x1500000
s_mov_b32 s26, 0x33
s_mov_b32 s27, 0x33
s_mov_b32 s28, 0x33
s_mov_b32 s29, 0x33
s_mov_b32 s30, 0x33
s_mov_b32 s31, 0x33
v_mov_b32_e32 v1, 5
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0xffffffff
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 6
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0x3e8
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 7
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0x96
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 0
v_mov_b32_e32 v2, 2
v_mov_b32_e32 v3, 0x1e
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
v_cmpx_eq_u32_e32 0, v0
v_mov_b32_e32 v1, 0
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0xc8
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 1
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0x65
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 2
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0x32
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 3
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0xffffffff
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 4
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0xc8
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 5
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0x32
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 6
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0x69
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 7
v_mov_b32_e32 v2, 0
v_mov_b32_e32 v3, 0x6b
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 0
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0xdeadbeef
image_atomic_swap v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 1
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0x1234
image_atomic_swap v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 2
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0x3e8
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 3
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0x0
image_store v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
v_mov_b32_e32 v3, 0x4d
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 4
v_mov_b32_e32 v2, 1
v_mov_b32_e32 v3, 0xffffffff
image_store v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
v_mov_b32_e32 v3, 0xffffffff
image_atomic_inc v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 1
v_mov_b32_e32 v2, 2
v_mov_b32_e32 v3, 0x0
image_store v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
v_mov_b32_e32 v3, 0x0
image_atomic_dec v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
v_mov_b32_e32 v1, 2
v_mov_b32_e32 v2, 2
v_mov_b32_e32 v3, 0x55
image_store v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_waitcnt vmcnt(0)
v_mov_b32_e32 v3, 0x0
image_atomic_swap v3, v[1:2], s[16:23] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm glc
s_waitcnt vmcnt(0)
image_store v3, v[1:2], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm
s_endpgm

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 3f1e223b (base f847748c) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host, Python 3.14.4): pass.

  • tests/semantic/test_image_atomic_cas.py: 8 passed on this head. With tools/ from f847748c all 8 fail, refused as Unsupported: image_atomic_swap (4), image_atomic_inc (3), image_atomic_dec (1).
  • scripts/run_gpu_tests.py, whole suite: 152/199 passed, 46 skipped, 1 failed, identical per test to main f847748c on this card. The failure is ps_sdwa_f16_const, the NVIDIA f16 denormal flush on main (rdna2spirv: widen f16 denormal operands without UnpackHalf2x16 (NVIDIA reads them as 0) #3220 / rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) #3040), not this PR.
  • cs_imgatomic, cs_imgatomic3d and ps_imgatomic pass, so the image atomics that were already there still work on the GPU.
  • ctest on the Linux build (CMake + Ninja, GCC): 41/41 passed.
  • Not covered: no GPU fixture uses image_atomic_swap/_inc/_dec, so the new ops ran on the CPU only. The wrap of inc/dec at the clamp value would be worth an exact-word fixture.

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

@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 3f1e223b (on main f847748c) on RTX 4070 (Ada, NVIDIA 621.01, subgroup 32..32), Windows 11 (10.0.26300), llvm-mc 23.1.6 (AMDGPU), spirv-val from Vulkan SDK 1.4.363.1, Python 3.14.6: pass.

  • The full tests/semantic suite: 7330 tests OK, 0 failures (7322 on the base; the +8 are test_image_atomic_cas.py). scripts/run_gpu_tests.py: 153/199 passed, 46 skipped, 0 failed, identical to f847748c.
  • Negative control: with only tools/rdna2spirv.py restored to f847748c, all 8 tests of test_image_atomic_cas.py fail (8 errors); with the change they pass.

Not tested: the instructions on a GPU (the PR has no GPU fixture; the tests run the translated SPIR-V in the interpreter), wave64, AMD/Intel. Tested with AI assistance (Claude); synthetic programs only, no game data.

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @s0las16.

At 3f1e223b, the translated 2D and 3D swap fixtures both write the value returned by OpAtomicExchange to v5 with glc, and leave v5 alone without it. The focused tests currently miss two regressions in that behavior. In separate temporary copies I (1) made the writeback unconditional and (2) changed only its destination from v5 to v6. All eight tests in test_image_atomic_cas.py passed on the head and on both modified copies.

For test_swap_glc_writes_vdata_back, I checked the exchange result through the active-lane OpSelect to the store and its VGPR access-chain index, for both dimensions and both glc settings. All four cases matched on the head. The unconditional change failed both glc-off cases; the wrong destination failed both glc-on cases. This would pin the behavior more precisely than looking for any store after the exchange.

CPU translation check on synthetic listings. AI-assisted review (Codex).

AI-assisted.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

7 participants