diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 3e40e95de6..3fb0d3c088 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -613,8 +613,9 @@ mod translated { } /// `translate` through the disk (`identity()` zero: not used). An enormous compute program - /// the direct translator made (`translate` says so) is kept under `rerouted_identity`, which - /// follows that translator's version: the HLSL path's identity does not. + /// the direct translator made, or the HLSL fallback of one it failed on (`translate` says + /// so), is kept under `rerouted_identity`, which follows that translator's version: the HLSL + /// path's identity does not. pub(super) fn cached( dir: &Path, blob: &[u8], @@ -685,7 +686,7 @@ mod translated { super::note_compute_path( blob, match (id == rerouted, Backend::current()) { - (true, _) => "direct DXIL (its HLSL is enormous), from the translated cache", + (true, _) => "direct DXIL (its HLSL is enormous), or its HLSL fallback, from the translated cache", (false, Backend::Hlsl) => "HLSL, from the translated cache", (false, _) => "direct DXIL or its HLSL fallback (XCL_DIRECT_DXIL), from the translated cache", }, @@ -729,8 +730,10 @@ fn enormous(stage: Stage, hlsl_len: usize) -> bool { // Changes here, in `fspx`, or in `console_translation` that alter the cached result must bump // `TRANSLATION_GLUE_REVISION` in build.rs; cache serialization changes need not bump it. -/// The program's DXIL, and whether it is an `enormous` one the direct translator made (its -/// disk cache entry then follows that translator's version: `translated::cached`). +/// The program's DXIL, and whether its disk cache entry follows the direct translator's version +/// (`translated::cached`): an `enormous` program the direct translator made, or one it failed +/// on, whose HLSL fallback a later direct translator is to replace (kept under the HLSL path's +/// identity, the fallback would be found first by every later build and never retried). fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: u32, split_wave64: bool) -> Result<(Compiled, bool), String> { // `XCL_DEBUG_CS_SUB` names this compute shader: the HLSL path, whatever its size let cs_subs = cs_subs(blob, stage); @@ -778,6 +781,8 @@ fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: } } let (mut t, profile) = console_translation(blob, stage, sampler_base, windows, split_wave64, false)?; + // an enormous program's HLSL fallback, kept under the direct translator's identity + let mut enormous_fallback = false; if cs_subs.is_none() && enormous(stage, t.hlsl.len()) && !crate::gpu::dxil::kernel::enabled() { if dxc::defer() { return Err("DXIL not compiled yet".into()); @@ -793,7 +798,10 @@ fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: note_compute_path(blob, "direct DXIL (its HLSL is enormous)"); return Ok((c, true)); } - Err(e) => direct_fallback("CS", &e), + Err(e) => { + direct_fallback("CS", &e); + enormous_fallback = true; + } } } if let Some([r, g, b]) = (stage == Stage::Pixel).then(|| debug_color(blob)).flatten() { @@ -826,7 +834,7 @@ fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: if stage == Stage::Compute { note_compute_path(blob, if cs_subs.is_some() { "HLSL, with XCL_DEBUG_CS_SUB" } else { "HLSL" }); } - Ok((Compiled { dispatch_chain: None, dxil, samplers: t.static_samplers, dispatch_remap: t.dispatch_remap }, false)) + Ok((Compiled { dispatch_chain: None, dxil, samplers: t.static_samplers, dispatch_remap: t.dispatch_remap }, enormous_fallback)) } /// `XCL_DEBUG_PS_SUB` / `XCL_DEBUG_CS_SUB` = `hash:from=>to||from=>to…` (a dump hash, hex): the @@ -2140,6 +2148,44 @@ mod shader_cache_tests { let _ = std::fs::remove_dir_all(&dir); } + /// The HLSL fallback of an enormous program the direct translator failed on + /// (`compile_console_shader` says it follows the direct translator) is kept under the + /// rerouted identity, so a later direct translator tries the program again. Kept under the + /// HLSL path's identity, as before, it is found first and never tried again. + #[test] + fn an_enormous_programs_fallback_is_retried_by_a_new_direct_translator() { + use translated::{cached_rerouted, rerouted_identity}; + let dir = std::env::temp_dir().join(format!("xcl-translated-fallback-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let (blob, old_blob) = (b"console-enormous-fallback".as_slice(), b"console-enormous-old-fallback".as_slice()); + let ident = 0x5678; + let go = |blob: &[u8], version: u64, marker: u8, follows_direct: bool, calls: &mut u32| { + cached_rerouted(&dir, ident, version, blob, Stage::Compute, 0, 1, false, || { + *calls += 1; + Ok((compiled(marker), follows_direct)) + }) + .unwrap() + .dxil[67] + }; + let mut calls = 0; + // direct 7 fails: the HLSL fallback (1), under the rerouted identity, found again + assert_eq!(go(blob, 7, 1, true, &mut calls), 1); + assert_eq!(go(blob, 7, 9, true, &mut calls), 1, "the fallback is a hit for the same direct translator"); + assert_eq!(calls, 1); + assert!(translated::file(&dir, rerouted_identity(ident, 7), blob, Stage::Compute, 0, 1, false).exists()); + assert!(!translated::file(&dir, ident, blob, Stage::Compute, 0, 1, false).exists(), "not under the HLSL path's identity"); + // direct 8 makes it: the program is translated again and its direct DXIL (2) kept + assert_eq!(go(blob, 8, 2, true, &mut calls), 2, "a new direct translator tries the program again"); + assert_eq!(go(blob, 8, 9, true, &mut calls), 2); + assert_eq!(calls, 2); + // the trap this avoids: a fallback under the HLSL path's identity (an entry stored before) + // is served to every later direct translator + assert_eq!(go(old_blob, 7, 3, false, &mut calls), 3); + assert_eq!(go(old_blob, 8, 4, true, &mut calls), 3, "found under the HLSL path's identity first"); + assert_eq!(calls, 3); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn rerouted_disk_entries_follow_compact_nesting_policy() { use std::process::Command;