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
181 changes: 150 additions & 31 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4447,8 +4447,10 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
// … and one built in an earlier run gets the blob kept on disk then (`driver_cache`)
let disk = driver_cache::path_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain);
let from_disk = cached.is_none();
let mut loaded = None;
if from_disk {
cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new);
loaded = disk.as_deref().and_then(driver_cache::load);
cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared);
}
if let Some(b) = &cached {
copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() };
Expand All @@ -4467,7 +4469,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
// a blob the driver does not take (another root signature at the same address…): compiled
forget_compute_blob(key);
if from_disk {
driver_cache::note(false, disk.as_deref());
driver_cache::note(false, loaded.take());
}
if mode == ComputeBuild::CachedWhole {
return (hr, in_driver, false);
Expand All @@ -4490,7 +4492,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
driver_cache::keep(p, &b, in_driver);
}
} else if reused && from_disk {
driver_cache::note(true, None);
driver_cache::note(true, loaded.take());
}
(hr, in_driver, reused)
}
Expand All @@ -4506,7 +4508,11 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
/// driver refuses the blob of another driver, adapter or root signature (the file is dropped
/// and the program compiled as before). `XCL_PSO_DISK_CACHE=0`: off.
pub(crate) mod driver_cache {
use std::io::Read;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsRawHandle;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

/// builds shorter than this are not worth a file
Expand Down Expand Up @@ -4562,20 +4568,72 @@ pub(crate) mod driver_cache {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(graphics_name(hash, bytes)))
}

