GPU: flush queued segments before evaluating memory and fence waits #3139

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3141 into main
fuzeofficlal commented 2026-11-26 09:57:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T09:57:26Z.


Problem

In crates/victory-gpu/src/hold.rs, Held::pump accumulates consecutive ready segments on a queue into piece and flushes piece via (w.run) before executing Item::Signal, as its comment notes (A Signal or Wait keeps its place: the piece before it runs first). However, neither Item::Segment (with non-empty ValueWaits) nor Item::FenceWait flushed piece before testing their wait condition.

Because an earlier segment on the same queue can modify the waited memory (via Write32/64BitValueBottomOfPipeX, shader writebacks, or direct writes to fence::gpu_address), evaluating waits against pre-segment memory causes two ordering bugs:

  1. Missed wait / synchronization bypass: when a command list resets a previously-set flag (e.g. Write64BitValueBottomOfPipeX(flag, 0)), then waits for another queue to set it back (Wait64BitValueX(flag == 1)), list::wait splits the list into two segments. Held::pump queues segment 0 in piece and immediately checks segment 1's wait against stale memory (flag == 1), erroneously passes, and submits both segments together before the peer queue runs. The same happens if a segment rewrites a fence's memory before an Item::FenceWait.
  2. Spurious wait accounting: when segment 0 writes the value that segment 1 waits on, segment 1 initially reads the stale value, marks itself blocked (fresh = false), and records a false stall in Pumped::waited.

Fix

Flush any pending piece on the current queue before evaluating non-empty ValueWaits on Item::Segment and before evaluating Item::FenceWait. Consecutive wait-free segments (waits.is_empty()) are unaffected and continue to batch into a single host submission.

Verification

Added four regression tests (all four confirmed failing on main 63bcca43 before the fix and passing after):

  • hold::tests::a_wait_runs_the_work_before_it_first
  • hold::tests::a_wait_for_an_earlier_segment_on_the_same_queue_does_not_stall
  • hold::tests::a_fence_wait_runs_the_work_before_it_first
  • queue::tests::in_list_wait_sees_bottom_of_pipe_writes_before_it (end-to-end guest command list using Write64BitValueBottomOfPipeX + Wait64BitValueX + ExecuteCommandLists, verified on both hardware D3D12 and VICTORY_D3D12_WARP=1)

Full checks passed:

  • hold::tests (13/13), queue::tests (6/6), and resource::tests::a_list_waiting_for_another_queues_write_reads_what_it_wrote on both native D3D12 and VICTORY_D3D12_WARP=1
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features (0 warnings)
  • rustfmt --edition 2024 --check (0 diffs on all modified/added lines)
  • cargo test -p xtask --locked policy::tests::repository_passes_policy (asset policy: 191 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6624092258 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3141). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T09:57:26Z. --- ### Problem In `crates/victory-gpu/src/hold.rs`, `Held::pump` accumulates consecutive ready segments on a queue into `piece` and flushes `piece` via `(w.run)` before executing `Item::Signal`, as its comment notes (`A Signal or Wait keeps its place: the piece before it runs first`). However, neither `Item::Segment` (with non-empty `ValueWait`s) nor `Item::FenceWait` flushed `piece` before testing their wait condition. Because an earlier segment on the same queue can modify the waited memory (via `Write32/64BitValueBottomOfPipeX`, shader writebacks, or direct writes to `fence::gpu_address`), evaluating waits against pre-segment memory causes two ordering bugs: 1. **Missed wait / synchronization bypass**: when a command list resets a previously-set flag (e.g. `Write64BitValueBottomOfPipeX(flag, 0)`), then waits for another queue to set it back (`Wait64BitValueX(flag == 1)`), `list::wait` splits the list into two segments. `Held::pump` queues segment 0 in `piece` and immediately checks segment 1's wait against stale memory (`flag == 1`), erroneously passes, and submits both segments together before the peer queue runs. The same happens if a segment rewrites a fence's memory before an `Item::FenceWait`. 2. **Spurious wait accounting**: when segment 0 writes the value that segment 1 waits on, segment 1 initially reads the stale value, marks itself blocked (`fresh = false`), and records a false stall in `Pumped::waited`. ### Fix Flush any pending `piece` on the current queue before evaluating non-empty `ValueWait`s on `Item::Segment` and before evaluating `Item::FenceWait`. Consecutive wait-free segments (`waits.is_empty()`) are unaffected and continue to batch into a single host submission. ### Verification Added four regression tests (all four confirmed failing on `main` `63bcca43` before the fix and passing after): - `hold::tests::a_wait_runs_the_work_before_it_first` - `hold::tests::a_wait_for_an_earlier_segment_on_the_same_queue_does_not_stall` - `hold::tests::a_fence_wait_runs_the_work_before_it_first` - `queue::tests::in_list_wait_sees_bottom_of_pipe_writes_before_it` (end-to-end guest command list using `Write64BitValueBottomOfPipeX` + `Wait64BitValueX` + `ExecuteCommandLists`, verified on both hardware D3D12 and `VICTORY_D3D12_WARP=1`) Full checks passed: - `hold::tests` (13/13), `queue::tests` (6/6), and `resource::tests::a_list_waiting_for_another_queues_write_reads_what_it_wrote` on both native D3D12 and `VICTORY_D3D12_WARP=1` - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` (0 warnings) - `rustfmt --edition 2024 --check` (0 diffs on all modified/added lines) - `cargo test -p xtask --locked policy::tests::repository_passes_policy` (`asset policy: 191 files ok`)
