Shaders: look up the second page of straddling title-memory writes in the page table #2484

Manually merged
github merged 1 commit from gh-pr/2484/head into gh-pr/2484/base 2026-11-26 05:07:09 +00:00
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 14:40 UTC · original
fuzeofficlal:fix/shader-memory-straddle-dirty-mark → main · +241 −37 · 2 files · 1 commits · merged 2026-11-25 21:57 UTC

Summary

Fixes two out-of-bounds / mis-indexed dirty-bitmap updates in Lower::memory (crates/victory-shader/src/emit/lower.rs) when a multi-byte title-memory store (bytes > 1, such as global_store_dwordx2 / x3 / x4) straddles a 256 MiB or 4 GiB boundary:

  1. 4 GiB boundary wraparound (lo + (bytes - 1) in 32 bits):
    Lower::memory previously computed the last byte's page delta from the low address dword alone:

    let end = self.s.m.bin(c, Bin::Add, lo, last);
    let pages = self.s.m.bin(c, Bin::Sub, end >> 16, lo >> 16);
    let next = self.s.m.bin(c, Bin::Add, e.entry, pages);
    

    A store straddling a 4 GiB boundary (for example 0x1_FFFF_FFF8 ..= 0x2_0000_0007, which memory::table and lo.wrapping_sub(start) explicitly allow within a single host copy) wrapped end to 7, yielding pages = 0xFFFF_0001 (-65535 mod 2^32) and issuing an atomicBinOp.i32 on the root dirty-bitmap UAV at byte offset ~+512 MiB past the 256.5 KiB mapped bitmap buffer.

  2. 256 MiB level-1 region boundary overflow (e.entry + 1):
    e.entry packs (block << 12) | page. When a store crossed from page 0xFFF of one 256 MiB region into page 0x000 of the next (0x1_0FFF_FFF8 ..= 0x1_1000_0007), e.entry + 1 overflowed the 12-bit page index into ((e.block + 1) << 12) | 0 instead of looking up l1 + 1 in the level-1 table (memory::table assigns blocks in descending copy-size order, so adjacent 256 MiB regions do not occupy e.block + 1):

    • At e.block == CAPACITY (512), (513 << 12) | 0 (word = 262_656 bytes) indexed past the (CAPACITY + 1) * 4096-bit bitmap.
    • For e.block < CAPACITY, marking (e.block + 1) << 12 either dirtied an unrelated region's page 0 or hit an unallocated/unowned block entry that caused memory::bind's defer_missed / defer_after check to treat the entire command list as having read unmapped memory (withholding all title-memory and marked-view copy-backs for that submission), while leaving the actual second page unmarked.

Changes

  • Split Lower::memory_page and Lower::memory_mark_index out of Lower::memory_entry and Lower::memory, plus Lower::mark_memory_bit for bitmap updates.
  • Emit the first dirty/miss bitmap update unconditionally under marks, and only when write && bytes > 1, !e.miss, and (lo >> 16) != (end_lo >> 16) (computed with 64-bit add64(lo, hi, bytes - 1, false) and 48-bit end_hi masking) look up (end_lo, end_hi) via memory_page + memory_mark_index inside a guarded block. This also avoids emitting a redundant second atomicBinOp.i32 on single-byte writes, missed writes, and non-straddling multi-byte writes.
  • Note on overlap with #2167: #2167 optimizes mark_dirty_bits with test-before-OR and coalesced loads while keeping the 32-bit end = lo + last; next = e.entry + pages calculation; whichever lands second is a straightforward merge in Lower::memory.

Verification

  • Added emit::memory_tests::straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages (crates/victory-shader/src/emit/memory_tests.rs), lowering and DXIL-validating a global_store_dwordx4 compute shader and executing it on both SM 6.6 hardware (RTX 5090) and D3D12 WARP across:
    1. a 4 GiB boundary (0x1_FFFF_FFF8 across l1 = 0x1F -> block 2 into l1 = 0x20 -> block 1),
    2. a 256 MiB boundary at e.block == 512 (CAPACITY) into an unmapped next region (0x1_0FFF_FFF8 -> marks 512 * 4096 + 0xFFF and missed region bit 0x11 without overflowing past block 512), and
    3. an aligned single-page store (0x1_FFFF_FF00).
  • cargo test -p victory-shader --lib emit::memory_tests (4/4 passed on hardware + WARP)
  • cargo clippy -p victory-shader --all-targets -- -D warnings
  • cargo xtask policy
