diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 574f582d86..3f2bad0d79 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4385,7 +4385,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL let key = compute_blob_key(dxil, rs as usize, copy.Flags.0); let mut cached = compute_blob(key); // … and one built in an earlier run gets the blob kept on disk then (`driver_cache`) - let disk = driver_cache::path(dxil, copy.Flags.0).map(|p| if chained { p.with_extension("chain.pso") } else { p }); + let disk = driver_cache::path_for(dxil, copy.Flags.0, chained); let from_disk = cached.is_none(); if from_disk { cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); @@ -4452,7 +4452,14 @@ pub(crate) mod driver_cache { /// The file of the pipeline made from `dxil` with `flags`. pub fn name(dxil: &[u8], flags: i32) -> String { - format!("{:016x}-{:x}-{:x}.pso", crate::gpu::shader::fnv64(dxil), dxil.len(), flags) + name_for(dxil, flags, false) + } + + /// Like [`name`], using `.chain.pso` for pipelines built with the dispatch chain's root + /// signature (`dispatch_chain`). + pub fn name_for(dxil: &[u8], flags: i32, chained: bool) -> String { + let ext = if chained { "chain.pso" } else { "pso" }; + format!("{:016x}-{:x}-{:x}.{ext}", crate::gpu::shader::fnv64(dxil), dxil.len(), flags) } /// The file of a translated graphics pipeline keyed by its full state + shader digest. @@ -4466,7 +4473,11 @@ pub(crate) mod driver_cache { } pub fn path(dxil: &[u8], flags: i32) -> Option { - enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name(dxil, flags))) + path_for(dxil, flags, false) + } + + pub fn path_for(dxil: &[u8], flags: i32, chained: bool) -> Option { + enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name_for(dxil, flags, chained))) } pub fn graphics_path(hash: u64, bytes: usize) -> Option { @@ -4556,6 +4567,9 @@ pub(crate) mod driver_cache { assert_ne!(name(b"abc", 0), name(b"abc", 1)); // (the length is in the name: two programs of one hash and different lengths differ) assert!(name(&[0u8; 0x1234], 2).ends_with("-1234-2.pso")); + assert_eq!(name_for(b"abc", 0, false), name(b"abc", 0)); + assert_ne!(name_for(b"abc", 0, true), name_for(b"abc", 0, false)); + assert!(name_for(&[0u8; 0x1234], 2, true).ends_with("-1234-2.chain.pso")); } #[test] diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index ff9ac68b2e..33fdbdc1fd 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1124,10 +1124,12 @@ fn measure_pso_result(input: &Input, hash: u64, repeat: usize, windows: u32, spl /// (the > 8 MB HLSL programs DXC compiles at `-O0`, #1269). struct Pipelines { dev: windows::Win32::Graphics::Direct3D12::ID3D12Device, - signatures: Mutex, windows::Win32::Graphics::Direct3D12::ID3D12RootSignature>>, + signatures: Mutex), windows::Win32::Graphics::Direct3D12::ID3D12RootSignature>>, made: AtomicUsize, refused: AtomicUsize, reused: AtomicUsize, + split_programs: AtomicUsize, + split_pieces: AtomicUsize, /// programs over `max_dxil` with no blob yet, and their DXIL bytes capped: AtomicUsize, capped_bytes: AtomicUsize, @@ -1137,6 +1139,8 @@ struct Pipelines { most: usize, /// 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, } /// `XCL_PIPELINE_MAX_KB`: the KiB of DXIL `--pipelines` still compiles (default 8 MiB; 0: no @@ -1200,6 +1204,127 @@ mod pipeline_cap_tests { assert_eq!((capped, made, refused), (1, 0, 0)); assert!(!path.exists(), "rejected blob should not be retried next run"); } + + #[test] + fn split_compute_program_prebuilds_and_caches_chain_pieces() { + use super::{Pipelines, recomp, Ordering}; + use crate::hle::d3d12x::real::driver_cache; + use std::sync::Arc; + use std::time::{SystemTime, UNIX_EPOCH}; + use windows::Win32::Graphics::Direct3D12::*; + + 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-split-pso-{}-{nonce}", std::process::id()))); + std::fs::create_dir(&scratch.0).expect("scratch cache directory"); + pipelines.cache_dir = Some(scratch.0.clone()); + + // Each piece binds the dispatch-chain UAV/CBV (space2), which only `dispatch_chain_root_signature` + // declares: building them with the regular ABI root signature would be refused by D3D12. + let piece0 = recomp::dxc::compile( + "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 + 1); mem.Store(0, meta.x); }", + "main", + "cs_6_6", + &[], + ) + .expect("piece0 DXC compile"); + let piece1 = recomp::dxc::compile( + "RWByteAddressBuffer mem : register(u2); RWByteAddressBuffer sc : register(u0, space2); \ + cbuffer ch : register(b0, space2) { uint4 meta; }; \ + [numthreads(1,1,1)] void main() { uint v = sc.Load(0); mem.Store(4, v + meta.w + 2); }", + "main", + "cs_6_6", + &[], + ) + .expect("piece1 DXC compile"); + // Larger than both pieces so a cap between piece size and whole size proves each piece + // is capped individually rather than against the unsplit whole container. + let whole = recomp::dxc::compile( + "RWByteAddressBuffer mem : register(u2); \ + [numthreads(1,1,1)] void main() { \ + uint x = mem.Load(0); uint y = mem.Load(4); uint z = mem.Load(8); uint w = mem.Load(12); \ + mem.Store(0, x ^ y ^ z ^ w ^ 0x12345678u); mem.Store(4, (x + y) * (z + w) + 7u); \ + mem.Store(8, (x * 3u) ^ (y * 5u) ^ (z * 7u)); mem.Store(12, x + y + z + w); }", + "main", + "cs_6_6", + &[], + ) + .expect("whole DXC compile"); + let max_piece = piece0.len().max(piece1.len()); + let compiled = recomp::pso::Compiled { + dispatch_chain: Some(Arc::new(crate::gpu::dxil::kernel::DispatchChain { + pieces: vec![piece0.clone(), piece1.clone()], + dwords_per_group: 4, + })), + dxil: whole.clone(), + samplers: vec![], + }; + + pipelines.max_dxil = max_piece; + pipelines.make(&compiled); + let p0_path = scratch.0.join(driver_cache::name_for(&piece0, 0, true)); + let p1_path = scratch.0.join(driver_cache::name_for(&piece1, 0, true)); + let whole_path = scratch.0.join(driver_cache::name_for(&whole, 0, false)); + assert_eq!(pipelines.split_programs.load(Ordering::Relaxed), 1); + assert_eq!(pipelines.split_pieces.load(Ordering::Relaxed), 2); + assert_eq!(pipelines.made.load(Ordering::Relaxed), 2); + assert_eq!(pipelines.refused.load(Ordering::Relaxed), 0); + assert_eq!(pipelines.reused.load(Ordering::Relaxed), 0); + assert_eq!(pipelines.capped.load(Ordering::Relaxed), 0); + assert!(p0_path.is_file() && p0_path.metadata().unwrap().len() > 0, "piece 0 .chain.pso missing"); + assert!(p1_path.is_file() && p1_path.metadata().unwrap().len() > 0, "piece 1 .chain.pso missing"); + assert!(!whole_path.exists(), "unsplit whole .pso should not be written for a split compute program"); + + // Second pass reuses both cached piece blobs. + pipelines.make(&compiled); + assert_eq!(pipelines.reused.load(Ordering::Relaxed), 2); + assert_eq!(pipelines.made.load(Ordering::Relaxed), 4); + assert_eq!(pipelines.refused.load(Ordering::Relaxed), 0); + + // Runtime-side check: the dispatch-chain root signature accepts each cached .chain.pso blob. + let chain_rs = recomp::pso::dispatch_chain_root_signature(&pipelines.dev, &[]).expect("dispatch chain RS"); + for (dxil, path) in [(&piece0, &p0_path), (&piece1, &p1_path)] { + let blob = std::fs::read(path).expect("read cached .chain.pso"); + let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::ManuallyDrop::new(Some(chain_rs.clone())), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr().cast(), BytecodeLength: dxil.len() }, + CachedPSO: D3D12_CACHED_PIPELINE_STATE { pCachedBlob: blob.as_ptr().cast(), CachedBlobSizeInBytes: blob.len() }, + ..Default::default() + }; + let pso = unsafe { pipelines.dev.CreateComputePipelineState::(&desc) } + .expect("runtime dispatch_chain RS accepts offline .chain.pso"); + drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); + let got = unsafe { pso.GetCachedBlob() }.expect("cached blob"); + assert_eq!(unsafe { got.GetBufferSize() }, blob.len()); + } + + // A corrupt .chain.pso over the per-piece cap is removed and capped without rebuilding. + std::fs::write(&p1_path, b"corrupt chain blob").expect("corrupt piece1 blob"); + pipelines.max_dxil = piece1.len() - 1; + let reused_before = pipelines.reused.load(Ordering::Relaxed); + pipelines.make(&compiled); + assert!(!p1_path.exists(), "rejected over-cap .chain.pso should be removed"); + assert_eq!(pipelines.capped.load(Ordering::Relaxed), 1); + if piece0.len() <= pipelines.max_dxil { + assert_eq!(pipelines.reused.load(Ordering::Relaxed), reused_before + 1); + } + } } // D3D12 devices and root signatures are free-threaded @@ -1216,49 +1341,108 @@ impl Pipelines { let dev = crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?; let most = std::env::var("XCL_PIPELINE_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(4usize).max(1); let max_dxil = pipeline_cap_kb(std::env::var("XCL_PIPELINE_MAX_KB").ok().as_deref()).saturating_mul(1024); - Ok(Pipelines { dev, signatures: Mutex::new(Default::default()), made: AtomicUsize::new(0), refused: AtomicUsize::new(0), reused: AtomicUsize::new(0), capped: AtomicUsize::new(0), capped_bytes: AtomicUsize::new(0), running: (Mutex::new(0), std::sync::Condvar::new()), most, max_dxil }) + Ok(Pipelines { + dev, + signatures: Mutex::new(Default::default()), + made: AtomicUsize::new(0), + refused: AtomicUsize::new(0), + reused: AtomicUsize::new(0), + split_programs: AtomicUsize::new(0), + split_pieces: AtomicUsize::new(0), + capped: AtomicUsize::new(0), + capped_bytes: AtomicUsize::new(0), + running: (Mutex::new(0), std::sync::Condvar::new()), + most, + max_dxil, + #[cfg(test)] + cache_dir: None, + }) } } - /// The compute pipeline of `c` (made and released). + /// The compute pipeline of `c` (made and released). When direct translation split the program + /// into a dispatch chain of at least two pieces, each piece is built and cached under the + /// dispatch-chain root signature (`.chain.pso`) as the runtime does (`dispatch_chain::build`), + /// instead of the unsplit whole container. fn make(&self, c: &recomp::pso::Compiled) { self.make_with_cache_path(c, None); } 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) { + 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); + if workers <= 1 { + for piece in &chain.pieces { + self.make_one(piece, &c.samplers, true, None); + } + } else { + let next = AtomicUsize::new(0); + std::thread::scope(|s| { + for _ in 0..workers { + s.spawn(|| loop { + let i = next.fetch_add(1, Ordering::Relaxed); + let Some(piece) = chain.pieces.get(i) else { break }; + self.make_one(piece, &c.samplers, true, None); + }); + } + }); + } + return; + } + } + self.make_one(&c.dxil, &c.samplers, false, path_override); + } + + fn make_one(&self, dxil: &[u8], samplers: &[recomp::pso::StaticSampler], chained: bool, path_override: Option) { use windows::Win32::Graphics::Direct3D12::*; - let key: Vec = c.samplers.iter().flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])).collect(); + let key: (bool, Vec) = (chained, samplers.iter().flat_map(|(w, cmp)| w.iter().copied().chain([*cmp as u32])).collect()); let rs = { let mut g = self.signatures.lock().unwrap(); match g.get(&key) { Some(rs) => rs.clone(), - None => match recomp::pso::abi_root_signature_with(&self.dev, true, &c.samplers) { - Ok(rs) => { - g.insert(key, rs.clone()); - rs - } - Err(_) => { - self.refused.fetch_add(1, Ordering::Relaxed); - return; + None => { + let created = if chained { + recomp::pso::dispatch_chain_root_signature(&self.dev, samplers) + } else { + recomp::pso::abi_root_signature_with(&self.dev, true, samplers) + }; + match created { + Ok(rs) => { + g.insert(key, rs.clone()); + rs + } + Err(_) => { + self.refused.fetch_add(1, Ordering::Relaxed); + return; + } } - }, + } } }; let mut desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { pRootSignature: std::mem::ManuallyDrop::new(Some(rs)), - CS: D3D12_SHADER_BYTECODE { pShaderBytecode: c.dxil.as_ptr() as *const _, BytecodeLength: c.dxil.len() }, + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr() as *const _, BytecodeLength: dxil.len() }, ..Default::default() }; use crate::hle::d3d12x::real::driver_cache; - let path = path_override.or_else(|| driver_cache::path(&c.dxil, desc.Flags.0)); + let path = path_override.or_else(|| { + #[cfg(test)] + if let Some(dir) = self.cache_dir.as_deref() { + return Some(dir.join(driver_cache::name_for(dxil, desc.Flags.0, chained))); + } + driver_cache::path_for(dxil, desc.Flags.0, chained) + }); let cached = path.as_deref().and_then(driver_cache::load); - let over_cap = self.max_dxil != 0 && c.dxil.len() > self.max_dxil; + let over_cap = self.max_dxil != 0 && dxil.len() > self.max_dxil; // over the cap and no blob from an earlier run: the driver's compile of this one takes // tens of minutes and gigabytes; the pass finishes without it, and the runtime builds it // when a title needs the pipeline. `XCL_PIPELINE_MAX_KB` 0 turns the cap off. if cached.is_none() && over_cap { self.capped.fetch_add(1, Ordering::Relaxed); - self.capped_bytes.fetch_add(c.dxil.len(), Ordering::Relaxed); + self.capped_bytes.fetch_add(dxil.len(), Ordering::Relaxed); drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return; } @@ -1286,7 +1470,7 @@ impl Pipelines { *self.running.0.lock().unwrap() -= 1; self.running.1.notify_one(); self.capped.fetch_add(1, Ordering::Relaxed); - self.capped_bytes.fetch_add(c.dxil.len(), Ordering::Relaxed); + self.capped_bytes.fetch_add(dxil.len(), Ordering::Relaxed); drop(std::mem::ManuallyDrop::into_inner(desc.pRootSignature)); return; } @@ -1464,6 +1648,10 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if let Some(p) = pipelines { println!("pipelines made on the GPU {}, refused {}", p.made.load(Ordering::Relaxed), p.refused.load(Ordering::Relaxed)); println!("pipelines loaded from the driver cache {}", p.reused.load(Ordering::Relaxed)); + let split_programs = p.split_programs.load(Ordering::Relaxed); + if split_programs != 0 { + println!("split compute programs: {} ({} pieces)", split_programs, p.split_pieces.load(Ordering::Relaxed)); + } let capped = p.capped.load(Ordering::Relaxed); if capped != 0 { println!("pipelines over the {} MiB DXIL cap, left to the runtime: {} ({} MiB of DXIL; XCL_PIPELINE_MAX_KB)", p.max_dxil >> 20, capped, p.capped_bytes.load(Ordering::Relaxed) >> 20); diff --git a/crates/xcl/tests/gpu_compute_e2e.rs b/crates/xcl/tests/gpu_compute_e2e.rs index 0d37e0b84e..e97fffc94f 100644 --- a/crates/xcl/tests/gpu_compute_e2e.rs +++ b/crates/xcl/tests/gpu_compute_e2e.rs @@ -124,6 +124,96 @@ fn offline_compute_pipeline_is_reused_by_a_second_process() { assert!(warm.contains("pipelines loaded from the driver cache 1"), "{warm}"); } +/// Wave32 compute program with conditional branches across distinct basic blocks so direct +/// DXIL splitting (`XCL_DIRECT_SPLIT_INSTRUCTIONS`) partitions it into multiple pieces. +fn split_program() -> Vec { + let list: &[(&str, &str, &[(&str, u64)], Option)] = &[ + // Mark wave32 (`detect_wave64` -> false) so `[32, 1, 1]` splits on every wave32 GPU. + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 126)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", V)], None), + ("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 1), ("SSRC1", 128)], None), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 6)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 129), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 131), ("VSRC1", 2)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 133), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 135), ("VSRC1", 2)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 137), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 139), ("VSRC1", 2)], None), + ("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 2), ("SSRC1", 128)], None), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 6)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 141), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 143), ("VSRC1", 2)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 145), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 147), ("VSRC1", 2)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 149), ("VSRC1", 2)], None), + ("V_XOR_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 151), ("VSRC1", 2)], None), + ("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 130), ("VSRC1", 0)], None), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 0), ("VSRC1", 4)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 128)], None), + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 4), ("DATA", 2), ("SADDR", 0x7D)], None), + ("S_ENDPGM", "ENC_SOPP", &[], None), + ]; + list.iter().flat_map(|(n, e, f, l)| encode(n, e, f, *l).unwrap_or_else(|x| panic!("{n}: {x}"))).collect() +} + +#[test] +fn offline_split_compute_pipeline_is_reused_by_a_second_process() { + use std::{fs, process::Command}; + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + if xcl::gpu::recomp::dxc::location().is_err() { + runner::skip_test("no DXC"); + return; + } + if !runner::wave32_supported().unwrap_or(false) { + runner::skip_test("no wave32 GPU"); + return; + } + let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let dir = std::env::temp_dir().join(format!("xcl-offline-split-pso-{}-{nonce}", std::process::id())); + let input = dir.join("input"); + let cache = dir.join("cache"); + fs::create_dir_all(&input).unwrap(); + fs::write(input.join("split.bin"), console_cs(&split_program(), 2 << 1 | 1 << 7, [32, 1, 1])).unwrap(); + let run = || { + let out = Command::new(env!("CARGO_BIN_EXE_xcl")) + .arg("shaders") + .arg(&input) + .args(["--precompile", "--pipelines", "--windows", "1"]) + .env("XCL_DXIL_CACHE", &cache) + .env("XCL_DIRECT_DXIL", "1") + .env("XCL_DIRECT_SPLIT_INSTRUCTIONS", "12") + .env("XCL_PSO_DISK_CACHE", "1") + .output() + .unwrap(); + let text = String::from_utf8_lossy(&out.stdout).into_owned(); + assert!(out.status.success(), "{text}\n{}", String::from_utf8_lossy(&out.stderr)); + text + }; + let cold = run(); + let mut files: Vec<_> = fs::read_dir(cache.join("driver")).unwrap().flatten().map(|e| e.path()).collect(); + files.sort(); + let pieces = files.len(); + assert!(pieces >= 2, "expected >= 2 split piece blobs in driver cache, got {files:?}\n{cold}"); + assert!( + files.iter().all(|p| p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.ends_with(".chain.pso"))), + "split compute precompile must write only .chain.pso files, got {files:?}" + ); + assert!(files.iter().all(|p| p.metadata().unwrap().len() > 0)); + assert!(cold.contains(&format!("pipelines made on the GPU {pieces}, refused 0")), "{cold}"); + assert!(cold.contains(&format!("split compute programs: 1 ({pieces} pieces)")), "{cold}"); + + let warm = run(); + assert!(warm.contains(&format!("pipelines loaded from the driver cache {pieces}")), "{warm}"); + + // A corrupt .chain.pso is rejected and rebuilt cleanly while sibling pieces stay reused. + fs::write(&files[0], b"invalid cached chain pipeline").unwrap(); + let recovered = run(); + assert!(recovered.contains(&format!("pipelines made on the GPU {pieces}, refused 0")), "{recovered}"); + assert!(recovered.contains(&format!("pipelines loaded from the driver cache {}", pieces - 1)), "{recovered}"); + assert!(files[0].metadata().unwrap().len() > b"invalid cached chain pipeline".len() as u64); + let _ = fs::remove_dir_all(&dir); +} + /// 16×8 threads per group (two wave64s), s[0:1] = output base and row width, TGID_X/Y in /// s2/s3, thread ids in v0/v1: each thread writes its global (x, y), and through LDS + /// s_barrier the (x, y) of the thread at the mirrored index, which is in the other wave.