From 2b39525699037440ce706963fd923c7f7e35a9b5 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 00:18:35 -0800 Subject: [PATCH] Recompiler: prune unused split-piece resource bindings Split pieces inherited the whole shader's static resource metadata even when optimization removed every handle use in that piece. Derive the reachable binding ranges after optimization and retain only matching metadata, while preserving survivor order and the shared chain root-signature ABI. The split parity fixture checks that scratch-only pieces omit the title-memory binding without changing GPU results. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/gpu/dxil/module.rs | 25 ++++++++++++++++ crates/xcl/src/gpu/dxil/module/split.rs | 39 +++++++++++++++++++++++-- crates/xcl/src/gpu/dxil/shader.rs | 11 +++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index b9712d4b57..da9932c11d 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -817,6 +817,31 @@ impl Module { self.try_to_bitcode(triple, datalayout).unwrap_or_else(|e| panic!("DXIL builder: {e}")) } + /// Static resource ranges referenced by reachable `createHandleFromBinding` + /// calls. A dynamic binding descriptor makes the result unknowable. + pub(super) fn referenced_bindings(&self) -> Option> { + let (order, _) = self.block_layout(); + let mut refs = Vec::new(); + for &block in &order { + for &inst in &self.blocks[block] { + let Op::Call(func, args) = &self.insts[inst as usize].op else { continue }; + if self.funcs[*func as usize].name != "dx.op.createHandleFromBinding" { + continue; + } + let Some(&Value::Const(bind)) = args.get(1) else { return None }; + let ConstDef::Aggregate(_, fields) = &self.consts[bind as usize] else { return None }; + let [lower, upper, space, class] = fields.as_slice() else { return None }; + refs.push(( + self.const_u64(*class)? as u8, + self.const_u64(*space)? as u32, + self.const_u64(*lower)? as u32, + self.const_u64(*upper)? as u32, + )); + } + } + Some(refs) + } + fn write_bitcode(&mut self, triple: &str, datalayout: &str) -> Vec { // Every type the writer references must exist before the type table: // pointers to the declared functions (call / metadata operands). diff --git a/crates/xcl/src/gpu/dxil/module/split.rs b/crates/xcl/src/gpu/dxil/module/split.rs index 7a88b9ae11..1d7ef33749 100644 --- a/crates/xcl/src/gpu/dxil/module/split.rs +++ b/crates/xcl/src/gpu/dxil/module/split.rs @@ -271,7 +271,12 @@ impl Shader { // a refused piece leaves the whole program unsplit: say so let refused = |e: String| crate::xlog_warn!("compute split: piece {} of {} refused, the program stays whole: {}", stage + 1, plan.blocks.len(), e.lines().next().unwrap_or_default()); - let blob = dst.try_finish(threads, Some(32)).map_err(|e| refused(e.to_string())).ok()?; + if !dst.m.terminated() { + dst.m.ret(); + } + dst.m.optimize(); + dst.prune_unused_bindings(); + let blob = dst.finish_optimized(threads, Some(32)).map_err(|e| refused(e.to_string())).ok()?; crate::gpu::dxil::validate::validate(&blob).map_err(refused).ok()?; Some(blob) } @@ -711,10 +716,33 @@ impl Module { #[cfg(test)] mod tests { use crate::gpu::{ - dxil::{kernel::{Entry, KernelOptions}, lower::Lw, module::{BinOp, CastOp, Pred}}, + dxil::{ + container::Stage, + kernel::{Entry, KernelOptions}, + lower::Lw, + module::{BinOp, CastOp, Pred}, + shader::{ResClass, Shader}, + }, recomp::{runner, CsInterface}, }; + #[test] + fn optimized_piece_prunes_only_unreferenced_static_bindings() { + let mut shader = Shader::new(Stage::Compute, (6, 6)); + let used = shader.raw_uav(2, 0); + let _unused = shader.cbv(7, 1, 16); + let zero = shader.m.i32c(0); + shader.raw_store(used, zero, &[zero]); + shader.m.ret(); + shader.m.optimize(); + + shader.prune_unused_bindings(); + + assert_eq!(shader.bindings.len(), 1); + assert_eq!(shader.bindings[0].class, ResClass::Uav); + assert_eq!((shader.bindings[0].space, shader.bindings[0].lower), (0, 2)); + } + #[test] fn oversized_loop_occupies_one_piece_up_to_the_hard_ceiling() { use super::{Block, Module}; @@ -844,6 +872,13 @@ mod tests { crate::gpu::dxil::validate::validate(&whole).expect("valid unsplit fixture"); let chain = chain.expect("split the synthetic program"); assert!(chain.pieces.len() >= 3); + let has_memory_binding: Vec<_> = chain.pieces.iter().map(|piece| { + crate::gpu::dxil::validate::disassemble(piece).unwrap() + .contains("ResBind { i32 0, i32 2, i32 2, i8 1 }") + }).collect(); + assert!(has_memory_binding.iter().any(|&present| present)); + assert!(has_memory_binding.iter().any(|&present| !present), + "pieces that only transport scratch must omit the title-memory binding"); let initial: Vec<_> = (1..=threads * 3).collect(); let expected: Vec<_> = initial.iter().enumerate().map(|(i, &v)| { let n = (i as u32 % 4) + 1; diff --git a/crates/xcl/src/gpu/dxil/shader.rs b/crates/xcl/src/gpu/dxil/shader.rs index a0a970925d..b72e1066e6 100644 --- a/crates/xcl/src/gpu/dxil/shader.rs +++ b/crates/xcl/src/gpu/dxil/shader.rs @@ -518,6 +518,17 @@ impl Shader { } } + /// Drop static binding metadata that this optimized shader cannot reach. + /// Dynamic binding descriptors conservatively keep the complete table. + pub(super) fn prune_unused_bindings(&mut self) { + let Some(refs) = self.m.referenced_bindings() else { return }; + self.bindings.retain(|binding| refs.iter().any(|&(class, space, lower, upper)| { + class == binding.class as u8 + && space == binding.space + && (lower..=upper).contains(&binding.lower) + })); + } + /// Writes metadata, bitcode and the signed container (panics on a builder error: tests; /// translators use [`Shader::try_finish`]). pub fn finish(self, numthreads: [u32; 3], wave_size: Option) -> Vec {