Shaders: scalar memory loads align each address term independently #3349

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

@⁠Moon0755 on GitHub · 2026-11-26 19:34 UTC · original
Moon0755:fix/smem-load-align → main · +163 −3 · 2 files · 1 commits · open

What

s_load_dword* from title memory (Lower::memory_smem) added the 64-bit base address, the signed 21-bit immediate offset, and the unsigned SGPR soffset before passing the byte address to self.memory. On RDNA2 hardware, scalar memory loads truncate each address component to a dword boundary independently before adding them: (base & ~3) + (soffset & ~3) + (imm & ~3) + 4*i.

Because the unaligned low bits of each term were added together instead of dropped first, combinations such as base = TITLE + 1 with imm = 3 or soffset = 2 with imm = 6 read the next dword (+4 / +8) instead of +0 / +4. Each term (lo, offset, and soffset when present) is now masked with !3 before add64.

For dword-aligned inputs the result is unchanged.

How

In crates/victory-shader/src/emit/lower.rs (Lower::memory_smem), mask lo, offset, and soffset (when not NULL) with !3 before summing them in add64. For negative 21-bit immediates, (offset as u32) & !3 preserves the sign-extended rounding down to a multiple of 4 (e.g. -1 & !3 = -4), keeping offset < 0 for add64.

lower.rs grows by 329 bytes (520,563 to 520,892).

Evidence

  • Hardware oracle: zDiazG/Leonida 87d7bf7c43 reports raw compute kernel measurements on AMD Radeon RX 6800 (gfx1030, driver 32.0.21045.29003, wave32 = wave64, 155 runs): each of base, soffset, and imm is truncated to a multiple of 4 before addition (base & 3 = 1 with imm = 3 -> data@0; soffset = 8 with imm = -4 -> @4; soffset = 0xb with imm = -1 -> @4; same rule for s_load* with pointer & 3 = 1 or 3). Only the stated rule and measured cases are used; no code is taken.
  • Red, then green in CI (WARP):
    • Test only (devin/ci-smem-load-align-negative): run 45883071279 — 302 passed, 1 failed (scalar_loads_truncate_address_terms_to_dwords_independently: returned [0xA110_0001, 0xA110_0002] instead of [0xA110_0000, 0xA110_0001]).
    • With the fix (devin/ci-smem-load-align): run 45883064141 — 303 passed, 0 failed; both Clippy runs clean; policy::tests::repository_passes_policy ... ok.
  • On two hardware adapters and WARP locally: tested both 96d6ddb (control) and 0f68e4d (fix) on AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 Max-Q, and WARP: fails with [0xA110_0001, 0xA110_0002] without the fix and passes on all three with the fix.

Other pull requests

  • #1939 (triage: merge candidate) and #1336 cover independent dword truncation for s_buffer_load* (Lower::smem) and do not touch Lower::memory_smem.
  • #1056 covers scalar loads through a root view's address, not term alignment in memory_smem.
  • Merges cleanly with #3127, #3158, #3192, and #3325; minor end-of-file test conflict with #3333 in memory_tests.rs.

Not checked

  • Negative immediates, wide s_load_dwordx2/x4/... loads, and s_store* are not covered by the new test.
  • Measured oracle is PC RDNA2 (gfx1030), not console silicon directly.
  • Whether any shipped title shader triggers unaligned s_load_dword* terms (no game corpus).

Synthetic tests only; no game files.

— Devin & Neon

