diff --git a/crates/xcl/src/gpu/dxil/mod.rs b/crates/xcl/src/gpu/dxil/mod.rs index 6ca3ed7b5a..b32aa54cd3 100644 --- a/crates/xcl/src/gpu/dxil/mod.rs +++ b/crates/xcl/src/gpu/dxil/mod.rs @@ -33,12 +33,12 @@ mod lower_window; pub mod module; pub mod ops; pub mod ops_core; -mod prelude; +pub(crate) mod prelude; pub mod reload; pub mod shader; pub mod ssa; pub mod validate; -mod waterfall; +pub(crate) mod waterfall; #[cfg(test)] mod compact_tests; diff --git a/crates/xcl/src/gpu/dxil/waterfall.rs b/crates/xcl/src/gpu/dxil/waterfall.rs index 94f8c3a585..5122ff910b 100644 --- a/crates/xcl/src/gpu/dxil/waterfall.rs +++ b/crates/xcl/src/gpu/dxil/waterfall.rs @@ -156,10 +156,15 @@ fn match_vcmp_eq_u64(d: &Decoded, s_base: u64, v_base: u64) -> Option { } } +/// Whether `collapse` collapses anything: on unless `XCL_WATERFALL_COLLAPSE=0`. +pub fn collapse_enabled() -> bool { + !std::env::var("XCL_WATERFALL_COLLAPSE").is_ok_and(|v| v.trim() == "0") +} + /// Collapse 4-dword sampler (`S#`) waterfall self-loops in `cfg` and return a map /// `mimg_pc -> vgpr_index_of_s3` for each collapsed `ENC_MIMG` instruction. pub fn collapse(cfg: &mut Cfg) -> BTreeMap { - if std::env::var("XCL_WATERFALL_COLLAPSE").is_ok_and(|v| v.trim() == "0") { + if !collapse_enabled() { return BTreeMap::new(); } let mut ssamp_vgpr = BTreeMap::new(); diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index ddcc6d134c..df57e81a52 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -344,6 +344,14 @@ mod translated { identity_tagged(base, tag, direct_version) } + /// `id` with the direct translator's output switches that are off (`XCL_RDNA_FLOAT=0`, + /// `XCL_WATERFALL_COLLAPSE=0`): its output without them is another translation. Both on + /// (the default) leave `id` as it is, so the entries already on disk stay valid. + pub(super) fn direct_switches_tagged(id: u64, rdna_float: bool, waterfall_collapse: bool) -> u64 { + let id = if rdna_float { id } else { identity_for(&id.to_string(), "xcl-rdna-float-0") }; + if waterfall_collapse { id } else { identity_for(&id.to_string(), "xcl-waterfall-collapse-0") } + } + fn identity_tagged(base: u64, tag: &str, direct_version: u64) -> u64 { let mut h = base; for part in ["xcl-direct-dxil", tag, &direct_version.to_string()] { @@ -383,6 +391,9 @@ mod translated { let id = if Backend::current() != Backend::Hlsl { identity_for(&id.to_string(), &format!("dispatch-split-{}", crate::gpu::dxil::kernel::dispatch_piece_limit())) } else { id }; + let id = if Backend::current() != Backend::Hlsl { + direct_switches_tagged(id, crate::gpu::dxil::prelude::rdna_float_enabled(), crate::gpu::dxil::waterfall::collapse_enabled()) + } else { id }; // `XCL_X_LDS_ORDER=0`: the split wave64 form without its LDS order, in both translators let id = if crate::gpu::recomp::emit::lds_order_wanted() { id } else { identity_for(&id.to_string(), "xcl-x-lds-order-0") }; // `XCL_PER_LANE=1` (direct compute only): its entries under their own identity @@ -669,18 +680,21 @@ mod translated { } /// What the direct translator's output depends on besides the HLSL path's identity: its - /// version and sources, compact dispatch policy, the split target, the per-lane form (`direct_compute`). + /// version and sources, compact dispatch policy, the split target, its output switches + /// (`direct_switches_tagged`), the per-lane form (`direct_compute`). fn direct_output() -> u64 { static D: OnceLock = OnceLock::new(); *D.get_or_init(|| { direct_output_identity( crate::gpu::dxil::kernel::compact_nest_tag().as_deref(), crate::gpu::dxil::kernel::dispatch_piece_limit(), + crate::gpu::dxil::prelude::rdna_float_enabled(), + crate::gpu::dxil::waterfall::collapse_enabled(), ) }) } - pub(super) fn direct_output_identity(nest_tag: Option<&str>, split_limit: usize) -> u64 { + pub(super) fn direct_output_identity(nest_tag: Option<&str>, split_limit: usize, rdna_float: bool, waterfall_collapse: bool) -> u64 { let id = direct_version(crate::gpu::dxil::kernel::TRANSLATOR_VERSION, env!("XCL_DIRECT_DXIL_SOURCE_ID")); let id = match crate::gpu::dxil::kernel::compact_mode() { Some(on) => identity_for(&id.to_string(), if on { "xcl-direct-compact-1" } else { "xcl-direct-compact-0" }), @@ -691,6 +705,7 @@ mod translated { None => id, }; let id = identity_for(&id.to_string(), &format!("dispatch-split-{split_limit}")); + let id = direct_switches_tagged(id, rdna_float, waterfall_collapse); if crate::gpu::recomp::lower_dxil::enabled() { identity_for(&id.to_string(), concat!("xcl-per-lane-", env!("XCL_PER_LANE_SOURCE_ID"))) } else { @@ -2274,6 +2289,96 @@ mod shader_cache_tests { assert_eq!(id(Backend::Hlsl, 7, built), id(Backend::Hlsl, 8, "edited")); } + /// `XCL_RDNA_FLOAT=0` and `XCL_WATERFALL_COLLAPSE=0` change the direct translator's output, + /// so each gives the direct entries (also the rerouted enormous ones) another identity; with + /// both on, the default, the identity is the one entries already on disk were stored under. + #[test] + fn direct_output_switches_that_are_off_get_their_own_identity() { + use translated::{direct_output_identity, direct_switches_tagged, identity_with, rerouted_identity, Backend}; + let direct = identity_with("s", "t", Backend::DirectCompute, 1); + assert_eq!(direct_switches_tagged(direct, true, true), direct, "the default keeps the identity"); + let ids = [ + direct, + direct_switches_tagged(direct, false, true), + direct_switches_tagged(direct, true, false), + direct_switches_tagged(direct, false, false), + ]; + assert!(ids.iter().all(|&i| i != 0)); + assert_eq!(ids.iter().collect::>().len(), ids.len()); + // an enormous program's direct entry under an HLSL identity: the switches reach it + // through the direct output's identity + let hlsl = identity_with("s", "t", Backend::Hlsl, 1); + let rerouted = |rdna_float, collapse| rerouted_identity(hlsl, direct_output_identity(None, 8, rdna_float, collapse)); + assert_ne!(rerouted(true, true), rerouted(false, true)); + assert_ne!(rerouted(true, true), rerouted(true, false)); + assert_ne!(rerouted(false, true), rerouted(true, false)); + } + + /// The switches as a run sets them (the identities use OnceLock, so each case is a process of + /// its own on one disk cache): a direct entry a default run stored is a miss with either switch + /// off, on the direct backend and for the rerouted entries of the HLSL one; an HLSL-path entry + /// is shared whatever the switches. + #[test] + fn direct_output_switches_split_the_disk_cache() { + use std::process::Command; + + const RUN: &str = "XCL_TEST_DIRECT_SWITCH_RUN"; + if let Ok(case) = std::env::var(RUN) { + let (marker, expected) = case.split_once(':').unwrap(); + let marker = marker.parse::().unwrap(); + let read = |blob: &[u8], value, rerouted| { + translated::cached(&dxc::cache_dir(), blob, Stage::Compute, 0, 1, true, || { + Ok((compiled(value), rerouted)) + }).unwrap().dxil[67] + }; + // the direct backend's own entry, or on the HLSL backend an enormous program the + // direct translator made (`enormous`) + let hlsl = std::env::var("XCL_DIRECT_DXIL").is_ok_and(|v| v == "0"); + assert_eq!(read(b"console-switched", marker, hlsl), expected.parse::().unwrap()); + if hlsl { + assert_eq!(read(b"console-plain", marker + 16, false), 17); + } + return; + } + + let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + for backend in ["all", "0"] { + let dir = std::env::temp_dir().join(format!("xcl-translated-switches-{backend}-{}-{nonce}", std::process::id())); + for (rdna_float, collapse, case) in [ + (None, None, "1:1"), + (Some("0"), None, "2:2"), + (None, Some("0"), "3:3"), + (Some("0"), Some("0"), "4:4"), + (None, None, "9:1"), + (Some("0"), None, "9:2"), + ] { + let mut child = Command::new(std::env::current_exe().unwrap()); + child + .args(["gpu::recomp::pso::shader_cache_tests::direct_output_switches_split_the_disk_cache", "--exact", "--nocapture"]) + .env(RUN, case) + .env("XCL_DXIL_CACHE", &dir) + .env("XCL_TRANSLATED_CACHE", "1") + .env("XCL_DIRECT_DXIL", backend) + .env_remove("XCL_RDNA_FLOAT") + .env_remove("XCL_WATERFALL_COLLAPSE"); + if let Some(v) = rdna_float { + child.env("XCL_RDNA_FLOAT", v); + } + if let Some(v) = collapse { + child.env("XCL_WATERFALL_COLLAPSE", v); + } + let out = child.output().unwrap(); + assert!( + out.status.success(), + "XCL_DIRECT_DXIL={backend}, XCL_RDNA_FLOAT={rdna_float:?}, XCL_WATERFALL_COLLAPSE={collapse:?}: {}\n{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + } + std::fs::remove_dir_all(&dir).unwrap(); + } + } + #[test] fn huge_direct_threshold_from_env() { assert_eq!(huge_direct_min_from(None), Some(HUGE_DIRECT_MIN_DEFAULT)); diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index bfc8319f0f..4e77872a8a 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -35,9 +35,10 @@ fn semantic_pipeline_setting(name: &str) -> bool { "XCL_DIRECT_COMPACT_NEST" | "XCL_DIRECT_DXIL" | "XCL_DIRECT_HUGE" | "XCL_DIRECT_SPLIT_INSTRUCTIONS" | "XCL_DXIL_NO_OPT" | "XCL_HEAP_FLAT" | "XCL_HEAP_WINDOWS" | "XCL_LDS_TOP" | "XCL_LINK_MEM" | "XCL_MEM_ACCESSORS" | - "XCL_MW" | "XCL_PAGE_ALIAS" | "XCL_PER_LANE" | "XCL_PIXEL" | + "XCL_MW" | "XCL_PAGE_ALIAS" | "XCL_PER_LANE" | "XCL_PIXEL" | "XCL_RDNA_FLOAT" | "XCL_RECOMP_DXIL" | "XCL_RECOMP_NO_STRUCTURED" | "XCL_SAFE_BARRIER" | - "XCL_SPLIT_WAVE64" | "XCL_WAVE64_IDIOMS" | "XCL_WAVE64_SPIN" | "XCL_WT" | "XCL_X64_ALL_MASKS" | + "XCL_SPLIT_WAVE64" | "XCL_WATERFALL_COLLAPSE" | "XCL_WAVE64_IDIOMS" | + "XCL_WAVE64_SPIN" | "XCL_WT" | "XCL_X64_ALL_MASKS" | "XCL_XC" | "XCL_XS" | "XCL_XW" | "XCL_X_FAIL" | "XCL_X_LDS_ORDER" | "XCL_X_LDS_TOP" | "XCL_X_SLOT" | "XCL_X_SPIN" | "XCL_X_THREADS" | "XCL_X_WAVES") } @@ -137,6 +138,10 @@ fn warm_settings_ignore_waits_watchdogs_and_traces_but_keep_pipeline_switches() all.extend(left_out); assert_eq!(plain, key(&all)); assert_ne!(plain, key(&[("XCL_DIRECT_DXIL", "1"), ("XCL_PAGE_ALIAS", "1")]), "page alias stays in the key"); + // switches of the direct translator's output (#3520) + for knob in ["XCL_RDNA_FLOAT", "XCL_WATERFALL_COLLAPSE"] { + assert_ne!(plain, key(&[("XCL_DIRECT_DXIL", "1"), (knob, "0")]), "{knob} stays in the key"); + } assert_eq!(plain, key(&[("XCL_DIRECT_DXIL", "1"), ("XCL_TRACEX", "1")]), "unknown diagnostics stay cache-neutral until reviewed"); }