D3D12.X: fail closed on heap retirement fences and sweep idle arenas without PAGES #2538

Open
github-bot wants to merge 1 commit from github-bot/gh-2538 into main AGit
Owner

@⁠Decodere on GitHub · 2026-11-25 22:06 UTC · original
Decodere:fix-vram-retire-fail-closed-arenas → main · +162 −26 · 1 files · 1 commits · open

Part of #1773
Part of #1585
Part of #1833

What is in it

Fixes three VRAM heap retirement and idle arena reclamation bugs in crates/xcl/src/hle/d3d12x/vram.rs (without touching Arenas::take_for):

  • Fail closed on CreateFence/Signal failure in RetireQueue::retire_with: Previously, under VRAM/commit pressure, if make_fence or signal failed on all live queues (with unmap_mark == None), retire_with saw marks.is_empty() and returned heaps immediately to drop while live queues were still using them; if it failed on 1 of N live queues, that queue was omitted so sweep_with dropped the heaps as soon as the remaining queues finished. RetiringBatch::marks is now Option<Vec<(F, u64)>> (None = unarmed), RetireQueue::capture_marks returns None if ANY queue fails make_fence or signal (preserving monotonic *v += 1 on any already-signaled queue), and retire_with returns heaps immediately ONLY when queues.is_empty() && unmap_mark.is_none(). Added RetireQueue::retry_unarmed_with (invoked in retire_heaps_and_sweep before retire_with/sweep_with) so transiently-unarmed batches capture one shared set of fresh marks across all live queues and recover without head-of-line blocking armed ready batches.
  • Reject D3D12 u64::MAX device-removed sentinel in RetireQueue::sweep_with: Added pure helper fence_reached(completed, target) (completed != u64::MAX && completed >= target) and applied it to unmap_mark, b.marks, and dead-queue fence pruning so ID3D12Fence::GetCompletedValue() == u64::MAX on device removal never falsely reaps pending batches or prunes fences.
  • Sweep idle arenas from before_execute when PAGES has no active Some(Pages) entry for the device: Added per-device last_sweep: Vec<(usize, Instant)>, record_sweep, and sweep_dropped_for_if_due (1 s interval, pruning last_sweep when no arenas remain for dev) to Arenas<H>, plus static ARENA_COUNT: AtomicUsize synchronized to arenas.list.len() on every ARENAS list mutation (place, before_execute). When do_reclaim is false and ARENA_COUNT > 0 (e.g. XCL_VRAM_ARENAS=1 or devices with only non-tiled D32S8/MSAA arenas), before_execute runs sweep_dropped_for_if_due and retires reclaimed heaps through retire_heaps_and_sweep, keeping a zero-lock fast path when ARENA_COUNT == 0.
  • Tests (hle::d3d12x::vram::tests, pure CPU, 0 skips):
    • retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers
    • retire_queue_rejects_device_removed_sentinel
    • arenas_sweep_dropped_for_if_due_reclaims_without_pages

Tested

  • Failing-first on unmodified origin/main (e1d84b7d9153f86a97d2ec5d209d196046fc0bea) with the RetireQueue tests:
    • retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers panicked at vram.rs:1703: all-fail must hold heaps.
    • retire_queue_rejects_device_removed_sentinel panicked at vram.rs:1719: u64::MAX sentinel must neither reap nor prune.
  • Post-fix targeted release tests on NVIDIA GeForce RTX 4060 (0 compiler warnings in vram.rs):
    • cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture: 22 passed; 0 failed; 1 ignored (churn_update_tile_mappings_timing), 0 skipped (all 3 new pure-CPU tests plus gpu_retire_queue_holds_heap_until_blocked_queue_passes_mark, d32s8_targets_take_their_own_size_of_video_memory, and reservable_agrees_with_the_driver passed).
  • Post-fix targeted release tests on WARP (XCL_ADAPTER=warp):
    • cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture: 22 passed; 0 failed; 1 ignored, 0 skipped.
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture):
    • 37 test binaries: 1426 passed; 0 failed; 46 ignored.
    • 29 pre-existing runtime SKIPPED messages (21 no wave64 on wave32-only RTX 4060, 3 per-lane form: refused, 3 no D3D12 debug layer / no d3d12SDKLayers.dll, 1 cannot disable the driver cache, 1 no heap windows).