pub fn load(p: &Path) -> Option<Vec<u8>> {
std::fs::read(p).ok().filter(|b| !b.is_empty())
/// Bytes read from one exact cache-file generation. Keeping its handle open lets a later
/// refusal dispose that file object without touching a replacement at the same pathname.
pub struct LoadedBlob {
bytes: Arc<Vec<u8>>,
/// `None` when the blob was readable but the cache entry could not be opened for deletion.
file: Option<std::fs::File>,
}

impl LoadedBlob {
#[cfg(test)]
pub fn bytes(&self) -> &[u8] { &self.bytes }
pub fn shared(&self) -> Arc<Vec<u8>> { self.bytes.clone() }

/// Remove the file object that supplied these bytes. POSIX disposition frees its name
/// while another process still has this generation open. If that exact operation is not
/// available, leave the stale entry in place rather than remove a pathname that may now
/// name a replacement.
pub fn invalidate(self) -> bool {
use windows_sys::Win32::Storage::FileSystem::{
FileDispositionInfoEx, SetFileInformationByHandle,
FILE_DISPOSITION_FLAG_DELETE, FILE_DISPOSITION_FLAG_POSIX_SEMANTICS,
FILE_DISPOSITION_INFO_EX,
};
let Some(file) = self.file else { return false };
let handle = file.as_raw_handle() as windows_sys::Win32::Foundation::HANDLE;
let ex = FILE_DISPOSITION_INFO_EX { Flags: FILE_DISPOSITION_FLAG_DELETE | FILE_DISPOSITION_FLAG_POSIX_SEMANTICS };
unsafe {
SetFileInformationByHandle(
handle,
FileDispositionInfoEx,
(&ex as *const FILE_DISPOSITION_INFO_EX).cast(),
std::mem::size_of::<FILE_DISPOSITION_INFO_EX>() as u32,
) != 0
}
}
}

/// A pipeline was made from a kept blob (`ok`), or the driver refused the blob: dropped.
pub fn note(ok: bool, p: Option<&Path>) {
pub fn load(p: &Path) -> Option<LoadedBlob> {
use windows_sys::Win32::Foundation::GENERIC_READ;
use windows_sys::Win32::Storage::FileSystem::{
DELETE, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE,
};
let open = |access| {
std::fs::OpenOptions::new()
.read(true)
.access_mode(access)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(p)
};
let (mut file, invalidatable) = open(GENERIC_READ | DELETE).map(|f| (f, true)).or_else(|_| open(GENERIC_READ).map(|f| (f, false))).ok()?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).ok()?;
(!bytes.is_empty()).then(|| LoadedBlob { bytes: Arc::new(bytes), file: invalidatable.then_some(file) })
}

/// A pipeline was made from a kept blob (`ok`), or the driver refused that exact blob: dropped.
pub fn note(ok: bool, loaded: Option<LoadedBlob>) {
if ok {
let n = HITS.fetch_add(1, Ordering::Relaxed) + 1;
if n.is_power_of_two() {
crate::xlog_info!("D3D12.X: {n} pipelines made from the driver's blob of an earlier run (kept on disk)");
}
} else {
if let Some(p) = p {
let _ = std::fs::remove_file(p);
if let Some(loaded) = loaded {
let _ = loaded.invalidate();
}
if STALE.fetch_add(1, Ordering::Relaxed) == 0 {
crate::xlog_info!("D3D12.X: a driver blob kept on disk was refused (another driver or adapter?) – such pipelines are compiled again");
Expand Down Expand Up @@ -4637,6 +4695,10 @@ pub(crate) mod driver_cache {
mod tests {
use super::*;

fn load_bytes(p: &Path) -> Option<Vec<u8>> {
load(p).map(|b| b.bytes().to_vec())
}

struct Directory(PathBuf);
impl Drop for Directory {
fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); }
Expand All @@ -4658,20 +4720,72 @@ pub(crate) mod driver_cache {
fn a_kept_blob_is_read_back_and_a_refused_one_is_dropped() {
let dir = std::env::temp_dir().join(format!("xcl-driver-cache-test-{}", std::process::id()));
let p = dir.join("driver").join(name(b"program", 0));
assert_eq!(load(&p), None, "nothing kept yet");
assert_eq!(load_bytes(&p), None, "nothing kept yet");
assert!(!write(&p, &[]), "an empty blob is not kept");
keep(&p, &[1, 2, 3], std::time::Duration::from_millis(10));
assert_eq!(load(&p), None, "a build of milliseconds is not kept");
assert_eq!(load_bytes(&p), None, "a build of milliseconds is not kept");
keep(&p, &[1, 2, 3], std::time::Duration::from_secs(1));
assert_eq!(load(&p).as_deref(), Some(&[1u8, 2, 3][..]));
assert_eq!(load_bytes(&p).as_deref(), Some(&[1u8, 2, 3][..]));
// no half file under the temporary name
assert_eq!(std::fs::read_dir(p.parent().unwrap()).unwrap().count(), 1);
// the driver took the blob: it stays; it refused it: gone, the program is compiled again
note(true, Some(&p));
note(true, load(&p));
assert!(p.exists());
note(false, Some(&p));
assert_eq!(load(&p), None);
note(false, load(&p));
assert_eq!(load_bytes(&p), None);
let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn an_older_process_cannot_remove_a_replacement_blob() {
const CHILD: &str = "XCL_DRIVER_CACHE_REPLACEMENT_CHILD";
let bytes = b"same bytes in both generations";
if let Some(path) = std::env::var_os(CHILD) {
let p = PathBuf::from(path);
assert!(write(&p, bytes), "replace the still-open old generation directly");
return;
}
let dir = std::env::temp_dir().join(format!("xcl-driver-cache-replace-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir(&dir).unwrap();
let _directory = Directory(dir.clone());
let p = dir.join("program.pso");
// Identical bytes make pathname/content comparisons unable to distinguish the two
// generations: the open file object is the identity that must protect the second.
assert!(write(&p, bytes));
let delayed_reader = load(&p).unwrap();
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "hle::d3d12x::real::driver_cache::tests::an_older_process_cannot_remove_a_replacement_blob", "--nocapture"])
.env(CHILD, &p)
.output()
.unwrap();
assert!(
output.status.success(),
"replacement process failed:\n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let _ = delayed_reader.invalidate();
assert_eq!(load_bytes(&p).as_deref(), Some(bytes.as_slice()));
}

#[test]
fn a_cache_symlink_never_disposes_its_target() {
use std::os::windows::fs::symlink_file;
let dir = std::env::temp_dir().join(format!("xcl-driver-cache-link-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir(&dir).unwrap();
let _directory = Directory(dir.clone());
let target = dir.join("outside.pso");
let link = dir.join("cache.pso");
std::fs::write(&target, b"not the cache entry").unwrap();
if symlink_file(&target, &link).is_err() {
return; // creating symlinks can require Developer Mode or an elevated token
}
if let Some(loaded) = load(&link) {
let _ = loaded.invalidate();
}
assert_eq!(std::fs::read(&target).unwrap(), b"not the cache entry");
}

#[test]
Expand All @@ -4685,11 +4799,11 @@ pub(crate) mod driver_cache {
let (_other_tmp, mut other) = create_staging_file(&p).unwrap();
let complete = b"complete second blob";
assert!(write(&p, complete));
assert_eq!(load(&p).as_deref(), Some(complete.as_slice()));
assert_eq!(load_bytes(&p).as_deref(), Some(complete.as_slice()));
// Resuming the older writer must not change a file already published by this
// invocation, even when the older payload is shorter than the newer one.
other.write_all(b"old").unwrap();
assert_eq!(load(&p).as_deref(), Some(complete.as_slice()), "published bytes must not alias another writer's staging file");
assert_eq!(load_bytes(&p).as_deref(), Some(complete.as_slice()), "published bytes must not alias another writer's staging file");
}

#[test]
Expand All @@ -4708,7 +4822,7 @@ pub(crate) mod driver_cache {
file.write_all(b"ours").unwrap();
drop(file);
std::fs::rename(&tmp, &p).unwrap();
assert_eq!(load(&p).as_deref(), Some(b"ours".as_slice()));
assert_eq!(load_bytes(&p).as_deref(), Some(b"ours".as_slice()));
assert_eq!(std::fs::read(&occupied).unwrap(), b"not ours");
}
}
Expand Down Expand Up @@ -5303,8 +5417,10 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const
let mut cached = graphics_blob(gkey);
let disk = driver_cache::graphics_path(gkey.0, gkey.1);
let from_disk = cached.is_none();
let mut loaded = None;
if from_disk {
cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new);
loaded = disk.as_deref().and_then(driver_cache::load);
cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared);
}
let build = create_graphics_pso_cached(
&mut copy,
Expand All @@ -5315,7 +5431,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const
forget_graphics_blob(gkey, blob);
}
if from_disk {
driver_cache::note(false, disk.as_deref());
driver_cache::note(false, loaded.take());
}
},
|label, has_cached, f| driver_compile_identified(label, bytes, gate_compute_driver_compile(has_cached), Some((ps_hash, has_cached)), f),
Expand All @@ -5328,7 +5444,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const
driver_cache::keep(p, &b, build.in_driver);
}
} else if build.reused && from_disk {
driver_cache::note(true, None);
driver_cache::note(true, loaded.take());
}
if hr < 0 {
note_translation_error(format!(
Expand Down Expand Up @@ -6077,7 +6193,8 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
let gdisk = gkey.and_then(|(hash, bytes)| driver_cache::graphics_path(hash, bytes));
let gram = gkey.and_then(graphics_blob);
let gfrom_disk = gram.is_none();
let gcached = gram.or_else(|| gdisk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new));
let mut gloaded = if gfrom_disk { gdisk.as_deref().and_then(driver_cache::load) } else { None };
let gcached = gram.or_else(|| gloaded.as_ref().map(driver_cache::LoadedBlob::shared));

let bytes = mesh_bytes + tp.cs.len();
let gate = bytes >= huge_pso_bytes() && std::thread::current().name().is_some_and(|n| n.starts_with("xcl-pso"));
Expand Down Expand Up @@ -6116,11 +6233,11 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
let mut greused = gcached.is_some();
if let Some((key, blob)) = gkey.zip(gcached.as_ref()).filter(|_| hr < 0 || mesh_pso.is_none()) {
drop(mesh_pso.take());
if gfrom_disk || forget_graphics_blob(key, blob) {
let rejected_path = gdisk.as_deref().filter(|p| {
gfrom_disk || driver_cache::load(p).as_deref() == Some(blob.as_ref())
});
driver_cache::note(false, rejected_path);
if gfrom_disk {
driver_cache::note(false, gloaded.take());
} else {
// An in-memory hit has no handle identifying the disk generation it came from.
forget_graphics_blob(key, blob);
}
take_cold_slot();
(hr, mesh_pso, gin_driver) = create_mesh("mesh pass retry", None);
Expand All @@ -6134,7 +6251,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
if greused {
if let Some(blob) = gcached.filter(|_| gfrom_disk) {
remember_graphics_blob(key, blob);
driver_cache::note(true, None);
driver_cache::note(true, gloaded.take());
}
} else {
let slow = driver_cache::worth_keeping(gin_driver);
Expand All @@ -6159,8 +6276,10 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
let mut ccached = compute_blob(ckey);
let cdisk = driver_cache::path(&tp.cs, 0);
let cfrom_disk = ccached.is_none();
let mut cloaded = None;
if cfrom_disk {
ccached = cdisk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new);
cloaded = cdisk.as_deref().and_then(driver_cache::load);
ccached = cloaded.as_ref().map(driver_cache::LoadedBlob::shared);
}
if let Some(b) = &ccached {
cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() };
Expand All @@ -6175,7 +6294,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
if hc < 0 && ccached.is_some() {
forget_compute_blob(ckey);
if cfrom_disk {
driver_cache::note(false, cdisk.as_deref());
driver_cache::note(false, cloaded.take());
}
take_cold_slot();
cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default();
Expand All @@ -6193,7 +6312,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
driver_cache::keep(p, &b, cin_driver);
}
} else if creused && cfrom_disk {
driver_cache::note(true, None);
driver_cache::note(true, cloaded.take());
}
drop(slot);
if hc < 0 || compute.is_null() {
Expand Down
20 changes: 17 additions & 3 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,15 @@ mod pipeline_cap_tests {
assert_eq!(output[0], 42);
assert_eq!((capped, made, refused), (1, 0, 0));
assert!(!path.exists(), "rejected blob should not be retried next run");

// Use invalid DXIL so the retry cannot publish a new blob over the stale one.
// The refused generation must still be removed when the cap is disabled.
std::fs::write(&path, b"deliberately invalid cached PSO").expect("stale blob");
pipelines.max_dxil = 0;
let invalid = recomp::pso::Compiled { dispatch_chain: None, dxil: vec![0; 32], samplers: vec![] };
pipelines.make_with_cache_path(&invalid, Some(path.clone()));
assert_eq!(pipelines.refused.load(Ordering::Relaxed), 1);
assert!(!path.exists(), "a failed uncached retry should leave the rejected blob invalidated");
}

#[test]
Expand Down Expand Up @@ -1525,7 +1534,8 @@ impl Pipelines {
}
driver_cache::path_for(dxil, desc.Flags.0, chained)
});
let cached = path.as_deref().and_then(driver_cache::load);
let mut loaded = path.as_deref().and_then(driver_cache::load);
let cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared);
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
Expand Down Expand Up @@ -1554,8 +1564,8 @@ impl Pipelines {
// A stale blob is not a usable cache hit. Do not let its presence bypass the offline
// size cap and start the expensive uncached driver compile.
if cached.is_some() && from_cache.is_none() && over_cap {
if let Some(path) = path.as_deref() {
let _ = std::fs::remove_file(path);
if let Some(loaded) = loaded.take() {
let _ = loaded.invalidate();
}
*self.running.0.lock().unwrap() -= 1;
self.running.1.notify_one();
Expand All @@ -1568,6 +1578,10 @@ impl Pipelines {
self.reused.fetch_add(1, Ordering::Relaxed);
Ok(pso)
} else {
// Wine refuses a replacement while the rejected generation still has an open handle.
if let Some(loaded) = loaded.take() {
let _ = loaded.invalidate();
}
desc.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default();
let pso = unsafe { self.dev.CreateComputePipelineState::<ID3D12PipelineState>(&desc) };
if let (Some(path), Ok(pso)) = (path.as_deref(), &pso) {
Expand Down
Loading