GPU: the parallel compare after a submission compares a big range from its first byte (its first cut wrapped below the copy's start) #1407

Closed
github-bot wants to merge 1 commit from github-bot/gh-1407 into main AGit
Owner

@⁠PiloteCode on GitHub · 2026-11-26 23:40 UTC · original
PiloteCode:pr/compare-cut → main · +38 −1 · 1 files · 1 commits · closed without merging

Based on a42b627e (main). One commit, one line and a test, in shadow.rs.

The bug was found by a review of stellarr-studio/kelvaroq (its patch 0009), which named it. This PR is that one-line fix, with a test of ours.

What was wrong

After a submission, Shadow::copy_back_with finds the 64 KiB pieces of a declared range that the shaders changed (Shadow::differing), and copies only those back into title memory. A range of 32 MiB or more is compared on several threads, 16 MiB or more each. Each slice is cut on a piece of title memory:

let at = from + (to - from) / threads * i;
(at - (start + at) % PIECE).max(from)

For the first cut, at is the range's start. When the copy's title buffer does not start on a 64 KiB boundary and the range starts within its first piece, that piece begins before the copy: (start + at) % PIECE is larger than at, and the subtraction wraps. max(from) keeps the huge value, so the first thread gets an empty slice:

  • in a release build, the first slice (a half to an eighth of the range, 16 MiB at least) is never compared, and what the shaders wrote there never reaches title memory;
  • with overflow checks (a debug build, the tests) it panics.

The other cuts lie 16 MiB or more into the range, past any such piece, and were right.

What changes

The cut saturates: at.saturating_sub((start + at) % PIECE).max(from). The first slice starts at the range's start; every other cut is as before.

Tested

On the software adapter (the test needs a buffer for its copy, nothing else of the device):

  • New test shadow::card_merge_tests::a_big_compare_finds_changes_in_its_first_slice: a 48 MiB copy whose title address lies 32 KiB into a piece, with changes at its first byte, in its first two pieces, at 3, 20 and 40 MiB and at its last byte, compared from its first byte. It checks that every change is found, that no unchanged piece is, and that each piece ends on a piece of title memory or at the range's end.
    • On main without the fix: it panics with "attempt to subtract with overflow" at the cut (debug). In a release build (cargo test --release) it finds only the three pieces past the first slice; the changes at 0, 100, 0xFFFF, 0x10005 and 3 MiB are missed.
    • With the fix: it passes, in both builds.
  • cargo test -p victory-gpu --features diagnostics -- --test-threads=1 --skip stacks_that_mean_nothing_are_walked_without_a_fault with VICTORY_D3D12_WARP=1: 247 passed, 1 failed, 7 ignored. The failure is pipeline::make::tests::pending_pipeline_is_recorded_for_the_next_launch, which fails the same way on main (246 passed, 1 failed). The skipped test ends the test process with an access violation on this machine, on main too.
  • cargo test -p victory-hle -- --test-threads=1: 103 passed (and 1 in its second test binary).
  • cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features: clean.
  • cargo build --release -p victory and cargo build --profile diag -p victory --features diagnostics pass.
  • cargo xtask policy fails on main itself (host.rs 533,931 bytes and emit/lower.rs 535,588, over the 524,288-byte limit). This branch adds 1,939 bytes to shadow.rs (234,708), which stays under it.

Overlaps with open pull requests

  • #1178 carries the same change (and return from for the first cut) among its 9,959 added lines (lazy write-back, ReBAR and AVX2 paths behind switches). Its description does not mention it.
  • No other open PR found that changes this cut (searched: the PRs about the copy-back, its scan, its threads and the compare).

Not tested

  • A title run of this branch. How often a big range starts inside the first piece of a copy whose buffer is not 64 KiB aligned was not counted.

Where this also lives

This fix is also in PiloteBox (https://github.com/PiloteCode/PiloteBox), a tree kept up to date for NVIDIA cards.

**@⁠PiloteCode** on GitHub · 2026-11-26 23:40 UTC · [original](https://github.com/fxceless/victory-fork/pull/1407) `PiloteCode:pr/compare-cut` → `main` · +38 −1 · 1 files · 1 commits · closed without merging Based on `a42b627e` (main). One commit, one line and a test, in `shadow.rs`. The bug was found by a review of stellarr-studio/kelvaroq (its patch 0009), which named it. This PR is that one-line fix, with a test of ours. ## What was wrong After a submission, `Shadow::copy_back_with` finds the 64 KiB pieces of a declared range that the shaders changed (`Shadow::differing`), and copies only those back into title memory. A range of 32 MiB or more is compared on several threads, 16 MiB or more each. Each slice is cut on a piece of title memory: ```rust let at = from + (to - from) / threads * i; (at - (start + at) % PIECE).max(from) ``` For the first cut, `at` is the range's start. When the copy's title buffer does not start on a 64 KiB boundary and the range starts within its first piece, that piece begins before the copy: `(start + at) % PIECE` is larger than `at`, and the subtraction wraps. `max(from)` keeps the huge value, so the first thread gets an empty slice: - in a release build, the first slice (a half to an eighth of the range, 16 MiB at least) is never compared, and what the shaders wrote there never reaches title memory; - with overflow checks (a debug build, the tests) it panics. The other cuts lie 16 MiB or more into the range, past any such piece, and were right. ## What changes The cut saturates: `at.saturating_sub((start + at) % PIECE).max(from)`. The first slice starts at the range's start; every other cut is as before. ## Tested On the software adapter (the test needs a buffer for its copy, nothing else of the device): - New test `shadow::card_merge_tests::a_big_compare_finds_changes_in_its_first_slice`: a 48 MiB copy whose title address lies 32 KiB into a piece, with changes at its first byte, in its first two pieces, at 3, 20 and 40 MiB and at its last byte, compared from its first byte. It checks that every change is found, that no unchanged piece is, and that each piece ends on a piece of title memory or at the range's end. - On `main` without the fix: it panics with "attempt to subtract with overflow" at the cut (debug). In a release build (`cargo test --release`) it finds only the three pieces past the first slice; the changes at 0, 100, 0xFFFF, 0x10005 and 3 MiB are missed. - With the fix: it passes, in both builds. - `cargo test -p victory-gpu --features diagnostics -- --test-threads=1 --skip stacks_that_mean_nothing_are_walked_without_a_fault` with `VICTORY_D3D12_WARP=1`: 247 passed, 1 failed, 7 ignored. The failure is `pipeline::make::tests::pending_pipeline_is_recorded_for_the_next_launch`, which fails the same way on `main` (246 passed, 1 failed). The skipped test ends the test process with an access violation on this machine, on `main` too. - `cargo test -p victory-hle -- --test-threads=1`: 103 passed (and 1 in its second test binary). - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and the same with `--all-features`: clean. - `cargo build --release -p victory` and `cargo build --profile diag -p victory --features diagnostics` pass. - `cargo xtask policy` fails on `main` itself (`host.rs` 533,931 bytes and `emit/lower.rs` 535,588, over the 524,288-byte limit). This branch adds 1,939 bytes to `shadow.rs` (234,708), which stays under it. ## Overlaps with open pull requests - **#1178** carries the same change (and `return from` for the first cut) among its 9,959 added lines (lazy write-back, ReBAR and AVX2 paths behind switches). Its description does not mention it. - No other open PR found that changes this cut (searched: the PRs about the copy-back, its scan, its threads and the compare). ## Not tested - A title run of this branch. How often a big range starts inside the first piece of a copy whose buffer is not 64 KiB aligned was not counted. ## Where this also lives This fix is also in PiloteBox (https://github.com/PiloteCode/PiloteBox), a tree kept up to date for NVIDIA cards.
Shadow::differing compares a range of 32 MiB or more on several threads,
each slice cut on a 64 KiB piece of title memory:
`(at - (start + at) % PIECE).max(from)`. For the first cut `at` is the
range's start. In a copy whose title buffer does not start on a piece,
compared from near its start, the piece that holds `at` begins before the
copy: the subtraction wrapped, `max(from)` kept the huge value, and the
first thread got an empty slice. In a release build the first slice (up
to half of the range) was never compared, so what shaders wrote there
never came back to title memory; with overflow checks it panicked.

The cut saturates (`at.saturating_sub(..).max(from)`), so the first slice
starts at the range's start. Found by a review of stellarr-studio/kelvaroq
(its patch 0009).

Test: a 48 MiB copy 32 KiB into a piece, changes in the first slice and
the others, compared from its first byte. Without the fix it panics with
overflow checks, and in a release build finds none of the changes in the
first 16 MiB; with it every change is found, and no unchanged piece.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:03:41 +00:00
Author
Owner

@⁠UwUCammie on GitHub · 2026-11-27 00:03 UTC · original

I backported this to the AMD branch in #35. The Windows MSVC regression passed under Wine 11.22 on the RX 7900 XT host fixture with RADV 26.3.1. The generated 48 MiB input found all the expected changes, including byte zero and the last byte, with no extra changed pieces.

**@⁠UwUCammie** on GitHub · 2026-11-27 00:03 UTC · [original](https://github.com/fxceless/victory-fork/pull/1407#issuecomment-7206129981) I backported this to the AMD branch in [#35](https://github.com/jtrudeau1530/VIctory-7900XTX/pull/35). The Windows MSVC regression passed under Wine 11.22 on the RX 7900 XT host fixture with RADV 26.3.1. The generated 48 MiB input found all the expected changes, including byte zero and the last byte, with no extra changed pieces.
github-bot closed this pull request 2026-11-27 02:19:17 +00:00
Author
Owner

@⁠fxceless on GitHub · 2026-11-27 01:36 UTC · original

Merged by hand in 81d54b06 (the context had moved on main). Thank you.

**@⁠fxceless** on GitHub · 2026-11-27 01:36 UTC · [original](https://github.com/fxceless/victory-fork/pull/1407#issuecomment-7207430927) Merged by hand in 81d54b06 (the context had moved on main). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.