**@⁠fuzeofficlal** on GitHub · 2026-11-25 14:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2484) `fuzeofficlal:fix/shader-memory-straddle-dirty-mark` → `main` · +241 −37 · 2 files · 1 commits · merged 2026-11-25 21:57 UTC ## Summary Fixes two out-of-bounds / mis-indexed dirty-bitmap updates in `Lower::memory` (`crates/victory-shader/src/emit/lower.rs`) when a multi-byte title-memory store (`bytes > 1`, such as `global_store_dwordx2` / `x3` / `x4`) straddles a 256 MiB or 4 GiB boundary: 1. **4 GiB boundary wraparound (`lo + (bytes - 1)` in 32 bits)**: `Lower::memory` previously computed the last byte's page delta from the low address dword alone: ```rust let end = self.s.m.bin(c, Bin::Add, lo, last); let pages = self.s.m.bin(c, Bin::Sub, end >> 16, lo >> 16); let next = self.s.m.bin(c, Bin::Add, e.entry, pages); ``` A store straddling a 4 GiB boundary (for example `0x1_FFFF_FFF8 ..= 0x2_0000_0007`, which `memory::table` and `lo.wrapping_sub(start)` explicitly allow within a single host copy) wrapped `end` to `7`, yielding `pages = 0xFFFF_0001` (`-65535` mod `2^32`) and issuing an `atomicBinOp.i32` on the root dirty-bitmap UAV at byte offset `~+512 MiB` past the 256.5 KiB mapped `bitmap` buffer. 2. **256 MiB level-1 region boundary overflow (`e.entry + 1`)**: `e.entry` packs `(block << 12) | page`. When a store crossed from page `0xFFF` of one 256 MiB region into page `0x000` of the next (`0x1_0FFF_FFF8 ..= 0x1_1000_0007`), `e.entry + 1` overflowed the 12-bit page index into `((e.block + 1) << 12) | 0` instead of looking up `l1 + 1` in the level-1 table (`memory::table` assigns blocks in descending copy-size order, so adjacent 256 MiB regions do not occupy `e.block + 1`): - At `e.block == CAPACITY` (`512`), `(513 << 12) | 0` (`word = 262_656` bytes) indexed past the `(CAPACITY + 1) * 4096`-bit bitmap. - For `e.block < CAPACITY`, marking `(e.block + 1) << 12` either dirtied an unrelated region's page 0 or hit an unallocated/unowned block entry that caused `memory::bind`'s `defer_missed` / `defer_after` check to treat the entire command list as having read unmapped memory (withholding all title-memory and marked-view copy-backs for that submission), while leaving the actual second page unmarked. ## Changes - Split `Lower::memory_page` and `Lower::memory_mark_index` out of `Lower::memory_entry` and `Lower::memory`, plus `Lower::mark_memory_bit` for bitmap updates. - Emit the first dirty/miss bitmap update unconditionally under `marks`, and only when `write && bytes > 1`, `!e.miss`, and `(lo >> 16) != (end_lo >> 16)` (computed with 64-bit `add64(lo, hi, bytes - 1, false)` and 48-bit `end_hi` masking) look up `(end_lo, end_hi)` via `memory_page` + `memory_mark_index` inside a guarded block. This also avoids emitting a redundant second `atomicBinOp.i32` on single-byte writes, missed writes, and non-straddling multi-byte writes. - Note on overlap with #2167: #2167 optimizes `mark_dirty_bits` with test-before-OR and coalesced loads while keeping the 32-bit `end = lo + last; next = e.entry + pages` calculation; whichever lands second is a straightforward merge in `Lower::memory`. ## Verification - Added `emit::memory_tests::straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages` (`crates/victory-shader/src/emit/memory_tests.rs`), lowering and DXIL-validating a `global_store_dwordx4` compute shader and executing it on both SM 6.6 hardware (RTX 5090) and D3D12 WARP across: 1. a 4 GiB boundary (`0x1_FFFF_FFF8` across `l1 = 0x1F -> block 2` into `l1 = 0x20 -> block 1`), 2. a 256 MiB boundary at `e.block == 512` (`CAPACITY`) into an unmapped next region (`0x1_0FFF_FFF8` -> marks `512 * 4096 + 0xFFF` and missed region bit `0x11` without overflowing past block 512), and 3. an aligned single-page store (`0x1_FFFF_FF00`). - `cargo test -p victory-shader --lib emit::memory_tests` (4/4 passed on hardware + WARP) - `cargo clippy -p victory-shader --all-targets -- -D warnings` - `cargo xtask policy`
Lower::memory previously computed the dirty bitmap bit for the last byte of a write by adding 32-bit wrapped page deltas to the first page's level-2 entry. Writes straddling a 4 GiB boundary wrapped the 32-bit addition to ~512 MiB out-of-bounds in the dirty bitmap UAV, and writes crossing a 256 MiB boundary overflowed the 12-bit page index into the wrong or out-of-bounds level-2 block. Guard the second bitmap update on non-missed 64 KiB page crossings and resolve the end address with 64-bit addition through the page table.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:07:08 +00:00
github manually merged commit 3c5fa89f23 into gh-pr/2484/base 2026-11-26 05:07:09 +00:00
Author
Owner

