Shader: check device removal before FMad readback #2448

Open
Damokles2 wants to merge 1 commit from upstream-pr/2450 into main
Damokles2 commented 2026-11-25 13:48:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Damokles2 on 2026-11-25T13:48:03Z.


Problem

fuses_fmad() checks for device removal inside its fence polling loop. D3D12 documents that GetCompletedValue() returns UINT64_MAX after removal. That value satisfies the loop's completion condition, so execution can reach readback without establishing that the GPU copy completed. Fence contract · Readback mapping contract

Fix

Check GetDeviceRemovedReason() immediately after the fence wait and before readback. The change preserves existing error conversion, polling behavior, resource lifetimes, FMad classification, and adapter initialization policy. A small private helper lets CPU-only tests exercise the same wait-to-readback decision used by the production probe. Device removal status contract

Regression and validation

The test-only baseline had three passing tests and one expected failure in immediate_removal_skips_readback. Readback was invoked once when zero calls were expected.

assertion `left == right` failed: unexpected readback count
  left: 1
 right: 0

The patched Windows-MSVC run validated de9a0315887daf72fb4edf7a7d6dc6673ec32cf6.

  • FMad regression — 4 passed, 0 failed.
  • Existing CPU-only dxbc tests — 2 passed, 0 failed.
  • Strict Clippy — passed with default and all features.
  • git diff --check — passed.
  • Rustfmt — no new formatting differences relative to upstream.

The single fork-only run passed on its first attempt and took 3 minutes 36 seconds on the runner. Windows-MSVC validation

The regression uses simulated fence and device-status responses. Native D3D12 device removal, GPU execution, and full workspace runtime coverage were outside its scope. The guard handles removal observed at the end of the wait. Device removal can still occur after the status check.

References and coordination

  • pes7 requested a separate follow-up because the probe runs during production adapter initialization and #1734 covers the test harness. Request in #1734
  • #2041 addresses a separate sampler-heap binding issue in the same function. Its shader hunk applied cleanly during compatibility review. PR #2041
  • The device-removal follow-up was claimed in #1633. Coordination claim

