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
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.