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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
Pipelines used by an earlier run are dequeued for preload before never-used speculative
pipelines resume building ahead; demanded pipelines still overtake either kind.
- Direct compute dispatches can split large programs into pipelines built in parallel.
With guest LDS, single-wave groups can use any thread-group dimensions for native wave32
and wave64 emulated on wave32.
`XCL_DIRECT_SPLIT_INSTRUCTIONS` sets the target instructions per piece (default 12500,
zero disables splitting). Loops stay in one piece. A strongly connected component over the
target gets its own piece, up to four times the target. Larger components and indirect
Expand Down
46 changes: 24 additions & 22 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn translate_on_this_thread(
}
}
let compute = matches!(entry, Entry::Compute(_));
let mesh = matches!(entry, Entry::Mesh(_));
let split = o.wave64 && o.split && !gfx;
// split wave64 LDS keeps the console wave's order through the halves' meets (`Lw::writes_lds`,
// `lanes::lds_in_order`); `XCL_X_LDS_ORDER=0` leaves that out, and the HLSL path takes it
Expand Down Expand Up @@ -362,13 +363,22 @@ fn translate_on_this_thread(
Entry::Compute(cs) => {
let (tid, gtid) = lw.setup_lanes_compute(cs.threads, uses_lds);
lw.safe_barrier_init();
if split && !uses_lds && cs.threads == [64, 1, 1] { split_tid = Some(tid); }
let total = cs.threads.iter().try_fold(1u32, |a, &b| a.checked_mul(b)).unwrap_or(0);
if split && total != 0 && total % 64 == 0 && (!uses_lds || total == 64) {
split_tid = Some(tid);
} else if !o.wave64 && !uses_lds && cs.threads[1] * cs.threads[2] > 1 && total > 32 && total % 32 == 0 {
split_tid = Some(tid);
}
group_runs = Some(compute_init(&mut lw, cs, tid, gtid));
cs.threads
}
Entry::Mesh(ms) => {
let (tid, _) = lw.setup_lanes_compute([ms.threads, 1, 1], uses_lds);
lw.safe_barrier_init();
// a two-pass mesh program's producer is a compute dispatch: it can run as a chain
if split && ms.threads != 0 && ms.threads % 64 == 0 && (!uses_lds || ms.threads == 64) {
split_tid = Some(tid);
}
mesh_init(&mut lw, ms, tid, io);
[ms.threads, 1, 1]
}
Expand Down Expand Up @@ -621,7 +631,7 @@ fn translate_on_this_thread(
lw.s.m.set_block(prologue);
lw.s.m.br(first);
let hw = if split || !o.wave64 { 32 } else { 64 };
let limit = if compute && (!split || split_tid.is_some()) { dispatch_piece_limit() } else { 0 };
let limit = if (compute && (!split || split_tid.is_some())) || (mesh && split_tid.is_some()) { dispatch_piece_limit() } else { 0 };
lw.finish_dispatch(numthreads, hw, limit, split_tid)
}

Expand Down Expand Up @@ -846,28 +856,16 @@ impl Lw {
}
let stage = self.s.stage;
let mut dispatch_chain = None;
let total = numthreads.iter().try_fold(1u32, |a, &b| a.checked_mul(b)).unwrap_or(0);
let native_w32 = !self.wave64 && !self.split && total >= 32 && total % 32 == 0
&& (self.lds.is_none() || total == 32);
let c = if matches!(stage, Stage::Vertex | Stage::Pixel) {
let g = self.gfx.take().ok_or("graphics entry without graphics I/O")?;
let info = if stage == Stage::Pixel { super::graphics::StageInfo::Pixel { early_depth_stencil: false } } else { super::graphics::StageInfo::Vertex };
self.s.try_finish_graphics(g.io, info)
} else if limit > 0 && (numthreads == [32, 1, 1] || split_tid.is_some()) && hw == 32 {
} else if limit > 0 && (native_w32 || split_tid.is_some()) && hw == 32 {
self.s.m.optimize();
// A single split wave64 meets its other half through group-shared counters its entry
// zeroes (`Partner::Spin`), and the exchange's running number counts from the
// program's start: a later piece of a chain, a new dispatch, would wait for counts
// its own counters never reach. Such a program stays whole.
if split_tid.is_some() && self.partner == Partner::Spin {
let size: usize = (0..self.s.m.block_count()).map(|b| self.s.m.block_insts(Block(b as u32)).len()).sum();
if size > limit {
static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if n < 4 || n.is_power_of_two() {
crate::xlog_info!("compute split: a split wave64 compute program of {size} instructions stays whole: its halves exchange through group-shared counters a later dispatch would start again at zero ({} so far)", n + 1);
}
}
} else {
dispatch_chain = self.s.split_compute(numthreads, split_tid, limit).map(std::sync::Arc::new);
}
dispatch_chain = self.s.split_compute(numthreads, split_tid, limit).map(std::sync::Arc::new);
self.s.finish_optimized(numthreads, Some(hw))
} else {
self.s.try_finish(numthreads, Some(hw))
Expand Down Expand Up @@ -921,7 +919,7 @@ fn dispatch_chain_native_wave64_builds_whole() {
lw.s.m.ret();
lw.s.m.optimize();
// Exercise the native-wave admission gate with an otherwise partitionable CFG.
assert!(lw.s.m.dispatch_plan(64).is_some());
assert!(lw.s.m.dispatch_plan(64, None).is_some());
let program = lw.finish_dispatch(threads, 64, 64, None).unwrap();
assert!(program.dispatch_chain.is_none());
super::validate::validate(&program.dxil).expect("valid native wave64 whole shader");
Expand Down Expand Up @@ -969,9 +967,13 @@ fn a_split_wave64_compute_program_stays_whole() {
lw.s.raw_store(mem, offset, &[late]);
lw.s.m.ret();
lw.s.m.optimize();
assert!(lw.s.m.dispatch_plan(64).is_some(), "the program could be cut");
assert!(lw.s.m.dispatch_plan(64, None).is_some(), "the program could be cut");
let program = lw.finish_dispatch(threads, 32, 64, Some(tid)).unwrap();
assert!(program.dispatch_chain.is_none());
let chain = program.dispatch_chain.as_ref().expect("split wave64 chain");
assert!(chain.pieces.len() > 1);
for piece in &chain.pieces {
super::validate::validate(piece).expect("valid split wave64 chain piece");
}
super::validate::validate(&program.dxil).expect("valid split wave64 whole shader");
}

Expand Down
13 changes: 11 additions & 2 deletions crates/xcl/src/gpu/dxil/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Lw {
let t = self.s.m.i1c(true);
let rank = self.s.wave_prefix_bit_count(t);
self.add(base, rank)
} else if y * z > 1 {
} else if y * z > 1 && total > lanes {
let i32 = self.s.m.i32();
let (g, at) = match (self.lds, lds_top_word) {
(Some(l), true) => (l, 8191),
Expand All @@ -152,8 +152,13 @@ impl Lw {
let base = self.sel(take, got, dflt);
let base = self.s.wave_read_lane_first(base);
let tid = self.add(base, hw);
self.s.barrier(barrier::GROUP_SYNC);
if lds_top_word && self.lds.is_some() {
self.s.barrier(barrier::GROUP_SYNC);
}
tid
} else if y * z > 1 {
// single native wave (`total <= lanes`): base is always 0, so no `xcl_wt` counter or barriers
self.s.wave_lane_index()
} else {
self.s.flattened_thread_id_in_group()
};
Expand All @@ -172,6 +177,8 @@ impl Lw {
}
let lane = if self.split {
self.andc(tid, 63)
} else if y * z > 1 && total <= lanes {
tid
} else {
self.s.wave_lane_index()
};
Expand Down Expand Up @@ -484,6 +491,7 @@ impl Lw {
let alive = self.cmpc(Pred::Eq, dead, 0);
let has = self.cmpc(Pred::Ne, xn, 0);
let go = self.land(alive, has);
let region = (self.cur(), self.s.m.block_count());
let _ = self.guarded(go, &mut |lw| {
lw.s.barrier(barrier::TGSM_FENCE);
let t = lw.s.m.i1c(true);
Expand Down Expand Up @@ -583,6 +591,7 @@ impl Lw {
});
let one = self.c(1);
let ho = self.xor(h, one);
self.s.m.finish_spin_region(region.0, region.1);
let theirs = at(self, ho);
let metv = self.rd(met);
let ok = self.nz(metv);
Expand Down
9 changes: 9 additions & 0 deletions crates/xcl/src/gpu/dxil/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub struct Module {
def_block: Vec<Block>,
blocks: Vec<Vec<u32>>,
preserve_block_boundary: Vec<bool>,
spin_regions: Vec<Vec<Block>>,
cur: Block,
insert_before_term: bool,
md: Vec<MdDef>,
Expand Down Expand Up @@ -302,6 +303,7 @@ impl Module {
blocks: vec![Vec::new()],
preserve_block_boundary: vec![false],
flat_reads: Vec::new(),
spin_regions: Vec::new(),
cur: Block(0),
insert_before_term: false,
md: Vec::new(),
Expand Down Expand Up @@ -559,6 +561,13 @@ impl Module {
pub fn preserve_block_boundary(&mut self, b: Block) {
self.preserve_block_boundary[b.0 as usize] = true;
}
/// Keep both halves' signal and wait in the same dispatch. The join can
/// begin the next piece since the exchange slots are checkpointed.
pub(super) fn finish_spin_region(&mut self, start: Block, first_new: usize) {
let mut region = vec![start];
region.extend((first_new..self.blocks.len()).map(|i| Block(i as u32)).filter(|&b| b != self.cur));
self.spin_regions.push(region);
}
pub fn entry_block(&self) -> Block {
Block(0)
}
Expand Down
Loading
Loading