Skip to content
Merged
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
30 changes: 29 additions & 1 deletion crates/xcl/build.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<PathBuf>) {
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;
Expand Down Expand Up @@ -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}");
}
3 changes: 2 additions & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 23 additions & 2 deletions crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -1007,6 +1012,22 @@ mod shader_cache_tests {
assert_eq!(ids.iter().collect::<std::collections::HashSet<_>>().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]
Expand Down
Loading