Regression run

Before (origin/main e1d84b7d9153f86a97d2ec5d209d196046fc0bea):

xcl-smoke v1
commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1508 (PresentX 1508)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14016 MB

After (fix-vram-retire-fail-closed-arenas 73ff10e9ba8c4e7d45362bb8637536b52aba1686):

xcl-smoke v1
commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1542 (PresentX 1542)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14007 MB

Both 60 s runs reach steady 60 60 60 presents per 2 s with 0 device removals, 0 crashes, 1542 vs 1508 frames shown (102.3%), and 14007 MB vs 14016 MB peak commit.

Not tested

  • Real D3D12 CreateFence OOM or DXGI_ERROR_DEVICE_REMOVED injection on hardware (both failure paths are exercised deterministically in the pure-CPU RetireQueue unit tests).
  • D3D12 debug-layer tests (d3d12SDKLayers.dll is not installed on this machine) and native wave64 GPU tests (RTX 4060 is wave32-only).

AI use

Written and verified with Google Antigravity (Gemini 4 Neon). All code changes, failing-first reproductions, targeted RTX 4060 and WARP test runs, the full release workspace suite, and both paired 60 s GPU smoke runs were executed and checked locally.

**@⁠Decodere** on GitHub · 2026-11-25 22:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/2538) `Decodere:fix-vram-retire-fail-closed-arenas` → `main` · +162 −26 · 1 files · 1 commits · open Part of #1773 Part of #1585 Part of #1833 **What is in it** Fixes three VRAM heap retirement and idle arena reclamation bugs in `crates/xcl/src/hle/d3d12x/vram.rs` (without touching `Arenas::take_for`): - **Fail closed on `CreateFence`/`Signal` failure in `RetireQueue::retire_with`:** Previously, under VRAM/commit pressure, if `make_fence` or `signal` failed on all live queues (with `unmap_mark == None`), `retire_with` saw `marks.is_empty()` and returned `heaps` immediately to drop while live queues were still using them; if it failed on 1 of N live queues, that queue was omitted so `sweep_with` dropped the heaps as soon as the remaining queues finished. `RetiringBatch::marks` is now `Option<Vec<(F, u64)>>` (`None` = unarmed), `RetireQueue::capture_marks` returns `None` if ANY queue fails `make_fence` or `signal` (preserving monotonic `*v += 1` on any already-signaled queue), and `retire_with` returns `heaps` immediately ONLY when `queues.is_empty() && unmap_mark.is_none()`. Added `RetireQueue::retry_unarmed_with` (invoked in `retire_heaps_and_sweep` before `retire_with`/`sweep_with`) so transiently-unarmed batches capture one shared set of fresh marks across all live queues and recover without head-of-line blocking armed ready batches. - **Reject D3D12 `u64::MAX` device-removed sentinel in `RetireQueue::sweep_with`:** Added pure helper `fence_reached(completed, target)` (`completed != u64::MAX && completed >= target`) and applied it to `unmap_mark`, `b.marks`, and dead-queue fence pruning so `ID3D12Fence::GetCompletedValue() == u64::MAX` on device removal never falsely reaps pending batches or prunes fences. - **Sweep idle arenas from `before_execute` when `PAGES` has no active `Some(Pages)` entry for the device:** Added per-device `last_sweep: Vec<(usize, Instant)>`, `record_sweep`, and `sweep_dropped_for_if_due` (1 s interval, pruning `last_sweep` when no arenas remain for `dev`) to `Arenas<H>`, plus `static ARENA_COUNT: AtomicUsize` synchronized to `arenas.list.len()` on every `ARENAS` list mutation (`place`, `before_execute`). When `do_reclaim` is false and `ARENA_COUNT > 0` (e.g. `XCL_VRAM_ARENAS=1` or devices with only non-tiled D32S8/MSAA arenas), `before_execute` runs `sweep_dropped_for_if_due` and retires reclaimed heaps through `retire_heaps_and_sweep`, keeping a zero-lock fast path when `ARENA_COUNT == 0`. - **Tests (`hle::d3d12x::vram::tests`, pure CPU, `0` skips):** - `retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers` - `retire_queue_rejects_device_removed_sentinel` - `arenas_sweep_dropped_for_if_due_reclaims_without_pages` **Tested** - Failing-first on unmodified `origin/main` (`e1d84b7d9153f86a97d2ec5d209d196046fc0bea`) with the `RetireQueue` tests: - `retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers` panicked at `vram.rs:1703`: `all-fail must hold heaps`. - `retire_queue_rejects_device_removed_sentinel` panicked at `vram.rs:1719`: `u64::MAX sentinel must neither reap nor prune`. - Post-fix targeted release tests on NVIDIA GeForce RTX 4060 (`0` compiler warnings in `vram.rs`): - `cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture`: `22 passed; 0 failed; 1 ignored` (`churn_update_tile_mappings_timing`), `0` skipped (all 3 new pure-CPU tests plus `gpu_retire_queue_holds_heap_until_blocked_queue_passes_mark`, `d32s8_targets_take_their_own_size_of_video_memory`, and `reservable_agrees_with_the_driver` passed). - Post-fix targeted release tests on WARP (`XCL_ADAPTER=warp`): - `cargo test --release -p xcl --lib hle::d3d12x::vram::tests -- --test-threads=1 --nocapture`: `22 passed; 0 failed; 1 ignored`, `0` skipped. - Full release workspace (`cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture`): - `37` test binaries: `1426 passed; 0 failed; 46 ignored`. - `29` pre-existing runtime `SKIPPED` messages (`21` `no wave64` on wave32-only RTX 4060, `3` `per-lane form: refused`, `3` `no D3D12 debug layer` / `no d3d12SDKLayers.dll`, `1` `cannot disable the driver cache`, `1` `no heap windows`). **Regression run** Before (`origin/main` `e1d84b7d9153f86a97d2ec5d209d196046fc0bea`): ``` xcl-smoke v1 commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1508 (PresentX 1508) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14016 MB ``` After (`fix-vram-retire-fail-closed-arenas` `73ff10e9ba8c4e7d45362bb8637536b52aba1686`): ``` xcl-smoke v1 commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1542 (PresentX 1542) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14007 MB ``` Both 60 s runs reach steady `60 60 60` presents per 2 s with `0` device removals, `0` crashes, `1542` vs `1508` frames shown (`102.3%`), and `14007 MB` vs `14016 MB` peak commit. **Not tested** - Real D3D12 `CreateFence` OOM or `DXGI_ERROR_DEVICE_REMOVED` injection on hardware (both failure paths are exercised deterministically in the pure-CPU `RetireQueue` unit tests). - D3D12 debug-layer tests (`d3d12SDKLayers.dll` is not installed on this machine) and native wave64 GPU tests (RTX 4060 is wave32-only). **AI use** Written and verified with Google Antigravity (Gemini 4 Neon). All code changes, failing-first reproductions, targeted RTX 4060 and WARP test runs, the full release workspace suite, and both paired 60 s GPU smoke runs were executed and checked locally.
Fix three VRAM heap retirement and idle arena reclamation bugs in vram.rs:

