Skip to content

D3D12.X: keep caller allocator for untracked lists and retain whole PSOs in dispatch_chain - #2333

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-2080-dispatch-chain-allocator-lifetime
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-2080-dispatch-chain-allocator-lifetime

Conversation

@Decodere

@Decodere Decodere commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What is in it

Follow-up to #2080 (Part of #2088, Part of #1269) fixing four runtime allocator/lifetime bugs across crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs, crates/xcl/src/hle/d3d12x/bind.rs, crates/xcl/src/hle/d3d12x/list_x.rs, and crates/xcl/src/hle/d3d12x/alloc_pool.rs:

  1. Preserve caller allocator for lists not tracked by alloc_pool: Extracted resolve_allocator (resolve_allocator_with) so when alloc_pool::enabled() is true and alloc_pool::recording_backing(list) returns None (as for console command lists created via dev_create_command_list_x and bundles), dispatch_chain falls back to the caller's allocator instead of 0. Previously, zeroing the allocator forced every console-list split compute dispatch onto the synchronous unsplit chain.whole() fallback.
  2. Register fresh mid-list split segments on their active alloc_pool backing: In list_x::split, when no idle spare exists and a fresh segment is created on pc_alloc = alloc_pool::current(alloc), call alloc_pool::created_on(p as usize, pc_alloc, ty.0) (and real::dispatch_chain::list_reset(p as usize)). Previously, leaving the new segment unregistered caused resolve_allocator to fall back to the title allocator A after an allocator swap (current(A) == B), so a later title Reset() reusing A would call dispatch_chain::allocator_reset(A) and drop scratch/retained PSOs while the split segment on B was still alive (caught by @pvfreis).
  3. Retain Arc<Chain> (and its bound whole PSO) until allocator_reset: Changed Recording::{scratch, first} to Option<Arc<Scratch>> / Option<ID3D12PipelineState>, updated dispatch_chain::whole(list, pso, allocator) (and its bind.rs ExecuteIndirect callsite), and added retain_whole(allocator, chain) on both the dispatch() whole fallback and ExecuteIndirect's whole() path. Whenever allocator != 0, Arc<Chain> is retained in RECORDINGS[allocator] (deduplicated per allocator, dropping any duplicate Arc<Chain> outside RECORDINGS.lock()) so forget(pso) while the command list is still in flight does not destroy the bound whole ID3D12PipelineState before allocator_reset(allocator).
  4. Update allocator type on alloc_pool::created_on: Changed s.types.entry(raw).or_insert(ty) to s.types.insert(raw, ty) (matching reset_list), so recycling a non-adopted test allocator address under a different command-list type does not keep a stale allocator type across parallel tests (caught by @ivall).

(Rebased onto origin/main 6d12f58a.)

Tested

  • Pre-fix reproductions:
    • whole_fallback_retains_chain_until_allocator_reset_preflight: confirmed unmodified dispatch() whole fallback failed (0 passed; 1 failed — Chain and its whole PSO dropped immediately on forget(pso)).
    • wait_split_fresh_segment_tracks_swapped_backing_allocator (WARP, before created_on): failed 0 passed; 1 failed (recording_backing_for_test(seg1) was None instead of Some(pc_alloc_b)).
  • Post-fix targeted tests (--release -- --test-threads=1 --nocapture, commit a7b93a85):
    • hle::d3d12x::real::dispatch_chain::tests: 4 passed, 0 failed, 0 ignored (0 skipped) on both RTX 4060 (0.41 s) and WARP (0.40 s), including a_list_recorded_after_another_goes_on_in_its_scratch.
    • hle::d3d12x::list_x::tests::wait_split_fresh_segment_tracks_swapped_backing_allocator: 1 passed, 0 failed, 0 ignored (0 skipped) on both RTX 4060 (0.23 s) and WARP (0.23 s), verifying a real alloc_pool swap A -> B, fresh mid-list split segment tracking on B, resolve_allocator returning B, and reuse/reset of A leaving seg1 keyed to B.
  • Parallel flake stress (cargo test --release -p xcl --lib -- dispatch_chain list_x::tests alloc_pool --test-threads=8): 20 consecutive runs, 0/20 failures (57 passed, 0 failed per run).
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture on RTX 4060): 37 test binaries, 1426 passed, 0 failed, 46 ignored (29 pre-existing hardware/debug-layer SKIPPED: 21 no wave64, 3 wave32 per-lane refused, 3 missing D3D12 debug layer, 1 driver cache toggle, 1 no heap windows; 0 skipped in our tests).

