Skip to content

rdna2spirv: guard all image_atomic_* by active lane - #3559

Merged
shayderrr merged 2 commits into
ItsNotRexed:mainfrom
SweatierKey:fix/image-atomic-active-guard
Nov 23, 2026
Merged

shayderrr merged 2 commits into
ItsNotRexed:mainfrom
SweatierKey:fix/image-atomic-active-guard

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Summary

Guard all 2D and 3D storage-image atomics (image_atomic_*) inside if (active) in tools/rdna2spirv.py (matching image_atomic_cmpswap, buffer_atomic, global_atomic, and lds_op) and add image_atomic_swap, image_atomic_inc, and image_atomic_dec.

Claim: #3381 (comment)

Bug & Motivation

Previously in Gen.image_misc:

  • Only image_atomic_cmpswap wrapped OpImageTexelPointer and OpAtomicCompareExchange inside an active-lane conditional branch.
  • The other 9 ops (add, sub, umin, umax, smin, smax, and, or, xor) executed OpAccessChain, OpImageTexelPointer, and OpAtomic* unconditionally in inactive lanes with a neutral operand (0 or 0xFFFFFFFF), performing spurious read-modify-writes on image memory and dereferencing potentially uninitialized descriptors/coordinates in inactive lanes.
  • image_atomic_swap (which has no neutral element) and image_atomic_inc/image_atomic_dec were Unsupported.
  • 2D and 3D image atomics duplicated the same logic twice and contained an unreachable if op == "image_store": block.

Fix

  • Unify 2D (%simages) and 3D (%simages3d) image atomics in Gen.image_misc under one if (active) branch that encloses OpAccessChain, OpImageTexelPointer, OpAtomic*, and the optional glc writeback, and remove the dead image_store block.
  • Validate vdata width (1 VGPR for 32-bit atomics, 2 VGPRs for image_atomic_cmpswap) so unsupported 64-bit forms fail loudly instead of silently dropping the high dword.
  • Add image_atomic_swap (OpAtomicExchange) and image_atomic_inc/image_atomic_dec (reusing Gen.atomic_inc_dec_cas on %_ptr_Image_uint) for both 2D and 3D images, with/without glc and a16.

Verification (CPU + spirv-val, no game data)

  • tests/semantic/test_image_atomics_guard.py (3/3 OK, all 13 ops × 2D/3D × glc/a16 + bad-width rejections, every module validated with spirv-as + spirv-val --target-env vulkan1.3, checking that OpAccessChain and OpImageTexelPointer sit inside an OpSelectionMerge-guarded block).
  • tests/semantic/test_fspx_rules.py (9/9 OK)
  • tests/test_text_write_line_endings.py (2/2 OK)

@evilclanker9000

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.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 8ca3b786 merged onto main f847748c on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 182/199 passed, 15 skipped (plain main: 182/199 passed, 15 skipped).
  • Python tests this PR touches: 3 passed, 49 subtests passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted CPU review at 8ca3b786 vs f847748c, complementing the native instruction results above. With initialized synthetic images, 328/328 modules passed Vulkan 1.2 and 1.3 SPIR-V validation. 1,000 CPU executions of the emitted SPIR-V matched the expected texel and VGPR results across 2D/3D, glc, a16, mixed/empty EXEC and CAS retries. Inactive lanes made zero storage-image descriptor-array accesses or atomic memory operations in these head cases.

  • Integration with #3187: Git reports one shared translator conflict. In scratch I kept this PR's guarded branch and used #3187's dynamic_storage_access for the image-array access. All 328 modules repeated the CPU/validator results. Both CLI assembler backends reported the required storage feature in 6/6 new swap/inc/dec cases after integration, versus 0/6 on this head. The standalone module validator passed either way, so it does not establish that the Vulkan consumer enables the required dynamic-storage feature. Preserve both changes when the branches are reconciled.
  • Regression: find_guarded_texel_block accepts the true target of any structured selection, and the CAS tests look for the load/exchange anywhere in the module. An initialized mixed-EXEC test that checks the returned register, image texel and zero inactive-lane memory effects would pin the behavior. Checking only the final texel cannot catch a neutral read-modify-write.
  • Please record the swap/inc/dec and EXEC rules in the translator contract in docs/design.md, with the scope of the hardware evidence above.

No GPU dispatches or game data in this review.

@Atlonsky

Copy link
Copy Markdown

Independent check on NVIDIA at 8ca3b78, including the full suite.

