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

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

@⁠Moon0755 on GitHub · 2026-11-26 20:45 UTC · original
Moon0755:fix/smem-offset-wrap → main · +133 −5 · 2 files · 1 commits · open

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):
    • Track unsigned 32-bit carry on imm + soffset (pred::IULT(sum, imm)) separately from the descriptor base offset and track per-dword carry on start + 4 * k (k > 0) inside a shared guard closure across all three Lower::smem paths (CODE, raw_view, and CBV), selecting 0 when either addition overflows 32 bits.
    • Note: touches Lower::smem alongside #1939 and #3351 — happy to rebase once either lands.
  • 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

**@⁠Moon0755** on GitHub · 2026-11-26 20:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3373) `Moon0755:fix/smem-offset-wrap` → `main` · +133 −5 · 2 files · 1 commits · open 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): - Track unsigned 32-bit carry on `imm + soffset` (`pred::IULT(sum, imm)`) separately from the descriptor `base` offset and track per-dword carry on `start + 4 * k` (`k > 0`) inside a shared `guard` closure across all three `Lower::smem` paths (`CODE`, `raw_view`, and `CBV`), selecting `0` when either addition overflows 32 bits. - Note: touches `Lower::smem` alongside #1939 and #3351 — happy to rebase once either lands. - **`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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:49:23 +00:00
Author
Owner

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

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

**@⁠Dukestar69** on GitHub · 2026-11-26 20:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/3373#pullrequestreview-6080331164) · **approved these changes** 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).
Author
Owner

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

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/3373#pullrequestreview-6080387088) · **approved these changes** 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.
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/3373/head:github-bot/gh-3373'
git switch 'github-bot/gh-3373'

Merge

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