Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions crates/xcl/src/gpu/dxil/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<(u8, u32, u32, u32)>> {
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<u8> {
// Every type the writer references must exist before the type table:
// pointers to the declared functions (call / metadata operands).
Expand Down
39 changes: 37 additions & 2 deletions crates/xcl/src/gpu/dxil/module/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions crates/xcl/src/gpu/dxil/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>) -> Vec<u8> {
Expand Down
Loading