**@⁠Moon0755** on GitHub · 2026-11-26 19:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/3349) `Moon0755:fix/smem-load-align` → `main` · +163 −3 · 2 files · 1 commits · open ### What `s_load_dword*` from title memory (`Lower::memory_smem`) added the 64-bit base address, the signed 21-bit immediate offset, and the unsigned SGPR `soffset` before passing the byte address to `self.memory`. On RDNA2 hardware, scalar memory loads truncate each address component to a dword boundary independently before adding them: `(base & ~3) + (soffset & ~3) + (imm & ~3) + 4*i`. Because the unaligned low bits of each term were added together instead of dropped first, combinations such as `base = TITLE + 1` with `imm = 3` or `soffset = 2` with `imm = 6` read the next dword (`+4` / `+8`) instead of `+0` / `+4`. Each term (`lo`, `offset`, and `soffset` when present) is now masked with `!3` before `add64`. For dword-aligned inputs the result is unchanged. ### How In `crates/victory-shader/src/emit/lower.rs` (`Lower::memory_smem`), mask `lo`, `offset`, and `soffset` (when not `NULL`) with `!3` before summing them in `add64`. For negative 21-bit immediates, `(offset as u32) & !3` preserves the sign-extended rounding down to a multiple of 4 (e.g. `-1 & !3 = -4`), keeping `offset < 0` for `add64`. `lower.rs` grows by 329 bytes (520,563 to 520,892). ### Evidence - **Hardware oracle:** [zDiazG/Leonida `87d7bf7c43`](https://github.com/zDiazG/Leonida/commit/87d7bf7c43) reports raw compute kernel measurements on AMD Radeon RX 6800 (gfx1030, driver 32.0.21045.29003, wave32 = wave64, 155 runs): each of `base`, `soffset`, and `imm` is truncated to a multiple of 4 before addition (`base & 3 = 1` with `imm = 3 -> data@0`; `soffset = 8` with `imm = -4 -> @4`; `soffset = 0xb` with `imm = -1 -> @4`; same rule for `s_load*` with `pointer & 3 = 1` or `3`). Only the stated rule and measured cases are used; no code is taken. - **Red, then green in CI (WARP):** - Test only (`devin/ci-smem-load-align-negative`): [run 45883071279](https://github.com/Moon0755/VIctory/actions/runs/45883071279) — 302 passed, 1 failed (`scalar_loads_truncate_address_terms_to_dwords_independently`: returned `[0xA110_0001, 0xA110_0002]` instead of `[0xA110_0000, 0xA110_0001]`). - With the fix (`devin/ci-smem-load-align`): [run 45883064141](https://github.com/Moon0755/VIctory/actions/runs/45883064141) — 303 passed, 0 failed; both Clippy runs clean; `policy::tests::repository_passes_policy ... ok`. - **On two hardware adapters and WARP locally:** tested both `96d6ddb` (control) and `0f68e4d` (fix) on AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 Max-Q, and WARP: fails with `[0xA110_0001, 0xA110_0002]` without the fix and passes on all three with the fix. ### Other pull requests - #1939 (`triage: merge candidate`) and #1336 cover independent dword truncation for `s_buffer_load*` (`Lower::smem`) and do not touch `Lower::memory_smem`. - #1056 covers scalar loads through a root view's address, not term alignment in `memory_smem`. - Merges cleanly with #3127, #3158, #3192, and #3325; minor end-of-file test conflict with #3333 in `memory_tests.rs`. ### Not checked - Negative immediates, wide `s_load_dwordx2/x4/...` loads, and `s_store*` are not covered by the new test. - Measured oracle is PC RDNA2 (gfx1030), not console silicon directly. - Whether any shipped title shader triggers unaligned `s_load_dword*` terms (no game corpus). Synthetic tests only; no game files. — Devin & Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:38:00 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 19:48 UTC · original · approved these changes

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Truncating base (lo), immediate offset, and soffset independently with & !3 before summing accurately matches RDNA2 SMEM dword-aligned address formation.
  • cargo test -p victory-shader --lib emit::memory_tests:: -- --skip heap_direct_texture_reads_mark_their_heap_index --test-threads=1: 5 passed, 0 failed on RTX 5090 and 5 passed, 0 failed on VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 19:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3349#pullrequestreview-6079751694) · **approved these changes** LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Truncating base (`lo`), immediate `offset`, and `soffset` independently with `& !3` before summing accurately matches RDNA2 SMEM dword-aligned address formation. - `cargo test -p victory-shader --lib emit::memory_tests:: -- --skip heap_direct_texture_reads_mark_their_heap_index --test-threads=1`: 5 passed, 0 failed on RTX 5090 and 5 passed, 0 failed on `VICTORY_WARP=1`. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

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

