From cab89ed546b2677b8c2fc419870efaf98724eb52 Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:49:14 -0700 Subject: [PATCH] Recompiler: direct DXIL makes a pointer to storage again in each block Module::optimize merged identical getelementptrs along the dominator tree, so one pointer into the wave64 exchange words or the LDS was used in up to 136 blocks of a program. dxil-spirv (under vkd3d-proton) restructures the blocks, and for five programs of the retail title's pack the block that makes the pointer then no longer dominates a use: the SPIR-V fails spirv-val ("ID ... defined in block ... does not dominate its use"). A value gets a phi there; a pointer does not. A getelementptr is now merged within its block only, unless it points into a constant global (the lookup tables, whose loads are merged along the tree). Direct pack of main aaa0936, compute, split wave64 (2,682 programs), through dxil-spirv legacy-struct-remove e084e35c and spirv-val --target-env vulkan1.3: 5 invalid before, 0 after; the same five through dxil-spirv 34f0ff8 (vkd3d-proton v3.1-90): invalid before, valid after. DXIL 935,669,256 B before, 936,012,404 B after (+0.04 %); 682 programs change. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/module/opt.rs | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/xcl/src/gpu/dxil/module/opt.rs b/crates/xcl/src/gpu/dxil/module/opt.rs index 5e867f55a0..357637b414 100644 --- a/crates/xcl/src/gpu/dxil/module/opt.rs +++ b/crates/xcl/src/gpu/dxil/module/opt.rs @@ -97,6 +97,7 @@ impl Module { fn cse_kind(&self, op: &Op) -> Cse { match op { + Op::Gep(p, _) if !self.is_const_global_ptr(*p) => Cse::Block, Op::Bin(..) | Op::Cast(..) | Op::Cmp(..) | Op::Select(..) | Op::Extract(..) | Op::Gep(..) => Cse::Dom, Op::Load(p, _) if self.is_const_global_ptr(*p) => Cse::Dom, Op::Call(f, _) if self.funcs[*f as usize].attrs == FnAttrs::ReadNone => Cse::Dom, @@ -1033,6 +1034,42 @@ mod tests { .count() } + #[test] + fn a_pointer_to_storage_is_made_again_in_each_block() { + let mut m = Module::new(); + m.define_entry("main"); + let i32t = m.i32(); + let arr = m.array(2, i32t); + let g = m.global("xcl_xs", arr, 3, None, false, 4); + let (zero, one) = (m.i32c(0), m.i32c(1)); + let lane_ty = m.func_ty(i32t, &[]); + let lane_fn = m.declare("dx.op.waveGetLaneIndex", lane_ty, FnAttrs::ReadNone); + let next = m.new_block(); + let done = m.new_block(); + let b = m.entry_block(); + m.set_block(b); + let lane = m.call(lane_fn, &[]); + let go = m.cmp(Pred::Eq, lane, zero); + for to in [Some(next), None] { + let p = m.gep(g, &[zero, one]); + m.store(p, zero, 4); + let q = m.gep(g, &[zero, one]); + m.store(q, one, 4); + match to { + Some(to) => { + m.cond_br(go, to, done); + m.set_block(to); + } + None => m.br(done), + } + } + m.set_block(done); + m.ret(); + m.optimize(); + let geps = |b: &Vec| b.iter().filter(|&&i| matches!(m.insts[i as usize].op, Op::Gep(..))).count(); + assert_eq!(m.blocks.iter().map(geps).collect::>(), [1, 1, 0]); + } + #[test] fn block_cse_invalidates_wave_and_quad_calls_across_discard() { let build = |op_name: &str, with_discard: bool| -> (Module, u32) {