@⁠Bj0rnis on GitHub · 2026-11-25 14:45 UTC · original

Independent Windows 11 + NVIDIA GeForce RTX 5070 Ti verification of #2484 at d0c55787 (clean git merge-tree with origin/main 940d32f6, Rust 1.98.1 MSVC):

  • victory-shader tests (RTX 5070 Ti): cargo test -p victory-shader --lib --locked -- --test-threads=1 -> 245 passed, 3 failed, 3 ignored (89.04s). The new test passes; all 3 failures reproduce identically on clean main 940d32f6 on RTX 5070 Ti (emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros [#1835], and emit::synthetic_tests::gathers_at_a_level_match_the_hardware).
  • Clippy: cargo clippy -p victory-shader --all-targets --locked -- -D warnings -> 0 warnings.
  • Asset policy: cargo xtask policy -> 182 files ok.

Not tested: live game workload / shader translation cache run.

AI-assisted test run and summary.

**@⁠Bj0rnis** on GitHub · 2026-11-25 14:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2484#issuecomment-7165764390) Independent Windows 11 + NVIDIA GeForce RTX 5070 Ti verification of #2484 at `d0c55787` (clean `git merge-tree` with `origin/main` `940d32f6`, Rust 1.98.1 MSVC): - **`victory-shader` tests (RTX 5070 Ti):** `cargo test -p victory-shader --lib --locked -- --test-threads=1` -> **245 passed, 3 failed, 3 ignored** (89.04s). The new test passes; all 3 failures reproduce identically on clean `main` `940d32f6` on RTX 5070 Ti (`emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` [#1835], and `emit::synthetic_tests::gathers_at_a_level_match_the_hardware`). - **Clippy:** `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` -> **0 warnings**. - **Asset policy:** `cargo xtask policy` -> **182 files ok**. Not tested: live game workload / shader translation cache run. AI-assisted test run and summary.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 14:55 UTC · original

Independent Windows verification (software WARP only, no hardware GPU in the final runs)

  • PR head: d0c55787be9521d42e986f74c19c7f5db42ab15d; origin/main: 940d32f69b22265690a0b5a15d6ded572ba33baf (merge base = origin/main; git merge-tree merges clean)
  • Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, WARP D3D10Warp.dll 10.0.26100.9818, dxil.dll 1.9.2602.17
  • Adapter: VICTORY_D3D12_WARP=1 and VICTORY_WARP=1 (the latter is what victory-shader's test Host::new actually reads; the code path is EnumWarpAdapter, i.e. Microsoft Basic Render Driver 0x1414). The tests do not print the adapter id, so 0x1414 is from the code path, not from log output.

Results:

  • cargo test -p victory-shader --lib --locked -- --test-threads=1: 246 passed, 2 failed, 3 ignored. All 4 memory_tests pass, including straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages (new) and writes_through_views_mark_their_pages.
  • The 2 failures are synthetic_tests::gathers_at_a_level_match_the_hardware and synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out (WARP gather result / "no pipeline for a wave of 32"). Both fail identically on origin/main under WARP, so they are not caused by this PR.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.
  • cargo xtask policy: asset policy: 182 files ok.

Diff review: the second-page lookup is sound. It computes the last byte with a 64-bit add (carry into hi, masked to 16 bits), looks the page up through memory_page instead of offsetting entry, and only does so when the write crosses a 64 KiB boundary, is not a miss, and is active. A wrap past 4 GiB also reads as "crossed". I found no logic issues and no overlap with other open PRs.

Caveats: environment notes for others running these tests on WARP: the Windows SDK 10.0.19041 dxil.dll makes the SM 6.6 tests crash with STATUS_ACCESS_VIOLATION on WARP (also on main); a current dxil.dll avoids it. Also, victory-shader tests ignore VICTORY_D3D12_WARP and only honor VICTORY_WARP=1, otherwise their "hardware" pass uses the real GPU. An earlier attempt of mine set only VICTORY_D3D12_WARP, so its "hardware" pass briefly ran on the physical adapter before crashing; all results above are from the later VICTORY_WARP=1 runs.

Verified by Claude Code (Claude Sonnet 5.5), an AI assistant, on behalf of a maintainer.

**@⁠oni-swr** on GitHub · 2026-11-25 14:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2484#issuecomment-7166027012) **Independent Windows verification (software WARP only, no hardware GPU in the final runs)** - PR head: `d0c55787be9521d42e986f74c19c7f5db42ab15d`; `origin/main`: `940d32f69b22265690a0b5a15d6ded572ba33baf` (merge base = origin/main; `git merge-tree` merges clean) - Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, WARP D3D10Warp.dll 10.0.26100.9818, dxil.dll 1.9.2602.17 - Adapter: `VICTORY_D3D12_WARP=1` and `VICTORY_WARP=1` (the latter is what victory-shader's test `Host::new` actually reads; the code path is `EnumWarpAdapter`, i.e. Microsoft Basic Render Driver 0x1414). The tests do not print the adapter id, so 0x1414 is from the code path, not from log output. Results: - `cargo test -p victory-shader --lib --locked -- --test-threads=1`: 246 passed, 2 failed, 3 ignored. All 4 `memory_tests` pass, including `straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages` (new) and `writes_through_views_mark_their_pages`. - The 2 failures are `synthetic_tests::gathers_at_a_level_match_the_hardware` and `synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out` (WARP gather result / "no pipeline for a wave of 32"). Both fail identically on `origin/main` under WARP, so they are not caused by this PR. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean. - `cargo xtask policy`: `asset policy: 182 files ok`. Diff review: the second-page lookup is sound. It computes the last byte with a 64-bit add (carry into hi, masked to 16 bits), looks the page up through `memory_page` instead of offsetting `entry`, and only does so when the write crosses a 64 KiB boundary, is not a miss, and is active. A wrap past 4 GiB also reads as "crossed". I found no logic issues and no overlap with other open PRs. Caveats: environment notes for others running these tests on WARP: the Windows SDK 10.0.19041 `dxil.dll` makes the SM 6.6 tests crash with STATUS_ACCESS_VIOLATION on WARP (also on main); a current dxil.dll avoids it. Also, victory-shader tests ignore `VICTORY_D3D12_WARP` and only honor `VICTORY_WARP=1`, otherwise their "hardware" pass uses the real GPU. An earlier attempt of mine set only `VICTORY_D3D12_WARP`, so its "hardware" pass briefly ran on the physical adapter before crashing; all results above are from the later `VICTORY_WARP=1` runs. Verified by Claude Code (Claude Sonnet 5.5), an AI assistant, on behalf of a maintainer.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:12 UTC · original · edited 2026-11-25 16:44 UTC

Automated triage for the maintainers.

Merge candidate.

In Lower::memory (crates/victory-shader/src/emit/lower.rs), the second dirty bit of a multi-byte write is no longer e.entry + (pages crossed) computed from the low dword. Main has two real bugs here:

  • A store that crosses 4 GiB wraps lo + bytes - 1, giving a huge entry and an out-of-range atomic on the root UAV bitmap.
  • A store that crosses a 256 MiB region turns entry + 1 into (block + 1) << 12, which is the wrong region's block.

The PR computes the last byte with add64 and looks it up again through the new memory_page and memory_mark_index helpers. It only does so inside a guarded block when the write actually crosses a 64 KiB page, is not a miss, and is active. The marking convention is unchanged (a page entry, or the region's level-1 bit when the region has no block). Values from the second lookup are used only inside the guard.

The new test straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages checks the exact bits, including the capacity-block edge. It overlaps textually with #2167.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 16:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2484#issuecomment-7167689580) · edited 2026-11-25 16:44 UTC <!-- victory-triage v1 head=3c5fa89f230d1c8760330e532342457c63b5b57f verdict=merge-candidate at=2026-11-25T16:42:59Z --> Automated triage for the maintainers. **Merge candidate.** In `Lower::memory` (`crates/victory-shader/src/emit/lower.rs`), the second dirty bit of a multi-byte write is no longer `e.entry + (pages crossed)` computed from the low dword. Main has two real bugs here: - A store that crosses 4 GiB wraps `lo + bytes - 1`, giving a huge entry and an out-of-range atomic on the root UAV bitmap. - A store that crosses a 256 MiB region turns `entry + 1` into `(block + 1) << 12`, which is the wrong region's block. The PR computes the last byte with `add64` and looks it up again through the new `memory_page` and `memory_mark_index` helpers. It only does so inside a guarded block when the write actually crosses a 64 KiB page, is not a miss, and is active. The marking convention is unchanged (a page entry, or the region's level-1 bit when the region has no block). Values from the second lookup are used only inside the guard. The new test `straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages` checks the exact bits, including the capacity-block edge. It overlaps textually with #2167. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
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!2484
No description provided.