Regression run

Paired 60 s scripts/smoke.ps1 runs on the same RTX 4060 host against origin/main (889ef3e8aa7f267323ab4879e735e65da6bcef10) and PR head (a7b93a853ec56621bdef65a695cd858094878bfe):

Baseline (origin/main):

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

PR head:

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

Not tested

  • Retail title run with XCL_CONSOLE_LISTS=1 or D3D12 debug-layer execution (d3d12SDKLayers.dll is not installed on this host; allocator fallback, Wait-split alloc_pool registration, and whole PSO lifetime retention are verified directly by the unit/D3D12 tests).
  • AMD/Intel GPUs and native wave64 hardware (this host has an NVIDIA RTX 4060).

AI use

Implemented and verified with Google Antigravity (Gemini 4 Neon).

Part of #2088
Part of #1269

@nnonno91

Copy link
Copy Markdown

Independent Radeon and RTX 5090 verification of af0b6672d1ed51a7d8855c61050262ad9c470464.

Tested

  • Windows 11 25H2 build 26200.9974, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and RTX 5090 driver 32.0.16.1742.
  • cargo test --locked -p xcl --lib hle::d3d12x::real::dispatch_chain::tests:: -- --nocapture --test-threads=1 with each adapter selected by XCL_ADAPTER. The checked-in GPU fixture uses the default device, so I temporarily changed only its adapter lookup and printed the selected device. Both runs gave 2 passed, 0 failed, 0 ignored and no skips.
  • The CPU test checked caller and tracked allocator selection. On both devices, the whole-fallback and indirect fixture retained the chain across forget(pso), deduplicated repeated recording, then released it on allocator reset. The adapter selection edit was restored.

Not tested

Submitted in-flight work outside the unit fixture, the parent, retail smoke and the full workspace suite.

AI use

OpenAI Codex (GPT-6.2 Sol) adapted the test's adapter lookup, ran the tests and wrote this report.

@fromthemoonx

fromthemoonx commented Nov 25, 2026 •

Copy link
Copy Markdown

Rerun on the new head 02ba2f9 (rebased onto be67bfc): passes on NVIDIA. The new wait_split_fresh_segment_tracks_swapped_backing_allocator passes 30 of 30, fails without the created_on line, and records no debug-layer messages.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, 02ba2f9 merged onto main 2898511 (clean) 1366 passed, 0 failed, 46 ignored; 25 SKIPPED lines; the new test ran and passed
same at the head 02ba2f9 1322 passed, 1 failed, 46 ignored: a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, from the base; passes on current main (c828d8c), not this PR
the new test alone, 30 runs; list_x::tests:: (37 tests), 5 runs; dispatch_chain::tests:: (3 tests), 30 runs 30 / 5 / 30 of 30
without alloc_pool::created_on(p as usize, pc_alloc, ty.0) (scratch) the new test fails 3 of 3 at list_x.rs:6987, recording_backing None
without only real::dispatch_chain::list_reset(p as usize) (scratch) the new test still passes: it does not cover that line
the new test with a scratch EnableDebugLayer() and an info-queue dump 0 stored messages

Earlier rerun, on e406771: Passed on NVIDIA, and with the debug layer on, the new D3D12 test was clean (0 messages). The fixture change (a bundle with SetComputeRootSignature, dispatching [1, 1, 1]) resolves the two messages reported below.

