Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ pub fn enabled() -> bool {
/// Builds the real pipeline: its raw pointer, 0 = failed.
pub type Job = Box<dyn FnOnce() -> 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<mpsc::Sender<Arc<Pending>>> {
static Q: OnceLock<Mutex<mpsc::Sender<Arc<Pending>>>> = OnceLock::new();
Q.get_or_init(|| {
Expand All @@ -178,7 +182,7 @@ fn queue() -> &'static Mutex<mpsc::Sender<Arc<Pending>>> {
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)
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 39 additions & 3 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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<T: Send>(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<bool>) -> 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}");
Expand Down Expand Up @@ -1365,7 +1399,9 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, 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;
Expand Down Expand Up @@ -1447,7 +1483,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
if d % 200 == 0 {
eprintln!(" {d}/{n} ({:.0} s)", t0.elapsed().as_secs_f64());
}
});
}).expect("a precompile thread");
}
});
println!(
Expand Down
Loading