Shaders: drop unaligned 32-bit global atomics #3390

Open
Moon0755 wants to merge 1 commit from upstream-pr/3390 into main
Moon0755 commented 2026-11-26 21:26:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-26T21:26:33Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5517354074 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3390). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-26T21:26:33Z. --- 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
fuzeofficlal commented 2026-11-26 21:38:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:38:58Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080746119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3390#pullrequestreview-6080746119). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:38:58Z. **Review:** APPROVED --- 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.
Jackster commented 2026-11-26 22:03:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:03:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204310339 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3390#issuecomment-7204310339). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:03:10Z. --- 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.
Aniseto commented 2026-11-26 22:19:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-26T22:19:43Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204575828 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3390#issuecomment-7204575828). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-26T22:19:43Z. --- 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.
Dukestar69 commented 2026-11-27 01:29:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-27T01:29:26Z.

Review: APPROVED


LGTM — verified on Windows (RTX 5090 + WARP):

  • Guarding global 32-bit atomics with (addr & 3) == 0 cleanly drops unaligned accesses and returns zero to active GLC lanes
  • cargo test -p victory-shader --lib unaligned_global_atomics_are_dropped_and_return_zero passed on RTX 5090 and under VICTORY_WARP=1
  • cargo clippy -p victory-shader --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (191 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6082283678 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3390#pullrequestreview-6082283678). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-27T01:29:26Z. **Review:** APPROVED --- LGTM — verified on Windows (RTX 5090 + WARP): - Guarding global 32-bit atomics with `(addr & 3) == 0` cleanly drops unaligned accesses and returns zero to active GLC lanes - `cargo test -p victory-shader --lib unaligned_global_atomics_are_dropped_and_return_zero` passed on RTX 5090 and under `VICTORY_WARP=1` - `cargo clippy -p victory-shader --all-targets -- -D warnings` passed with 0 warnings - `cargo xtask policy` passed (`191 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3390:upstream-pr/3390'
git switch 'upstream-pr/3390'

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/3390'
git switch 'upstream-pr/3390'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3390'
git switch 'upstream-pr/3390'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3390'
git switch 'main'
git merge --squash 'upstream-pr/3390'
git switch 'main'
git merge --ff-only 'upstream-pr/3390'
git switch 'main'
git merge 'upstream-pr/3390'
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!3390
No description provided.