Skip to content

Shaders: lower 32-bit and 64-bit integer global atomics (cmpswap, csub, inc, dec, _x2) - #1915

Open
ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/global-atomics-and-x2
Open

ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/global-atomics-and-x2

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown

Summary

Completes Lower::flat_write in crates/victory-shader/src/emit/lower.rs for the remaining 32-bit and 64-bit integer/f32 RDNA 2 global_atomic_* instructions (20 newly lowered or upgraded opcodes), across both title-memory (self.memory) and direct root-UAV global accesses.

What changed

  • 32-bit global atomics:
    • global_atomic_cmpswap (0x31): lowers directly to dx.op.atomicCompareExchange.i32 (op 79) with RDNA 2 (new, compare) vdata order (vdata[0], vdata[1]).
    • global_atomic_csub (0x34): unsigned clamping subtract CAS loop (old >= val) ? old - val : 0.
    • global_atomic_inc (0x3c) and global_atomic_dec (0x3d): unsigned wrapping CAS loops ((old >= val) ? 0 : old + 1 and (old == 0 || old > val) ? val : old - 1).
    • global_atomic_fcmpswap (0x3e): IEEE-754 ordered equality (pred::OEQ) CAS loop with RDNA 2 FTZ (-0.0 == +0.0, NaN != NaN); exits without modifying memory on mismatch.
    • global_atomic_fmin (0x3f) and global_atomic_fmax (0x40): replaces the previous !glc-only two-atomic integer workaround with a single linearizable CAS loop using ftz + max_min_flushed, enabling glc = 1 return values and exact RDNA 2 IEEE-754 NaN (non-NaN wins) and -0.0 < +0.0 semantics.
  • 64-bit integer _x2 global atomics (flags::INT64 | flags::ATOMIC64_HEAP, without falsely setting ATOMIC64_TYPED):
    • global_atomic_{swap,add,sub,smin,umin,smax,umax,and,or,xor}_x2 (0x50, 0x52..=0x5b) via dx.op.atomicBinOp.i64 (op 78).
    • global_atomic_cmpswap_x2 (0x51) via dx.op.atomicCompareExchange.i64 (op 79) with 4-VGPR vdata ([new_lo, new_hi, cmp_lo, cmp_hi]).
    • global_atomic_{inc,dec}_x2 (0x5c, 0x5d) via 64-bit atomicCompareExchange.i64 CAS loops.
  • Safety & barrier correctness:
    • CAS loops seed their initial old value via an atomic cmpxchg(0, 0) read, which is coherent under contention and returns 0 on null/OOB views so unmapped title-memory pages exit cleanly on iteration 1.
    • Separates data_dwords (1, 2, or 4) from access_bytes (4 or 8) when marking written title-memory pages dirty, so cmpswap (4 B) and cmpswap_x2 (8 B) do not over-mark adjacent 64 KiB pages.
    • Includes Fields::Flat stores/atomics in writes_uavs so s_barrier emits the UAV memory fence bit in shaders performing global writes/atomics.
    • Cleanly continues to reject global_atomic_{fcmpswap,fmin,fmax}_x2 until DXIL double is modelled.

Tests added (crates/victory-shader/src/emit/memory_tests.rs)

  • global_atomics_32bit_cmpswap_csub_inc_dec_and_float_match_rdna2: 28 deterministic 32-bit cases (glc = 1 memory + return checks for cmpswap, csub, inc, dec, fcmpswap, fmin, fmax including unsigned high bit, u32::MAX, zero limits, -0.0/+0.0, NaN, subnormal FTZ) + unmapped title-memory CAS termination check.
  • global_atomics_cas_loop_handles_contention_and_exec_mask: 32-lane wave with divergent EXEC (16 active lanes contending on the same global address via global_atomic_inc glc, 16 disabled lanes verifying VGPR preservation).
  • global_atomics_64bit_x2_match_rdna2: all 13 _x2 integer global atomics across 19 cases (carry/borrow across bit 31/32, signed vs unsigned min/max, 64-bit cmpswap high-dword mismatch, u64::MAX/zero wrap limits, glc = 0 path), plus non-title-memory root-UAV global_atomic_add_x2 and SFI0 feature bit assertions.
  • global_atomic_refuses_unsupported_f64_forms: verifies fcmpswap_x2 / fmin_x2 / fmax_x2 return ShaderError::Unsupported.

