Skip to content

rdna2spirv: global_atomic_inc/dec and buffer_atomic_inc/dec - #3097

Closed
LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-inc-dec
Closed

LouisRiverstone wants to merge 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:atomic-inc-dec

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

global_atomic_inc/dec and buffer_atomic_inc/dec raised Unsupported. RDNA2 ISA, unsigned:

  • inc: M = (M >= D) ? 0 : M + 1
  • dec: M = (M == 0 || M > D) ? D : M - 1

With glc, the old M comes back.

Implementation: no SPIR-V atomic does this, so each is an OpAtomicCompareExchange loop.

Tests: tests/semantic/test_atomic_inc_dec.py:

  • both ops on a 10×4 value grid, including D = 0, D = M, M > D and the 0/~0 edges
  • no glc (vdst kept)
  • buffer values and an out-of-range element
  • 32 lanes on one dword: the interpreter runs them in lockstep, so all 32 read the same value, all but one exchange fail, and they retry. The result must equal 32 inc/dec applied one after another (D = 100, a wrap at D = 7, D = 0).
  • 20 seeded cases
  • the _x2 and slc forms are still rejected

Without the change the file fails (120 errors); with it, 7/7 pass. test_global_atomics.py's rejected list now uses global_atomic_inc_x2. python -I -m unittest discover -s tests/semantic: OK.

docs/design.md: new row after "Global atomics".

Tested on CPU only; no GPU run. No game files. Claimed on #2891 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

All four raised Unsupported. inc stores (M >= D) ? 0 : M + 1 and dec
(M == 0 || M > D) ? D : M - 1 (unsigned, RDNA2 ISA); no SPIR-V atomic does
that, so they are an OpAtomicCompareExchange loop (atomic_cas_loop, the
loop global_atomic_float uses), exact under contention. Operands, glc and
inactive lanes as the other global atomics; the buffer forms use
buffer_atomic's address and bounds.

tests/semantic/test_atomic_inc_dec.py covers value grids, no glc, buffer
bounds, 32 lanes contending on one dword (equal to 32 sequential updates)
and seeded values; test_global_atomics.py's rejected list now uses
global_atomic_inc_x2. docs/design.md row added.
@CharlyRien

Copy link
Copy Markdown