Looks good to me — masking lo, the sign-extended 21-bit offset, and soffset independently with !3 before add64 matches RDNA2 SMEM effective-address calculation ((SBASE & ~3) + (sext(OFFSET) & ~3) + (zext(SOFFSET) & ~3)), including negative immediates and 32-bit carry propagation.

Verified locally on Windows 11 with head 0f68e4d6 cleanly merged onto main (63bcca43):

  • crates/victory-shader/src/emit/lower.rs is 520,892 bytes (3,396 bytes below the 524,288 limit), and the newly added lines in lower.rs:4495-4501 match rustfmt --edition 2024.
  • cargo run -p xtask -- policy (191 files ok) and cargo clippy --locked -p victory-shader --all-targets -- -D warnings (both default and --all-features) pass.
  • cargo test --locked -p victory-shader --lib emit::memory_tests (with Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll present):
    • VICTORY_WARP=0 (--skip heap_direct_texture_reads_mark_their_heap_index): 5 passed; 0 failed (1.56s, including scalar_loads_truncate_address_terms_to_dwords_independently).
    • VICTORY_WARP=1 (full emit::memory_tests): 6 passed; 0 failed (0.30s).

Minor non-blocking formatting nit: rustfmt --edition 2024 --check reports two line-wrap diffs on added lines at crates/victory-shader/src/emit/memory_tests.rs:1020 and 1061. (Also note the sibling PC-relative CODE path in Lower::smem still combines unmasked imm + soffset separately.)

**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/3349#pullrequestreview-6079912039) · **approved these changes** Looks good to me — masking `lo`, the sign-extended 21-bit `offset`, and `soffset` independently with `!3` before `add64` matches RDNA2 `SMEM` effective-address calculation (`(SBASE & ~3) + (sext(OFFSET) & ~3) + (zext(SOFFSET) & ~3)`), including negative immediates and 32-bit carry propagation. Verified locally on Windows 11 with head `0f68e4d6` cleanly merged onto `main` (`63bcca43`): - `crates/victory-shader/src/emit/lower.rs` is `520,892` bytes (`3,396` bytes below the `524,288` limit), and the newly added lines in `lower.rs:4495-4501` match `rustfmt --edition 2024`. - `cargo run -p xtask -- policy` (`191 files ok`) and `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (both default and `--all-features`) pass. - `cargo test --locked -p victory-shader --lib emit::memory_tests` (with `Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll` present): - `VICTORY_WARP=0` (`--skip heap_direct_texture_reads_mark_their_heap_index`): `5 passed; 0 failed` (`1.56s`, including `scalar_loads_truncate_address_terms_to_dwords_independently`). - `VICTORY_WARP=1` (full `emit::memory_tests`): `6 passed; 0 failed` (`0.30s`). Minor non-blocking formatting nit: `rustfmt --edition 2024 --check` reports two line-wrap diffs on added lines at `crates/victory-shader/src/emit/memory_tests.rs:1020` and `1061`. (Also note the sibling PC-relative `CODE` path in `Lower::smem` still combines unmasked `imm + soffset` separately.)
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 20:10 UTC · original

WARP verification of head 0f68e4d6d43287c4ade1765a1eb660728ad303c2 (control: main 63bcca43).

New test present at head (in emit/memory_tests.rs): scalar_loads_truncate_address_terms_to_dwords_independently.

test (own process, --exact) main + test head
scalar_loads_truncate_address_terms_to_dwords_independently FAIL (hardware assertion mismatch) pass

