Skip to content
Open
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
244 changes: 179 additions & 65 deletions crates/xcl/src/gpu/dxil/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ struct Deps {
/// Input components each value depends on, through data flow, memory and
/// control dependence (as DXC's `ComputeViewIdState`).
fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Placed]) -> Deps {
compute_deps_with_peak(s, inputs, outputs, prims).0
}

fn compute_deps_with_peak(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Placed]) -> (Deps, usize) {
let m = &s.m;
let nb = m.block_count();
// Successors and the branch condition of each block.
Expand Down Expand Up @@ -824,83 +828,193 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
in_base: &'a [Option<u32>],
inputs: &'a [Placed],
}
fn block_deps(cx: &Cx, b: usize, st: &mut Sets) -> BTreeSet<u32> {
if let Some(d) = Sets::known(&st.blocks, &b, st.pass, &mut st.cut) {
return d;
}
Sets::open(&mut st.blocks, b);
let mut d = BTreeSet::new();
for &a in &cx.ctrl[b] {
if let Some(c) = cx.cond[a] {
d.extend(value_deps(cx, c, st));
#[derive(Clone, Copy)]
enum DepNode {
Block(usize),
Val(Value),
}
enum DepChildren<'a> {
List { items: Vec<DepNode>, next: usize },
Values { items: &'a [Value], next: usize },
}
impl<'a> DepChildren<'a> {
fn next_child(&mut self) -> Option<DepNode> {
match self {
Self::List { items, next } => {
let c = items.get(*next).copied()?;
*next += 1;
Some(c)
}
Self::Values { items, next } => {
let &v = items.get(*next)?;
*next += 1;
Some(DepNode::Val(v))
}
}
if a != b {
d.extend(block_deps(cx, a, st));
}

fn owned_len(&self) -> usize {
match self {
Self::List { items, .. } => items.len(),
Self::Values { .. } => 0,
}
}
Sets::close(&mut st.blocks, b, d, st.pass, &mut st.grew)
}
fn value_deps(cx: &Cx, v: Value, st: &mut Sets) -> BTreeSet<u32> {
let Value::Inst(i) = v else { return BTreeSet::new() };
if let Some(d) = Sets::known(&st.values, &v, st.pass, &mut st.cut) {
return d;
}
Sets::open(&mut st.values, v);
let m = &cx.s.m;
let mut d = BTreeSet::new();
match m.inst_info(i) {
InstInfo::Call(name, args) if name.starts_with("dx.op.loadInput.") => {
let id = m.const_u64(args[1]).unwrap() as usize;
let col = m.const_u64(args[3]).unwrap() as u32;
if let Some(base) = cx.in_base[id] {
match m.const_u64(args[2]) {
Some(r) => {
d.insert(base + r as u32 * 4 + col);
}
None => {
for r in 0..cx.inputs[id].e.nrows() {
d.insert(base + r * 4 + col);
struct DepFrame<'a> {
node: DepNode,
children: DepChildren<'a>,
deps: BTreeSet<u32>,
}
fn eval_dep<'a>(
cx: &Cx<'a>,
start: DepNode,
st: &mut Sets,
peak_children: &mut usize,
) -> BTreeSet<u32> {
let mut stack: Vec<DepFrame<'a>> = Vec::new();
let mut live_children = 0usize;
let mut cur = start;
loop {
let ready: Option<BTreeSet<u32>> = match cur {
DepNode::Block(b) => {
if let Some(d) = Sets::known(&st.blocks, &b, st.pass, &mut st.cut) {
Some(d)
} else {
Sets::open(&mut st.blocks, b);
let mut children = Vec::new();
for &a in &cx.ctrl[b] {
if let Some(c) = cx.cond[a] {
children.push(DepNode::Val(c));
}
if a != b {
children.push(DepNode::Block(a));
}
d.extend(value_deps(cx, args[2], st));
}
if children.is_empty() {
Some(Sets::close(&mut st.blocks, b, BTreeSet::new(), st.pass, &mut st.grew))
} else {
let first = children[0];
let children = DepChildren::List { items: children, next: 1 };
live_children += children.owned_len();
*peak_children = (*peak_children).max(live_children);
stack.push(DepFrame { node: DepNode::Block(b), children, deps: BTreeSet::new() });
cur = first;
continue;
}
}
}
}
InstInfo::Call(_, args) => {
for &a in args {
d.extend(value_deps(cx, a, st));
}
}
InstInfo::Phi(list) => {
for &(x, from) in list {
d.extend(value_deps(cx, x, st));
d.extend(block_deps(cx, from.0 as usize, st));
if let Some(c) = cx.cond[from.0 as usize] {
d.extend(value_deps(cx, c, st));
}
}
}
InstInfo::Load(p) => {
d.extend(value_deps(cx, p, st));
if let Some(vs) = cx.root_stores.get(&root(cx.s, p)) {
for &x in vs {
d.extend(value_deps(cx, x, st));
DepNode::Val(v) => match v {
Value::Inst(i) => {
if let Some(d) = Sets::known(&st.values, &v, st.pass, &mut st.cut) {
Some(d)
} else {
Sets::open(&mut st.values, v);
let m = &cx.s.m;
let mut d = BTreeSet::new();
let mut list = Vec::new();
let mut first_and_rest: Option<(DepNode, DepChildren<'a>)> = None;
match m.inst_info(i) {
InstInfo::Call(name, args) if name.starts_with("dx.op.loadInput.") => {
let id = m.const_u64(args[1]).unwrap() as usize;
let col = m.const_u64(args[3]).unwrap() as u32;
if let Some(base) = cx.in_base[id] {
match m.const_u64(args[2]) {
Some(r) => {
d.insert(base + r as u32 * 4 + col);
}
None => {
for r in 0..cx.inputs[id].e.nrows() {
d.insert(base + r * 4 + col);
}
list.push(DepNode::Val(args[2]));
}
}
}
}
InstInfo::Call(_, args) => {
if let Some((&first, rest)) = args.split_first() {
first_and_rest = Some((DepNode::Val(first), DepChildren::Values { items: rest, next: 0 }));
}
}
InstInfo::Phi(phi_list) => {
for &(x, from) in phi_list {
list.push(DepNode::Val(x));
list.push(DepNode::Block(from.0 as usize));
if let Some(c) = cx.cond[from.0 as usize] {
list.push(DepNode::Val(c));
}
}
}
InstInfo::Load(p) => {
let stores = cx.root_stores.get(&root(cx.s, p)).map(Vec::as_slice).unwrap_or(&[]);
first_and_rest = Some((DepNode::Val(p), DepChildren::Values { items: stores, next: 0 }));
}
InstInfo::Other(ops) => {
list.extend(ops.into_iter().map(DepNode::Val));
}
InstInfo::Store(..) | InstInfo::Term(..) => {}
}
let _ = cx.block_of;
if first_and_rest.is_none() && !list.is_empty() {
let first = list[0];
first_and_rest = Some((first, DepChildren::List { items: list, next: 1 }));
}
match first_and_rest {
None => {
Some(Sets::close(&mut st.values, v, d, st.pass, &mut st.grew))
}
Some((first, children)) => {
live_children += children.owned_len();
*peak_children = (*peak_children).max(live_children);
stack.push(DepFrame { node: DepNode::Val(v), children, deps: d });
cur = first;
continue;
}
}
}
}
_ => Some(BTreeSet::new()),
},
};
let mut result = ready.unwrap();
loop {
let Some(top) = stack.last_mut() else {
return result;
};
top.deps.extend(result);
if let Some(next_node) = top.children.next_child() {
cur = next_node;
break;
}
let frame = stack.pop().unwrap();
live_children -= frame.children.owned_len();
result = match frame.node {
DepNode::Block(b) => Sets::close(&mut st.blocks, b, frame.deps, st.pass, &mut st.grew),
DepNode::Val(v) => Sets::close(&mut st.values, v, frame.deps, st.pass, &mut st.grew),
};
}
InstInfo::Other(ops) => {
for x in ops {
d.extend(value_deps(cx, x, st));
}
}
InstInfo::Store(..) | InstInfo::Term(..) => {}
}
let _ = cx.block_of;
Sets::close(&mut st.values, v, d, st.pass, &mut st.grew)
}
fn block_deps<'a>(
cx: &Cx<'a>,
b: usize,
st: &mut Sets,
peak_children: &mut usize,
) -> BTreeSet<u32> {
eval_dep(cx, DepNode::Block(b), st, peak_children)
}
fn value_deps<'a>(
cx: &Cx<'a>,
v: Value,
st: &mut Sets,
peak_children: &mut usize,
) -> BTreeSet<u32> {
eval_dep(cx, DepNode::Val(v), st, peak_children)
}

let cx = Cx { s, block_of: &block_of, ctrl: &ctrl, cond: &cond, root_stores: &root_stores, in_base: &in_base, inputs };
let mut out: HashMap<(u8, u32), BTreeSet<u32>> = HashMap::new();
let mut peak_children = 0usize;
let bases = |p: &[Placed]| -> Vec<Option<u32>> { p.iter().map(|e| e.at.map(|(r, c)| r * 4 + c as u32)).collect() };
let (ob, pb) = (bases(outputs), bases(prims));
// (code without a loop is done after one pass: nothing was cut)
Expand All @@ -921,10 +1035,10 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
let Some(base) = base[id] else { continue };
let col = m.const_u64(args[3]).unwrap() as u32;
let row = m.const_u64(args[2]).unwrap() as u32;
let mut d = value_deps(&cx, args[4], &mut sets);
d.extend(block_deps(&cx, b, &mut sets));
let mut d = value_deps(&cx, args[4], &mut sets, &mut peak_children);
d.extend(block_deps(&cx, b, &mut sets, &mut peak_children));
if args.len() > 5 {
d.extend(value_deps(&cx, args[5], &mut sets));
d.extend(value_deps(&cx, args[5], &mut sets, &mut peak_children));
}
out.entry((dir, base + row * 4 + col)).or_default().extend(d);
}
Expand All @@ -933,7 +1047,7 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
break;
}
}
Deps { out }
(Deps { out }, peak_children)
}

/// The global (or other base) a pointer is derived from.
Expand Down
101 changes: 101 additions & 0 deletions crates/xcl/src/gpu/dxil/graphics_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,104 @@ fn ps_static_resources_match_dxc() {
assert_eq!(part(&c, b"SFI0"), part(DXC_PS_RESOURCES, b"SFI0"));
assert_valid(&c);
}

#[test]
fn deep_phi_deps_do_not_overflow_stack() {
let handle = std::thread::Builder::new()
.name("dxil-graphics-deps-test".into())
.stack_size(2 * 1024 * 1024)
.spawn(|| {
let mut s = Shader::new(Stage::Pixel, (6, 6));
let mut io = GraphicsIo::new(Stage::Pixel);
let pos = io.input(SigElement::new("SV_Position", 0, SemKind::Position, CompType::F32, 4));
let tgt = io.output(SigElement::new("SV_Target", 0, SemKind::Target, CompType::F32, 4));
let x = io.load_input(&mut s, pos, 0, 0);
let f32 = s.m.f32();
let one = f(&mut s, 1.0);
let cond = s.m.i1c(true);
let mut cur_val = x;
for _ in 0..4000 {
let prev_b = s.m.current_block();
let then_b = s.m.new_block();
let join_b = s.m.new_block();
s.m.cond_br(cond, then_b, join_b);
s.m.set_block(then_b);
let added = s.m.fbin(BinOp::Add, cur_val, one, 0);
s.m.br(join_b);
s.m.set_block(join_b);
let phi = s.m.phi(f32);
s.m.add_incoming(phi, cur_val, prev_b);
s.m.add_incoming(phi, added, then_b);
cur_val = phi;
}
for c in 0..4u8 {
io.store_output(&mut s, tgt, 0, c, cur_val);
}
let c = s.try_finish_graphics(io, StageInfo::Pixel { early_depth_stencil: false }).expect("graphics finish");
assert_valid(&c);
})
.expect("spawn bounded-stack thread");
handle.join().expect("graphics compute_deps overflowed or panicked");
}

fn ps_shared_root_load_store_chain(steps: u32) -> (Shader, GraphicsIo) {
let mut s = Shader::new(Stage::Pixel, (6, 6));
let mut io = GraphicsIo::new(Stage::Pixel);
let pos = io.input(SigElement::new("SV_Position", 0, SemKind::Position, CompType::F32, 4));
let _unused = io.input(SigElement::user("TEXCOORD", 0, CompType::F32, 1));
let tgt = io.output(SigElement::new("SV_Target", 0, SemKind::Target, CompType::F32, 4));
let x = io.load_input(&mut s, pos, 0, 0);
let f32 = s.m.f32();
let arr_ty = s.m.array(u64::from(steps) + 1, f32);
let arr = s.m.alloca(arr_ty, 4);
let zero = s.m.i32c(0);
let one = f(&mut s, 1.0);
let p0 = s.m.gep(arr, &[zero, zero]);
s.m.store(p0, x, 4);
for k in 0..steps {
let idx = s.m.i32c(k);
let pk = s.m.gep(arr, &[zero, idx]);
let ld = s.m.load(pk, 4);
let next = s.m.fbin(BinOp::Add, ld, one, 0);
let next_idx = s.m.i32c(k + 1);
let p_next = s.m.gep(arr, &[zero, next_idx]);
s.m.store(p_next, next, 4);
}
let last_idx = s.m.i32c(steps);
let plast = s.m.gep(arr, &[zero, last_idx]);
let out = s.m.load(plast, 4);
for c in 0..4u8 {
io.store_output(&mut s, tgt, 0, c, out);
}
(s, io)
}

#[test]
fn deep_shared_root_loads_borrow_store_list_without_quadratic_children() {
let handle = std::thread::Builder::new()
.name("dxil-graphics-load-store-deps-test".into())
.stack_size(2 * 1024 * 1024)
.spawn(|| {
const STEPS: u32 = 1024;
let (mut s, io) = ps_shared_root_load_store_chain(STEPS);
s.m.ret();
s.m.optimize();
let ins = io.place(Dir::In);
let outs = io.place(Dir::Out);
let prims = io.place(Dir::Prim);
let (deps, peak_live_children) = compute_deps_with_peak(&s, &ins, &outs, &prims);
for c in 0..4u32 {
assert_eq!(deps.out.get(&(0, c)).map(|d| d.iter().copied().collect::<Vec<_>>()), Some(vec![0]));
}
assert!(
peak_live_children <= (STEPS as usize) * 4,
"expected linear live child storage, got {peak_live_children} for {STEPS} steps"
);

let (s, io) = ps_shared_root_load_store_chain(STEPS);
let c = s.try_finish_graphics(io, StageInfo::Pixel { early_depth_stencil: false }).expect("graphics finish");
assert_valid(&c);
})
.expect("spawn bounded-stack thread");
handle.join().expect("graphics shared-root load/store test overflowed or panicked");
}
Loading
Loading