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

Open
Moon0755 wants to merge 1 commit from upstream-pr/3373 into main
Moon0755 commented 2026-11-26 20:45:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-26T20:45:45Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5516917319 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3373). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-26T20:45:45Z. --- 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
Dukestar69 commented 2026-11-26 20:47:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T20:47:13Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6080331164 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3373#pullrequestreview-6080331164). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T20:47:13Z. **Review:** APPROVED --- 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 commented 2026-11-26 20:54:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:54:53Z.

Review: APPROVED


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.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080387088 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3373#pullrequestreview-6080387088). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T20:54:53Z. **Review:** APPROVED --- 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 commented 2026-11-26 21:33:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T21:33:15Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7203836747 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3373#issuecomment-7203836747). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T21:33:15Z. --- 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 commented 2026-11-26 21:34:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T21:34:53Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7203862783 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3373#issuecomment-7203862783). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T21:34:53Z. --- 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](https://github.com/passerbyyhx/VIctory/commit/3fae64e6f916de9915aa11d4f1318357a008ec92). 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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3373:upstream-pr/3373'
git switch 'upstream-pr/3373'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3373'
git switch 'upstream-pr/3373'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3373'
git switch 'upstream-pr/3373'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3373'
git switch 'main'
git merge --squash 'upstream-pr/3373'
git switch 'main'
git merge --ff-only 'upstream-pr/3373'
git switch 'main'
git merge 'upstream-pr/3373'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3373
No description provided.