Skip to content

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

Open
Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/smem-load-align
Open

Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/smem-load-align

Conversation

@Moon0755

Copy link
Copy Markdown

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

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants