From 50228bb747077d302888cb2c5693dc798a22e154 Mon Sep 17 00:00:00 2001 From: Alp Pesen <5701011+AlpPesen@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:34:33 +0300 Subject: [PATCH] Recompiler: invalidate block-local DXIL CSE wave/quad cache across dx.op.discard In dxil::module::opt, cse_kind() caches dx.op.wave* and dx.op.quad* calls in a block-local map (local). Because dx.op.discard demotes lanes to helper lanes mid-block without terminating the block, subsequent wave/quad intrinsics in the same block observe a different active lane set and must not reuse pre-discard values. Clear local when visiting dx.op.discard and add a unit test verifying that identical dx.op.waveActiveBallot and dx.op.quadReadLaneAt calls on opposite sides of dx.op.discard are kept while non-discard calls in the same block still CSE. Fixes #1891. Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/gpu/dxil/module/opt.rs | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/crates/xcl/src/gpu/dxil/module/opt.rs b/crates/xcl/src/gpu/dxil/module/opt.rs index d852d8a413..c9b4f1a6d9 100644 --- a/crates/xcl/src/gpu/dxil/module/opt.rs +++ b/crates/xcl/src/gpu/dxil/module/opt.rs @@ -468,6 +468,10 @@ impl Module { undo.push((key, old)); } } + } else if matches!(&op, Op::Call(f, _) if self.funcs[*f as usize].name == "dx.op.discard") { + // `dx.op.discard` demotes lanes mid-block, changing the active lane + // set for subsequent wave/quad intrinsics in the same basic block. + local.clear(); } } } @@ -571,3 +575,65 @@ fn resolve(repl: &mut [Option], v: Value) -> Value { } r } + +#[cfg(test)] +mod tests { + use super::*; + + fn count_calls(m: &Module, target_fn: u32) -> usize { + m.blocks + .iter() + .flat_map(|b| b.iter()) + .filter(|&&i| matches!(m.insts[i as usize].op, Op::Call(f, _) if f == target_fn)) + .count() + } + + #[test] + fn block_cse_invalidates_wave_and_quad_calls_across_discard() { + let build = |op_name: &str, with_discard: bool| -> (Module, u32) { + let mut m = Module::new(); + m.define_entry("main"); + let b = m.entry_block(); + m.set_block(b); + + let void = m.void(); + let i1 = m.i1(); + let i32t = m.i32(); + + let wave_ty = m.func_ty(i32t, &[i32t, i1]); + let wave_fn = m.declare(op_name, wave_ty, FnAttrs::NoUnwind); + let discard_ty = m.func_ty(void, &[i32t, i1]); + let discard_fn = m.declare("dx.op.discard", discard_ty, FnAttrs::NoUnwind); + let sink_ty = m.func_ty(void, &[i32t, i32t]); + let sink_fn = m.declare("dx.op.storeOutput.i32", sink_ty, FnAttrs::NoUnwind); + + let c_op = m.i32c(116); + let c_true = m.i1c(true); + let c_discard_op = m.i32c(82); + + let first = m.call(wave_fn, &[c_op, c_true]); + if with_discard { + m.call(discard_fn, &[c_discard_op, c_true]); + } + let second = m.call(wave_fn, &[c_op, c_true]); + m.call(sink_fn, &[first, second]); + m.ret(); + m.optimize(); + (m, wave_fn) + }; + + // Without `dx.op.discard`, identical wave/quad calls in the same block still CSE to 1. + let (m_no_discard, f_no_discard) = build("dx.op.waveActiveBallot", false); + assert_eq!(count_calls(&m_no_discard, f_no_discard), 1); + + // Across `dx.op.discard`, active lanes change so both calls must be preserved. + for op_name in ["dx.op.waveActiveBallot", "dx.op.quadReadLaneAt"] { + let (m_with_discard, f_with_discard) = build(op_name, true); + assert_eq!( + count_calls(&m_with_discard, f_with_discard), + 2, + "{op_name} must not be CSE-merged across dx.op.discard" + ); + } + } +}