Shaders: drop unaligned 32-bit global atomics #3390

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

@⁠Moon0755 on GitHub · 2026-11-26 21:26 UTC · original
Moon0755:fix/global-atomic-align → main · +157 −1 · 2 files · 1 commits · open

On RDNA2 (gfx1036, measured natively in wave32 and wave64 in Leonida docs/design.md), 32-bit global_atomic* enforces natural dword alignment ((addr & 3) == 0), matching buffer_atomic* (#3389). An unaligned atomic leaves memory untouched, returns 0 to active glc lanes, and preserves vdst/vdata on inactive lanes. Without a guard, D3D12/WARP executes atomicBinOp.i32 at the dword-truncated address.

Changes

  • crates/victory-shader/src/emit/lower.rs (Lower::flat_write, 521,458 bytes / +895 vs main, 2,830 bytes below the 524,288 limit):
    • When lowering atomics, check (lo & 3) == 0 for title memory and (off & 3) == 0 (with root_view_offset applied) for root views before begin_guarded(access_guard) and self.memory (so dropped accesses do not dirty-mark pages).
    • Return 0 via end_guarded on active unaligned glc lanes while keeping inactive lanes untouched via self.write(inst, Reg::V(reg), r, guard).
    • Note: overlaps Lower::flat_write / memory_tests.rs with #1915 (needs rebase); happy to rebase and extend _x2 to (addr & 7) == 0 once #1915 lands.
  • crates/victory-shader/src/emit/memory_tests.rs:
    • Added unaligned_global_atomics_are_dropped_and_return_zero.

Verification

— Devin & Neon

**@⁠Moon0755** on GitHub · 2026-11-26 21:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3390) `Moon0755:fix/global-atomic-align` → `main` · +157 −1 · 2 files · 1 commits · open On RDNA2 (`gfx1036`, measured natively in wave32 and wave64 in Leonida `docs/design.md`), 32-bit `global_atomic*` enforces natural dword alignment (`(addr & 3) == 0`), matching `buffer_atomic*` (#3389). An unaligned atomic leaves memory untouched, returns `0` to active `glc` lanes, and preserves `vdst`/`vdata` on inactive lanes. Without a guard, D3D12/WARP executes `atomicBinOp.i32` at the dword-truncated address. ### Changes - **`crates/victory-shader/src/emit/lower.rs`** (`Lower::flat_write`, `521,458` bytes / `+895` vs `main`, `2,830` bytes below the `524,288` limit): - When lowering atomics, check `(lo & 3) == 0` for title memory and `(off & 3) == 0` (with `root_view_offset` applied) for root views before `begin_guarded(access_guard)` and `self.memory` (so dropped accesses do not dirty-mark pages). - Return `0` via `end_guarded` on active unaligned `glc` lanes while keeping inactive lanes untouched via `self.write(inst, Reg::V(reg), r, guard)`. - Note: overlaps `Lower::flat_write` / `memory_tests.rs` with #1915 (`needs rebase`); happy to rebase and extend `_x2` to `(addr & 7) == 0` once #1915 lands. - **`crates/victory-shader/src/emit/memory_tests.rs`**: - Added `unaligned_global_atomics_are_dropped_and_return_zero`. ### Verification - **Fork CI control** (test only on `main`): https://github.com/Moon0755/VIctory/actions/runs/45895433559 (`failure` — 302 passed, 1 failed solely on `unaligned_global_atomics_are_dropped_and_return_zero`, writing `[0, 14, ...]` and returning `7`) - **Fork CI fix** (`fbd2a99`): https://github.com/Moon0755/VIctory/actions/runs/45897735457 (`success` — 303 passed / 0 failed, Clippy + diagnostics Clippy clean, `policy::tests::repository_passes_policy ... ok`) - **Local 3-host verification**: `WARP`, `AMD Radeon(TM) Graphics`, and `NVIDIA GeForce RTX 2060 with Max-Q Design` — all fail without the fix and pass on `fbd2a99`. — Devin & Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:06:20 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 21:38 UTC · original · approved these changes

LGTM — verified against origin/main (63bcca43) on Windows x64 (RTX 5090 + dxil.dll and WARP).

  • Guarding global_atomic_* on (lo & 3) == 0 / (off & 3) == 0 combined with the existing EXEC guard (crates/victory-shader/src/emit/lower.rs:1012-1035) cleanly skips self.memory lookup/dirty marking, atomicBinOp.i32, and mark_view_write on misaligned lanes, returning 0 on active glc = 1 lanes while preserving inactive EXEC lanes' prior VGPRs.
  • Covers all supported 32-bit global_atomic_* forms (swap/add/sub/smin/umin/smax/umax/and/or/xor, plus fmin/fmax when !glc); 64-bit _x2 and cmpswap remain rejected earlier.
  • Minor non-blocking test note (P3): In unaligned_global_atomics_are_dropped_and_return_zero (crates/victory-shader/src/emit/memory_tests.rs:372), pre-seeding v6 with a nonzero sentinel before the misaligned call would distinguish returning zero from leaving the zero-initialized register untouched.
  • Checks:
    • cargo test --locked -p victory-shader --lib -- --test-threads=1 emit::memory_tests:: emit::lower::: 38 passed, 0 failed under VICTORY_WARP=0 (skipping heap_direct_texture_reads_mark_their_heap_index); 39 passed, 0 failed under VICTORY_WARP=1 (including unaligned_global_atomics_are_dropped_and_return_zero).
    • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean.
    • cargo xtask policy: 191 files ok (lower.rs = 521,458 B); rustfmt --edition 2024: clean on changed hunks.

Synthetic tests only; no game data used.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 21:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/3390#pullrequestreview-6080746119) · **approved these changes** LGTM — verified against `origin/main` (`63bcca43`) on Windows x64 (RTX 5090 + `dxil.dll` and WARP). - Guarding `global_atomic_*` on `(lo & 3) == 0` / `(off & 3) == 0` combined with the existing EXEC guard (`crates/victory-shader/src/emit/lower.rs:1012-1035`) cleanly skips `self.memory` lookup/dirty marking, `atomicBinOp.i32`, and `mark_view_write` on misaligned lanes, returning `0` on active `glc = 1` lanes while preserving inactive EXEC lanes' prior VGPRs. - Covers all supported 32-bit `global_atomic_*` forms (`swap`/`add`/`sub`/`smin`/`umin`/`smax`/`umax`/`and`/`or`/`xor`, plus `fmin`/`fmax` when `!glc`); 64-bit `_x2` and `cmpswap` remain rejected earlier. - **Minor non-blocking test note (P3)**: In `unaligned_global_atomics_are_dropped_and_return_zero` (`crates/victory-shader/src/emit/memory_tests.rs:372`), pre-seeding `v6` with a nonzero sentinel before the misaligned call would distinguish returning zero from leaving the zero-initialized register untouched. - **Checks**: - `cargo test --locked -p victory-shader --lib -- --test-threads=1 emit::memory_tests:: emit::lower::`: 38 passed, 0 failed under `VICTORY_WARP=0` (skipping `heap_direct_texture_reads_mark_their_heap_index`); 39 passed, 0 failed under `VICTORY_WARP=1` (including `unaligned_global_atomics_are_dropped_and_return_zero`). - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default and `--all-features`): clean. - `cargo xtask policy`: 191 files ok (`lower.rs` = 521,458 B); `rustfmt --edition 2024`: clean on changed hunks. Synthetic tests only; no game data used.
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 22:03 UTC · original

AMD check at fbd2a99 with a red baseline on 63bcca4, on an RX 7900 XTX (RDNA3, driver 32.0.32029.2010, Windows 11):

  • Head: cargo test -p victory-shader --lib unaligned_global_atomics — 1 passed, with no no SM 6.6 …; skipped line, so both hardware and WARP ran.
  • Control on 63bcca4 with only memory_tests.rs from this head: the same test fails on the card — the misaligned global_atomic_add is applied (14 where the fixture expects 7), so the guard is doing its job.

Synthetic shaders only, no game files. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 22:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3390#issuecomment-7204310339) AMD check at `fbd2a99` with a red baseline on `63bcca4`, on an RX 7900 XTX (RDNA3, driver 32.0.32029.2010, Windows 11): - Head: `cargo test -p victory-shader --lib unaligned_global_atomics` — 1 passed, with no `no SM 6.6 …; skipped` line, so both hardware and WARP ran. - Control on `63bcca4` with only `memory_tests.rs` from this head: the same test fails on the card — the misaligned `global_atomic_add` is applied (`14` where the fixture expects `7`), so the guard is doing its job. Synthetic shaders only, no game files. AI-assisted.
Author
Owner

@⁠Aniseto on GitHub · 2026-11-26 22:19 UTC · original

Ran the unaligned-global-atomic test at fbd2a99 on an NVIDIA Blackwell desktop part:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build.

cargo test -p victory-shader --lib unaligned_global_atomics -- --test-threads=1
1 passed; 0 failed; 0 ignored; 0 measured; 305 filtered out      (exit 0)

The one matching test is emit::memory_tests::unaligned_global_atomics_are_dropped_and_return_zero; it passes here. It is a translation/reference test and does not issue a real unaligned D3D12 atomic on the adapter. The thread has an AMD check at this head, so this adds a Windows MSVC run on a 32-lane NVIDIA card.

Not tested: real unaligned global_atomic* dispatches on hardware, 64-bit _x2 atomics, a paired smoke run, the full workspace suite, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

**@⁠Aniseto** on GitHub · 2026-11-26 22:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3390#issuecomment-7204575828) Ran the unaligned-global-atomic test at `fbd2a99` on an NVIDIA Blackwell desktop part: Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build. ``` cargo test -p victory-shader --lib unaligned_global_atomics -- --test-threads=1 1 passed; 0 failed; 0 ignored; 0 measured; 305 filtered out (exit 0) ``` The one matching test is `emit::memory_tests::unaligned_global_atomics_are_dropped_and_return_zero`; it passes here. It is a translation/reference test and does not issue a real unaligned D3D12 atomic on the adapter. The thread has an AMD check at this head, so this adds a Windows MSVC run on a 32-lane NVIDIA card. Not tested: real unaligned `global_atomic*` dispatches on hardware, 64-bit `_x2` atomics, a paired smoke run, the full workspace suite, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash Free** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3390'
git switch 'github-bot/gh-3390'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3390'
git switch 'github-bot/gh-3390'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3390'
git switch 'main'
git merge --squash 'github-bot/gh-3390'
git switch 'main'
git merge --ff-only 'github-bot/gh-3390'
git switch 'main'
git merge 'github-bot/gh-3390'
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!3390
No description provided.