Environment: Windows 11 Pro 26200, Python 3.15.2, NVIDIA GeForce RTX 3080 (Ampere, 32-wide subgroup), driver 616.92 (Vulkan 1.4.351), llvm-mc 23.1.6.

  • run_gpu_tests.py --build runtime/build/vulkan --filter atomic → cs_imgatomic, cs_imgatomic3d, ps_imgatomic all PASS → 3/3 passed
  • python -I -m unittest discover -s tests\semantic -p test_image_atomics_guard.py → Ran 3 tests ... OK
  • Full suite: python -I -m unittest discover -s tests\semantic → Ran 7325 tests in 586s ... OK (expected failures=1)

The gfx1036 measurements in the description and the AMD run above cover the hardware question; this adds the 32-wide NVIDIA path plus the whole translator suite, which the targeted runs do not reach.

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Rebased onto main (2d3369a4, head 9f48a2f7) after 064b66a5/931dd42b merged IMG_ATOMICS_EXTRA:

  • Preserved main's emit_image_atomic_extra (swap/inc/dec/fmin/fmax/fcmpswap) and moved OpAccessChain into the if (active) block.
  • Guarded the remaining 9 standard 2D/3D IMG_ATOMICS (add/sub/umin/umax/smin/smax/and/or/xor) with OpSelectionMerge + OpBranchConditional so inactive lanes skip OpAccessChain / OpImageTexelPointer / OpAtomic* instead of executing neutral-value RMWs.
    Verified: test_image_atomics_guard.py (3/3) + test_image_atomics_32.py (3/3) OK with spirv-val.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 / MoltenVK GPU-suite no-regression check of head 9f48a2f769eecc0a711f319c8019b7dfb74fd2ab (hardware reports Apple M2 / Mac14,3, not M1), macOS 26.4, MoltenVK 1.4.2, subgroup range 4..32, llvm-mc 23.1.6. Clean full build on AppleClang 17.

python -I scripts/run_gpu_tests.py --build runtime/build/vulkan: 220 shaders translated, 220 pass spirv-val; 152/217 passed, 58 skipped, 7 failed (the known MoltenVK expected set) — identical per test to the PR's base 3eb4410b8de7a6d28cb81557e7bca6de864fda56 on this same box (0 status differences across the 216 common test entries). This is a no-regression data point only: no GPU fixture here exercises image_atomic_*, so I am not claiming the new active-lane guard or swap/inc/dec ops were verified on hardware. No code changes, no game data; counts only.

AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

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

Both checked-in compute fixtures, cs_imgatomic and cs_imgatomic3d, passed on this head. I also replaced the first lane-0-only image_atomic_add … glc in each fixture with image_atomic_swap … glc in temporary copies.

Target Previous value returned Value stored
2D uint texel (0,0) 100 0xfffffff0
3D uint texel (1,2,3) 257 0x10

All four shaders passed spirv-val. For each dispatch the runner checked all 64 words in each of the uint image, sint image and volume. The two variants matched after changing only the expected destination texel in each case. These readbacks check observable store and return values with valid image bindings.

AI-assisted verification.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested 9f48a2f7 merged into main 98fa503f on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 10.0.26200, Vulkan SDK 1.4.363.1, test programs from the existing runtime/build (this PR changes no C++): pass.

  • GPU: cs_imgatomic, cs_imgatomic3d (all 64 words exact) and ps_imgatomic (0 of 256 texels, 0 of 4096 render-target pixels wrong, odd columns discarded): PASS.
  • tests/semantic/test_image_atomics_guard.py and the other test_image_atomic* tests: OK.

AI-assisted (Claude Code).

@KkelebekK

Copy link
Copy Markdown

Tested 9f48a2f (merge base 2d3369a; merges cleanly with current main 3faac55) on AMD Radeon 780M (RADV PHOENIX, Mesa 26.2.6) and NVIDIA GeForce RTX 4070 Laptop (620.70.06, plain and LEONIDA_W64_EMULATE=1), CachyOS Linux 7.3.4, llvm-mc 23.1.4, test programs built from this head: pass.

  • test_image_atomics_guard.py + test_image_atomics_32.py: 6 passed, 79 subtests (pytest); unittest on tests/semantic/test_image_atomics*.py: 6 OK.
  • run_gpu_tests.py --filter atomic: cs_imgatomic, cs_imgatomic3d, ps_imgatomic pass on all three configurations (3/3).
  • Full run_gpu_tests.py: 780M 202/217 passed, 15 skipped; 4070 170/217, 47 skipped; 4070 emulation 201/217, 16 skipped. No FAIL, same as main at 98fa503.

No GPU fixture issues image_atomic_swap/inc/dec, so this shows no regression in the guarded paths but does not check the new ops on a device. Synthetic inputs only, no game data. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 9f48a2f merged with main 3faac55 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_image_atomics_guard.py, test_value_semantics.py and the rdna2spirv and shader_checks tests: Windows 8/8, Linux 8/8. Negative control with main's tools/rdna2spirv.py: test_image_atomics_guard fails (41 failures on both).

@lunarminus

Copy link
Copy Markdown

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

  • The head already contains main 561b65a8, so the merge onto d0835dd9 was a fast-forward to a8d937f1, which is one commit past 561b65a8. This PR changes no runtime/ or scripts/ file, so the test programs are a build of 561b65a8, and the per-test control is 561b65a8 without the PR, run on this machine.
  • test_image_atomics_guard.py: unittest 3/3 OK. pytest: 3 passed, 49 subtests passed. spirv-as/spirv-val were on PATH and ran 48 times each.
  • unittest discover -s tests/semantic -p 'test_image*.py': 49 tests OK.
  • Negative control: the new test against 561b65a8's tools/rdna2spirv.py fails (failures=41).
  • run_gpu_tests.py, plain and LEONIDA_W64_EMULATE=1: 238/238 shaders translated and passing spirv-val, 216/232 passed, 16 skipped, 0 failed in both modes.
    • Against 561b65a8 without the PR: 0 per-test differences over all 232 tests, in both modes.
    • Against d0835dd9 without the PR (196/210, 14 skipped): no status change among the 210 shared tests. The 22 extra tests (20 PASS, 2 SKIP) came with main after d0835dd9 and match the 561b65a8 control.
    • cs_imgatomic, cs_imgatomic3d and ps_imgatomic PASS in both modes.
  • The PR adds no GPU fixture. The existing imgatomic fixtures use add/sub/umin/umax/smin/smax/and/or/xor/cmpswap, so this run covers the newly guarded standard ops on this GPU but not image_atomic_swap/inc/dec.

AI-assisted (Claude Code).

@DavidM1911

Copy link
Copy Markdown

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

  • tests/semantic/test_image_atomics_guard.py: 3/3 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; cs_imgatomic and cs_imgatomic3d ok.
  • check.py, NVIDIA GeForce RTX 3060 Ti (driver 610.88): 164/203 pass, 35 skipped; cs_imgatomic and cs_imgatomic3d ok.

The only failures on both GPUs — cs_bvh, cs_bvh32, cs_heap_init, ps_array_resinfo — reproduce identically with main's shaders on the same binaries, so they are environmental here, not from this change.

AI-assisted. No game files.

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested a8d937f merged onto main 561b65a on Apple M4, macOS 27.2.1, MoltenVK 1.4.2, Python 3.15.2 and 3.12.16: needs changes. The translated count drops on the corpus.

  • Corpus slice (shard 0/64): this head translates 404 and 404 pass spirv-val; main translates 415 and 415 pass. All 11 shaders it loses fail with image_atomic_umax data.

    • The cause is the new width check at tools/rdna2spirv.py:12718-12720. It refuses image_atomic_umax with two data VGPRs and dmask:0x3, which is a 64-bit atomic. Main translates this form, but wrongly: as a 32-bit umax on the low dword.
    • Over the whole corpus this form occurs 954 times, in 685 shaders. I translated the 686 shaders that have a multi-VGPR image-atomic operand: main translates all 686 and all 686 pass spirv-val; this head translates 1. Under AGENTS.md that is a regression of 685 shaders.
    • 6 of the 404 .spv files this head does produce differ from main. That is expected from the guard.
  • Suggestion: keep main's behaviour for the nine standard ops in this PR, with a comment that dmask 0x3 is 64-bit. Do 64-bit image atomics as a separate item. It needs an R64_UINT storage view with Int64ImageEXT (VK_EXT_shader_image_atomic_int64), and a fallback for devices without that extension. Here is what the M4 has under MoltenVK 1.4.2:

    • STORAGE_IMAGE_ATOMIC only on R32_UINT and R32_SINT.
    • No VK_EXT_shader_image_atomic_int64 and no VK_KHR_shader_atomic_int64.
    • shaderBufferInt64Atomics and shaderImageFloat32Atomics both false.

    So the M4 would need that fallback too. The 64-bit item is worth a finding:.

  • docs/design.md:401 still says that inactive lanes "run with the operand that changes nothing". With this PR they skip the access entirely. Please update that sentence, and add the width rule to the same contract row.

    • The neutral values in IMG_ATOMICS are now unused: see the comment at tools/rdna2spirv.py:12169 and spv, _ = … at :12734. Drop them or fix the comment.
    • The test docstring still names atomic_inc_dec_cas, which the rebase replaced with main's emit_image_atomic_extra.
    • swap, inc and dec now come from main, so "add swap/inc/dec" in the title is stale.
  • The guard itself looks right:

    • OpAccessChain, OpImageTexelPointer, the atomic and the glc write-back all sit in the if (active) block. dst leaves an inactive lane's VGPR alone.
    • The access goes through dynamic_storage_access, so StorageImageArrayDynamicIndexing is still declared and the modules pass vulkan1.3 spirv-val.
    • inc and dec are main's CAS loops with the measured wrap rules: inc gives (M >= D) ? 0 : M + 1, and dec gives (M == 0 || M > D) ? D : M - 1. They are not OpAtomicIIncrement/IDecrement, which don't wrap.
  • GPU (run_gpu_tests.py --filter imgatomic, under the lock): cs_imgatomic, cs_imgatomic3d and ps_imgatomic pass 3/3 on both main and this head. Both translate 4 shaders, and all 4 pass spirv-val. No fixture issues swap, inc or dec.

  • Tests:

    • On both Pythons: test_image_atomics_guard.py 3 OK, test_image_atomics_32.py 3 OK, test_fspx_rules.py 9 OK, test_mem_crash_forms.py 1 OK.
    • compileall and git diff --check are clean.
    • Semantic suite: Ran 6153 tests, OK (skipped=1, expected failures=1); main 561b65a is Ran 6150, OK (skipped=1, expected failures=1). The extra 3 are the new tests, and there are no failures or errors on either side.
  • Mutations:

    • Main's translator with the new test kept: 41 failures.
    • Caught: the access chain moved out of the guard (36 failures), the same move in the swap/inc/dec helper (4), the width check removed (1), and the inc compare made strict (caught only through the opcode marker).
    • Survived: guarding on a constant true instead of the active mask, which was already raised at 8ca3b78 (find_guarded_texel_block accepts the true target of any selection). Also survived: dropping the glc write-back, and swapping the select arms of the inc or dec wrap. The inc/dec code is main's, and no test executes it.
    • Fix: have find_guarded_texel_block require the branch condition to be an OpLoad %bool %active. Also assert an OpStore to the data VGPR inside the guarded block exactly when glc is set.

