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

Open
github-bot wants to merge 1 commit from github-bot/gh-1915 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-25 00:51 UTC · original
ESAT586:shader/global-atomics-and-x2 → main · +953 −71 · 2 files · 1 commits · open

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.

**@⁠ESAT586** on GitHub · 2026-11-25 00:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/1915) `ESAT586:shader/global-atomics-and-x2` → `main` · +953 −71 · 2 files · 1 commits · open ## Summary Completes `Lower::flat_write` in [`crates/victory-shader/src/emit/lower.rs`](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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:48:41 +00:00
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 02:07 UTC · original

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.

**@⁠Headache3318** on GitHub · 2026-11-25 02:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7150351704) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:17 UTC · original · edited 2026-11-25 22:22 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7150549427) · edited 2026-11-25 22:22 UTC <!-- victory-triage v1 head=c77af8ea9536261ff784f18f05dfc7aa87bbc700 verdict=needs-rebase at=2026-11-25T22:25:30Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 11:05 UTC · original

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** on GitHub · 2026-11-25 11:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7160577411) 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.
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 11:34 UTC · original

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.

**@⁠ESAT586** on GitHub · 2026-11-25 11:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7161216807) 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.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/memory_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1915/head:github-bot/gh-1915'
git switch 'github-bot/gh-1915'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1915'
git switch 'github-bot/gh-1915'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1915'
git switch 'github-bot/gh-1915'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1915'
git switch 'main'
git merge --squash 'github-bot/gh-1915'
git switch 'main'
git merge --ff-only 'github-bot/gh-1915'
git switch 'main'
git merge 'github-bot/gh-1915'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1915
No description provided.