From bf200e1633d8ba5c7eb3865693b3b939f0537499 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:00:30 -0700 Subject: [PATCH 1/2] D3D12.X: a split program's failed whole build is not tried again `Chain::whole` kept the whole pipeline of a split compute program once it was built, and returned a failure without keeping it. So every later call that needs that form built it again: each `ExecuteIndirect` of the pipeline and each dispatch that gets no scratch, on the title's thread and under the chain's lock. The program is large by construction (splitting is for large programs), so each try can take the driver seconds or more and fail again. The outcome is kept in the `OnceLock`, a failure too. Its `get_or_init` lets one caller build and the others wait, so the separate lock goes. A failure is now final for the program, as a failed build is in `pso_async`. The `ExecuteIndirect` warning for it is capped as the `Dispatch` one is: it now comes at the call rate, not once a compile. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/bind.rs | 5 ++- .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 34 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 3f31c48b43..24c1c0360e 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -342,7 +342,10 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us Ok(Some(whole)) => list.SetPipelineState(&whole), Ok(None) => {} Err(e) => { - crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}"); + static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 4 { + crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}"); + } count(DISPATCHES_SKIPPED); return; } diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 6fa76b4cbf..09461f14f1 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -53,18 +53,12 @@ struct Chain { tail: Vec, words: u32, whole_code: Vec, - whole: OnceLock, - build_whole: Mutex<()>, + whole: OnceLock>, } impl Chain { unsafe fn whole(&self) -> Result { - if let Some(p) = self.whole.get() { return Ok(p.clone()); } - let _guard = self.build_whole.lock().unwrap(); - if let Some(p) = self.whole.get() { return Ok(p.clone()); } - let p = self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState)).ok_or("whole compute pipeline")?; - let _ = self.whole.set(p.clone()); - Ok(p) + self.whole.get_or_init(|| self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState))).clone().ok_or_else(|| "whole compute pipeline".into()) } } @@ -91,7 +85,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT let mut pieces = built.into_iter().collect::>>()?.into_iter(); let first = pieces.next()?; let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group, - whole_code: whole.to_vec(), whole: OnceLock::new(), build_whole: Mutex::new(()) }); + whole_code: whole.to_vec(), whole: OnceLock::new() }); PROGRAMS.fetch_add(1, Ordering::Relaxed); PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed); *result = first.into_raw(); @@ -235,7 +229,7 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, #[cfg(test)] mod tests { - use super::{scratch_bytes, LEAST}; + use super::*; const MB: u64 = 1 << 20; @@ -284,4 +278,24 @@ mod tests { assert_eq!(scratch_bytes(9 * MB, 9 * MB, 4 * MB, None, 400 * MB, 384 * MB), (9 * MB, true)); assert_eq!(scratch_bytes(9 * MB, 9 * MB, 4 * MB, Some(MB), 400 * MB, 384 * MB), (9 * MB, true)); } + + #[test] + fn a_whole_form_that_fails_to_build_is_not_built_again() { + static CALLS: AtomicU64 = AtomicU64::new(0); + unsafe extern "system" fn refused(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT { + CALLS.fetch_add(1, Ordering::Relaxed); + crate::util::E_FAIL + } + unsafe { + let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let signature = pso::dispatch_chain_root_signature(&dev, &[]).unwrap(); + let builder = Arc::new(PipelineBuilder { device: dev, create: refused, signature, flags: D3D12_PIPELINE_STATE_FLAG_NONE, node: 0, record: 0 }); + let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new() }; + assert!(chain.whole().is_err()); + assert!(chain.whole().is_err()); + assert_eq!(CALLS.load(Ordering::Relaxed), 1); + } + } } From 8bfaa12f1fdfcb7d4ca319ae5f365cedb7201a1c Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:28:18 -0700 Subject: [PATCH 2/2] D3D12.X: a split program's whole form that ran out of memory is tried once more A failed whole build is kept so it is not compiled again at every call that needs it. Out of memory can pass, though, and then a program that failed once under memory pressure never got its whole form for the rest of the run (luckyteagirl's review). Such a failure now waits 10 s and is tried once more; a second failure, and any other, stays final. Once, because a driver can also refuse a program with out of memory every time (`translated_compute_pso_once` records an AMD one, 15 to 23 s a try), and then each try would be seconds on the title's thread again. Co-Authored-By: Claude Opus 5.5 --- .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 09461f14f1..1b9e784fc8 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -39,12 +39,12 @@ struct PipelineBuilder { } impl PipelineBuilder { - unsafe fn pipeline(&self, dxil: &[u8], iid: &GUID) -> Option { + unsafe fn pipeline(&self, dxil: &[u8], iid: &GUID) -> Result { let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { NodeMask: self.node, Flags: self.flags, ..Default::default() }; let mut raw = std::ptr::null_mut(); let (hr, _, _) = compute_container(self.device.as_raw(), self.create, &desc, self.signature.as_raw(), dxil, self.record, iid, &mut raw, ComputeBuild::Chain); - (hr >= 0 && !raw.is_null()).then(|| ID3D12PipelineState::from_raw(raw)) + if hr >= 0 && !raw.is_null() { Ok(ID3D12PipelineState::from_raw(raw)) } else { Err(hr) } } } @@ -54,14 +54,36 @@ struct Chain { words: u32, whole_code: Vec, whole: OnceLock>, + /// after a whole build that ran out of memory: when it may be tried, once more + retry_at: Mutex>, } impl Chain { unsafe fn whole(&self) -> Result { - self.whole.get_or_init(|| self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState))).clone().ok_or_else(|| "whole compute pipeline".into()) + if let Some(p) = self.whole.get() { return p.clone().ok_or_else(|| "whole compute pipeline".into()); } + let mut retry_at = self.retry_at.lock().unwrap(); + if let Some(p) = self.whole.get() { return p.clone().ok_or_else(|| "whole compute pipeline".into()); } + if retry_at.is_some_and(|t| std::time::Instant::now() < t) { return Err("whole compute pipeline: out of memory".into()); } + match self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState)) { + Ok(p) => { + let _ = self.whole.set(Some(p.clone())); + Ok(p) + } + Err(hr) if hr == crate::util::E_OUTOFMEMORY && retry_at.is_none() => { + *retry_at = Some(std::time::Instant::now() + WHOLE_RETRY); + Err("whole compute pipeline: out of memory".into()) + } + Err(_) => { + let _ = self.whole.set(None); + Err("whole compute pipeline".into()) + } + } } } +/// How long a split program's whole form waits after a build that ran out of memory, before its last try +const WHOLE_RETRY: std::time::Duration = std::time::Duration::from_secs(10); + static CHAINS: LazyLock>>> = LazyLock::new(Default::default); pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUTE_PIPELINE_STATE_DESC, @@ -85,7 +107,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT let mut pieces = built.into_iter().collect::>>()?.into_iter(); let first = pieces.next()?; let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group, - whole_code: whole.to_vec(), whole: OnceLock::new() }); + whole_code: whole.to_vec(), whole: OnceLock::new(), retry_at: Mutex::new(None) }); PROGRAMS.fetch_add(1, Ordering::Relaxed); PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed); *result = first.into_raw(); @@ -292,10 +314,36 @@ mod tests { }; let signature = pso::dispatch_chain_root_signature(&dev, &[]).unwrap(); let builder = Arc::new(PipelineBuilder { device: dev, create: refused, signature, flags: D3D12_PIPELINE_STATE_FLAG_NONE, node: 0, record: 0 }); - let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new() }; + let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new(), retry_at: Mutex::new(None) }; + assert!(chain.whole().is_err()); + assert!(chain.whole().is_err()); + assert_eq!(CALLS.load(Ordering::Relaxed), 1); + } + } + + #[test] + fn a_whole_form_that_ran_out_of_memory_is_tried_once_more_after_a_pause() { + static CALLS: AtomicU64 = AtomicU64::new(0); + unsafe extern "system" fn out_of_memory(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT { + CALLS.fetch_add(1, Ordering::Relaxed); + crate::util::E_OUTOFMEMORY + } + unsafe { + let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let signature = pso::dispatch_chain_root_signature(&dev, &[]).unwrap(); + let builder = Arc::new(PipelineBuilder { device: dev, create: out_of_memory, signature, flags: D3D12_PIPELINE_STATE_FLAG_NONE, node: 0, record: 0 }); + let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new(), retry_at: Mutex::new(None) }; assert!(chain.whole().is_err()); assert!(chain.whole().is_err()); assert_eq!(CALLS.load(Ordering::Relaxed), 1); + *chain.retry_at.lock().unwrap() = Some(std::time::Instant::now()); + assert!(chain.whole().is_err()); + assert_eq!(CALLS.load(Ordering::Relaxed), 2); + *chain.retry_at.lock().unwrap() = Some(std::time::Instant::now()); + assert!(chain.whole().is_err()); + assert_eq!(CALLS.load(Ordering::Relaxed), 2); } } }