result
cargo test --workspace --no-fail-fast -- --test-threads=4 1274 passed, 0 failed, 46 ignored; 25 SKIPPED lines; test_title_draws_with_a_root_signature_under_the_debug_layer ran and passed
hle::d3d12x::real::dispatch_chain::tests:: (3 tests, now with main's a_chains_scratch_is_bounded_by_what_all_recordings_hold), 30 runs 30 of 30
whole_fallback_and_indirect_retain_chain_until_allocator_reset with a scratch EnableDebugLayer() and an info-queue dump passes, 0 stored messages

First run, on af0b667: Passed on NVIDIA with the D3D12 debug layer installed, and the e2e debug-layer messages were the same as on main 06c9f55. 1267 passed, 0 failed, 46 ignored. dispatch_chain::tests:: passed 30 of 30. With the layer turned on inside the new D3D12 test, its fixture recorded error id 952 (Dispatch with no root signature set) and warning id 1296 (group count 4294967295 > 65535), from dispatch(&list, p1, a1, [u32::MAX, 1, 1]) on a list with no root signature. The list was never executed, so nothing broke.

Tested: RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug, Graphics Tools (D3D12 debug layer) installed.

Not tested: a title with XCL_CONSOLE_LISTS=1, a release build, AMD / Intel.

AI use: Claude Code (Claude Opus 5.5) ran the tests and the scratch debug-layer check, and wrote this report.

@luckyteagirl

Copy link
Copy Markdown
Contributor

Independent RX 6800M check on exact head af0b6672d1ed51a7d8855c61050262ad9c470464.

Tested

  • Windows 11 build 26200, discrete AMD Radeon RX 6800M (driver 30.0.13002.13003), Rust 1.99 MSVC, DXC/DXIL 1.9.2609.5.
  • Clean checkout: cargo test --locked -p xcl --lib hle::d3d12x::real::dispatch_chain::tests:: -- --test-threads=1 --show-output: 2 passed, 0 failed, 0 ignored, 0 explicit skips. The checked-in fixture uses the default D3D12 device, so this run alone does not identify the adapter.
  • Local test-only change to the GPU fixture selected via XCL_ADAPTER=6800M and printed dispatch_chain test adapter: AMD Radeon RX 6800M. With XCL_REQUIRE_GPU=wave64, the same filter gave 2 passed, 0 failed, 0 ignored, 0 explicit skips. The GPU fixture exercised both dispatch() whole fallback and repeated whole() calls, including retention across forget(pso), deduplication, and release at allocator reset. The selector change was restored; the checkout is clean.

This adds a discrete RDNA2 and older AMD driver to the existing Radeon/NVIDIA checks. I did not run the full workspace suite or a retail-title smoke comparison for this PR, and these focused tests do not cover an in-flight console command list.

AI use: OpenAI Codex (GPT-6) ran the checks and prepared this report.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…#2296, xcl-dev#2297, xcl-dev#2320, xcl-dev#2323, xcl-dev#2329, xcl-dev#2333

Integrates shader heap-base addresses (xcl-dev#1803), fenced superseded texture unmaps (xcl-dev#2270), cross-queue VRAM heap reclamation fencing (xcl-dev#2275, using create_raw_fence under Wine), PcCall frame clamping and orphan PSO drop (xcl-dev#2296), PSO pointer guards (xcl-dev#2297), KickoffX idle spare list switching (xcl-dev#2320, preserving UnadoptGuard), console-list descriptor heap binding (xcl-dev#2323), ClearUnorderedAccessView PC handles (xcl-dev#2329), and dispatch_chain PSO retention (xcl-dev#2333).
@Decodere
Decodere force-pushed the fix-2080-dispatch-chain-allocator-lifetime branch from af0b667 to e406771 Compare November 25, 2026 15:50
@Decodere

Copy link
Copy Markdown
Contributor Author

Thank you @nnonno91, @fromthemoonx, and @luckyteagirl for testing across RTX 5090, RX 7900 XTX / Arc A770, and RX 6800M!

Rebased #2333 cleanly onto origin/main (08294413 -> e406771b) to integrate with e9ddf17/0829441 slabbed bounded scratch:

  • Adapted Recording::scratch to Option<Arc<Scratch>> while keeping the SLABS / scratch_for / record_dispatch_chain design and a_chains_scratch_is_bounded_by_what_all_recordings_hold intact.
  • Updated whole_fallback_and_indirect_retain_chain_until_allocator_reset so the dispatch() whole fallback test uses a D3D12_COMMAND_LIST_TYPE_BUNDLE command list with SetComputeRootSignature and [1, 1, 1] groups (deterministically hitting the fallback without huge grid args, addressing @fromthemoonx's note about D3D12 debug layer ID 952/1296).
  • Re-verified dispatch_chain::tests (3 passed, 0 failed on both RTX 4060 and WARP), full release workspace (1274 passed, 0 failed), and paired 60 s smoke (1553 -> 1624, 0 device removals/crashes).

@pvfreis

pvfreis commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

By reading only, on e406771:

With default switches this fallback also reaches Wait-split segments, and after an allocator swap the title's allocator is the wrong key for them.

  • With no idle spare, try_split_for_wait creates the segment on alloc_pool::current(alloc) without registering it, so recording_backing is None. On main its split dispatches go whole; here they chain.
  • After a swap it records on PC allocator B, but its scratch is in RECORDINGS[A]. A title Reset with B busy and A free reuses A (title_reset_in); reset_pc_allocator(A) drops scratch and chain, while the recording on B can be submitted again.

Fix: register it where it is made, alloc_pool::created_on(p as usize, pc_alloc, ty.0); #1071 (draft, conflicting) has that hunk.

Tested: nothing run. Not tested: whether a retail title hits it.

AI use: Claude Code (Claude Opus 5.5) found it, Claude Fable 5.2 checked it; checked by a person before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@Decodere
Decodere force-pushed the fix-2080-dispatch-chain-allocator-lifetime branch from e406771 to 02ba2f9 Compare November 25, 2026 18:46
@Decodere

Copy link
Copy Markdown
Contributor Author

Good catch @pvfreis — verified directly in list_x::try_split_for_wait (None spare branch): creating a fresh mid-list Wait split segment on alloc_pool::current(alloc) without calling alloc_pool::created_on(p as usize, pc_alloc, ty.0) left recording_backing(seg1) as None, so after an allocator swap (A -> B) resolve_allocator fell back to title allocator A instead of B.

Updated the branch to 02ba2f94 (rebased onto be67bfcf):

  • Register newly created Wait-split segments via alloc_pool::created_on(p as usize, pc_alloc, ty.0) (and real::dispatch_chain::list_reset(p as usize)).
  • Added a WARP- and hardware-runnable regression test wait_split_fresh_segment_tracks_swapped_backing_allocator in list_x.rs (confirmed failing before the fix with recording_backing == None, passing 1/0 on RTX 4060 and WARP after, including across an A -> B -> A allocator swap/reuse cycle).
  • Re-verified dispatch_chain::tests (3/0 RTX 4060 & WARP), mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares (1/0), full release workspace (1322/1 — sole failure is origin/main's pre-existing #2074 test), and paired 60 s smoke (be67bfcf 1525 frames / 13969 MB vs 02ba2f94 1544 frames / 13976 MB, 0 removals/crashes).

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…1946, xcl-dev#1803, xcl-dev#2297, xcl-dev#2320, xcl-dev#2333

Carries forward still-open high-value upstream PRs onto upstream/main (2898511)
+ our updated Wine PR stack (xcl-dev#867, xcl-dev#1395-successor, xcl-dev#871, xcl-dev#877, xcl-dev#1732).
Preserves UnadoptGuard in xcl-dev#2320 KickoffX reset_one.

Assisted-by: Antigravity (Gemini 4 Neon)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…s) + xcl-dev#2333 list_x

Integrates xcl-dev#2384 (shaders read buffers in a GPU view of the title's memory)
preserving xcl-dev#877 ensure_lazy_mapped in dev_create_uav_at_x, plus the xcl-dev#2333
Wait-split backing allocator tracking in list_x.rs.

Assisted-by: Antigravity (Gemini 4 Neon)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
, xcl-dev#2333, and UnadoptGuard test submits

Assisted-by: Antigravity (Gemini 4 Neon)
@ivall

ivall commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Read in full (body, thread, diff) and tested on AMD RDNA4 (native wave64), Windows, DXC 1.8.2502.11, debug. The three fixes look right to me. One thing holds the merge: the new test wait_split_fresh_segment_tracks_swapped_backing_allocator is flaky when tests run in parallel, and the cause is in created_on.

What happens (02ba2f9 alone, no other PR)

  • wait_split_fresh_segment --test-threads=1: 40 of 40 runs pass.
  • list_x::tests --test-threads=8: 11 of 40 runs fail; dispatch_chain list_x::tests --test-threads=8: 13 of 40. Always this test, at assert_eq!(reset_fn(wrap_c, a_raw, 0), 0) (list_x.rs:6963), E_INVALIDARG. No other test failed in those runs.

Cause

  • alloc_pool::created_on does s.types.entry(raw).or_insert(ty). Other list_x tests make lists on allocators that are never adopted, so forget never runs for them and their type stays in types under that address.
  • When a_title gets such an address, its type stays the old one. The title Reset then makes the backing allocator with that type (DIRECT), and the COMPUTE list cannot be reset on it.
  • Logged in 20 more runs: the recorded type was 0 (DIRECT) in the 4 that failed and 2 (COMPUTE) in the 16 that passed. Tests that left the entry: e.g. cross_queue_wait64_before_signal_does_not_deadlock_submit_thread, cross_queue_cycle_and_same_queue_fifo_do_not_deadlock.
  • I did not find a way to reach it in a title, where allocators are adopted and forget removes the entry. I only checked that by reading.

Fix I tried

  • In created_on, s.types.insert(raw, ty); instead of or_insert: a list that was just made on the allocator has the allocator's type, so the newer value is the right one.
  • With it: list_x::tests --test-threads=8 0 of 60 runs failed, dispatch_chain list_x::tests alloc_pool --test-threads=8 0 of 60.

@Decodere, could you take that line (or forget the address in the test) and rerun your smoke for the new head? main at 06055a7 with this PR, #2097, #2368, #2408 and #2383: whole suite 1422 passed, 2 failed, 46 ignored (fuzz_validate_and_gpu_1 and _3, the same on main here); with XCL_DIRECT_DXIL=1, gpu:: hle::d3d12x 952 passed, the same 2 failed. I will merge it once the test is stable. #2404 and #2372 touch the same code and then rebase over it.

Not tested: a title run with this PR; the two fixed cases that the tests do not reach.

AI use: Claude Code (Claude Opus 5.5) read the thread and the diff, ran the tests, found the cause and wrote this on the maintainer's behalf.

…SOs in dispatch_chain

Fix four lifetime/bookkeeping issues around `dispatch_chain` and `alloc_pool`:

1. `dispatch_chain::resolve_allocator` previously returned `0` when
   `alloc_pool::recording_backing(list)` was `None` (untracked / direct PC
   list or `XCL_ALLOC_SWAP=0`), dropping scratch and retained-PSO cleanup
   for that list. Fall back to `caller_alloc` (keeping `resolve_allocator`
   non-zero when `caller_alloc != 0`).
2. `dispatch_chain::dispatch_whole` previously dropped the resolved whole
   `ID3D12PipelineState` immediately after `SetPipelineState` instead of
   retaining it against the backing allocator until reset/release, risking
   use-after-free while GPU execution is in flight. Retain it on
   `resolve_allocator(list, caller_alloc)`.
3. `list_x::try_split_for_wait` (`None` spare branch) created a fresh
   mid-list Wait split segment on `pc_alloc = alloc_pool::current(alloc)`
   without calling `alloc_pool::created_on(p as usize, pc_alloc, ty.0)`
   (or `real::dispatch_chain::list_reset`), leaving `recording_backing`
   as `None` after an allocator swap. Register the fresh segment on
   `pc_alloc` so `resolve_allocator` tracks the swapped backing allocator.
4. `alloc_pool::created_on` used `s.types.entry(raw).or_insert(ty)`
   instead of `s.types.insert(raw, ty)` (unlike `reset_list`), preserving
   stale allocator types when a non-adopted test allocator address was
   reused by a different command-list type. Overwrite with the newly
   created list's type (`s.types.insert(raw, ty)`).

Add unit tests covering `resolve_allocator` / whole-PSO retention and
fresh Wait-split segment tracking across an `alloc_pool` swap.

Part of xcl-dev#2080
Part of xcl-dev#1452

Assisted-by: Google Antigravity (Gemini 4 Neon)

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No source-level defect found in the reviewed paths. The untracked-list allocator fallback and retained whole-PSO lifetime both line up with the backing-list registration and allocator-reset release paths, and the tests cover the main fallback/dedup cases. The head is mergeable_state=dirty, so please rebase and refresh CI before merge.

@Decodere
Decodere force-pushed the fix-2080-dispatch-chain-allocator-lifetime branch from 02ba2f9 to a7b93a8 Compare November 25, 2026 20:16
@Decodere

Copy link
Copy Markdown
Contributor Author

Thanks for tracking that down! Updated alloc_pool::created_on to s.types.insert(raw, ty) (matching reset_list), rebased cleanly onto 6d12f58a (a7b93a85), verified 0/20 failures across 20 consecutive --test-threads=8 runs of dispatch_chain list_x::tests alloc_pool (57 passed, 0 failed each), full release workspace 1426 passed, 0 failed, and fresh paired 60 s RTX 4060 smoke (889ef3e8 1550 frames vs a7b93a85 1522 frames, 0 device removals/crashes). Ready to merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants