From 0fd51f9fed6d38171cdd833690c679e7838880e9 Mon Sep 17 00:00:00 2001 From: Nico <53304086+thisguyStan@users.noreply.github.com> Date: Wed, 25 Nov 2026 03:44:06 +0100 Subject: [PATCH] Shaders: offline pipeline builds use the runtime workers' 64 MB stack `xcl shaders --measure-pso` called CreateComputePipelineState on the main thread, whose stack xcl.exe reserves at 1 MB, and `--precompile` (with `--pipelines`) on scoped threads with Rust's 2 MB default. The driver compiles on the calling thread, so a measurement of what a build costs could end on a stack limit of XCL's own thread instead: on RADV under Wine, `--measure-pso` of the flat compact form of the largest compute programs ended with "thread 'main' has overflowed its stack" (#1269). ClosetPie107 reported the `--pipelines` side there first. Name the stack of the runtime's pipeline workers (`pso_async::WORKER_STACK`, 64 MB) and run both offline paths on a thread with it, so they build as the workers do. The runtime itself is unchanged. A first-time compute build at a bind (`XCL_LAZY_PSO` on, no `XCL_PSO_WAIT_MS`) still runs on the title's thread, with the stack the title gave it. Part of #1269. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/pso_async.rs | 8 +++-- crates/xcl/src/shaders.rs | 42 ++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 39f338143..f7d5f6ed6 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -169,6 +169,10 @@ pub fn enabled() -> bool { /// Builds the real pipeline: its raw pointer, 0 = failed. pub type Job = Box usize + Send>; +/// The stack of the threads that build pipelines: translation recurses deeply on big programs, +/// and the driver compiles on the calling thread (`xcl shaders` builds on the same) +pub const WORKER_STACK: usize = 64 << 20; + fn queue() -> &'static Mutex>> { static Q: OnceLock>>> = OnceLock::new(); Q.get_or_init(|| { @@ -178,7 +182,7 @@ fn queue() -> &'static Mutex>> { let n = crate::gpu::recomp::dxc::compile_threads(); for k in 0..n { let rx = rx.clone(); - let _ = std::thread::Builder::new().name(format!("xcl-pso-{k}")).stack_size(64 << 20).spawn(move || loop { + let _ = std::thread::Builder::new().name(format!("xcl-pso-{k}")).stack_size(WORKER_STACK).spawn(move || loop { let next = rx.lock().unwrap().recv(); let Ok(p) = next else { return }; // (None: a bind took it and builds it itself) @@ -352,7 +356,7 @@ fn preload() -> Option<&'static Preload> { STARTED.call_once(|| { let n = (crate::gpu::recomp::dxc::compile_threads() / 4).clamp(2, 4); for k in 0..n { - let _ = std::thread::Builder::new().name(format!("xcl-pso-pre-{k}")).stack_size(64 << 20).spawn(move || { + let _ = std::thread::Builder::new().name(format!("xcl-pso-pre-{k}")).stack_size(WORKER_STACK).spawn(move || { unsafe { use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_LOWEST}; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 68ee30332..3557e744d 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -12,6 +12,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; use crate::gpu::{dxbc, pack, rdna2, recomp, shader, xbox}; +use crate::hle::d3d12x::pso_async::WORKER_STACK; /// `--iface`: how console programs connect to their DXBC signatures (hypothesis checks for /// the VS/PS interface of the recompiler). @@ -1029,12 +1030,45 @@ enum MeasureError { Failed(String), } +/// `f` on a thread with the stack of the runtime's pipeline workers (`WORKER_STACK`): the driver +/// compiles on the calling thread, and on the main thread's 1 MB a build the workers make could +/// overflow (RADV under Wine on the compact form's biggest programs, #1269) +fn on_worker_stack(name: &str, f: impl FnOnce() -> T + Send) -> T { + std::thread::scope(|s| { + let h = std::thread::Builder::new().name(name.into()).stack_size(WORKER_STACK).spawn_scoped(s, f).expect("a pipeline thread"); + h.join().unwrap_or_else(|p| std::panic::resume_unwind(p)) + }) +} + +#[cfg(test)] +mod worker_stack_tests { + use super::{on_worker_stack, WORKER_STACK}; + + fn stack_reserve() -> usize { + let (mut lo, mut hi) = (0usize, 0usize); + unsafe { windows_sys::Win32::System::Threading::GetCurrentThreadStackLimits(&mut lo, &mut hi) }; + hi - lo + } + + #[test] + fn offline_builds_run_on_the_runtime_workers_stack() { + assert!(on_worker_stack("xcl-test", stack_reserve) >= WORKER_STACK); + assert_eq!(on_worker_stack("xcl-test", || 7), 7); + } + + #[test] + fn a_panic_on_the_worker_reaches_the_caller() { + let r = std::panic::catch_unwind(|| on_worker_stack("xcl-test", || panic!("build"))); + assert!(r.is_err()); + } +} + /// Compile one exact console compute shader through DXC, then time only the driver's /// `CreateComputePipelineState`. The implicit D3D and driver caches must both be disabled; /// refusing to do that is an error rather than a misleading warm-cache measurement. /// This is the HLSL path the runtime uses by default (not `XCL_DIRECT_DXIL`). fn measure_pso(input: &Input, hash: u64, repeat: usize, windows: u32, split: Option) -> i32 { - match measure_pso_result(input, hash, repeat, windows, split) { + match on_worker_stack("xcl-measure-pso", || measure_pso_result(input, hash, repeat, windows, split)) { Ok(()) => 0, Err(MeasureError::Unavailable(e) | MeasureError::Failed(e)) => { eprintln!("xcl shaders --measure-pso: {e}"); @@ -1365,7 +1399,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on ); std::thread::scope(|s| { for _ in 0..threads { - s.spawn(|| loop { + // translation and `--pipelines`' driver builds run here: the stack of the runtime's + // pipeline workers, not a default thread's + std::thread::Builder::new().stack_size(WORKER_STACK).spawn_scoped(s, || loop { let i = next.fetch_add(1, Ordering::Relaxed); if i >= n { break; @@ -1447,7 +1483,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option, on if d % 200 == 0 { eprintln!(" {d}/{n} ({:.0} s)", t0.elapsed().as_secs_f64()); } - }); + }).expect("a precompile thread"); } }); println!(