AI-assisted.

@SweatierKey SweatierKey changed the title rdna2spirv: guard all image_atomic_* by active lane and add swap/inc/dec rdna2spirv: guard all image_atomic_* by active lane Nov 23, 2026
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @sovljanskii for catching the 685-shader dmask:0x3 corpus regression and the mutation/doc gaps! Pushed f02a7ea92:

  • Restored corpus compatibility for 2-VGPR dmask:0x3 standard ops (tools/rdna2spirv.py): kept main's low-dword 32-bit behavior for the 9 standard IMG_ATOMICS ops (with a comment noting dmask:0x3 is 64-bit and belongs in a separate Int64ImageEXT + fallback PR), while retaining the strict 2-VGPR check for image_atomic_cmpswap and main's width checks in emit_image_atomic_extra.
  • Cleaned up IMG_ATOMICS, docs/design.md:401, test docstring, and PR title: dropped the unused neutral operands from IMG_ATOMICS, updated docs/design.md to reflect if (active) guarding and the dmask:0x3 note, updated the stale atomic_inc_dec_cas reference, and removed the stale and add swap/inc/dec suffix from the PR title.
  • Tightened tests/semantic/test_image_atomics_guard.py: find_guarded_texel_block now verifies the OpBranchConditional condition is an OpLoad %bool %active, asserts OpStore to v0 inside the guarded block iff glc is set, and checks that 2-VGPR dmask:0x3 image_atomic_umax stays translated, guarded, and valid.

Verified locally (Linux, Python 3.13.5, spirv-as + spirv-val, LEONIDA_REQUIRE_SPIRV_TOOLS=1):

  • Full python3 -I -m unittest discover -s tests/semantic: 6153 tests OK (skipped=1, expected failures=1, 0 failures/errors).
  • test_image_atomics_guard.py + test_image_atomics_32.py (6/6), test_image_load_typed.py (23/23), test_docs_*.py (33/33).

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

Review

Reviewed correctness fix in PR #3559: rdna2spirv: guard all image_atomic_* by active lane and add swap/inc/dec.

Scope: tests/semantic/test_image_atomics_guard.py, tools/rdna2spirv.py
Diff: +221 / -80 across 2 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @SweatierKey - I went through rdna2spirv: guard all image_atomic_ by active lane and add swap/inc/dec* and the diff around tests/semantic/test_image_atomics_guard.py. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

@shayderrr
shayderrr merged commit f469596 into ItsNotRexed:main Nov 23, 2026
0 of 5 checks passed
evilclanker9000 added a commit to evilclanker9000/Leonida that referenced this pull request Nov 23, 2026
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.