From 468f8dfb3d45f99ad532fd1d5d3edf6cf7e4d97a Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:18:31 +0000 Subject: [PATCH] D3D12.X: a reused spare keeps the allocator of deferred work still on the GPU Since #2118 a dispatch whose pipeline is still being built is recorded at the submit on an allocator kept in the ListState of the recording's first segment (own_alloc), which holds its only reference. When that first segment is a stand-in (#1452), the next Reset puts it among the spares, and a later split reuses it as soon as its own submit is done: split removed and dropped its ListState, releasing the allocator without the in-flight guard on_release has. With a Wait64BitValueX between the first segment and the deferred dispatch, the two are submitted in blocks with their own fence values, so the deferred segment can still be on the GPU then, recorded on a destroyed allocator. split now keeps own_alloc of the reused spare's old state, moved into its new one, only while alloc_pool::allocator_in_flight says the GPU may still run work recorded on it. Kept, the allocator stays with its PC list as it did while the list was a spare, and on_release keeps it while it is in flight. Once the GPU is done with it, it goes with the old state, released outside the LISTS lock as before this change. Moving it unconditionally would leave it (and the scratch of its split dispatches, dispatch_chain::RECORDINGS) charged against XCL_SPLIT_SCRATCH_TOTAL_MB: the list is reset from then on only through alloc_pool::reset_list (split, deferred_segment, record_deferred), not reset_pc, so reset_own_alloc does not run for it. Tests a_spare_reused_by_a_split_keeps_an_own_allocator_still_on_the_gpu and a_spare_reused_by_a_split_drops_an_own_allocator_the_gpu_is_done_with. Without the change the first fails (reference count 2, expected 3: the allocator is released while the GPU may still run work recorded on it); with the allocator moved unconditionally the second fails (the reused spare's state keeps an allocator the GPU is done with). With the change both pass, 3 of 3 runs. cargo build --release -p xcl and cargo test --workspace --no-fail-fast -- --test-threads=4, twice: 1421 passed, 4 failed, 46 ignored, 25 SKIPPED both times; the 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (E_OUTOFMEMORY), failing on this machine without this change too. Co-Authored-By: Claude Fable 5.2 Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 129 +++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..8b80def542 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2835,7 +2835,10 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ let old_next = { let mut g = LISTS.lock().unwrap(); let m = g.get_or_insert_with(HashMap::new); - let old = m.remove(&(next_pc as usize)); + let mut old = m.remove(&(next_pc as usize)); + // (a spare that was a first segment keeps the allocator its deferred segments recorded on while the GPU may still + // run them; once it is done the allocator goes with the old state, and its split dispatches' scratch with it) + let own_alloc = old.as_mut().and_then(|o| o.own_alloc.take_if(|a| super::alloc_pool::allocator_in_flight(a.as_raw() as usize))); if let Some(ref o) = old { if !o.pending_waits.is_empty() { PENDING_WAIT_LISTS.fetch_sub(1, Ordering::AcqRel); @@ -2866,6 +2869,7 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ pred_in_state: false, predication, bindings: bindings.clone(), + own_alloc, ..Default::default() }, ); @@ -7079,6 +7083,129 @@ mod tests { } } + /// A recording's first segment stood in for its primary list and holds the allocator its + /// deferred dispatches recorded on. At the next Reset it goes to the spares; a split of a later + /// recording reuses it while those dispatches still run on the GPU (a block after a Wait64). + /// The allocator must outlive them. + #[test] + fn a_spare_reused_by_a_split_keeps_an_own_allocator_still_on_the_gpu() { + use super::super::{alloc_pool, bind, title_list}; + + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let a = alloc.as_raw() as usize; + let new_list = |on: &ID3D12CommandAllocator| { + let l = dev.CreateCommandList::<_, _, ID3D12GraphicsCommandList>(0, ty, on, None).unwrap().into_raw(); + real::adopt(l, real::Kind::CommandList); + on_create(l, on.as_raw() as usize, 0, ty); + l + }; + // the old first segment and the allocator of its deferred segments + let own: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let spare = new_list(&alloc); + assert_eq!(pc:: i32>(spare, PC_CLOSE)(spare), 0); + let primary = new_list(&alloc); + bind::reset(primary, a, 0); + let w = title_list::wrap(primary); + LISTS.lock().unwrap().as_mut().unwrap().get_mut(&(spare as usize)).unwrap().own_alloc = Some(own.clone()); + title_list::put_spare(w, spare); + + // a deferred segment recorded on it, held on the GPU behind a queue Wait + let deferred = new_list(&own); + assert_eq!(pc:: i32>(deferred, PC_CLOSE)(deferred), 0); + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(*(*(q.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + q.Wait(&gate, 1).unwrap(); + exec(q.as_raw(), 1, &deferred); + alloc_pool::executed(q.as_raw(), &[deferred]); + assert!(alloc_pool::allocator_in_flight(own.as_raw() as usize)); + + // a later recording splits and gets the spare (its own submit is done) + let next = split(primary, None); + assert_eq!(next, Some(spare), "the split reuses the spare"); + // (this test holds one reference; the spare's state held the other) + let refs = (Interface::vtable(&own).base__.base__.base__.base__.AddRef)(own.as_raw()); + (Interface::vtable(&own).base__.base__.base__.base__.Release)(own.as_raw()); + + gate.Signal(1).unwrap(); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("the queue finishes"); + let removed = dev.GetDeviceRemovedReason(); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(release(w), 0); + drop(ID3D12GraphicsCommandList::from_raw(deferred)); + + assert_eq!(refs, 3, "the allocator of work still on the GPU stays held"); + assert!(removed.is_ok()); + } + } + + /// As above, with the GPU done with the deferred segment before the split: the reused spare + /// keeps no allocator nothing records on any more (its split dispatches' scratch is freed with it). + #[test] + fn a_spare_reused_by_a_split_drops_an_own_allocator_the_gpu_is_done_with() { + use super::super::{alloc_pool, bind, title_list}; + + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + unsafe { + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let a = alloc.as_raw() as usize; + let new_list = |on: &ID3D12CommandAllocator| { + let l = dev.CreateCommandList::<_, _, ID3D12GraphicsCommandList>(0, ty, on, None).unwrap().into_raw(); + real::adopt(l, real::Kind::CommandList); + on_create(l, on.as_raw() as usize, 0, ty); + l + }; + let own: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let spare = new_list(&alloc); + assert_eq!(pc:: i32>(spare, PC_CLOSE)(spare), 0); + let primary = new_list(&alloc); + bind::reset(primary, a, 0); + let w = title_list::wrap(primary); + LISTS.lock().unwrap().as_mut().unwrap().get_mut(&(spare as usize)).unwrap().own_alloc = Some(own.clone()); + title_list::put_spare(w, spare); + + // the deferred segment recorded on it has run + let deferred = new_list(&own); + assert_eq!(pc:: i32>(deferred, PC_CLOSE)(deferred), 0); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) = + std::mem::transmute(*(*(q.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + exec(q.as_raw(), 1, &deferred); + alloc_pool::executed(q.as_raw(), &[deferred]); + q.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 5000).expect("the queue finishes"); + assert!(!alloc_pool::allocator_in_flight(own.as_raw() as usize)); + + let next = split(primary, None); + assert_eq!(next, Some(spare), "the split reuses the spare"); + // (only this test holds it now) + let refs = (Interface::vtable(&own).base__.base__.base__.base__.AddRef)(own.as_raw()); + (Interface::vtable(&own).base__.base__.base__.base__.Release)(own.as_raw()); + let held = LISTS.lock().unwrap().as_ref().unwrap().get(&(spare as usize)).is_some_and(|s| s.own_alloc.is_some()); + + let removed = dev.GetDeviceRemovedReason(); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(w as *const *const usize)).add(2)); + assert_eq!(release(w), 0); + drop(ID3D12GraphicsCommandList::from_raw(deferred)); + + assert!(!held, "the reused spare's state keeps no allocator the GPU is done with"); + assert_eq!(refs, 2, "the allocator the GPU is done with is released"); + assert!(removed.is_ok()); + } + } + /// Reproduces the exact intra-list fork-join pattern seen on retail compute queues: /// q_compute list O_c: Write64(cell_A, frame) -> Wait64(cell_B >= frame) -> Write64(cell_C, frame) /// q_direct list O_d: Wait64(cell_A >= frame) -> Write64(cell_B, frame)