From 593c65bf0695ab22055266816531f9d5c44ed278 Mon Sep 17 00:00:00 2001 From: Roroca-1 Date: Tue, 24 Nov 2026 14:16:25 -0500 Subject: [PATCH 1/2] Shaders: infer double extensions from emitted instructions An unreachable conversion survives in the instruction arena after optimization. Scan block instructions when inferring flags and invalidate cached containers. --- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/module.rs | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 371ac0622c..12d45829ba 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -35,7 +35,7 @@ pub struct KernelOptions { /// Version of the direct translator's output, part of the on-disk translation cache's /// identity (`recomp::pso`): bump it on any change that alters the emitted containers /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 4; +pub const TRANSLATOR_VERSION: u32 = 5; /// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this /// translator, with the HLSL path as the fallback for what it cannot translate yet. diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index 3462ca3610..225cff450b 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -1350,7 +1350,7 @@ impl Module { /// integers (DXC's shader-flag rules; `Fma` is flagged by its helper). pub fn uses_double_extensions(&self) -> bool { let Some(f64) = self.type_ids.get(&TypeDef::Double).copied() else { return false }; - self.insts.iter().any(|d| match &d.op { + self.blocks.iter().flatten().map(|&i| &self.insts[i as usize]).any(|d| match &d.op { Op::Bin(BinOp::UDiv | BinOp::SDiv, ..) => d.ty == f64, Op::Cast(CastOp::FPToUI | CastOp::FPToSI, v, _) => self.value_ty(*v) == f64, Op::Cast(CastOp::UIToFP | CastOp::SIToFP, _, t) => *t == f64, @@ -1459,3 +1459,24 @@ impl Module { self.globals.iter().filter(|g| g.addrspace == addrspace).map(|g| self.alloc_size_layout(g.ty, native16)).sum() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn unreachable_conversion_does_not_require_double_extensions() { + let mut m = Module::new(); + m.define_entry("main"); + m.ret(); + let dead = m.new_block(); + m.set_block(dead); + let seven = m.i32c(7); + let double = m.f64(); + m.cast(CastOp::SIToFP, seven, double); + m.ret(); + assert!(m.uses_double_extensions()); + m.optimize(); + assert!(!m.uses_double_extensions()); + } +} From ce8c103945a6965450946d89c071e152dae5881b Mon Sep 17 00:00:00 2001 From: Roroca-1 Date: Tue, 24 Nov 2026 14:18:12 -0500 Subject: [PATCH 2/2] Shaders: reuse the predicate when reading a zero-extended SCC The direct lowering stores SCC as an i32 zero extension and tests it against zero. Reuse the i1 instruction for this exact round trip and invalidate older cached containers. --- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/module.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 12d45829ba..da094d0c1f 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -35,7 +35,7 @@ pub struct KernelOptions { /// Version of the direct translator's output, part of the on-disk translation cache's /// identity (`recomp::pso`): bump it on any change that alters the emitted containers /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 5; +pub const TRANSLATOR_VERSION: u32 = 6; /// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this /// translator, with the HLSL path as the fallback for what it cannot translate yet. diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index 225cff450b..3a74a5eb63 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -569,6 +569,18 @@ impl Module { } pub fn cmp(&mut self, p: Pred, a: Value, b: Value) -> Value { let t = self.i1(); + // SCC is stored as a zero-extended predicate and read back with != 0. + if let (Pred::Ne, Value::Inst(i), Value::Const(k)) = (p, a, b) { + if let (Some(Op::Cast(CastOp::ZExt, Value::Inst(bit), dst)), Some(ConstDef::Int(ty, 0))) = + (self.insts.get(i as usize).map(|d| &d.op), self.consts.get(k as usize)) + { + if dst == ty && self.int_width(*dst) == Some(32) + && self.insts.get(*bit as usize).is_some_and(|d| d.ty == t) && !self.terminated() + { + return Value::Inst(*bit); + } + } + } self.push(Op::Cmp(p, a, b), t) } pub fn select(&mut self, c: Value, t: Value, f: Value) -> Value { @@ -1464,6 +1476,24 @@ impl Module { mod tests { use super::*; + #[test] + fn scc_nonzero_test_reuses_the_original_predicate() { + let mut m = Module::new(); + let i32t = m.i32(); + let zero = m.i32c(0); + let ptr = m.alloca(i32t, 4); + let word = m.load(ptr, 4); + let bit = m.cmp(Pred::Ne, word, zero); + let scc = m.cast(CastOp::ZExt, bit, i32t); + let before = m.insts.len(); + let read = m.cmp(Pred::Ne, scc, zero); + assert_eq!(read, bit); + assert_eq!(m.insts.len(), before); + assert_ne!(m.cmp(Pred::Eq, scc, zero), bit); + let one = m.i32c(1); + assert_ne!(m.cmp(Pred::Ne, scc, one), bit); + } + #[test] fn unreachable_conversion_does_not_require_double_extensions() { let mut m = Module::new();