1. Fail closed in RetireQueue::retire_with when CreateFence or Signal fails on
   any live queue: change RetiringBatch::marks to Option<Vec<(F, u64)>>, factor
   RetireQueue::capture_marks (returning None on any queue failure while
   keeping fence values monotonic for already-signaled queues), and return
   heaps immediately only when queues.is_empty() && unmap_mark.is_none(). Add
   RetireQueue::retry_unarmed_with (invoked in retire_heaps_and_sweep before
   retire_with/sweep_with) so transiently-unarmed batches capture one shared
   set of fresh marks and recover without head-of-line blocking armed ready
   batches.
2. Reject the D3D12 u64::MAX device-removed fence sentinel via fence_reached
   in RetireQueue::sweep_with so GetCompletedValue() == u64::MAX neither reaps
   pending batches nor prunes dead-queue fences.
3. Sweep idle arenas from before_execute even when PAGES has no active entry
   for the device (e.g. XCL_VRAM_ARENAS=1 or devices with only non-tiled
   D32S8/MSAA arenas): track per-device last_sweep in Arenas<H> with
   sweep_dropped_for_if_due (1 s interval, pruning last_sweep when empty) and
   synchronize ARENA_COUNT to arenas.list.len() on every ARENAS list mutation
   so the zero-arena fast path avoids locking.

