diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index c56279aad..acd88a9bd 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -626,6 +626,12 @@ impl Conts { /// leaves at its next iteration and the program runs to its end. const LOOP_GUARD: &str = "if (xcl_budget == 0u) break; xcl_budget--;"; +/// Structured code longer than this many bytes per byte of program falls back to the `pc` form. +/// Each guard of the structured form lists every pending skip flag, so it grows with the +/// square of the flags: one retail program made 455 B per byte (148 MB, 14 MB as `pc` blocks), +/// the next largest 61 (#1307). +const STRUCTURED_BYTES_PER_CODE_BYTE: usize = 128; + /// 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`. @@ -1372,6 +1378,7 @@ pub(crate) fn constant_samplers(insts: &[Decoded], leaders: &BTreeSet) -> B } pub fn translate(code: &[u8], opt: &Options) -> Result { + let program = code; let normalized; let opt = if opt.mem_windows & HEAP_WINDOWS != 0 { normalized = Options { mem_windows: opt.mem_windows & !HEAP_WINDOWS, heap_windows: true, ..opt.clone() }; @@ -1475,6 +1482,9 @@ pub fn translate(code: &[u8], opt: &Options) -> Result { // structured control flow: plain if / loops, no block dispatch on `pc` (much // smaller for DXC and the driver on big programs) emit_structured(&sctx, &mut em, &mut code, 0, u64::MAX, None, 0, &Conts::default(), &BTreeSet::new(), 1, &mut blocks, &mut flags); + if code.len() > STRUCTURED_BYTES_PER_CODE_BYTE * program.len() { + return translate(program, &Options { structured: false, ..opt.clone() }); + } } else { if gs_jump.is_some() && opt.split_wave64 && opt.wave64 { code.push_str(" uint pc = xcl_entry_pc;\n"); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 0705c4593..06f7e5ec4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7529,6 +7529,38 @@ fn fspx_containers_translate_and_compile() { assert_eq!(ok, n); } +/// Structured code past `STRUCTURED_BYTES_PER_CODE_BYTE` is translated in the `pc` form (#1307). +/// n conditional forward jumps whose targets all come after them, in the same order, keep up to +/// n skip flags pending, and every guard lists them: 200 cross the bound, 50 do not. This checks +/// the switch only. Here the `pc` form keeps the same stretch under one `pc` guard and is no +/// smaller; the gain is on programs whose `pc` form is split into blocks, such as the retail +/// one of #1307 (148 MB -> 14 MB). +#[test] +fn oversized_structured_code_falls_back_to_pc_blocks() { + let crossing = |n: usize| { + let i = |name: &str, e: &str, f: &[(&str, u64)]| encode(name, e, f, None).unwrap(); + let mut code: Vec = Vec::new(); + let mut at = Vec::new(); + for k in 0..n as u64 { + code.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)])); + code.extend(i("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 128 + k % 16)])); + at.push(code.len()); + code.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0)])); + } + for a in at { + let d = ((code.len() - a) / 4 - 1) as u16; + code[a..a + 2].copy_from_slice(&d.to_le_bytes()); + code.extend(i("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 0), ("VSRC1", 1)])); + } + code.extend(i("S_ENDPGM", "ENC_SOPP", &[])); + let t = translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + t.hlsl.contains("pc ==") + }; + assert!(!crossing(50), "50 crossing jumps: structured"); + assert!(crossing(200), "200 crossing jumps: past the bound, pc blocks"); +} + /// Random nests of loops with `continue` / `break` of any loop around them – backward jumps out of /// inner loops, loops whose end is an inner loop's – give the same state as structured code and as /// the block dispatcher (`structured: false`).