AI-assisted development disclosure — OpenAI Codex assisted with investigation, implementation, regression testing, CI validation, and preparation of this PR.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5492585491 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450). Originally posted by [Damokles2](https://github.com/Damokles2) on 2026-11-25T13:48:03Z. --- ### Problem `fuses_fmad()` checks for device removal inside its fence polling loop. D3D12 documents that `GetCompletedValue()` returns `UINT64_MAX` after removal. That value satisfies the loop's completion condition, so execution can reach readback without establishing that the GPU copy completed. [Fence contract](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12fence-getcompletedvalue) · [Readback mapping contract](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12resource-map) ### Fix Check `GetDeviceRemovedReason()` immediately after the fence wait and before readback. The change preserves existing error conversion, polling behavior, resource lifetimes, FMad classification, and adapter initialization policy. A small private helper lets CPU-only tests exercise the same wait-to-readback decision used by the production probe. [Device removal status contract](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-getdeviceremovedreason) ### Regression and validation The test-only baseline had three passing tests and one expected failure in `immediate_removal_skips_readback`. Readback was invoked once when zero calls were expected. ```text assertion `left == right` failed: unexpected readback count left: 1 right: 0 ``` The patched Windows-MSVC run validated `de9a0315887daf72fb4edf7a7d6dc6673ec32cf6`. - FMad regression — 4 passed, 0 failed. - Existing CPU-only `dxbc` tests — 2 passed, 0 failed. - Strict Clippy — passed with default and all features. - `git diff --check` — passed. - Rustfmt — no new formatting differences relative to upstream. The single fork-only run passed on its first attempt and took 3 minutes 36 seconds on the runner. [Windows-MSVC validation](https://github.com/Damokles2/VIctory/actions/runs/45625862310) The regression uses simulated fence and device-status responses. Native D3D12 device removal, GPU execution, and full workspace runtime coverage were outside its scope. The guard handles removal observed at the end of the wait. Device removal can still occur after the status check. ### References and coordination - pes7 requested a separate follow-up because the probe runs during production adapter initialization and #1734 covers the test harness. [Request in #1734](https://github.com/Marvisak/VIctory/pull/1734#issuecomment-7162834168) - #2041 addresses a separate sampler-heap binding issue in the same function. Its shader hunk applied cleanly during compatibility review. [PR #2041](https://github.com/Marvisak/VIctory/pull/2041) - The device-removal follow-up was claimed in #1633. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7163212046) AI-assisted development disclosure — OpenAI Codex assisted with investigation, implementation, regression testing, CI validation, and preparation of this PR.
DenRakEiw commented 2026-11-25 13:55:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:55:25Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Checking for device removal before FMad readback cleanly avoids reading garbage after a removed device.
  • cargo test -p victory-shader hosts_that_do_not_fuse_are_named: 1/1 passed on RTX 5090, 1/1 passed on WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058902109 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#pullrequestreview-6058902109). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:55:25Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Checking for device removal before FMad readback cleanly avoids reading garbage after a removed device. - `cargo test -p victory-shader hosts_that_do_not_fuse_are_named`: 1/1 passed on RTX 5090, 1/1 passed on WARP. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 14:11:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T14:11:05Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — device removal checked before FMad readback.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059173199 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#pullrequestreview-6059173199). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:11:05Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — device removal checked before FMad readback.
kotekompotek commented 2026-11-25 15:04:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T14:16:32Z.


Correct: GetCompletedValue returns u64::MAX on a removed device, which satisfied the < 1 wait and let Map run on dead readback — the FMad verdict then read garbage or failed as its own error, hiding the removal. The post-wait status check closes it. Extracting read_fmad_after_wait makes all four paths unit-testable without a device; the test matrix covers MAX-immediate, mid-poll removal, and both healthy outcomes.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165025672 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#issuecomment-7165025672). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T14:16:32Z. --- Correct: `GetCompletedValue` returns `u64::MAX` on a removed device, which satisfied the `< 1` wait and let `Map` run on dead readback — the FMad verdict then read garbage or failed as its own error, hiding the removal. The post-wait status check closes it. Extracting `read_fmad_after_wait` makes all four paths unit-testable without a device; the test matrix covers MAX-immediate, mid-poll removal, and both healthy outcomes.
Marvisak commented 2026-11-25 16:09:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:58:37Z.


Automated triage for the maintainers.

Merge candidate, low priority. In crates/victory-shader/src/emit/host.rs fuses_fmad, a device removed by the time the fence reads complete (it then reports u64::MAX, which ends the wait) now returns the "device removed" error instead of mapping the readback; the wait-then-readback order is moved into a small helper with CPU-only tests. The only caller, crates/victory-gpu/src/host.rs at adapter setup, uses the result just to pick a log line, so the visible effect is a correct message in that case. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167481122 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#issuecomment-7167481122). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:58:37Z. --- <!-- victory-triage v1 head=de9a0315887daf72fb4edf7a7d6dc6673ec32cf6 verdict=merge-candidate at=2026-11-25T15:58:31Z --> Automated triage for the maintainers. Merge candidate, low priority. In `crates/victory-shader/src/emit/host.rs` `fuses_fmad`, a device removed by the time the fence reads complete (it then reports `u64::MAX`, which ends the wait) now returns the "device removed" error instead of mapping the readback; the wait-then-readback order is moved into a small helper with CPU-only tests. The only caller, `crates/victory-gpu/src/host.rs` at adapter setup, uses the result just to pick a log line, so the visible effect is a correct message in that case. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 01:04:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:55:24Z.


Code review and RTX 5090 + WARP + DXIL verification at head de9a0315887d merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-shader/src/emit/host.rs:720-751):
    • Extracting read_fmad_after_wait and checking device_status() (GetDeviceRemovedReason()) after the fence wait completes and before readback.Map(...) cleanly closes the D3D12 device-removal window where ID3D12Fence::GetCompletedValue() returns u64::MAX, breaks the wait loop, and would otherwise read zeros from the readback buffer and misclassify FMAD support.
    • Checked interaction with #2922 (emit/host.rs:691-694 sampler heap binding): ~25 lines apart, zero merge conflict and complementary.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.0.26100.0, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-shader --all-targets -- -D warnings (default + --all-features): 0 warnings
    • emit::host emit::tests emit::lower::tests: 55/55 passed on both RTX 5090 and WARP
    • emit::semantics_tests::hosts_that_do_not_fuse_are_named: passed (WARP = Ok(false), RTX 5090 hardware fuses FMad = true).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179249291 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#issuecomment-7179249291). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:55:24Z. --- Code review and RTX 5090 + WARP + DXIL verification at head `de9a0315887d` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-shader/src/emit/host.rs:720-751`)**: - Extracting `read_fmad_after_wait` and checking `device_status()` (`GetDeviceRemovedReason()`) after the fence wait completes and before `readback.Map(...)` cleanly closes the D3D12 device-removal window where `ID3D12Fence::GetCompletedValue()` returns `u64::MAX`, breaks the wait loop, and would otherwise read zeros from the readback buffer and misclassify FMAD support. - Checked interaction with `#2922` (`emit/host.rs:691-694` sampler heap binding): ~25 lines apart, zero merge conflict and complementary. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `dxil.dll` 10.0.26100.0, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-shader --all-targets -- -D warnings` (default + `--all-features`): 0 warnings - `emit::host emit::tests emit::lower::tests`: **55/55 passed** on both RTX 5090 and WARP - `emit::semantics_tests::hosts_that_do_not_fuse_are_named`: passed (`WARP = Ok(false)`, RTX 5090 hardware fuses FMad = `true`). LGTM.
fuzeofficlal commented 2026-11-27 02:49:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:49:25Z.

Review: APPROVED


Reviewed at de9a0315887daf72fb4edf7a7d6dc6673ec32cf6 — APPROVE.

Verification

  • cargo xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets --all-features -- -D warnings, and rustfmt --edition 2024 --check (zero new formatting diffs) all passed.
  • Verified read_fmad_after_wait (crates/victory-shader/src/emit/host.rs:735-751) re-checks GetDeviceRemovedReason() after exiting the fence wait loop—catching the D3D12 u64::MAX device-removal sentinel cleanly before readback.Map, matching Host::draw (host.rs:1327-1329) with paired Map/Unmap cleanup.
  • Passed emit::host::fmad_wait_tests (4/4), and verified zero regressions on both hardware (VICTORY_WARP=0) and WARP (VICTORY_WARP=1) across emit::host::tests (1/1), emit::valu_tests::v_fma_ (1/1), and emit::semantics_tests::hosts_that_do_not_fuse_are_named (confirming true on hardware and false on WARP).

Non-blocking notes

  • Host::dispatch (host.rs:2061) still lacks the same post-wait GetDeviceRemovedReason() check.
  • Adding a unit case in fmad_wait_tests where completed_value() returns 1 and the post-wait device_status() returns Err(DXGI_ERROR_DEVICE_REMOVED) would assert zero readback calls for the post-loop check specifically.
<!-- greyfinch-sync:Marvisak/VIctory:review:6082835496 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2450#pullrequestreview-6082835496). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T02:49:25Z. **Review:** APPROVED --- Reviewed at `de9a0315887daf72fb4edf7a7d6dc6673ec32cf6` — **APPROVE**. ### Verification - `cargo xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`, and `rustfmt --edition 2024 --check` (zero new formatting diffs) all passed. - Verified `read_fmad_after_wait` (`crates/victory-shader/src/emit/host.rs:735-751`) re-checks `GetDeviceRemovedReason()` after exiting the fence wait loop—catching the D3D12 `u64::MAX` device-removal sentinel cleanly before `readback.Map`, matching `Host::draw` (`host.rs:1327-1329`) with paired `Map`/`Unmap` cleanup. - Passed `emit::host::fmad_wait_tests` (4/4), and verified zero regressions on both hardware (`VICTORY_WARP=0`) and WARP (`VICTORY_WARP=1`) across `emit::host::tests` (1/1), `emit::valu_tests::v_fma_` (1/1), and `emit::semantics_tests::hosts_that_do_not_fuse_are_named` (confirming `true` on hardware and `false` on WARP). ### Non-blocking notes - `Host::dispatch` (`host.rs:2061`) still lacks the same post-wait `GetDeviceRemovedReason()` check. - Adding a unit case in `fmad_wait_tests` where `completed_value()` returns `1` and the post-wait `device_status()` returns `Err(DXGI_ERROR_DEVICE_REMOVED)` would assert zero readback calls for the post-loop check specifically.
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/2450:upstream-pr/2450'
git switch 'upstream-pr/2450'

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