Skip to content

Shaders: scalar buffer load offsets do not wrap in 32 bits - #3373

Open
Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/smem-offset-wrap
Open

Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/smem-offset-wrap

Conversation

@Moon0755

Copy link
Copy Markdown

Scalar s_buffer_load* does not wrap (imm + soffset) or per-dword start + 4*k in 32 bits: an unsigned carry means the access passed 2^32 bytes (beyond any 32-bit NUM_RECORDS bound), so RDNA2 hardware (gfx1036, wave32 = wave64, measured with sOFF = 0xfffffffc, IMM = 8 and sOFF = 0xfffffff0, IMM = 0x20 returning 0 in zDiazG/Leonida) treats it as out of bounds and returns 0 rather than wrapping back to low dwords.

This directly resolves the 32-bit wrap question raised on #1393.

Changes

  • crates/victory-shader/src/emit/lower.rs (Lower::smem, 521,627 bytes / +1,064 vs main, 2,661 bytes below the 524,288 limit):
  • crates/victory-shader/src/emit/stride_tests.rs:
    • Added a_scalar_buffer_loads_offset_sum_does_not_wrap_in_32_bits covering s_buffer_load_dword at s5 = 0xffff_fffc, offset:0x8 (wraps to +4 without the fix) and s_buffer_load_dwordx2 at s5 = 0xffff_fffc, offset:0x0 (second dword wraps to +0 without the fix).

Verification

  • Fork CI control (test only on main): https://github.com/Moon0755/VIctory/actions/runs/45887853618 (failure — 302 passed, 1 failed solely on a_scalar_buffer_loads_offset_sum_does_not_wrap_in_32_bits, reading [8×32, 0×32, 7×32] instead of 0)
  • Fork CI fix (ae3e603): https://github.com/Moon0755/VIctory/actions/runs/45892937466 (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 ae3e603.

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

  • Detecting 32-bit unsigned wrap on imm + soffset and per-dword start + 4 * k and selecting 0 cleanly prevents wrapped scalar buffer load offsets from reading back into the start of the buffer.
  • cargo test -p victory-shader --lib emit::stride_tests::a_scalar_buffer_loads_offset_sum_does_not_wrap_in_32_bits -- --test-threads=1: passed on RTX 5090 and 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.

LGTM — guarding 32-bit unsigned carry on imm + soffset and per-dword start + 4 * k separately from built_offset's base/extra cleanly prevents wrapped s_buffer_load_dword* offsets from reading back into low dwords across CODE, raw_view, and CBV paths while leaving s_load_dword* untouched.

Verified

  • crates/victory-shader/src/emit/lower.rs size: 521,627 bytes (under the 524,288-byte limit).
  • cargo run --locked -p xtask -- policy: 191 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --all-features): clean.
  • rustfmt --edition 2024: clean on all PR-touched hunks in lower.rs and stride_tests.rs.
  • VICTORY_WARP=0 (RTX 5090): emit::stride_tests 7 passed, 2 failed (only the two pre-existing #1835 a_heap_buffer_read_* failures; a_scalar_buffer_loads_offset_sum_does_not_wrap_in_32_bits passes).
  • VICTORY_WARP=1: emit::stride_tests 9 passed, 0 failed; emit::lower 33 passed, 0 failed.

Non-blocking notes

  • Pre-existing host-address edge cases: if a built descriptor adds nonzero base/extra (base = 16, imm = 0, soffset = 0xffff_fffc), imm + soffset does not carry while base + sum still wraps in 32 bits; on NULL soffset, over stays None (lower.rs:4153) and the constant CBV path's base + 4 * k (lower.rs:4248) can still wrap/overflow on 0xffff_fffc.
  • On srv || root_uav, raw_view (lower.rs:4212) runs with active = None before guard selects 0, so a carry-zeroed title-memory load can still record a page-miss mark if the wrapped offset hits an unmapped page.
  • a_scalar_buffer_loads_offset_sum_does_not_wrap_in_32_bits checks zero outputs on a zero-initialized UAV over the raw_view path; adding an in-range store as a positive control (and note the shared Lower::smem overlap with #3351) is nice to keep in mind.

@Jackster

Copy link
Copy Markdown

AMD check at ae3e603 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 a_scalar_buffer_loads_offset_sum — 1 passed. The test loops hardware and WARP and prints no no SM 6.6 …; skipped line, so both backends ran.
  • Control on 63bcca4 with only stride_tests.rs from this head: the same test fails on the card — the wrapped offsets appear as 8 in the first 32 dwords and 7 in the last 32 where the fixture expects zeros.

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

@passerbyyhx

Copy link
Copy Markdown

I tested both scalar-load changes in one shader, with the same synthetic program on Windows D3D12 using an RTX 4090 Laptop and Microsoft WARP. The fixture pre-fills the output UAV with 0xdeadbeef, reads two distinct nonzero words from each input path, and leaves one guard word untouched.

Source Heap carry cases CODE table cases
#3351 2060d202 alone The sum carry returned the second input word. The second dword after carry returned the first input word. The tested bounds and independent alignment cases matched.
#3373 ae3e6033 alone The two carry cases returned zero as expected. The first out-of-range word returned the in-bounds physical trailer 0xabad1dea; the independent-alignment case returned the adjacent word.
Local integration 880dfa7d Both carry cases returned zero. The last valid word was preserved, the out-of-range word was zero, and the independently aligned address returned the intended word.

Both adapters gave the same rows. Removing one zero-result store left its word at 0xdeadbeef while the positive reads still succeeded. Each independent head passed one of the three combined-fixture tests; the integration passed all three, each on both adapters. Executables were built in distinct Cargo target directories and had distinct SHA-256 hashes. This is D3D12 translation evidence for the synthetic program, not a fresh RDNA2 instruction measurement.

Integration diff and portable fixture. Run cargo test --locked -p victory-shader --lib scalar_combination_tests:: -- --nocapture --test-threads=1 on a Windows machine with both adapters.

AI-assisted with Codex.

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.

5 participants