Add three pure-CPU unit tests in vram::tests covering fail-closed retirement
and recovery, u64::MAX sentinel rejection, and throttled idle arena sweeping.

Part of #1773
Part of #1585
Part of #1833

Assisted-by: Google Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:33:16 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 22:17 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 22:17 UTC · [original](https://github.com/xcl-dev/xcl/pull/2538#issuecomment-7176185181) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
Author
Owner

@⁠emirduran707 on GitHub · 2026-11-25 22:24 UTC · original

Independent AMD RDNA4 (native wave64) test report on exact head 73ff10e9ba8c4e7d45362bb8637536b52aba1686 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1424 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed: hle::d3d12x::vram::tests::retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers and retire_queue_rejects_device_removed_sentinel.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1606 (PresentX 1606)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13931 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.
**@⁠emirduran707** on GitHub · 2026-11-25 22:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/2538#issuecomment-7176319820) Independent AMD RDNA4 (native wave64) test report on exact head `73ff10e9ba8c4e7d45362bb8637536b52aba1686` (clean detached checkout, no local changes). **Tested** - AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2602.17; tests debug, smoke release. - `cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4`: **1424 passed, 2 failed, 46 ignored; 8 SKIPPED** (6 in `lower_dxil::tests::{f16,f32}_programs_on_the_gpu` refused by per-lane form; `heap_window_out_of_memory_falls_back_to_the_switch_form`; `pso_measurement_compiles_an_exact_synthetic_record`). All native wave64 GPU tests ran and passed. - Both PR tests passed: `hle::d3d12x::vram::tests::retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers` and `retire_queue_rejects_device_removed_sentinel`. - The 2 failures (`gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1` seed 722 and `fuzz_validate_and_gpu_3` seed 1599) are pre-existing on unmodified `main` on this GPU. **Regression run** (60 s retail-title smoke, `XCL_NETWORK=0`, release) ``` xcl-smoke v1 commit: 73ff10e9ba8c4e7d45362bb8637536b52aba1686 gpu: AMD Radeon RX 9060 XT title: 21222967 seconds: 60 exit: 0x0 frames shown: 1606 (PresentX 1606) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 13931 MB ``` (Matched `main` `68a284a` baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.) **Not tested** - D3D12 debug layer; Intel GPUs. **AI use** - Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.
Author
Owner

@⁠SlopTart on GitHub · 2026-11-25 23:24 UTC · original

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers and retire_queue_rejects_device_removed_sentinel: 2 passed. The sentinel test is a failure-path unit test; it does not exercise a real device-lost event on this lane.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

**@⁠SlopTart** on GitHub · 2026-11-25 23:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/2538#issuecomment-7177527053) **Tested**: current PR head merged onto current `origin/main` (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target `x86_64-pc-windows-gnu` run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29). `gpu_` suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR. Targeted run of `retire_queue_fails_closed_on_fence_or_signal_failure_and_recovers` and `retire_queue_rejects_device_removed_sentinel`: 2 passed. The sentinel test is a failure-path unit test; it does not exercise a real device-lost event on this lane. **Not tested**: retail titles (no title data on this lane, counts only). **AI use**: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2538/head:github-bot/gh-2538'
git switch 'github-bot/gh-2538'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2538'
git switch 'github-bot/gh-2538'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2538'
git switch 'github-bot/gh-2538'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2538'
git switch 'main'
git merge --squash 'github-bot/gh-2538'
git switch 'main'
git merge --ff-only 'github-bot/gh-2538'
git switch 'main'
git merge 'github-bot/gh-2538'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.