From ee1efda6ff514963fec75e9691683c3671c90719 Mon Sep 17 00:00:00 2001 From: Krzysztof Kania <9355751+KaniaKrzysztof@users.noreply.github.com> Date: Sat, 28 Nov 2026 14:11:50 +0100 Subject: [PATCH 1/2] Recompiler: initialize static sampler handles before the entry branch DispatchX can branch before the first image instruction requests a sampler. Insert both handle operations before the terminator and restore the caller insertion state. Exercise two static samplers with full and partial groups. Assisted-by: OpenAI Codex (GPT-6) --- crates/xcl/src/gpu/dxil/lower_image.rs | 3 +++ crates/xcl/src/gpu/recomp/tests.rs | 32 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/crates/xcl/src/gpu/dxil/lower_image.rs b/crates/xcl/src/gpu/dxil/lower_image.rs index 07a89a970c..1f78144a60 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -44,9 +44,12 @@ impl Lw { None => { let k = self.static_used.len() as u32; let cur = self.cur(); + // DispatchX can terminate the physical entry before this sampler's first use. + let before = self.s.m.set_insert_before_term(true); self.s.m.set_block(self.init.entry); let h = self.s.sampler(self.sampler_base + k, 1, has_c); self.s.m.set_block(cur); + self.s.m.set_insert_before_term(before); self.static_used.push((w, has_c, h)); h } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 3443a603fb..8e4019f689 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3212,6 +3212,38 @@ fn gpu_static_samplers_from_constants_in_code() { assert_eq!([fl(10), fl(11), fl(12), fl(13)], [2.0, 0.0, 2.0, 1.0], "lane {l}: static point sampler"); assert_eq!([fl(20), fl(21), fl(22), fl(23)], [1.5, 0.0, 1.5, 1.0], "lane {l}: static linear sampler"); } + if runner::wave32_supported() != Ok(true) { + return runner::skip_test("no wave32"); + } + // The compute prologue branches on DispatchX thread limits. Both sampler handles + // must dominate their uses on either path, including lanes masked off at entry. + let mut compute = vec![ + ("V_LSHLREV_B32", "ENC_VOP2", vec![("VDST", 8), ("SRC0", 131), ("VSRC1", 0)], None), // lane * 8 bytes + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 9), ("SRC0", 128)], None), + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 0), ("SRC0", 255)], Some(f(0.5))), + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 1), ("SRC0", 255)], Some(f(0.125))), + ]; + compute.extend(list); + compute.extend([ + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 11), ("SRC0", V + 20)], None), + ("GLOBAL_STORE_DWORDX2", "ENC_FLAT_GLBL", vec![("ADDR", 8), ("DATA", 10), ("SADDR", 0x7D)], None), + ]); + let refs: Vec<_> = compute.iter().map(|(n, e, fields, literal)| (*n, *e, fields.as_slice(), *literal)).collect(); + let cs = super::CsInterface { threads: [32, 1, 1], user_sgprs: 24, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let options = crate::gpu::dxil::kernel::KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + let translated = crate::gpu::dxil::kernel::translate_compute(&prog(&refs), options, &cs).unwrap(); + assert_eq!(translated.static_samplers, [(point, false), (linear, false)]); + let sentinel = 0xa5a5_5a5a; + let initial = [sentinel; 64]; + for (limit, active) in [(0, 32), (17, 17)] { + st.sgpr[super::IO_THREAD_LIMIT[0]] = limit; + let result = runner::run_with_statics(&translated.dxil, &st, &initial, &tex, &[], &translated.static_samplers).unwrap(); + let mut expected = initial; + for lane in expected[..active * 2].chunks_exact_mut(2) { + lane.copy_from_slice(&[f(2.0), f(1.5)]); + } + assert_eq!(result.mem, expected, "DispatchX thread limit {limit}"); + } } /// A static sampler's S# word that the compiler writes bit-reversed (`s_brev_b32 s26, 40` is From 43902aa2a05f56f870f6db60fb9b46e47833c0e8 Mon Sep 17 00:00:00 2001 From: Krzysztof Kania <9355751+KaniaKrzysztof@users.noreply.github.com> Date: Sat, 28 Nov 2026 14:11:50 +0100 Subject: [PATCH 2/2] Recompiler: initialize compact register slots after their first value Keep lazy allocas in entry. Initialize an instruction-valued slot in its defining block so the DispatchX EXEC phi dominates its store. Exercise full and partial groups in forced compact lowering. Assisted-by: OpenAI Codex (GPT-6) --- crates/xcl/src/gpu/dxil/kernel.rs | 37 +++++++++++++++++++++++++++++++ crates/xcl/src/gpu/dxil/lower.rs | 3 +++ 2 files changed, 40 insertions(+) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index b9ba57daac..e45299afed 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -1036,3 +1036,40 @@ fn geom_init(lw: &mut Lw, g: &GsInterface, _tid: Value, io: super::shader::RawUa lw.wr(base, none); lw.geom = Some(super::lower_geom::GeomCtx { g: g.clone(), emitted, strip, ntri, base, tri, maxt, rec }); } + +#[cfg(test)] +#[test] +fn gpu_compact_slots_use_the_dispatch_prologues_exec_mask() { + use crate::gpu::rdna2::encode; + use crate::gpu::recomp::{runner, IO_THREAD_LIMIT}; + + if runner::wave32_supported() != Ok(true) { + return runner::skip_test("no wave32"); + } + // Each active lane stores its id in mem[id]. The execution mask itself + // is the ALU/store predicate, so a stale full mask overwrites sentinels. + let mut code = Vec::new(); + for (name, enc, fields) in [ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 256)][..]), + ("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 0), ("SRC0", 130), ("VSRC1", 0)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 128)]), + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 0), ("DATA", 2), ("SADDR", 0x7D)]), + ("S_ENDPGM", "ENC_SOPP", &[]), + ] { + code.extend(encode(name, enc, fields, None).unwrap()); + } + let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 0, tgid: [false; 3], tg_size: false, tid_components: 1 }; + let options = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + let kernel = translate_entry(&code, options, Entry::Compute(cs), Some(true), None).unwrap(); + let initial = [0xa5a5_5a5a; 32]; + for (limit, active) in [(0, 32), (17, 17)] { + let mut io = [0; 128]; + io[IO_THREAD_LIMIT[0]] = limit; + let got = runner::dispatch(&kernel.dxil, [1, 1, 1], &io, &initial).unwrap(); + let mut expected = initial; + for (lane, word) in expected[..active].iter_mut().enumerate() { + *word = lane as u32; + } + assert_eq!(&got[..initial.len()], &expected, "DispatchX thread limit {limit}"); + } +} diff --git a/crates/xcl/src/gpu/dxil/lower.rs b/crates/xcl/src/gpu/dxil/lower.rs index 0244dbbfac..46f07c551e 100644 --- a/crates/xcl/src/gpu/dxil/lower.rs +++ b/crates/xcl/src/gpu/dxil/lower.rs @@ -104,6 +104,9 @@ impl Init { s.m.set_block(self.entry); let i32 = s.m.i32(); let p = s.m.alloca(i32, 4); + // The slot lives in entry, but a DispatchX EXEC mask is defined at the + // prologue join. Initialize there so the store sees the selected mask. + s.m.set_block(s.m.def_block(v).unwrap_or(self.entry)); s.m.store(p, v, 4); s.m.set_block(cur); s.m.set_insert_before_term(before);