From 1625b9df91f6bef6f2b98d27a492a9b8cdae59f8 Mon Sep 17 00:00:00 2001 From: KOLESNiii <102358205+KOLESNiii@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:43:31 +0000 Subject: [PATCH] Shaders: split compute pieces share one pool of piece builders `--pipelines` and startup shader preparation started a fresh scoped group of up to `XCL_PIPELINE_THREADS` helpers for every split compute program. All the groups wait at the same `running` gate, so 17 shader workers each on a four-piece program could hold 68 helpers for 4 driver slots. Each helper also reads its piece's driver blob from disk before the gate, so on a warm run every waiting helper holds a blob in memory (#2880). `precompile` now starts `XCL_PIPELINE_THREADS` piece builders once. A split program's worker queues its pieces and waits for them as before, so progress and the summary counters do not change. At most `XCL_PIPELINE_THREADS` pieces are past `make_one`'s blob read at a time. `make` outside `precompile` (the tests) keeps its own helpers. A queued piece counts as done when it is dropped, and the last builder to exit drops what is still queued, so a panic in every builder cannot leave a worker waiting for pieces nobody builds. Not measured yet: the cost of the old scheme on a split-heavy corpus is the open question in #2880. Part of #2880 Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/shaders.rs | 260 +++++++++++++++++++++++++++++++++++++- 1 file changed, 255 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 4c5a014974..35ab461376 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -9,8 +9,9 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Mutex; +use std::sync::{Arc, Condvar, Mutex}; +use crate::gpu::dxil::kernel::DispatchChain; use crate::gpu::{dxbc, pack, rdna2, recomp, shader, xbox}; /// `--iface`: how console programs connect to their DXBC signatures (hypothesis checks for @@ -1220,10 +1221,44 @@ struct Pipelines { /// biggest takes ~1.5 GB in the driver – fifteen at once took 24 GB running: (Mutex, std::sync::Condvar), most: usize, + /// pieces of split compute programs waiting for a piece builder (`build_pieces`) + pieces: (Mutex, Condvar), /// DXIL bytes a pipeline is still handed to the driver (`XCL_PIPELINE_MAX_KB`, 8 MiB; 0: off) max_dxil: usize, #[cfg(test)] cache_dir: Option, + /// tests: a piece builder panics instead of building its piece + #[cfg(test)] + panic_pieces: bool, +} + +/// A piece of a split compute program, queued for a piece builder (`Pipelines::build_pieces`). +struct PieceJob { + chain: Arc, + samplers: Arc<[recomp::pso::StaticSampler]>, + piece: usize, + /// pieces of the program not yet built: its worker waits for 0 + left: Arc<(Mutex, Condvar)>, +} + +// a job counts as done when it is dropped: built, its build panicked, or the last builder +// exited with it still queued, so the program's worker never waits for a piece nobody builds +impl Drop for PieceJob { + fn drop(&mut self) { + let mut n = self.left.0.lock().unwrap_or_else(|e| e.into_inner()); + *n -= 1; + if *n == 0 { + self.left.1.notify_all(); + } + } +} + +#[derive(Default)] +struct PieceQueue { + jobs: std::collections::VecDeque, + /// piece builders still running; 0: `make` builds the pieces itself + builders: usize, + closed: bool, } /// `XCL_PIPELINE_MAX_KB`: the KiB of DXIL `--pipelines` still compiles (default 8 MiB; 0: no @@ -1408,6 +1443,124 @@ mod pipeline_cap_tests { assert_eq!(pipelines.reused.load(Ordering::Relaxed), reused_before + 1); } } + + #[test] + fn split_programs_share_the_piece_builders() { + use super::{Pipelines, recomp, Ordering}; + use crate::hle::d3d12x::real::driver_cache; + use std::sync::Arc; + use std::time::{SystemTime, UNIX_EPOCH}; + + struct Scratch(std::path::PathBuf); + impl Drop for Scratch { + fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } + } + if let Err(reason) = recomp::dxc::location() { + recomp::runner::skip_test(&format!("no DXC: {reason}")); + return; + } + let mut pipelines = match Pipelines::new() { + Ok(pipelines) => pipelines, + Err(reason) => { + recomp::runner::skip_test(&format!("no D3D12 device: {reason}")); + return; + } + }; + let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos(); + let scratch = Scratch(std::env::temp_dir().join(format!("xcl-piece-queue-{}-{nonce}", std::process::id()))); + std::fs::create_dir(&scratch.0).expect("scratch cache directory"); + pipelines.cache_dir = Some(scratch.0.clone()); + pipelines.most = 2; + + // four split programs of three pieces each, every piece a different DXIL + let programs: Vec = (0..4u32) + .map(|p| { + let pieces = (0..3u32) + .map(|i| { + let hlsl = format!( + "RWByteAddressBuffer mem : register(u2); RWByteAddressBuffer sc : register(u0, space2); \ + cbuffer ch : register(b0, space2) {{ uint4 meta; }}; \ + [numthreads(1,1,1)] void main() {{ sc.Store(0, meta.w + {i}); mem.Store({}, meta.x + {p}); }}", + i * 4 + ); + recomp::dxc::compile(&hlsl, "main", "cs_6_6", &[]).expect("piece DXC compile") + }) + .collect(); + recomp::pso::Compiled { + dispatch_chain: Some(Arc::new(crate::gpu::dxil::kernel::DispatchChain { pieces, dwords_per_group: 4 })), + dxil: Vec::new(), + samplers: vec![], + } + }) + .collect(); + + let (p, dir) = (&pipelines, &scratch.0); + std::thread::scope(|s| { + p.open_pieces(p.most); + for _ in 0..p.most { + s.spawn(|| p.build_pieces()); + } + let workers: Vec<_> = programs.iter().map(|c| s.spawn(move || { + p.make(c); + // `make` returns once every piece of its program is built and cached + for piece in &c.dispatch_chain.as_ref().unwrap().pieces { + let path = dir.join(driver_cache::name_for(piece, 0, true)); + assert!(path.is_file() && path.metadata().unwrap().len() > 0, "piece .chain.pso missing when make returned"); + } + })).collect(); + // the pool is closed before a failed worker's panic goes on, or the scope never ends + let joined: Vec<_> = workers.into_iter().map(|w| w.join()).collect(); + let drained = p.pieces.0.lock().unwrap().jobs.is_empty(); + p.close_pieces(); + for r in joined { + if let Err(panic) = r { std::panic::resume_unwind(panic); } + } + assert!(drained); + }); + assert_eq!(p.split_programs.load(Ordering::Relaxed), 4); + assert_eq!(p.split_pieces.load(Ordering::Relaxed), 12); + assert_eq!(p.made.load(Ordering::Relaxed), 12); + assert_eq!(p.refused.load(Ordering::Relaxed), 0); + assert_eq!(*p.running.0.lock().unwrap(), 0); + } + + #[test] + fn split_program_returns_when_every_piece_builder_panics() { + use super::{Pipelines, recomp}; + use std::sync::Arc; + use std::time::Duration; + + let mut pipelines = match Pipelines::new() { + Ok(pipelines) => pipelines, + Err(reason) => { + recomp::runner::skip_test(&format!("no D3D12 device: {reason}")); + return; + } + }; + pipelines.most = 2; + pipelines.panic_pieces = true; + // plain threads, not a scope, so a hang fails the test instead of blocking it + let p: &'static Pipelines = Box::leak(Box::new(pipelines)); + p.open_pieces(p.most); + let builders: Vec<_> = (0..p.most).map(|_| std::thread::spawn(|| p.build_pieces())).collect(); + // three pieces for two builders: one is still queued when both have panicked + let program = recomp::pso::Compiled { + dispatch_chain: Some(Arc::new(crate::gpu::dxil::kernel::DispatchChain { pieces: vec![vec![0u8; 4]; 3], dwords_per_group: 4 })), + dxil: Vec::new(), + samplers: vec![], + }; + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + p.make(&program); + let _ = tx.send(()); + }); + assert!(rx.recv_timeout(Duration::from_secs(30)).is_ok(), "make still waits for pieces after every builder panicked"); + for b in builders { + assert!(b.join().is_err(), "the builder should have panicked"); + } + let q = p.pieces.0.lock().unwrap(); + assert!(q.jobs.is_empty() && q.builders == 0); + } } // D3D12 devices and root signatures are free-threaded @@ -1443,9 +1596,12 @@ impl Pipelines { capped_bytes: AtomicUsize::new(0), running: (Mutex::new(0), std::sync::Condvar::new()), most, + pieces: Default::default(), max_dxil, #[cfg(test)] cache_dir: None, + #[cfg(test)] + panic_pieces: false, }) } } @@ -1460,7 +1616,7 @@ impl Pipelines { fn make_with_cache_path(&self, c: &recomp::pso::Compiled, path_override: Option) { if path_override.is_none() { - if let Some(chain) = c.dispatch_chain.as_deref().filter(|chain| chain.pieces.len() >= 2) { + if let Some(chain) = c.dispatch_chain.as_ref().filter(|chain| chain.pieces.len() >= 2) { self.split_programs.fetch_add(1, Ordering::Relaxed); self.split_pieces.fetch_add(chain.pieces.len(), Ordering::Relaxed); let workers = chain.pieces.len().min(self.most); @@ -1468,7 +1624,8 @@ impl Pipelines { for piece in &chain.pieces { self.make_one(piece, &c.samplers, true, None); } - } else { + } else if !self.queue_pieces(chain, &c.samplers) { + // no piece builders (not called from `precompile`): helpers of its own let next = AtomicUsize::new(0); std::thread::scope(|s| { for _ in 0..workers { @@ -1486,6 +1643,86 @@ impl Pipelines { self.make_one(&c.dxil, &c.samplers, false, path_override); } + /// The pieces of `chain` built by the piece builders (`build_pieces`), waiting until all are + /// built. False, with nothing queued, when no builders run: the caller builds them itself. + fn queue_pieces(&self, chain: &Arc, samplers: &[recomp::pso::StaticSampler]) -> bool { + let left = Arc::new((Mutex::new(chain.pieces.len()), Condvar::new())); + { + let mut q = self.pieces.0.lock().unwrap(); + if q.builders == 0 || q.closed { + return false; + } + let samplers: Arc<[recomp::pso::StaticSampler]> = samplers.into(); + for piece in 0..chain.pieces.len() { + q.jobs.push_back(PieceJob { chain: chain.clone(), samplers: samplers.clone(), piece, left: left.clone() }); + } + } + self.pieces.1.notify_all(); + let mut n = left.0.lock().unwrap(); + while *n != 0 { + n = left.1.wait(n).unwrap(); + } + true + } + + /// A piece builder: builds queued pieces of split compute programs until `close_pieces`. + /// `precompile` starts `most` of them once, so all split programs share the same threads, and + /// at most `most` pieces have loaded their driver blob at a time, instead of up to `most` + /// helpers per program, each with its blob, waiting at `running` (#2880). + fn build_pieces(&self) { + // when the last builder exits (a panic, or the queue closed and empty), what is still + // queued is dropped, which releases the programs' workers; later programs build their + // pieces themselves, and the panic reaches `precompile` when its scope ends + struct Exit<'a>(&'a Pipelines); + impl Drop for Exit<'_> { + fn drop(&mut self) { + let mut q = self.0.pieces.0.lock().unwrap_or_else(|e| e.into_inner()); + q.builders = q.builders.saturating_sub(1); + if q.builders == 0 { + let orphans = std::mem::take(&mut q.jobs); + drop(q); + drop(orphans); + } + } + } + let _exit = Exit(self); + loop { + let job = { + let mut q = self.pieces.0.lock().unwrap(); + loop { + if let Some(job) = q.jobs.pop_front() { + break job; + } + if q.closed { + return; + } + q = self.pieces.1.wait(q).unwrap(); + } + }; + #[cfg(test)] + if self.panic_pieces { + panic!("test: piece build fails"); + } + // dropping `job` counts the piece as done, also when the build panics + self.make_one(&job.chain.pieces[job.piece], &job.samplers, true, None); + } + } + + /// `builders` piece builders are about to start (`build_pieces`). + fn open_pieces(&self, builders: usize) { + let mut q = self.pieces.0.lock().unwrap(); + q.builders = builders; + q.closed = false; + } + + /// No more pieces come: the builders finish the queue and return. + fn close_pieces(&self) { + let mut q = self.pieces.0.lock().unwrap(); + q.closed = true; + drop(q); + self.pieces.1.notify_all(); + } + fn make_one(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], chained: bool, path_override: Option) { use windows::Win32::Graphics::Direct3D12::*; let key: (bool, Vec) = (chained, samplers.iter().flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])).collect()); @@ -1690,7 +1927,14 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if split { ", split wave64 for a GPU with 32-lane waves" } else { "" } ); std::thread::scope(|s| { - for _ in 0..threads { + // the pieces of split compute programs: `most` builders for all of them (#2880) + if let Some(p) = pipelines.filter(|p| p.most > 1) { + p.open_pieces(p.most); + for _ in 0..p.most { + s.spawn(|| p.build_pieces()); + } + } + let workers: Vec<_> = (0..threads).map(|_| { s.spawn(|| loop { if startup.is_some_and(|p| p.cancel.load(Ordering::Relaxed)) { break; } let i = next.fetch_add(1, Ordering::Relaxed); @@ -1797,7 +2041,13 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on } } record_done(); - }); + }) + }).collect(); + // a worker returns once its pieces are built: when all have, nothing is queued + let joined: Vec<_> = workers.into_iter().map(|w| w.join()).collect(); + if let Some(p) = pipelines { p.close_pieces(); } + for r in joined { + if let Err(panic) = r { std::panic::resume_unwind(panic); } } }); let failed = failed.into_inner();