From 17285b327e5e93640f564ede019a15c9cec1185b Mon Sep 17 00:00:00 2001 From: itsd-effex <360506289+itsd-effex@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:42:33 +0000 Subject: [PATCH 1/4] Shaders: DXC compiles sources over 8 MB one at a time A translated source over 8 MB of HLSL (the -O0 threshold; a handful of programs in a retail pack) takes several GB in DXC. Several of them compiling at once stack that memory, and the admission check (`wait_for_memory`) reads the commit headroom, which under Wine includes all of the host's swap, so it lets them through: three such programs in `xcl pipelines` reached a 25 GB RAM cap in 23 s and 20.8 GB of swap in 48 s on a 32 GB machine, with none finished. One at a time, two of them finished in 219 s with peak RAM at 11 GB. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/dxc.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 907511e63c..c6c927ba25 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -477,6 +477,10 @@ pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result< rx.recv().map_err(|_| "DXC: the compile thread exited".to_string())? } +/// HLSL over this size is compiled at -O0 and one at a time (`compile_here`). +const ENORMOUS_SOURCE: usize = 8 * 1024 * 1024; +static ENORMOUS_ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(()); + fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result, String> { let c = compiler()?; let mut args: Vec> = ["-E", entry, "-T", profile, "-HV", "2021"].iter().map(|a| crate::util::wide(a)).collect(); @@ -487,8 +491,13 @@ fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result // compile). -O0 only as a safety net for enormous sources. // -O1 from 1 MB on: the same DXIL size at a quarter less time (a retail game's 4.8 MB HLSL of // a 19 600-instruction CS: 13.5 s instead of 17.7 at -O3; -O0 10.6 s, but twice the DXIL) + // Enormous sources (over ENORMOUS_SOURCE, a handful in a retail pack) one at a time: each takes + // GBs in DXC, and the commit checks that hold compiles back (`wait_for_memory`) do not see that + // under Wine, where the commit limit includes all of the host's swap. Three of them at once + // took 29 GB of RAM and 20 GB of swap in `xcl pipelines` on a 32 GB machine. + let _enormous = (src.len() > ENORMOUS_SOURCE).then(|| ENORMOUS_ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner())); if !extra.iter().any(|a| a.starts_with("-O")) { - if src.len() > 8 * 1024 * 1024 { + if src.len() > ENORMOUS_SOURCE { args.push(crate::util::wide("-O0")); } else if src.len() > 1024 * 1024 { args.push(crate::util::wide("-O1")); From 8d821a921f9ead2df1177c2d6136e26900a2bdf3 Mon Sep 17 00:00:00 2001 From: itsd-effex <360506289+itsd-effex@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:21:14 +0000 Subject: [PATCH 2/4] Tests: enormous DXC sources from several threads compile one at a time The gate is now a small guard type (`EnormousTurn`). In test builds it counts how many compiles hold it at once, and a source with a marker comment counts as enormous, so the test needs no 8 MB shader. Four threads compile marked sources through `compile_here`; at most one is ever inside. Without the gate (a lock of its own per compile) the test sees all four inside at once and fails. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/dxc.rs | 75 +++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index c6c927ba25..7fa4f2e3ff 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -481,6 +481,30 @@ pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result< const ENORMOUS_SOURCE: usize = 8 * 1024 * 1024; static ENORMOUS_ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(()); +/// A compile's turn among enormous sources (`compile_here`): held for the whole DXC call. +struct EnormousTurn(#[allow(dead_code)] std::sync::MutexGuard<'static, ()>); + +impl EnormousTurn { + fn wanted(src: &str) -> bool { + src.len() > ENORMOUS_SOURCE || (cfg!(test) && src.contains(enormous_tests::MARK)) + } + + fn take() -> EnormousTurn { + let guard = ENORMOUS_ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner()); + #[cfg(test)] + enormous_tests::entered(); + EnormousTurn(guard) + } +} + +#[cfg(test)] +impl Drop for EnormousTurn { + fn drop(&mut self) { + // before the field: the next compile can only start once this one is counted out + enormous_tests::left(); + } +} + fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result, String> { let c = compiler()?; let mut args: Vec> = ["-E", entry, "-T", profile, "-HV", "2021"].iter().map(|a| crate::util::wide(a)).collect(); @@ -495,7 +519,7 @@ fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result // GBs in DXC, and the commit checks that hold compiles back (`wait_for_memory`) do not see that // under Wine, where the commit limit includes all of the host's swap. Three of them at once // took 29 GB of RAM and 20 GB of swap in `xcl pipelines` on a 32 GB machine. - let _enormous = (src.len() > ENORMOUS_SOURCE).then(|| ENORMOUS_ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner())); + let _enormous = EnormousTurn::wanted(src).then(EnormousTurn::take); if !extra.iter().any(|a| a.starts_with("-O")) { if src.len() > ENORMOUS_SOURCE { args.push(crate::util::wide("-O0")); @@ -1258,3 +1282,52 @@ mod cache_tests { }); } } + +#[cfg(test)] +mod enormous_tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + + /// In test builds a source with this comment counts as enormous (`EnormousTurn::wanted`), so the + /// test needs no 8 MB shader. + pub(super) const MARK: &str = "// xcl-test: enormous source"; + static INSIDE: AtomicUsize = AtomicUsize::new(0); + static MOST_INSIDE: AtomicUsize = AtomicUsize::new(0); + + pub(super) fn entered() { + let n = INSIDE.fetch_add(1, Ordering::SeqCst) + 1; + MOST_INSIDE.fetch_max(n, Ordering::SeqCst); + } + + pub(super) fn left() { + INSIDE.fetch_sub(1, Ordering::SeqCst); + } + + /// Enormous sources compiled from several threads at once go through DXC one at a time. + #[test] + fn enormous_sources_compile_one_at_a_time() { + if compiler().is_err() { + crate::gpu::recomp::runner::skip_test("no DXC"); + return; + } + let threads: Vec<_> = (0..4u32) + .map(|i| { + std::thread::spawn(move || { + // distinct sources, each with a little work for DXC + let mut src = format!("{MARK}\nRWByteAddressBuffer o : register(u0);\n[numthreads(64, 1, 1)]\nvoid main(uint3 t : SV_DispatchThreadID) {{\n uint v = t.x * {};\n", i + 3); + for k in 0..200 { + src.push_str(&format!(" v = (v ^ {k}u) * 2654435761u + (v >> {});\n", k % 31 + 1)); + } + src.push_str(" o.Store(t.x * 4, v);\n}\n"); + compile_here(&src, "main", "cs_6_6", &[]).map(|d| d.len()) + }) + }) + .collect(); + for t in threads { + let dxil = t.join().unwrap().expect("the test shader compiles"); + assert!(dxil > 0); + } + assert_eq!(INSIDE.load(Ordering::SeqCst), 0); + assert_eq!(MOST_INSIDE.load(Ordering::SeqCst), 1, "two enormous sources were in DXC at once"); + } +} From 9c77995172bc53853545369db662826b057495f6 Mon Sep 17 00:00:00 2001 From: itsd-effex <360506289+itsd-effex@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:27:54 +0000 Subject: [PATCH 3/4] Shaders: the enormous-source test hook is compiled in test builds only `EnormousTurn::wanted` named the test module in a `cfg!(test)` expression, which non-test builds still compile: `cargo build` failed (E0433). The marker check is now a `#[cfg(test)]` block. The test in the previous commit was added after a review asked for one. Suggested-by: Aniseto Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/dxc.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 7fa4f2e3ff..8ffa1b98b4 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -486,7 +486,11 @@ struct EnormousTurn(#[allow(dead_code)] std::sync::MutexGuard<'static, ()>); impl EnormousTurn { fn wanted(src: &str) -> bool { - src.len() > ENORMOUS_SOURCE || (cfg!(test) && src.contains(enormous_tests::MARK)) + #[cfg(test)] + if src.contains(enormous_tests::MARK) { + return true; + } + src.len() > ENORMOUS_SOURCE } fn take() -> EnormousTurn { From c4a169e93361441b8c60c55a6d2196cf66a6d89d Mon Sep 17 00:00:00 2001 From: itsd-effex <360506289+itsd-effex@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:19:27 +0000 Subject: [PATCH 4/4] Shaders: enormous sources wait for their turn before the DXC pool A queued enormous compile took a DXC worker and a wait_for_memory slot and then waited on the one-at-a-time lock while holding both; once its turn came it went into DXC without checking memory again. compile() now takes the turn on the caller's thread, before the job is sent to the pool. Suggested-by: MatyEjsr Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/dxc.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 8ffa1b98b4..11a3fb9ca5 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -471,17 +471,22 @@ fn pool() -> &'static std::sync::Mutex> { /// Compile HLSL to signed DXIL. `profile` e.g. "cs_6_6"; `extra` are dxc arguments. pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result, String> { + // Enormous sources wait for their turn here, on the caller's thread, so that a waiting one + // holds neither a DXC worker nor a `wait_for_memory` slot, and is admitted against the + // memory left once the one before it has finished. + let _enormous = EnormousTurn::wanted(src).then(EnormousTurn::take); let (reply, rx) = std::sync::mpsc::channel(); let job = Job { src: src.into(), entry: entry.into(), profile: profile.into(), extra: extra.iter().map(|s| s.to_string()).collect(), reply, wanted: crate::hle::d3d12x::pso_async::wanted_here() }; pool().lock().unwrap().send(job).map_err(|_| "DXC: no compile threads".to_string())?; rx.recv().map_err(|_| "DXC: the compile thread exited".to_string())? } -/// HLSL over this size is compiled at -O0 and one at a time (`compile_here`). +/// HLSL over this size is compiled at -O0 (`compile_here`) and one at a time (`compile`). const ENORMOUS_SOURCE: usize = 8 * 1024 * 1024; static ENORMOUS_ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(()); -/// A compile's turn among enormous sources (`compile_here`): held for the whole DXC call. +/// A compile's turn among enormous sources (`compile`): held from before the memory check to the +/// end of the DXC call. struct EnormousTurn(#[allow(dead_code)] std::sync::MutexGuard<'static, ()>); impl EnormousTurn { @@ -522,8 +527,8 @@ fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result // Enormous sources (over ENORMOUS_SOURCE, a handful in a retail pack) one at a time: each takes // GBs in DXC, and the commit checks that hold compiles back (`wait_for_memory`) do not see that // under Wine, where the commit limit includes all of the host's swap. Three of them at once - // took 29 GB of RAM and 20 GB of swap in `xcl pipelines` on a 32 GB machine. - let _enormous = EnormousTurn::wanted(src).then(EnormousTurn::take); + // took 29 GB of RAM and 20 GB of swap in `xcl pipelines` on a 32 GB machine. `compile` takes + // their turn (`EnormousTurn`). if !extra.iter().any(|a| a.starts_with("-O")) { if src.len() > ENORMOUS_SOURCE { args.push(crate::util::wide("-O0")); @@ -1323,7 +1328,7 @@ mod enormous_tests { src.push_str(&format!(" v = (v ^ {k}u) * 2654435761u + (v >> {});\n", k % 31 + 1)); } src.push_str(" o.Store(t.x * 4, v);\n}\n"); - compile_here(&src, "main", "cs_6_6", &[]).map(|d| d.len()) + compile(&src, "main", "cs_6_6", &[]).map(|d| d.len()) }) }) .collect();