smaxxey commented 2026-11-26 11:03:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by smaxxey on 2026-11-26T10:13:17Z.


Ran this on an RTX 4060 Laptop GPU (Ada, 8 GB, driver 32.0.16.1742), Windows 11 26300, Rust 1.98.1. No game files. Head 469d7a6 merged locally onto main 63bcca4.

  • The four new tests pass, cargo test -p victory-gpu --lib --locked -- --test-threads=1: the three hold::tests cases and queue::tests::in_list_wait_sees_bottom_of_pipe_writes_before_it on the hardware device.
  • cargo test -p victory-gpu --lib --locked: 233 passed, 2 failed, same as main 63bcca4 on this card: only the two NVIDIA failures of #1835 (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views), nothing new.

Tested with Claude Code's help.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189070625 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3141#issuecomment-7189070625). Originally posted by [smaxxey](https://github.com/smaxxey) on 2026-11-26T10:13:17Z. --- Ran this on an **RTX 4060 Laptop GPU** (Ada, 8 GB, driver 32.0.16.1742), Windows 11 26300, Rust 1.98.1. No game files. Head `469d7a6` merged locally onto `main` 63bcca4. - The four new tests pass, `cargo test -p victory-gpu --lib --locked -- --test-threads=1`: the three `hold::tests` cases and `queue::tests::in_list_wait_sees_bottom_of_pipe_writes_before_it` on the hardware device. - `cargo test -p victory-gpu --lib --locked`: 233 passed, 2 failed, same as `main` 63bcca4 on this card: only the two NVIDIA failures of #1835 (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`), nothing new. Tested with Claude Code's help.
Dukestar69 commented 2026-11-26 13:04:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T12:11:58Z.

Review: APPROVED


Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib queue:: -- --test-threads=1: 6/6 passed (including in_list_wait_sees_bottom_of_pipe_writes_before_it).
  • cargo test -p victory-gpu --lib hold:: -- --test-threads=1: passed.
  • cargo test -p victory-gpu --lib fence:: -- --test-threads=1: 2/2 passed; draw::: 20/20 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly flushes queued segments before evaluating in-list memory and fence waits.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6073917500 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3141#pullrequestreview-6073917500). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T12:11:58Z. **Review:** APPROVED --- Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib queue:: -- --test-threads=1`: 6/6 passed (including `in_list_wait_sees_bottom_of_pipe_writes_before_it`). - `cargo test -p victory-gpu --lib hold:: -- --test-threads=1`: passed. - `cargo test -p victory-gpu --lib fence:: -- --test-threads=1`: 2/2 passed; `draw::`: 20/20 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly flushes queued segments before evaluating in-list memory and fence waits. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3141:upstream-pr/3141'
git switch 'upstream-pr/3141'

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/3141'
git switch 'upstream-pr/3141'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3141'
git switch 'upstream-pr/3141'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3141'
git switch 'main'
git merge --squash 'upstream-pr/3141'
git switch 'main'
git merge --ff-only 'upstream-pr/3141'
git switch 'main'
git merge 'upstream-pr/3141'
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!3139
No description provided.