From 66da2d923eb6ded6abd5201776cae2ceb7853f52 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:02:22 -0700 Subject: [PATCH 1/4] Recompiler: the compact form's class key marks its sampler section Hardening from the review of #2034. Whether an instruction has a static sampler follows from the moves before it in its block, which are in the key, so no two blocks share a key today; the flag makes the key's sections delimit themselves. The six largest compute programs of a retail pack come out byte for byte as before. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/compact.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/compact.rs b/crates/xcl/src/gpu/dxil/compact.rs index 064f851c3..920d59ca7 100644 --- a/crates/xcl/src/gpu/dxil/compact.rs +++ b/crates/xcl/src/gpu/dxil/compact.rs @@ -68,9 +68,9 @@ pub fn plan(cfg: &Cfg, lw: &Lw) -> Option { key.push(d.words.len() as u64); key.extend(d.words.iter().map(|&w| w as u64)); key.push(d.literal.map_or(u64::MAX, |l| l as u64)); - if let Some(s) = lw.static_samplers.get(&d.pc) { - key.extend(s.iter().map(|&w| w as u64)); - } + let sampler = lw.static_samplers.get(&d.pc); + key.push(sampler.is_some() as u64); + key.extend(sampler.into_iter().flatten().map(|&w| w as u64)); // what `Lw::write_mask` asks of this address, for any register a mask can go to if let Some(&l) = lw.whole_live.get(&d.pc) { let sdwa = ext_encoding(d).and_then(|e| crate::gpu::rdna2::field_value(e, &d.words, "SDST")); From f1b313fb500f65645f50e653a8bf7abc497802b2 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:02:22 -0700 Subject: [PATCH 2/4] Recompiler: direct compact form stores a branch's target in the entry The third-largest compute program of a retail pack repeats itself as the two largest do (13,890 blocks of 2,643 kinds), but its branches go to 6,530 different pairs of offsets. As an index into a table of pairs its tables were about 24,000 words, over the limit, and it kept the plain form: 3.9 MB of DXIL. In it, as in the other large programs, a block's second successor is the next block or the same as its first. When every conditional branch of a program falls through to the next block, a table entry now holds the taken successor itself if that makes the tables smaller: 11,287 words here, in one table. The limit goes from 8,192 to 12,288 words, the size #1315 measured an RTX 5070 Ti accepting in the HLSL form, and the program takes the form. Part of #1269. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/compact.rs | 55 ++++++++++++++++-------- crates/xcl/src/gpu/dxil/compact_tests.rs | 44 +++++++++++++++++++ 2 files changed, 80 insertions(+), 19 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/compact.rs b/crates/xcl/src/gpu/dxil/compact.rs index 920d59ca7..bf9f66015 100644 --- a/crates/xcl/src/gpu/dxil/compact.rs +++ b/crates/xcl/src/gpu/dxil/compact.rs @@ -17,21 +17,23 @@ pub struct Plan { pub next: Vec<(u32, u32)>, /// the distinct pairs of offsets from a block to its two successors, sorted pub steps: Vec<(u32, u32)>, + /// an entry holds the block's taken successor itself; the other one is the next block + pub inline: bool, } /// Most 32-bit words of tables a program may need to take the form by itself. -const MAX_TABLE_WORDS: usize = 8192; +const MAX_TABLE_WORDS: usize = 12288; impl Plan { - /// Bits of a table entry: the class, and the index of the pair of offsets. + /// Bits of a table entry: the class, and the taken successor or the index of the pair of offsets. fn widths(&self) -> (u32, u32) { - (bits(self.rep.len()), bits(self.steps.len())) + (bits(self.rep.len()), if self.inline { bits(self.order.len()) } else { bits(self.steps.len()) }) } - /// 32-bit words of the two tables. + /// 32-bit words of the tables. pub fn words(&self) -> usize { let (cb, pb) = self.widths(); - (self.order.len() * (cb + pb) as usize).div_ceil(32) + 1 + self.steps.len() * 2 + (self.order.len() * (cb + pb) as usize).div_ceil(32) + 1 + if self.inline { 0 } else { self.steps.len() * 2 } } } @@ -60,6 +62,7 @@ pub fn plan(cfg: &Cfg, lw: &Lw) -> Option { } let mut ids: HashMap<(Vec, &'static str), u32> = HashMap::new(); let (mut class, mut rep, mut next) = (Vec::new(), Vec::new(), Vec::new()); + let mut falls = true; for (n, &b) in order.iter().enumerate() { let blk = &cfg.blocks[b]; let mut key = vec![cfg.headers.contains(&b) as u64]; @@ -84,7 +87,10 @@ pub fn plan(cfg: &Cfg, lw: &Lw) -> Option { let (end, to) = match blk.term { Term::End => ("end", (END, END)), Term::Jump(t) => ("jump", (number[t], number[t])), - Term::Cond(i, t, f) => (cfg.insts[i].inst.name, (number[t], number[f])), + Term::Cond(i, t, f) => { + falls &= number[f] == n as u32 + 1; + (cfg.insts[i].inst.name, (number[t], number[f])) + } }; let fresh = ids.len() as u32; let c = *ids.entry((key, end)).or_insert(fresh); @@ -95,7 +101,10 @@ pub fn plan(cfg: &Cfg, lw: &Lw) -> Option { next.push(to); } let steps: BTreeSet<(u32, u32)> = next.iter().enumerate().map(|(b, &to)| offsets(b, to)).collect(); - let p = Plan { order, class, rep, next, steps: steps.into_iter().collect() }; + let mut p = Plan { order, class, rep, next, steps: steps.into_iter().collect(), inline: false }; + let pairs = p.words(); + p.inline = falls; + p.inline = falls && p.words() < pairs; let (cb, pb) = p.widths(); (cb + pb <= 32).then_some(p) } @@ -134,17 +143,21 @@ pub fn emit(lw: &mut Lw, cfg: &Cfg, p: &Plan, head: Block, exit: Block) -> LResu let n = p.order.len(); let (cb, pb) = p.widths(); let width = cb + pb; - let mut packed = vec![0u32; p.words() - p.steps.len() * 2]; + let mut packed = vec![0u32; (n * width as usize).div_ceil(32) + 1]; for (b, &to) in p.next.iter().enumerate() { - let pair = p.steps.binary_search(&offsets(b, to)).expect("a pair of the plan") as u32; - let e = (p.class[b] | pair << cb) as u64; + let field = match (p.inline, to.0) { + (true, END) => 0, + (true, t) => t, + _ => p.steps.binary_search(&offsets(b, to)).expect("a pair of the plan") as u32, + }; + let e = (p.class[b] | field << cb) as u64; let at = b * width as usize; packed[at / 32] |= (e << (at % 32)) as u32; packed[at / 32 + 1] |= (e << (at % 32) >> 32) as u32; } - let steps: Vec = p.steps.iter().flat_map(|&(t, f)| [t, f]).collect(); let blocks = table(lw, "xcl_blocks", &packed); - let steps = table(lw, "xcl_steps", &steps); + let steps: Vec = p.steps.iter().flat_map(|&(t, f)| [t, f]).collect(); + let steps = (!p.inline).then(|| table(lw, "xcl_steps", &steps)); let (at, to) = (lw.tmp_var(), lw.tmp_var()); let (at, to, budget) = (slot(lw, at), slot(lw, to), slot(lw, VAR_BUDGET)); @@ -164,13 +177,17 @@ pub fn emit(lw: &mut Lw, cfg: &Cfg, p: &Plan, head: Block, exit: Block) -> LResu let hi = lw.shl(hi, up); let e = lw.or(lo, hi); let class = lw.andc(e, (1u32 << cb) - 1); - let pair = lw.lshrc(e, cb); - let pair = lw.andc(pair, (1u32 << pb) - 1); - let pi = lw.shlc(pair, 1); - let step_t = word(lw, steps, pi); - let pi1 = lw.addc(pi, 1); - let step_f = word(lw, steps, pi1); - let (taken, not_taken) = (lw.add(b, step_t), lw.add(b, step_f)); + let field = lw.lshrc(e, cb); + let field = lw.andc(field, (1u32 << pb) - 1); + let (taken, not_taken) = if let Some(steps) = steps { + let pi = lw.shlc(field, 1); + let step_t = word(lw, steps, pi); + let pi1 = lw.addc(pi, 1); + let step_f = word(lw, steps, pi1); + (lw.add(b, step_t), lw.add(b, step_f)) + } else { + (field, lw.addc(b, 1)) + }; let cases: Vec = p.rep.iter().map(|_| lw.block(Some(0))).collect(); let latch = lw.block(Some(0)); let arms: Vec<(Value, Block)> = cases.iter().enumerate().skip(1).map(|(k, &c)| (lw.c(k as u32), c)).collect(); diff --git a/crates/xcl/src/gpu/dxil/compact_tests.rs b/crates/xcl/src/gpu/dxil/compact_tests.rs index c88ead868..a711ea003 100644 --- a/crates/xcl/src/gpu/dxil/compact_tests.rs +++ b/crates/xcl/src/gpu/dxil/compact_tests.rs @@ -102,6 +102,50 @@ fn gpu_compact_form_spends_the_loop_budget_as_the_plain_form() { assert_eq!(&c.sgpr[..2], &p.sgpr[..2]); } +fn early_outs(copies: usize) -> Vec { + let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); + let mut v = Vec::new(); + for k in 0..copies { + v.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 1)])); + v.extend(i("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 2)])); + v.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", ((copies - 1 - k) * 3 + 1) as u64)])); + } + v.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 3), ("SSRC0", 129)])); + v.extend(i("S_ENDPGM", "ENC_SOPP", &[])); + v +} + +#[test] +fn branches_to_many_places_keep_their_target_in_the_entry() { + let code = early_outs(1000); + let cfg = super::cfg::Cfg::build(&code).unwrap(); + let leaders = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); + let o = KernelOptions { wave64: true, split: true, mem_windows: 1, sampler_base: 0 }; + let lw = super::lower::Lw::new(&cfg.insts, &leaders, o, &super::kernel::Entry::Kernel); + let p = super::compact::plan(&cfg, &lw).unwrap(); + assert_eq!((p.order.len(), p.steps.len()), (1002, 1002)); + assert!(p.words() < 500, "{} words", p.words()); +} + +#[test] +fn gpu_compact_form_with_targets_in_the_entry_runs_as_the_plain_form() { + match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) { + (Ok(_), Ok(true), Ok(_)) => {} + (a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }), + } + let mut st = KernelState::default(); + (st.sgpr[1], st.sgpr[2]) = (3, 12); + let code = early_outs(40); + let o = KernelOptions { wave64: true, split: runner::split_wave64(), mem_windows: 1, sampler_base: 0 }; + let run = |compact: bool| { + let k = translate_full_as(&code, o, compact).unwrap_or_else(|e| panic!("compact {compact}: {e}")); + runner::run_with_statics(&k.dxil, &st, &[], &[], &[], &k.static_samplers).expect("GPU run") + }; + let (c, p) = (run(true), run(false)); + assert_eq!(&c.sgpr[..4], &[12, 3, 12, 0]); + assert_eq!((c.sgpr, c.vgpr, c.scc), (p.sgpr, p.vgpr, p.scc)); +} + #[test] fn a_mask_read_whole_after_one_copy_only_splits_the_class() { let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); From 623aaaaa2227c029f430c85fd2448e5069e7c437 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:50:42 -0700 Subject: [PATCH 3/4] Tests: the direct compact form runs a table over 8,192 words on a card The limit on the compact form's tables went from 8,192 to 12,288 words and no test ran a table in between. This one does: two segments of 8,192 blocks each, 16,387 blocks of 5 classes, a table of 9,219 words with the targets in the entries, taken by the rule with XCL_DIRECT_COMPACT unset. Five starting values leave it by falling through, by an early carry, at the last branch of the first segment and at entries past the old limit, and every register is compared. The fixture is WebIdentity1's, from their test report on the pull request (RTX 3090 in the split wave64 form, AMD integrated graphics with native wave64). Its prints, timing and memory read are taken out, and it returns when XCL_DIRECT_COMPACT is set, as the size test does. Co-authored-by: WebIdentity1 <212448219+WebIdentity1@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/compact_tests.rs | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/crates/xcl/src/gpu/dxil/compact_tests.rs b/crates/xcl/src/gpu/dxil/compact_tests.rs index a711ea003..56d2665f1 100644 --- a/crates/xcl/src/gpu/dxil/compact_tests.rs +++ b/crates/xcl/src/gpu/dxil/compact_tests.rs @@ -168,3 +168,64 @@ fn a_mask_read_whole_after_one_copy_only_splits_the_class() { assert_eq!(p.order.len(), 4); assert_ne!(p.class[0], p.class[2]); } + +#[test] +fn gpu_compact_inline_table_above_old_cap_checks_late_entries() { + if compact_mode().is_some() { + return; + } + match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) { + (Ok(_), Ok(true), Ok(_)) => {} + (a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }), + } + const COPIES: usize = 8192; + let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); + let mut code = Vec::new(); + code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 2)])); + for r in [2, 3] { + code.extend(i("V_MOV_B32", "ENC_VOP1", &[("VDST", r), ("SRC0", 256 + r)])); + } + for marker in [3, 4] { + for k in 0..COPIES { + code.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 1)])); + code.extend(i("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 256), ("VSRC1", 1)])); + let offset = (COPIES - 1 - k) * 3 + 1; + assert!(offset <= i16::MAX as usize); + code.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", offset as u64)])); + } + code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", marker), ("SSRC0", 129)])); + } + code.extend(i("S_ENDPGM", "ENC_SOPP", &[])); + let o = KernelOptions { wave64: true, split: runner::split_wave64(), mem_windows: 1, sampler_base: 0 }; + let cfg = super::cfg::Cfg::build(&code).unwrap(); + let leaders = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); + let lw = super::lower::Lw::new(&cfg.insts, &leaders, o, &super::kernel::Entry::Kernel); + let p = super::compact::plan(&cfg, &lw).unwrap(); + assert!(p.inline && p.words() > 8192 && p.words() <= 12288); + assert!(super::compact::wanted(&cfg, &p)); + let k = translate_full(&code, o).unwrap(); + for initial in [0u32, u32::MAX - 1, 0u32.wrapping_sub(COPIES as u32), 0u32.wrapping_sub((2 * COPIES) as u32), 0u32.wrapping_sub(COPIES as u32 + 1)] { + let mut st = KernelState::default(); + st.sgpr[0] = initial; + st.sgpr[1] = 1; + st.sgpr[2] = 0x1234; + for lane in 0..KERNEL_LANES { + st.vgpr[lane][..4].copy_from_slice(&[lane as u32, 17, 0x12345678, 0x87654321]); + } + let (mut value, mut count, mut marks, mut carry) = (initial, 0u32, [0u32; 2], false); + for mark in &mut marks { + for n in 0..COPIES { + (value, carry) = value.overflowing_add(1); + count += 1; + if carry { break; } + if n + 1 == COPIES { *mark = 1; } + } + } + let out = runner::run_with_statics(&k.dxil, &st, &[], &[], &[], &k.static_samplers).expect("GPU run"); + assert_eq!(&out.sgpr[..5], &[value, 1, 0x1234, marks[0], marks[1]], "initial {initial:#x}"); + assert_eq!(out.scc, carry as u32); + for lane in 0..KERNEL_LANES { + assert_eq!(&out.vgpr[lane][..4], &[lane as u32, 17 + count * lane as u32, 0x12345678, 0x87654321], "initial {initial:#x} lane {lane}"); + } + } +} From 14ae010731985fa15bb4939aca30616b114cfbd9 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:19:02 -0700 Subject: [PATCH 4/4] Recompiler: the compact form's larger tables only on a GPU with 32-lane waves The table limit went from 8,192 to 12,288 words on what an RTX 5070 Ti's driver took. On an RX 9070 XT (bielfreedom's report on the pull request) the program that the larger limit lets in built in neither form within 24 GB of memory, and its compact form reached 24 GB in 31 s against about 205 s for the plain one. The larger limit now applies where the GPU runs only 32-lane waves, which is what direct_compute already knows about the device; a GPU with 64-lane waves keeps the limit of 8,192 words, so that program stays plain there as it is on main. The test translates the 9,219-word program for both kinds of GPU. Its builder is the one of the card test above it, lifted into a function. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/compact.rs | 8 +++-- crates/xcl/src/gpu/dxil/compact_tests.rs | 44 +++++++++++++++++------- crates/xcl/src/gpu/dxil/kernel.rs | 28 +++++++++------ crates/xcl/src/gpu/recomp/pso.rs | 2 +- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/compact.rs b/crates/xcl/src/gpu/dxil/compact.rs index bf9f66015..17945c9d6 100644 --- a/crates/xcl/src/gpu/dxil/compact.rs +++ b/crates/xcl/src/gpu/dxil/compact.rs @@ -22,7 +22,9 @@ pub struct Plan { } /// Most 32-bit words of tables a program may need to take the form by itself. -const MAX_TABLE_WORDS: usize = 12288; +pub const TABLE_WORDS: usize = 8192; +/// `TABLE_WORDS` on a GPU that runs only 32-lane waves. +pub const TABLE_WORDS_WAVE32: usize = 12288; impl Plan { /// Bits of a table entry: the class, and the taken successor or the index of the pair of offsets. @@ -38,11 +40,11 @@ impl Plan { } /// Whether a program takes the form by itself: large, mostly repeated blocks, small tables. -pub fn wanted(cfg: &Cfg, p: &Plan) -> bool { +pub fn wanted(cfg: &Cfg, p: &Plan, tables: usize) -> bool { let insts = |n: usize| cfg.blocks[p.order[n]].insts.len(); let all: usize = (0..p.order.len()).map(insts).sum(); let once: usize = p.rep.iter().map(|&n| insts(n)).sum(); - large(cfg) && once * 10 < all * 3 && p.words() <= MAX_TABLE_WORDS + large(cfg) && once * 10 < all * 3 && p.words() <= tables } /// Whether a program is large enough for `wanted` to be asked. diff --git a/crates/xcl/src/gpu/dxil/compact_tests.rs b/crates/xcl/src/gpu/dxil/compact_tests.rs index 56d2665f1..ae931bc11 100644 --- a/crates/xcl/src/gpu/dxil/compact_tests.rs +++ b/crates/xcl/src/gpu/dxil/compact_tests.rs @@ -169,16 +169,7 @@ fn a_mask_read_whole_after_one_copy_only_splits_the_class() { assert_ne!(p.class[0], p.class[2]); } -#[test] -fn gpu_compact_inline_table_above_old_cap_checks_late_entries() { - if compact_mode().is_some() { - return; - } - match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) { - (Ok(_), Ok(true), Ok(_)) => {} - (a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }), - } - const COPIES: usize = 8192; +fn two_segments(copies: usize) -> Vec { let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); let mut code = Vec::new(); code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 2)])); @@ -186,23 +177,50 @@ fn gpu_compact_inline_table_above_old_cap_checks_late_entries() { code.extend(i("V_MOV_B32", "ENC_VOP1", &[("VDST", r), ("SRC0", 256 + r)])); } for marker in [3, 4] { - for k in 0..COPIES { + for k in 0..copies { code.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 1)])); code.extend(i("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 256), ("VSRC1", 1)])); - let offset = (COPIES - 1 - k) * 3 + 1; + let offset = (copies - 1 - k) * 3 + 1; assert!(offset <= i16::MAX as usize); code.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", offset as u64)])); } code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", marker), ("SSRC0", 129)])); } code.extend(i("S_ENDPGM", "ENC_SOPP", &[])); + code +} + +#[test] +fn a_gpu_with_64_lane_waves_keeps_the_first_table_limit() { + if compact_mode().is_some() { + return; + } + let code = two_segments(8192); + let o = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; + let cs = crate::gpu::recomp::CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let size = |wave32_only| super::kernel::translate_compute_on(&code, o, &cs, wave32_only).unwrap().dxil.len(); + let (compact, plain) = (size(true), size(false)); + assert!(compact * 20 < plain, "{compact} B on a GPU with 32-lane waves only, {plain} B on one with 64-lane waves"); +} + +#[test] +fn gpu_compact_inline_table_above_old_cap_checks_late_entries() { + if compact_mode().is_some() { + return; + } + match (runner::adapter(), runner::wave64_programs_supported(), crate::gpu::recomp::dxc::location()) { + (Ok(_), Ok(true), Ok(_)) => {} + (a, _, d) => return runner::skip_test(if a.is_err() { "no GPU" } else if d.is_err() { "no DXC" } else { "no wave64" }), + } + const COPIES: usize = 8192; + let code = two_segments(COPIES); let o = KernelOptions { wave64: true, split: runner::split_wave64(), mem_windows: 1, sampler_base: 0 }; let cfg = super::cfg::Cfg::build(&code).unwrap(); let leaders = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); let lw = super::lower::Lw::new(&cfg.insts, &leaders, o, &super::kernel::Entry::Kernel); let p = super::compact::plan(&cfg, &lw).unwrap(); assert!(p.inline && p.words() > 8192 && p.words() <= 12288); - assert!(super::compact::wanted(&cfg, &p)); + assert!(super::compact::wanted(&cfg, &p, super::compact::TABLE_WORDS_WAVE32)); let k = translate_full(&code, o).unwrap(); for initial in [0u32, u32::MAX - 1, 0u32.wrapping_sub(COPIES as u32), 0u32.wrapping_sub((2 * COPIES) as u32), 0u32.wrapping_sub(COPIES as u32 + 1)] { let mut st = KernelState::default(); diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 94ce0b37d..5823020f5 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -79,33 +79,33 @@ pub fn compact_mode() -> Option { /// `translate_full` in the compact form (`compact = true`) or the plain one. #[cfg(test)] pub fn translate_full_as(code: &[u8], o: KernelOptions, compact: bool) -> Result { - translate_entry(code, o, Entry::Kernel, Some(compact)) + translate_entry(code, o, Entry::Kernel, Some(compact), super::compact::TABLE_WORDS_WAVE32) } pub fn translate_full(code: &[u8], o: KernelOptions) -> Result { - translate_entry(code, o, Entry::Kernel, compact_mode()) + translate_entry(code, o, Entry::Kernel, compact_mode(), super::compact::TABLE_WORDS_WAVE32) } /// A mesh-mode (NGG) program: the first pass that writes one record per group into /// `res` (the recompiler's `Mode::Mesh`; the pass-through mesh shader draws them). pub fn translate_mesh(code: &[u8], o: KernelOptions, ms: &MsInterface) -> Result { - translate_entry(code, o, Entry::Mesh(ms.clone()), None) + translate_entry(code, o, Entry::Mesh(ms.clone()), None, 0) } /// A vertex shader (the recompiler's `Mode::Vertex`; wave-size independent). pub fn translate_vertex(code: &[u8], o: KernelOptions, vs: &VsInterface) -> Result { - translate_entry(code, o, Entry::Vertex(vs.clone()), None) + translate_entry(code, o, Entry::Vertex(vs.clone()), None, 0) } /// A pixel shader (the recompiler's `Mode::Pixel`; wave-size independent). pub fn translate_pixel(code: &[u8], o: KernelOptions, ps: &PsInterface) -> Result { - translate_entry(code, o, Entry::Pixel(ps.clone()), None) + translate_entry(code, o, Entry::Pixel(ps.clone()), None, 0) } /// A geometry-mode (ES+GS) program: the compute pass that writes one mesh record per /// group of 21 triangles into `res` (the recompiler's `Mode::Geometry`). Always wave64. pub fn translate_geometry(code: &[u8], o: KernelOptions, g: &GsInterface) -> Result { - translate_entry(code, KernelOptions { wave64: true, ..o }, Entry::Geometry(g.clone()), None) + translate_entry(code, KernelOptions { wave64: true, ..o }, Entry::Geometry(g.clone()), None, 0) } /// The entry ABI. @@ -121,17 +121,23 @@ pub enum Entry { /// A compute-mode program (the CS ABI of `cs`). pub fn translate_compute(code: &[u8], o: KernelOptions, cs: &CsInterface) -> Result { - translate_entry(code, o, Entry::Compute(cs.clone()), compact_mode()) + translate_compute_on(code, o, cs, true) } -fn translate_entry(code: &[u8], o: KernelOptions, entry: Entry, compact: Option) -> Result { +/// `translate_compute` for a GPU: one that runs only 32-lane waves takes larger compact tables. +pub fn translate_compute_on(code: &[u8], o: KernelOptions, cs: &CsInterface, wave32_only: bool) -> Result { + let tables = if wave32_only { super::compact::TABLE_WORDS_WAVE32 } else { super::compact::TABLE_WORDS }; + translate_entry(code, o, Entry::Compute(cs.clone()), compact_mode(), tables) +} + +fn translate_entry(code: &[u8], o: KernelOptions, entry: Entry, compact: Option, tables: usize) -> Result { // SSA construction recurses along the CFG (one level per guarded memory access between // a register's definition and its use): a thread with a large stack for big programs let code = code.to_vec(); std::thread::Builder::new() .name("xcl-dxil-translate".into()) .stack_size(256 << 20) - .spawn(move || translate_on_this_thread(&code, o, entry, compact)) + .spawn(move || translate_on_this_thread(&code, o, entry, compact, tables)) .map_err(|e| format!("translator thread: {e}"))? .join() .map_err(|e| { @@ -155,7 +161,7 @@ fn mem_windows(s: &mut Shader, mem_windows: u32, coherent: bool) -> (Vec ((0..n).map(|k| if coherent { s.raw_uav_coherent(2 + k, 0) } else { s.raw_uav(2 + k, 0) }).collect(), None) } -fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry, compact: Option) -> Result { +fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry, compact: Option, tables: usize) -> Result { let geo = match &entry { Entry::Geometry(g) => Some(g.clone()), _ => None, @@ -238,7 +244,7 @@ fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry, compact } let plan = if (compute || kernel) && compact.unwrap_or_else(|| super::compact::large(&cfg)) { super::compact::plan(&cfg, &lw) } else { None }; - let plan = plan.filter(|p| compact.unwrap_or_else(|| super::compact::wanted(&cfg, p))); + let plan = plan.filter(|p| compact.unwrap_or_else(|| super::compact::wanted(&cfg, p, tables))); if let Some(p) = &plan { crate::xlog_info!("[SHADER] direct DXIL compact form: {} blocks of {} kinds", p.order.len(), p.rep.len()); } diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index b6dd120ff..aa228dd6e 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -734,7 +734,7 @@ fn direct_compute(blob: &[u8], sampler_base: u32, windows: u32, split_wave64: bo } } } - let k = crate::gpu::dxil::kernel::translate_compute(code, o, &cs)?; + let k = crate::gpu::dxil::kernel::translate_compute_on(code, o, &cs, split_wave64)?; dump_direct(blob, &k.dxil); Ok(Compiled { dxil: k.dxil, samplers: k.static_samplers }) }