Tested 1014e55 (merged on current main) on RTX 3090, driver 616.56, Windows 11 23H2: pass. With this translator change, run_gpu_tests.py gives 143/189 passed, 46 skipped (wave64 on a 32-wide GPU), the same as main on this GPU. ctest matches main here (3 known failures on this machine: d3d12_shader_cache, see #2167; and d3d12_vulkan_device / d3d12_typeless_rtv, see #2498).

Overlap: #2448 (open, another author) implements the same instructions, 32-bit global_atomic_{inc,dec} and buffer_atomic_{inc,dec}. The two branches conflict in tools/rdna2spirv.py, docs/design.md and the same new tests/semantic/test_atomic_inc_dec.py (checked with git merge-tree). This PR cites a claim in #2891, while #2448 doesn't name one. Worth settling which goes in before both get reviewed.

AI-assisted test run.

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

Reviewed and verified locally (Windows 11, Python 3.14, SPIRV-Tools / Vulkan 1.3):

Audit & Verification Details

  1. Compilation & Unit Tests:
    • python -m compileall -q make_game.py tools tests passes cleanly.
    • python -I tests/semantic/test_atomic_inc_dec.py -v: 7/7 test suites pass (including 32-lane contention, boundary checks, buffer out-of-bounds glc=0, seeded values, rejected forms).
    • python -I tests/semantic/test_global_atomics.py -v: 12/12 pass with no regression.
  2. SPIR-V Validation:
    • Assembled generated SPIR-V for both global_atomic_inc and �uffer_atomic_dec via spirv-as --target-env vulkan1.3 and validated via spirv-val --target-env vulkan1.3. Both pass with 0 errors.
    • Loop structure (OpLoopMerge, OpBranchConditional, OpAtomicCompareExchange with Device scope %uint_1 on PhysicalStorageBuffer) is strictly compliant.
  3. ISA Semantics:
    • RDNA2 ISA formulas for unsigned inc ((M >= D) ? 0 : M + 1) and dec ((M == 0 || M > D) ? D : M - 1) are faithfully implemented via OpUGreaterThanEqual / OpIEqual + OpUGreaterThan + OpLogicalOr.
    • Inactive lane and buffer out-of-bounds preservation and glc return semantics strictly observed.
    • docs/design.md row updated cleanly without game assets.

Excellent work. Approving!

@TheMrBeez

Copy link
Copy Markdown

Tested 1014e55 (base 16282101): the formulas on native gfx1036, and the CAS loops under real contention on two GPUs. Pass. AI-assisted run, synthetic shaders only, no game files.

1. The formulas on hardware. The PR takes inc/dec from the ISA guide. I ran the native instructions on an AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02) through tools/hw_oracle: buffer_atomic_inc/dec (offen, RAW V#) and global_atomic_inc/dec, glc, one lane, with M and D each from {0, 1, 2, 7, 1000, 0x7fffffff, 0x80000000, 0xfffffffe, 0xffffffff}. 648/648 (81 pairs × 2 ops × 2 forms × wave32/wave64) match inc: M >= D ? 0 : M + 1 and dec: M == 0 || M > D ? D : M - 1 for the memory, and return the old M.

2. The translated CAS loops under contention. A hand-written compute shader (not part of this PR), translated with this head and run through vk_compute_test: 64 groups × 64 threads = 4096 threads on four dwords (initially 0xEEEEEEEE), glc on all:

Op D final memory (= 4096 sequential ops) 4096 returned values
buffer_atomic_inc 1000 0x5b = the sequential old values (as a multiset)
buffer_atomic_dec 777 0x23c =
global_atomic_inc 50 0xf =
global_atomic_dec 0x12345 0x11346 =

The same on the NVIDIA GeForce RTX 5080 (driver 621.01) and the gfx1036 iGPU. spirv-val --target-env vulkan1.2 OK. With every op the same, the final value doesn't depend on order, and each returned value appearing exactly once means no update was lost or applied twice.

Controls: main at the base refuses the shader (buffer_atomic_inc Unsupported). With inc's OpUGreaterThanEqual changed to OpUGreaterThan (a local edit, since reverted), both inc rows fail on the RTX 5080 (A = 0x57, C = 0x27).

Also: tests/semantic/test_atomic_inc_dec.py and test_global_atomics.py OK; scripts/run_gpu_tests.py with this translator: RTX 5080 143/189 (46 skipped, wave64), iGPU 174/189 (15 skipped), no failures. Windows 11 Pro 10.0.26200, Vulkan SDK 1.4.363.0, llvm-mc and AMD clang from rocm-sdk-core 10.0.0. Only wave32 was run for the Vulkan part (the translator gives the shader subgroup size 32).

I didn't look at the overlap with #2448 that @CharlyRien pointed out.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @CharlyRien for spotting the overlap. #2448 (FaKL-Code) implements the same four ops the same way (a CAS loop with the same inc/dec formulas), was opened first (17:05) and was claimed on #2300, so I missed it when I searched before claiming. Closing this in favour of #2448.

@TheMrBeez's hardware results above check the formulas on gfx1036 (648/648) and the CAS loops under 4096-thread contention on an RTX 5080 and gfx1036. They apply to #2448 as well, since both PRs emit the same formulas and loop.

My #3193 (global/buffer_atomic_csub) was stacked on this branch. I'll rebase it onto #2448's atomic_inc_dec_cas once #2448 merges.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 1014e55c7ebfa10ff17a666bbe2cb59df46aae2b 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, no regression. RDNA1/RADV data point complementary to the NVIDIA result in the comments.

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --build runtime/build/linux: 159/189 passed, 28 skipped, 2 failed (194 shaders translated, 194 pass spirv-val), identical to clean base 16282101 (159/189, 28 skipped, 2 failed). The same result on 1f:00.0.
  • The two failures (cs_signed_zero_nan, cs_w32_salu64) are pre-existing with identical words on base. This PR adds semantic tests only, so no new GPU fixture was available to run.
  • Card ca:00.0 gave 158/189 on two runs, each with a different extra single-test pixel mismatch (ps_fmac_f16_mods then ps_scc_logic64, 16–32 of 4096 pixels). Both tests passed 3/3 in isolation on that card; the card is known intermittent here, so this is not attributed to the PR.
  • Command: pytest tests/semantic/test_atomic_inc_dec.py tests/semantic/test_global_atomics.py -q: 19 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.

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.

5 participants