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

Open
ESAT586 wants to merge 1 commit from upstream-pr/1915 into main
ESAT586 commented 2026-11-25 00:51:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T00:51:50Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6594575770 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T00:51:50Z. --- ## 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.
catminou4 commented 2026-11-25 03:02:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:49:13Z.

Review: COMMENTED


Merge conflict with current main (940d32f69b) — 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.)

<!-- greyfinch-sync:Marvisak/VIctory:review:6051093492 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915#pullrequestreview-6051093492). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:49:13Z. **Review:** COMMENTED --- Merge conflict with current main (940d32f69b) — `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.)
Headache3318 commented 2026-11-25 03:03:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Headache3318 on 2026-11-25T02:07:48Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150351704 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7150351704). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T02:07:48Z. --- 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 2026-11-25 03:03:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T02:17:59Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150549427 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7150549427). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T02:17:59Z. --- <!-- 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)_
ESAT586 commented 2026-11-25 11:05:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T11:05:40Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7160577411 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7160577411). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T11:05:40Z. --- 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.
upstream-sync changed title from [PR #1915] Shaders: lower 32-bit and 64-bit global atomics (cmpswap, csub, inc, dec, float, _x2) to [PR #1915] Shaders: lower 32-bit and 64-bit integer global atomics (cmpswap, csub, inc, dec, _x2) 2026-11-25 12:02:40 +00:00
ESAT586 commented 2026-11-25 12:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T11:34:11Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7161216807 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1915#issuecomment-7161216807). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T11:34:11Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1915:upstream-pr/1915'
git switch 'upstream-pr/1915'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1915'
git switch 'upstream-pr/1915'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1915'
git switch 'upstream-pr/1915'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1915'
git switch 'main'
git merge --squash 'upstream-pr/1915'
git switch 'main'
git merge --ff-only 'upstream-pr/1915'
git switch 'main'
git merge 'upstream-pr/1915'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1913
No description provided.