diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 36e127919d..8054d379d9 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -629,7 +629,7 @@ pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result< profile: profile.into(), extra: extra.iter().map(|s| s.to_string()).collect(), reply, - wanted: crate::hle::d3d12x::pso_async::wanted_here(), + wanted: demand(), queue_stall: crate::stall::span(crate::stall::StallKind::DxcQueue), }; pool().push(job).map_err(|_| "DXC: no compile threads".to_string())?; @@ -957,16 +957,92 @@ pub fn compile_cached_in(dir: &Path, src: &str, entry: &str, profile: &str) -> R compile_cached_in_with(dir, src, entry, profile, || compile(src, entry, profile, &[])) } +/// One compile several callers need (a translated shader, `pso::shader_flight`; a DXC source, +/// `cache_flight`): the first to `enter` does it, the others wait for it. The compile is wanted +/// while any of them is (`demand`): a pipeline the title waits for can share the compile of a +/// background one that started first, which used to be held back as unwanted. +#[derive(Default)] +pub struct Flight { + lock: Mutex<()>, + /// the demand of each caller waiting to enter (`demand`), by `WAITER` + waiting: Mutex>, +} + +static WAITER: AtomicU64 = AtomicU64::new(0); + +thread_local! { + /// the flights this thread is in: their waiters wait for what it compiles + static ENTERED: RefCell>> = const { RefCell::new(Vec::new()) }; +} + +/// This thread is in the flight until it drops this. +pub struct InFlight<'a> { + flight: &'a Arc, + _lock: std::sync::MutexGuard<'a, ()>, +} + +impl Drop for InFlight<'_> { + fn drop(&mut self) { + ENTERED.with(|e| { + let mut e = e.borrow_mut(); + if let Some(at) = e.iter().rposition(|f| Arc::ptr_eq(f, self.flight)) { + e.remove(at); + } + }); + } +} + +impl Flight { + /// Wait for the caller in it, if any, and go in; while this caller waits, its `demand` is + /// the flight's too. + pub fn enter(self: &Arc) -> InFlight<'_> { + let lock = match self.lock.try_lock() { + Ok(lock) => lock, + Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(), + Err(std::sync::TryLockError::WouldBlock) => { + let id = WAITER.fetch_add(1, Ordering::Relaxed); + self.waiting.lock().unwrap().push((id, demand())); + let lock = self.lock.lock().unwrap_or_else(|e| e.into_inner()); + self.waiting.lock().unwrap().retain(|(w, _)| *w != id); + lock + } + }; + ENTERED.with(|e| e.borrow_mut().push(self.clone())); + InFlight { flight: self, _lock: lock } + } + + fn wanted(&self) -> bool { + self.waiting.lock().unwrap().iter().any(|(_, wanted)| wanted()) + } + + #[cfg(test)] + pub(crate) fn waiters(&self) -> usize { + self.waiting.lock().unwrap().len() + } +} + +/// Whether one of the title's threads waits for what this thread compiles, to be asked later and +/// from another thread: `pso_async::wanted_here`, or one of the callers waiting for a flight this +/// thread is in wants it (`Flight`). +pub fn demand() -> Wanted { + let own = crate::hle::d3d12x::pso_async::wanted_here(); + let entered = ENTERED.with(|e| e.borrow().clone()); + if entered.is_empty() { + return own; + } + Box::new(move || own() || entered.iter().any(|f| f.wanted())) +} + type CacheFlight = (PathBuf, u64); -fn cache_flight(key: CacheFlight) -> Arc> { - static FLIGHTS: OnceLock>>>> = OnceLock::new(); +fn cache_flight(key: CacheFlight) -> Arc { + static FLIGHTS: OnceLock>>> = OnceLock::new(); let mut flights = FLIGHTS.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); flights.retain(|_, v| v.strong_count() != 0); if let Some(flight) = flights.get(&key).and_then(Weak::upgrade) { return flight; } - let flight = Arc::new(Mutex::new(())); + let flight = Arc::new(Flight::default()); flights.insert(key, Arc::downgrade(&flight)); flight } @@ -1179,7 +1255,7 @@ where } counters.misses.fetch_add(1, Ordering::Relaxed); let flight = cache_flight((dir.clone(), key)); - let _guard = flight.lock().unwrap_or_else(|e| e.into_inner()); + let _guard = flight.enter(); let _process_guard = process_cache_lock(&file)?; // Another caller may have populated the cache while this one waited. if let Some(b) = cached_file(&file) { @@ -1566,6 +1642,65 @@ mod cache_tests { let _ = std::fs::remove_dir_all(&dir); } + /// Two builds of different translated shaders reach the same DXC source, a background one + /// first: it is compiled once, and the compile is wanted from the moment the title waits for + /// the second build. + #[test] + fn a_waiter_for_the_same_source_makes_its_compile_wanted() { + use crate::hle::d3d12x::pso_async; + use std::time::Instant; + if compile_threads() < 2 { + eprintln!("SKIPPED a_waiter_for_the_same_source_makes_its_compile_wanted: one PSO worker"); + return; + } + let dir = std::env::temp_dir().join(format!("xcl-dxil-demand-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let counters: &'static CacheCounters = Box::leak(Box::default()); + let (owner, joiner) = (0x7E57_2611usize, 0x7E57_2612usize); + let (demand_tx, demand_rx) = std::sync::mpsc::channel(); + let (go, finish) = std::sync::mpsc::channel::<()>(); + { + let dir = dir.clone(); + pso_async::submit(owner, true, Box::new(move || { + let r = compile_cached_in_observed(&dir, "converging source", "main", "cs_6_6", counters, || { + demand_tx.send(demand()).unwrap(); + let _ = finish.recv_timeout(Duration::from_secs(10)); + Ok(synthetic_dxil(6)) + }); + if r.is_ok() { 0x2611 } else { 0 } + })); + } + let wanted = demand_rx.recv_timeout(Duration::from_secs(10)).expect("the background build compiles"); + assert!(!wanted(), "nobody waits for the background build"); + { + let dir = dir.clone(); + pso_async::submit(joiner, true, Box::new(move || { + let r = compile_cached_in_observed(&dir, "converging source", "main", "cs_6_6", counters, || Ok(synthetic_dxil(7))); + if r.is_ok_and(|(b, hit)| hit && b == synthetic_dxil(6)) { 0x2612 } else { 0 } + })); + } + let flight = cache_flight((normalized_cache_dir(&dir), cache_key("converging source", "main", "cs_6_6"))); + let t = Instant::now(); + while flight.waiters() == 0 && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!(flight.waiters(), 1, "the second build waits for the compile"); + assert!(!wanted(), "nobody waits for either build"); + pso_async::need_by_submit(joiner); + let urgent = wanted(); + go.send(()).unwrap(); + assert!(urgent, "the title waits for the second build: the shared compile is wanted"); + let t = Instant::now(); + while (pso_async::is_pending(owner) || pso_async::is_pending(joiner)) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!((pso_async::resolve(owner), pso_async::resolve(joiner)), (0x2611, 0x2612), "both built, the second from the first's compile"); + assert_eq!(counters.snapshot().compilations, 1); + assert_eq!(flight.waiters(), 0); + let _ = (pso_async::released(owner), pso_async::released(joiner)); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn different_cache_keys_do_not_block_each_other() { let dir = std::env::temp_dir().join(format!("xcl-dxil-parallel-{}", std::process::id())); diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 24d12ecb04..f4bc42852f 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -988,14 +988,14 @@ impl ShaderCache { /// Per-`ShaderKey` in-flight mutex so concurrent PSO workers requesting the same shader coalesce /// into a single translation/compilation instead of translating the same program N times in parallel. -fn shader_flight(key: ShaderKey) -> Arc> { - static FLIGHTS: OnceLock>>>> = OnceLock::new(); +fn shader_flight(key: ShaderKey) -> Arc { + static FLIGHTS: OnceLock>>> = OnceLock::new(); let mut flights = FLIGHTS.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); flights.retain(|_, v| v.strong_count() != 0); if let Some(flight) = flights.get(&key).and_then(Weak::upgrade) { return flight; } - let flight = Arc::new(Mutex::new(())); + let flight = Arc::new(dxc::Flight::default()); flights.insert(key, Arc::downgrade(&flight)); flight } @@ -1021,7 +1021,7 @@ fn cached_shader( return Ok(shader); } let flight = shader_flight(key.clone()); - let _guard = flight.lock().unwrap_or_else(|e| e.into_inner()); + let _guard = flight.enter(); if let Some(shader) = cache.lock().unwrap().get(&key, blob) { dxc::note_memory_hit(); return Ok(shader); @@ -2509,6 +2509,69 @@ mod shader_cache_tests { }); } + /// A background build compiles a shader and a pipeline the title waits for needs the same + /// one: it is compiled once, and the compile is wanted from the moment the title waits for + /// the second pipeline – what a DXC job of the first build asks while it is held back. + #[test] + fn a_waiter_for_a_shared_shader_makes_its_compile_wanted() { + use crate::hle::d3d12x::pso_async; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::time::{Duration, Instant}; + if dxc::compile_threads() < 2 { + eprintln!("SKIPPED a_waiter_for_a_shared_shader_makes_its_compile_wanted: one PSO worker"); + return; + } + let cache = Arc::new(Mutex::new(ShaderCache::new(1 << 20))); + let shared = key(0x2608_0001); + let compiles = Arc::new(AtomicUsize::new(0)); + let (owner, joiner) = (0x7E57_2601usize, 0x7E57_2602usize); + let (demand_tx, demand_rx) = std::sync::mpsc::channel(); + let (go, finish) = std::sync::mpsc::channel::<()>(); + { + let (cache, shared, compiles) = (cache.clone(), shared.clone(), compiles.clone()); + pso_async::submit(owner, true, Box::new(move || { + let r = cached_shader(&cache, shared, b"shared", || { + compiles.fetch_add(1, Ordering::SeqCst); + demand_tx.send(dxc::demand()).unwrap(); + let _ = finish.recv_timeout(Duration::from_secs(10)); + Ok(compiled(8)) + }); + if r.is_ok() { 0x2601 } else { 0 } + })); + } + let wanted = demand_rx.recv_timeout(Duration::from_secs(10)).expect("the background build compiles"); + assert!(!wanted(), "nobody waits for the background build"); + { + let (cache, shared, compiles) = (cache.clone(), shared.clone(), compiles.clone()); + pso_async::submit(joiner, true, Box::new(move || { + let r = cached_shader(&cache, shared, b"shared", || { + compiles.fetch_add(1, Ordering::SeqCst); + Ok(compiled(9)) + }); + if r.is_ok_and(|s| s.dxil == synthetic_dxil(8)) { 0x2602 } else { 0 } + })); + } + let flight = shader_flight(shared); + let t = Instant::now(); + while flight.waiters() == 0 && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!(flight.waiters(), 1, "the second build waits for the compile"); + assert!(!wanted(), "nobody waits for either build"); + pso_async::need_by_submit(joiner); + let urgent = wanted(); + go.send(()).unwrap(); + assert!(urgent, "the title waits for the second build: the shared compile is wanted"); + let t = Instant::now(); + while (pso_async::is_pending(owner) || pso_async::is_pending(joiner)) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!((pso_async::resolve(owner), pso_async::resolve(joiner)), (0x2601, 0x2602), "both built, the second from the first's compile"); + assert_eq!(compiles.load(Ordering::SeqCst), 1); + assert_eq!(flight.waiters(), 0); + let _ = (pso_async::released(owner), pso_async::released(joiner)); + } + #[test] fn shader_cache_does_not_serialize_different_compiles() { let cache = Mutex::new(ShaderCache::new(1 << 20));