So the test catches a real bug on main and is fixed by the PR. victory-shader lib: head 299 passed / 4 failed (the unrelated four below); control 299 passed / 7 failed (the same four plus this PR's test and #3351's two, which I carried in the same control checkout). Note the PR does not cover negative immediates or wide loads (it says so itself); I did not extend it.

Setup (Windows 11, no physical GPU): VICTORY_WARP=1, VICTORY_D3D12_WARP=1, VICTORY_DXIL = Windows SDK 10.0.26100.0 x64\dxil.dll, toolchain 1.98.1, -j 4. Head and control are separate checkouts with separate target dirs. Control = main 63bcca43c6a00a758510398fa43096ce7e4f7df0 plus only the PR's new/changed test code (no source change).

Suites (cargo test -j 4 --locked -p victory-shader --lib, -p victory-gpu --lib): victory-gpu lib 231 passed / 0 failed / 1 ignored on head and control. victory-shader lib has the same 4 failures on head and control, all in emit::synthetic_tests and independent of this PR: a_mask_joined_with_data_at_a_uniform_loop_is_its_number, permutes_mask_high_address_bits_and_divergent_branches and permutes_read_the_lane_named_and_zero_from_lanes_left_out (no pipeline for a wave of 32, a WARP limitation), and gathers_at_a_level_match_the_hardware (lane-4 gather values differ on WARP). Re-running one of them alone in its own process fails the same way on main and on the head, so they are deterministic on WARP, not flaky. No flakes seen in the PR's tests.

Synthetic tests only; no game files, dumps, shader names or hashes. Test report only, no code changes. Claimed on #1633. AI use: Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 20:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3349#issuecomment-7202380212) WARP verification of head `0f68e4d6d43287c4ade1765a1eb660728ad303c2` (control: main `63bcca43`). **New test present at head** (in `emit/memory_tests.rs`): `scalar_loads_truncate_address_terms_to_dwords_independently`. | test (own process, `--exact`) | main + test | head | |---|---|---| | `scalar_loads_truncate_address_terms_to_dwords_independently` | FAIL (`hardware` assertion mismatch) | pass | So the test catches a real bug on main and is fixed by the PR. victory-shader lib: head 299 passed / 4 failed (the unrelated four below); control 299 passed / 7 failed (the same four plus this PR's test and #3351's two, which I carried in the same control checkout). Note the PR does not cover negative immediates or wide loads (it says so itself); I did not extend it. **Setup** (Windows 11, no physical GPU): `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, `VICTORY_DXIL` = Windows SDK 10.0.26100.0 `x64\dxil.dll`, toolchain 1.98.1, `-j 4`. Head and control are separate checkouts with separate target dirs. Control = main `63bcca43c6a00a758510398fa43096ce7e4f7df0` plus only the PR's new/changed test code (no source change). **Suites** (`cargo test -j 4 --locked -p victory-shader --lib`, `-p victory-gpu --lib`): victory-gpu lib 231 passed / 0 failed / 1 ignored on head and control. victory-shader lib has the same 4 failures on head and control, all in `emit::synthetic_tests` and independent of this PR: `a_mask_joined_with_data_at_a_uniform_loop_is_its_number`, `permutes_mask_high_address_bits_and_divergent_branches` and `permutes_read_the_lane_named_and_zero_from_lanes_left_out` (`no pipeline for a wave of 32`, a WARP limitation), and `gathers_at_a_level_match_the_hardware` (lane-4 gather values differ on WARP). Re-running one of them alone in its own process fails the same way on main and on the head, so they are deterministic on WARP, not flaky. No flakes seen in the PR's tests. Synthetic tests only; no game files, dumps, shader names or hashes. Test report only, no code changes. Claimed on #1633. AI use: Claude Code (Claude Sonnet 5.5).
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/3349/head:github-bot/gh-3349'
git switch 'github-bot/gh-3349'

Merge

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