Skip to content

rdna2spirv: guard all image_atomic_* by active lane and add swap/inc/dec - #3559

Open
SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:fix/image-atomic-active-guard
Open

SweatierKey wants to merge 1 commit 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)

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

@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
SweatierKey force-pushed the fix/image-atomic-active-guard branch from 8ca3b78 to 9f48a2f Compare November 23, 2026 03:57
@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.

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.

6 participants