Skip to content
Draft
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder
- For compute shaders that XCL can split, the runtime first tries the saved driver blob for
the whole shader. A usable blob lets the GPU run it in one dispatch. If the blob is missing
or the driver rejects it, XCL builds the split pieces.
- A split program first seen through `ExecuteIndirect` is also kept in
`used-indirect-split.txt` beside the DXIL cache. A later `--pipelines --used-only` pass still
builds its direct-dispatch pieces and additionally saves both its pieces and whole fallback under
the indirect root; other split programs do not pay for those potentially long driver compiles.
- **`--pipelines` size cap**: a compute program whose DXIL is over 8 MiB and that has no saved
blob is left to the runtime instead of being compiled in the pass — those compiles take tens
of minutes and gigabytes and stopped the pass on the biggest programs. `XCL_PIPELINE_MAX_KB`
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ unsafe extern "system" fn hk_nt_terminate_process(h: isize, status: i32) -> i32
}
crate::log::flush();
crate::hle::d3d12x::pso_async::flush_used_bounded(std::time::Duration::from_millis(100));
crate::hle::d3d12x::real::flush_indirect_split_records(std::time::Duration::from_millis(100));
}
let orig: unsafe extern "system" fn(isize, i32) -> i32 = std::mem::transmute(ORIG_NT_TERMINATE.load(Ordering::Acquire));
orig(h, status)
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4874,6 +4874,9 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const

pub(super) mod dispatch_chain;
pub(crate) use dispatch_chain::log_counts as log_dispatch_chain_counts;
pub(crate) use dispatch_chain::earlier_indirect_used as earlier_indirect_split_records;
pub(crate) use dispatch_chain::flush_indirect_used as flush_indirect_split_records;
pub(crate) use dispatch_chain::shutdown_indirect_used as shutdown_indirect_split_records;

pub(super) mod remap_fallback {
use super::*;
Expand Down
57 changes: 57 additions & 0 deletions crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! PSOs for a split compute entry. The first piece is the real PSO returned
//! to the caller. Other pieces and the lazily built whole form belong to it.
use super::*;
use super::super::used_journal::Journal;
use crate::gpu::{dxil::kernel::DispatchChain, recomp::pso};
use std::collections::HashSet;
use std::sync::{Arc, LazyLock, OnceLock, Weak};

static PROGRAMS: AtomicU64 = AtomicU64::new(0);
Expand All @@ -14,6 +16,55 @@ static LIVE: AtomicU64 = AtomicU64::new(0);
static PEAK: AtomicU64 = AtomicU64::new(0);
static WANTED: AtomicU64 = AtomicU64::new(0);

struct IndirectUsed {
now: Journal,
}

static INDIRECT_USED: OnceLock<IndirectUsed> = OnceLock::new();

fn parse_used(text: &str) -> HashSet<u64> {
text.lines()
.filter_map(|line| u64::from_str_radix(line.trim(), 16).ok())
.filter(|&record| record != 0)
.collect()
}

fn indirect_used_path() -> std::path::PathBuf {
crate::gpu::recomp::dxc::cache_dir().join("used-indirect-split.txt")
}

fn indirect_used() -> &'static IndirectUsed {
INDIRECT_USED.get_or_init(|| {
if cfg!(test) {
return IndirectUsed { now: Journal::memory(HashSet::new()) };
}
let path = indirect_used_path();
let earlier = parse_used(&std::fs::read_to_string(&path).unwrap_or_default());
let now = Journal::file(path, earlier.clone());
IndirectUsed { now }
})
}

fn note_indirect_use(record: u64) {
indirect_used().now.note(record);
}

pub(crate) fn earlier_indirect_used() -> HashSet<u64> {
parse_used(&std::fs::read_to_string(indirect_used_path()).unwrap_or_default())
}

pub(crate) fn shutdown_indirect_used(timeout: std::time::Duration) {
if let Some(used) = INDIRECT_USED.get() {
let _ = used.now.shutdown_bounded(timeout);
}
}

pub(crate) fn flush_indirect_used(timeout: std::time::Duration) {
if let Some(used) = INDIRECT_USED.get() {
let _ = used.now.flush_bounded(timeout);
}
}

pub(crate) fn log_counts() {
let mib = |a: &AtomicU64| a.load(Ordering::Relaxed) >> 20;
crate::xlog_info!("compute split: {} programs split, {} pieces built, {} chained dispatches issued in {} slabs; scratch {} MiB (most {}, largest dispatch {} in one slab)",
Expand Down Expand Up @@ -457,6 +508,7 @@ pub(in crate::hle::d3d12x) unsafe fn execute_indirect(
bind: impl FnOnce(usize) -> Result<(), String>,
) -> Result<bool, String> {
let Some(chain) = CHAINS.lock().unwrap().get(&pso).cloned() else { return Ok(false) };
note_indirect_use(chain.builder.record);
match chain.whole.ready() {
None => return Ok(false),
Some(Ok(_)) => {}
Expand Down Expand Up @@ -503,6 +555,11 @@ pub(in crate::hle::d3d12x) unsafe fn execute_indirect(
mod tests {
use super::*;

#[test]
fn indirect_used_records_parse_unique_nonzero_hex() {
assert_eq!(parse_used("1\n0000000000000002\ninvalid\n0\n1\n"), [1, 2].into());
}

const MB: u64 = 1 << 20;

#[test]
Expand Down
3 changes: 1 addition & 2 deletions crates/xcl/src/hle/d3d12x/used_journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ impl Journal {
#[cfg(test)]
pub(super) fn contains(&self, key: u64) -> bool { self.shared.state.lock().unwrap_or_else(|e| e.into_inner()).seen.contains(&key) }

#[cfg(test)]
fn flush_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, false) }
pub(super) fn flush_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, false) }

pub(super) fn shutdown_bounded(&self, timeout: Duration) -> bool { self.wait_bounded(timeout, true) }

Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub fn host_main(boot_path: &Path) -> i32 {
crate::report::write_final(None);
crate::log::flush();
crate::hle::d3d12x::pso_async::shutdown_used();
crate::hle::d3d12x::real::shutdown_indirect_split_records(std::time::Duration::from_millis(500));
crate::hle::d3d12x::real::end_title(0);
});
}
Expand All @@ -182,5 +183,6 @@ pub fn host_main(boot_path: &Path) -> i32 {
crate::report::write_final(None);
crate::log::flush();
crate::hle::d3d12x::pso_async::shutdown_used();
crate::hle::d3d12x::real::shutdown_indirect_split_records(std::time::Duration::from_millis(500));
code as i32
}
Loading
Loading