From e56c368079bb51c1ceccf60fbcbb1e1860f3b3c5 Mon Sep 17 00:00:00 2001 From: vakaobr <15154330+vakaobr@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:34:59 -0300 Subject: [PATCH] Recompiler: the translated cache keys on the HLSL emitter's switches The on-disk translation cache folds the DXC toolchain, the backend and the direct-DXIL switches into its identity, but not the switches the HLSL emitter reads: XCL_SAFE_BARRIER=0, XCL_WAVE64_SPIN=0, XCL_X64_ALL_MASKS and XCL_RECOMP_NO_STRUCTURED. A run with one of them set loaded the translation an earlier run had cached without it, so XCL_SAFE_BARRIER=0 still ran the spin barrier for every cached program unless XCL_TRANSLATED_CACHE=0 was set too. A non-default value of any of them now adds a tag to the identity (rerouted entries derive from it and follow). Their defaults leave the identity unchanged, so existing caches stay valid. --- crates/xcl/src/gpu/recomp/pso.rs | 55 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 00ac0a064b..5dd9eb8dc4 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -266,14 +266,36 @@ mod translated { identity_for(&id.to_string(), &format!("dispatch-split-{}", crate::gpu::dxil::kernel::dispatch_piece_limit())) } else { 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() { + let id = if Backend::current() != Backend::Hlsl && crate::gpu::recomp::lower_dxil::enabled() { identity_for(&id.to_string(), concat!("xcl-per-lane-", env!("XCL_PER_LANE_SOURCE_ID"))) } else { id - } + }; + with_emit_switches(id, |name| std::env::var_os(name).map(|v| v.to_string_lossy().into_owned())) }) } + /// `id` for a recompiler that runs with the switches `get` returns. The switches the HLSL + /// emitter reads (`XCL_SAFE_BARRIER=0`, `XCL_WAVE64_SPIN=0`, `XCL_X64_ALL_MASKS`, + /// `XCL_RECOMP_NO_STRUCTURED`) change the translation of the same bytes: without this a run + /// with one of them set loaded what an earlier run without it had cached. Their defaults + /// leave `id` as it is, so existing entries stay valid. + pub(super) fn with_emit_switches(id: u64, get: impl Fn(&str) -> Option) -> u64 { + let off = |name: &str| get(name).is_some_and(|v| v.trim() == "0"); + let mut tag = String::new(); + for (set, part) in [ + (off("XCL_SAFE_BARRIER"), "safe-barrier-0;"), + (off("XCL_WAVE64_SPIN"), "wave64-spin-0;"), + (get("XCL_X64_ALL_MASKS").is_some(), "x64-all-masks;"), + (get("XCL_RECOMP_NO_STRUCTURED").is_some(), "no-structured;"), + ] { + if set { + tag.push_str(part); + } + } + if id == 0 || tag.is_empty() { id } else { identity_for(&id.to_string(), &tag) } + } + /// a second hash of the bytes, independent of `fnv64`'s fn hash2(b: &[u8]) -> u64 { b.iter().fold(0x9E37_79B9_7F4A_7C15u64, |h, &x| (h ^ x as u64).wrapping_mul(0xFF51_AFD7_ED55_8CCD).rotate_left(29)) @@ -1357,6 +1379,35 @@ mod shader_cache_tests { assert_eq!(ids.iter().collect::>().len(), ids.len()); } + /// A recompiler switch that changes the emitted HLSL gives the cache another identity, so a + /// run with `XCL_SAFE_BARRIER=0` does not load the spin-barrier translation an earlier run + /// stored; the switches' defaults keep the identity (and the existing entries). + #[test] + fn emit_switches_change_the_translated_identity() { + use translated::{identity_for, with_emit_switches}; + let base = identity_for("s", "t"); + let with = |vars: &[(&str, &str)]| { + let vars: Vec<(String, String)> = vars.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(); + with_emit_switches(base, move |name| vars.iter().find(|(k, _)| k == name).map(|(_, v)| v.clone())) + }; + assert_eq!(with(&[]), base); + assert_eq!(with(&[("XCL_SAFE_BARRIER", "1"), ("XCL_WAVE64_SPIN", "1")]), base, "defaults written out"); + let ids = [ + base, + with(&[("XCL_SAFE_BARRIER", "0")]), + with(&[("XCL_SAFE_BARRIER", " 0 ")]), + with(&[("XCL_WAVE64_SPIN", "0")]), + with(&[("XCL_X64_ALL_MASKS", "1")]), + with(&[("XCL_RECOMP_NO_STRUCTURED", "")]), + with(&[("XCL_SAFE_BARRIER", "0"), ("XCL_WAVE64_SPIN", "0")]), + ]; + assert_eq!(ids[1], ids[2], "the emitter trims the value too"); + let distinct: std::collections::HashSet<_> = ids.iter().collect(); + assert_eq!(distinct.len(), ids.len() - 1); + assert!(ids.iter().all(|&i| i != 0)); + assert_eq!(with_emit_switches(0, |_| Some("0".into())), 0, "a disabled cache stays disabled"); + } + /// 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]