diff --git a/crates/xcl/build.rs b/crates/xcl/build.rs index 64a6b08bd..311d5c923 100644 --- a/crates/xcl/build.rs +++ b/crates/xcl/build.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; // `pso.rs` also contains cache storage and pipeline setup. Keep it out of the source hash so // those changes do not discard translations; bump this only when its container parsing, @@ -30,6 +30,21 @@ const PER_LANE_SOURCES: &[&str] = &[ "src/gpu/recomp/ssa.rs", ]; +/// The direct DXIL translator's sources (`XCL_DIRECT_DXIL`), part of only its own cache entries' +/// identity: every `.rs` file under `dir` but the test files (`*tests.rs`; tests inside the other +/// files are hashed with them), so a new one cannot be forgotten. +fn direct_sources(dir: &Path, out: &mut Vec) { + for entry in std::fs::read_dir(dir).expect("list direct DXIL sources") { + let path = entry.expect("list direct DXIL sources").path(); + let name = path.file_name().unwrap_or_default().to_string_lossy(); + if path.is_dir() { + direct_sources(&path, out); + } else if name.ends_with(".rs") && !name.ends_with("tests.rs") { + out.push(path); + } + } +} + fn add(h: &mut u64, bytes: &[u8]) { for &byte in bytes { *h ^= byte as u64; @@ -67,4 +82,17 @@ fn main() { add(&mut p, &[0]); } println!("cargo:rustc-env=XCL_PER_LANE_SOURCE_ID={p:016x}"); + // a directory: cargo looks at every file under it + println!("cargo:rerun-if-changed=src/gpu/dxil"); + let mut sources = Vec::new(); + direct_sources(&root.join("src/gpu/dxil"), &mut sources); + sources.sort(); + let mut d = 0xcbf2_9ce4_8422_2325u64; + for path in &sources { + add(&mut d, path.strip_prefix(root).unwrap_or(path).to_string_lossy().replace('\\', "/").as_bytes()); + add(&mut d, &[0]); + add(&mut d, &std::fs::read(path).expect("read direct DXIL source")); + add(&mut d, &[0]); + } + println!("cargo:rustc-env=XCL_DIRECT_DXIL_SOURCE_ID={d:016x}"); } diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d28a2ffcb..27f08cb33 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -35,7 +35,8 @@ pub struct KernelOptions { } /// Version of the direct translator's output, part of the on-disk translation cache's -/// identity (`recomp::pso`): bump it on any change that alters the emitted containers +/// identity (`recomp::pso`): bump it when `pso::direct_compute` / `direct_graphics` change the +/// containers. An edit of `gpu/dxil` needs no bump, `XCL_DIRECT_DXIL_SOURCE_ID` is its hash /// (the `gpu/dxil` sources are not in build.rs's translator source hash). pub const TRANSLATOR_VERSION: u32 = 7; diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 8a8b95b06..e78d0f889 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -208,9 +208,14 @@ mod translated { } } + /// The direct translator's version: `TRANSLATOR_VERSION` and the hash of its sources (build.rs). + pub(super) fn direct_version(version: u32, source: &str) -> u64 { + identity_for(source, &version.to_string()) + } + /// `identity_for` of `backend`: unchanged for the HLSL path (its entries stay valid), /// tagged with the direct translator's version and routing mode otherwise. - pub(super) fn identity_with(source: &str, toolchain: &str, backend: Backend, direct_version: u32) -> u64 { + pub(super) fn identity_with(source: &str, toolchain: &str, backend: Backend, direct_version: u64) -> u64 { let base = identity_for(source, toolchain); let tag = match backend { Backend::Hlsl => return base, @@ -239,7 +244,7 @@ mod translated { env!("XCL_TRANSLATOR_SOURCE_ID"), super::dxc::toolchain_identity(), Backend::current(), - crate::gpu::dxil::kernel::TRANSLATOR_VERSION, + direct_version(crate::gpu::dxil::kernel::TRANSLATOR_VERSION, env!("XCL_DIRECT_DXIL_SOURCE_ID")), ); // `XCL_PER_LANE=1` (direct compute only): its entries under their own identity if Backend::current() != Backend::Hlsl && crate::gpu::recomp::lower_dxil::enabled() { @@ -1007,6 +1012,22 @@ mod shader_cache_tests { assert_eq!(ids.iter().collect::>().len(), ids.len()); } + /// An edit of the direct translator's sources changes the direct entries' identity (no + /// `TRANSLATOR_VERSION` bump needed) and leaves the HLSL path's. + #[test] + fn direct_dxil_identity_tracks_its_sources() { + use translated::{direct_version, identity_with, Backend}; + let built = env!("XCL_DIRECT_DXIL_SOURCE_ID"); + assert_eq!(built.len(), 16); + assert!(u64::from_str_radix(built, 16).is_ok()); + let id = |backend, version, source| identity_with("s", "t", backend, direct_version(version, source)); + for backend in [Backend::DirectCompute, Backend::DirectAll] { + assert_ne!(id(backend, 7, built), id(backend, 7, "edited"), "{backend:?}: edited sources, same version"); + assert_ne!(id(backend, 7, built), id(backend, 8, built), "{backend:?}: a version bump"); + } + assert_eq!(id(Backend::Hlsl, 7, built), id(Backend::Hlsl, 8, "edited")); + } + /// Hit / miss through a temp cache dir: a direct entry is reused by the same identity and /// is a miss for the HLSL identity (and the other way round) for the same request. #[test]