diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 7851791c94..68143f0c31 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4747,7 +4747,12 @@ impl Em { let ty = if *cmp { "SamplerComparisonState" } else { "SamplerState" }; let _ = writeln!(h, "{ty} xcl_ss{k} : register(s{k}, space1);"); } - h.push_str(PRELUDE); + // `XCL_LOOP_BUDGET`: only a budget other than the default changes the prelude, so the + // HLSL (and the DXIL cache keyed by it) stays as it was without the variable + match super::loop_budget() { + n if n == 1 << 16 => h.push_str(PRELUDE), + n => h.push_str(&PRELUDE.replacen("static uint xcl_budget = 1u << 16;", &format!("static uint xcl_budget = {n}u;"), 1)), + } if matches!(self.opt.mode, Mode::Pixel(_)) { h.push_str(PRELUDE_PIXEL); } diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index d1e7d8bc4e..47cae3e57e 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -639,7 +639,7 @@ impl Conts { } /// At the top of every emitted loop: a lane's loop iterations are capped (`xcl_budget`, in -/// the prelude: 65 536) – console code that loops on what XCL passed it wrong (an address, a +/// the prelude: 65 536, or what the variable `XCL_LOOP_BUDGET` sets, `loop_budget`) – console code that loops on what XCL passed it wrong (an address, a /// count) must not keep the PC GPU busy for minutes (a process with such work in flight cannot /// even be terminated). Once the budget is spent every loop /// leaves at its next iteration and the program runs to its end. @@ -651,6 +651,17 @@ const LOOP_GUARD: &str = "if (xcl_budget == 0u) break; xcl_budget--;"; /// the next largest 61 (#1307). const STRUCTURED_BYTES_PER_CODE_BYTE: usize = 128; +/// The loop iteration budget of a lane (`LOOP_GUARD`): `XCL_LOOP_BUDGET`, a whole number from 1, +/// or 65 536. A lower one shows whether a GPU busy for seconds is a loop running on data it +/// was not given (#505); a translation with it set is not cached (`pso::cache_allowed`). +pub(crate) fn loop_budget() -> u32 { + loop_budget_from(std::env::var("XCL_LOOP_BUDGET").ok().as_deref()) +} + +fn loop_budget_from(v: Option<&str>) -> u32 { + v.and_then(|v| v.trim().parse::().ok()).filter(|&n| n > 0).unwrap_or(1 << 16) +} + /// The loops the translator emits: ranges spanned by backward jumps (far jumps included), /// partly overlapping ones merged until they nest, sorted. `x64_masks` uses them for the /// exits through `LOOP_GUARD`. diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index c9a50b72b4..b75ae8fd73 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -767,7 +767,7 @@ fn cached_shader( } fn cache_allowed(stage: Stage, present: impl Fn(&str) -> bool) -> bool { - !["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT"] + !["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT", "XCL_LOOP_BUDGET"] .iter() .any(|name| present(name)) && (stage != Stage::Pixel @@ -1717,7 +1717,7 @@ mod shader_cache_tests { #[test] fn shader_cache_bypasses_debug_overrides() { - for name in ["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT"] { + for name in ["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT", "XCL_LOOP_BUDGET"] { assert!(!cache_allowed(Stage::Compute, |v| v == name)); assert!(!cache_allowed(Stage::Vertex, |v| v == name)); assert!(!cache_allowed(Stage::Pixel, |v| v == name)); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 8965a41caf..31b8f87dde 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7997,6 +7997,53 @@ fn gpu_s_round_mode_controls_f16_rounding_across_modes() { } } +/// `XCL_LOOP_BUDGET`: a whole number from 1, else the default 65 536. +#[test] +fn loop_budget_reads_a_whole_number_from_one() { + use super::loop_budget_from as f; + assert_eq!(f(None), 65_536); + assert_eq!(f(Some("4096")), 4096); + assert_eq!(f(Some(" 17 ")), 17); + assert_eq!(f(Some("0")), 65_536); + assert_eq!(f(Some("-5")), 65_536); + assert_eq!(f(Some("lots")), 65_536); +} + +/// The prelude's loop budget (`XCL_LOOP_BUDGET`, 65 536 unset) caps a loop whose trip count +/// never comes (`s_cmp_lg` against 0 after the first increment) stops after that many +/// iterations, 4096 as well as 65 536. +#[test] +fn gpu_loop_budget_caps_a_loop() { + if !gpu_ready() { + return; + } + let p = prog(&[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 128)], None), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 10), ("SSRC0", 10), ("SSRC1", 129)], None), + ("S_CMP_LG_U32", "ENC_SOPC", &[("SSRC0", 10), ("SSRC1", 1)], None), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFD)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", 10)], None), + ]); + let opt = Options { wave64: false, hw_wave: 32, ..Options::default() }; + let hlsl = translate(&p, &opt).unwrap().hlsl; + // without the variable the prelude is the one of before it existed + let line = format!("static uint xcl_budget = {};", if super::loop_budget() == 1 << 16 { "1u << 16".to_string() } else { format!("{}u", super::loop_budget()) }); + assert!(hlsl.contains(&line), "{hlsl}"); +let mut st = KernelState::default(); + st.sgpr[1] = 0; + let runs: Vec = [4096u32, 65_536] + .iter() + .map(|b| { + let h = hlsl.replacen(&line, &format!("static uint xcl_budget = {b}u;"), 1); + let dxil = super::dxc::compile(&h, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e} +{h}")); + runner::run(&dxil, &st, &[]).unwrap().vgpr[0][0] + }) + .collect(); + assert_eq!(runs[1] - runs[0], 65_536 - 4096, "iterations run: {runs:?}"); + assert!((4096..=4097).contains(&runs[0]), "iterations run: {runs:?}"); +} + #[test] fn gpu_mad_nf_f16_rounds_intermediate_in_binary16() { if !gpu_ready() {