Verification

Tested on Windows 11, NVIDIA GeForce RTX 3060 Ti (SM 6.6, OPTIONS11 64-bit heap atomics), Microsoft D3D12 WARP (SM 6.6, OPTIONS11 64-bit heap atomics), and Windows SDK dxil.dll validator:

  • cargo test -p victory-shader --lib memory_tests -- --test-threads=1 --nocapture: 6/6 passed (RTX 3060 Ti + WARP + dxil.dll; 64-bit _x2 atomics verified on both adapters)
  • VICTORY_WARP=1 cargo test -p victory-shader --lib -- --test-threads=1: 245/247 passed (the 2 failures are pre-existing upstream main tests unrelated to this PR: gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out)
  • cargo clippy --workspace --all-targets -- -D warnings: 0 warnings
  • cargo clippy --workspace --all-targets --all-features -- -D warnings: 0 warnings
  • cargo xtask policy: 182 files ok

AI & Clean-Room Disclosure

Authored with AI assistance (Gemini 4 Neon — Extra High Mode). Clean-room implementation derived solely from public AMD RDNA 2 ISA and DXIL specifications and synthetic tests; no copyrighted game binaries, dumps, or assets were used.

@Headache3318

Copy link
Copy Markdown

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head fd1adb3 applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 142 passed, 0 failed, 2 ignored. All tests pass. The new global atomic tests are GPU-specific (require dxil.dll/WARP) and are skipped on Linux.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 4 pre-existing errors in test code (fail identically on main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-shader/src/emit/lower.rs and crates/victory-shader/src/emit/memory_tests.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

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

Merge conflict with current main (940d32f) — git merge origin/main fails on the PR head; a rebase is needed before this can land. Shaders: lower 32-bit and 64-bit global atomics (cmpswap, csub, inc, dec) looks like the right direction; I will re-test after the author rebases. (Note: #1911/#1919/#1925 are the same GPU-skip test cluster — whichever lands second will need to reconcile.)

@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased onto \main\ (\940d32f) at \cb6cfb6\ (preserving \mark_view_write(h, off, access_bytes, None)\ from \cd94d35\ for view-backed 32/64-bit global atomics). Verified on RTX 3060 Ti + WARP: \emit::memory_tests\ 7/7 passed (64-bit atomics ran on 2 adapters), strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

@ESAT586
ESAT586 force-pushed the shader/global-atomics-and-x2 branch from fd1adb3 to cb6cfb6 Compare November 25, 2026 11:05
@ESAT586
ESAT586 force-pushed the shader/global-atomics-and-x2 branch from cb6cfb6 to 01d1ff6 Compare November 25, 2026 11:34
@ESAT586 ESAT586 changed the title Shaders: lower 32-bit and 64-bit global atomics (cmpswap, csub, inc, dec, float, _x2) Shaders: lower 32-bit and 64-bit integer global atomics (cmpswap, csub, inc, dec, _x2) Nov 25, 2026
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Scoped to integer global atomics + the \writes_uavs\ \Fields::Flat\ fence fix at $sha: removed the float CAS-loop changes so \main's existing two-pass \global_atomic_fmin/\ max\ (!glc) behaviour is preserved untouched and \ cmpswap\ stays refused until hardware-evidenced in a separate PR. Re-verified on RTX 3060 Ti + WARP: \emit::memory_tests\ 7/7 passed (64-bit atomics ran on 2 adapters), strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants