Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
4aadaab
D3D12.X: an ExecuteIndirect of a split program runs its pieces
Kalmarv Nov 25, 2026
dbc793d
Tests: a split program's ExecuteIndirect matches the whole program
Kalmarv Nov 25, 2026
9125edb
D3D12.X: a chained ExecuteIndirect drops slabs only past 32 before it…
Kalmarv Nov 25, 2026
f0cef78
D3D12.X: a chained ExecuteIndirect under the title's predication is n…
Kalmarv Nov 25, 2026
70f687a
Tests: the predicated ExecuteIndirect test binds the chain's root sig…
Kalmarv Nov 25, 2026
3ca9071
D3D12.X: once a split program's whole form is built, its last Execute…
Kalmarv Nov 25, 2026
0755bd7
Tests: the chained ExecuteIndirect device test binds descriptor heaps
Kalmarv Nov 25, 2026
50205bc
D3D12.X: a chained ExecuteIndirect takes on a list the pool does not …
Kalmarv Nov 25, 2026
073f8ea
D3D12.X: a list made or released at an address drops the chain scratch
Kalmarv Nov 26, 2026
551a75e
D3D12.X: a PC allocator put aside lets go of its chain recordings onc…
Kalmarv Nov 26, 2026
97a70a7
D3D12.X: a title thread does not wait behind a background whole build…
Kalmarv Nov 26, 2026
99ca688
D3D12.X: the split-program counter line says what holds chain recordings
Kalmarv Nov 26, 2026
c9904d7
D3D12.X: whole forms of split programs are built in turn, not a threa…
Kalmarv Nov 26, 2026
5b9d4ff
D3D12.X: a chained ExecuteIndirect keeps the most rounds until the wh…
Kalmarv Nov 26, 2026
19be659
D3D12.X: the pieces of a split program read their slab from a root CBV
Kalmarv Nov 25, 2026
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
35 changes: 35 additions & 0 deletions crates/xcl/src/gpu/dxil/module/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,41 @@ mod tests {
let result = runner::dispatch_split_by(&chain, [2, 3, 2], &initial, most).unwrap();
assert_eq!(result, expected, "split {threads}, {most} groups at once");
}
// through ExecuteIndirect (`pso::record_slicer`): one command, several with empty ones
// and a count that leaves the last out, a stride with other words, in slabs; and with
// the whole form built, as a program has it from its first ExecuteIndirect on: its
// arguments are then empty and recorded before the rounds
let one = [2, 3, 2];
let several = [0, 1, 1, 2, 3, 2, 2, 3, 2];
let wide = [2, 3, 2, 7, 7, 0, 0, 0, 7, 7];
let runs: [(&[u32], u32, u32, Option<u32>); 3] = [(&one, 12, 1, None), (&several, 12, 3, Some(2)), (&wide, 20, 2, None)];
for (args, stride, commands, count) in runs {
for (most, rounds) in [(12, 1), (12, 3), (5, 4), (2, 6), (1, 12), (1, 16)] {
for built in [None, Some(&whole[..])] {
let ind = runner::ChainIndirect { args, stride, commands, count, rounds, whole: built };
let (result, tail) = runner::dispatch_split_indirect(&chain, &ind, &initial, most).unwrap();
let slabs = crate::gpu::recomp::pso::slab_count([2, 3, 2], most) as u32;
let what = format!("split {threads}, {args:?}, {most} groups, {rounds} rounds, whole form {}", built.is_some());
assert_eq!(tail, [0, 0, slabs, 0], "{what}");
assert_eq!(result, expected, "{what}");
}
}
}
// more slabs than rounds: the slicer says so, the rounds dispatch nothing, and the whole
// form runs instead on the slicer's arguments
for (most, rounds) in [(1, 11), (2, 2), (5, 2)] {
let slabs = crate::gpu::recomp::pso::slab_count([2, 3, 2], most) as u32;
let ind = runner::ChainIndirect { args: &one, stride: 12, commands: 1, count: None, rounds, whole: Some(&whole) };
let (result, tail) = runner::dispatch_split_indirect(&chain, &ind, &initial, most).unwrap();
assert_eq!(tail, [1, 0, slabs, 0], "split {threads}, {most} groups, {rounds} rounds");
assert_eq!(result, expected, "split {threads}, whole form for {most} groups, {rounds} rounds");
// … and without one the rounds still run: their groups are done, the others not
let ind = runner::ChainIndirect { whole: None, ..ind };
let (result, _) = runner::dispatch_split_indirect(&chain, &ind, &initial, most).unwrap();
let done: usize = crate::gpu::recomp::pso::dispatch_slabs([2, 3, 2], most).iter().take(rounds as usize).map(|s| s.1.iter().product::<u32>() as usize).sum();
let changed = result.iter().zip(&initial).filter(|(a, b)| a != b).count();
assert!(changed > 0 && changed < words && done < 12, "split {threads}, {most} groups, {rounds} rounds: {changed} words changed for {done} groups");
}
println!("CHAIN_BRANCH threads={threads} pieces={} scratch_words_per_group={} checked_words={words} guards=16", chain.pieces.len(), chain.dwords_per_group);
}
}
Expand Down
225 changes: 217 additions & 8 deletions crates/xcl/src/gpu/recomp/pso.rs

Large diffs are not rendered by default.

101 changes: 93 additions & 8 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ pub fn dispatch(dxil: &[u8], groups: [u32; 3], sgprs: &[u32], mem: &[u32]) -> Re
})
}
.map_err(|x| format!("CreateComputePipelineState: {x}"))?;
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pso, &gpu.rs, groups, sgprs, mem, 1, false, None).map(|(m, _)| m)
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pso, &gpu.rs, groups, sgprs, mem, 1, false, None, None).map(|(m, _)| m)
}

#[cfg(test)]
Expand Down Expand Up @@ -1651,9 +1651,83 @@ pub(crate) fn dispatch_split_samples(
}).map_err(e)
}).collect::<Result<_,_>>()?;
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pieces[0], &rs, groups, sgprs, mem, reps, timed,
Some((&pieces, chain.dwords_per_group, most)))
Some((&pieces, chain.dwords_per_group, most)), None)
}

/// A split program's ExecuteIndirect: the title's commands (one Dispatch, `stride` bytes each, as
/// words), their count, the rounds, and the whole form for more slabs than rounds.
#[cfg(test)]
pub(crate) struct ChainIndirect<'a> {
pub args: &'a [u32],
pub stride: u32,
pub commands: u32,
pub count: Option<u32>,
pub rounds: u32,
pub whole: Option<&'a [u8]>,
}

/// `dispatch_split_by` through ExecuteIndirect as the runtime records it (`pso::record_slicer`,
/// the ABI bound again, the whole form on the slicer's arguments, `record_dispatch_chain_indirect`):
/// the memory and the slicer's words after its commands (partial, 0, slabs, 0).
#[cfg(test)]
pub(crate) fn dispatch_split_indirect(
chain: &crate::gpu::dxil::kernel::DispatchChain, ind: &ChainIndirect, mem: &[u32], most: u64,
) -> Result<(Vec<u32>, [u32; 4]), String> {
let mut g = GPU.lock().unwrap();
let gpu = g.get_or_insert_with(init).as_mut().map_err(|e| e.clone())?;
let dev = gpu.dev.clone();
let rs = super::pso::dispatch_chain_root_signature(&dev, &[])?;
let make = |dxil: &[u8]| unsafe {
dev.CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC {
pRootSignature: transmute_copy(&rs),
CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr().cast(), BytecodeLength: dxil.len() },
..Default::default()
}).map_err(e)
};
let pieces: Vec<ID3D12PipelineState> = chain.pieces.iter().map(|d| make(d)).collect::<Result<_, _>>()?;
let whole = ind.whole.map(make).transpose()?;
let mut words = ind.args.to_vec();
words.push(ind.count.unwrap_or(0));
let count_at = ind.args.len() as u64 * 4;
let args = buffer(&dev, words.len() as u64 * 4, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, false)?;
let commands = unsafe { super::pso::chain_commands_buffer(&dev, ind.rounds, ind.commands) }.ok_or("chain command buffer")?;
let one = super::pso::dispatch_command_signature(&dev)?;
let at = super::pso::chain_tail_offset(ind.rounds);
unsafe {
let mut p = std::ptr::null_mut();
args.Map(0, None, Some(&mut p)).map_err(e)?;
std::ptr::copy_nonoverlapping(words.as_ptr(), p as *mut u32, words.len());
args.Unmap(0, None);
}
let mut record = |list: &ID3D12GraphicsCommandList, scratch: &ID3D12Resource, views: [u64; 3]| unsafe {
let va = args.GetGPUVirtualAddress();
super::pso::record_slicer(list, va, ind.stride, ind.commands, ind.count.map(|_| va + count_at), ind.rounds, most, whole.is_some(), &commands)?;
list.SetComputeRootSignature(&rs);
for (i, v) in views.into_iter().enumerate() {
list.SetComputeRootUnorderedAccessView(i as u32, v);
}
if let Some(whole) = &whole {
list.SetPipelineState(whole);
list.ExecuteIndirect(&one, ind.commands, &commands, at + 16, None::<&ID3D12Resource>, 0);
}
super::pso::record_dispatch_chain_indirect(list, &one, &commands, ind.rounds, scratch, &pieces);
Ok(())
};
let mem = dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pieces[0], &rs, [1, 1, 1], &[], mem, 1, false,
Some((&pieces, chain.dwords_per_group, most)), Some(&mut record)).map(|(m, _)| m)?;
unsafe {
let mut p = std::ptr::null_mut();
commands.Map(0, None, Some(&mut p)).map_err(e)?;
let tail = std::ptr::read_volatile((p as *const u8).add(at as usize) as *const [u32; 4]);
commands.Unmap(0, None);
Ok((mem, tail))
}
}

/// Records a chain's dispatch in `dispatch_with` instead of `record_dispatch_chain`: the list,
/// the scratch (`most` groups) and the addresses of root views 0-2.
type RecordChain<'a> = &'a mut dyn FnMut(&ID3D12GraphicsCommandList, &ID3D12Resource, [u64; 3]) -> Result<(), String>;

/// `dispatch` run `reps` times in one command list (a UAV barrier between them, memory not
/// reset), each between two GPU timestamps: GPU milliseconds per dispatch – no pipeline,
/// buffer, upload, submission or CPU wait time in them. For benchmarks.
Expand All @@ -1668,7 +1742,7 @@ pub fn dispatch_timed(dxil: &[u8], groups: [u32; 3], sgprs: &[u32], mem: &[u32],
})
}
.map_err(|x| format!("CreateComputePipelineState: {x}"))?;
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pso, &gpu.rs, groups, sgprs, mem, reps.max(1), true, None).map(|(_, t)| t)
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pso, &gpu.rs, groups, sgprs, mem, reps.max(1), true, None, None).map(|(_, t)| t)
}

#[cfg(test)]
Expand Down Expand Up @@ -1709,7 +1783,7 @@ pub(crate) fn profile_buffer_forms(
(&pieces[i][0], &rs, Some((pieces[i].as_slice(), words, most)))
};
let (out, times) = dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, pso, signature,
groups, sgprs, mem, 12, true, chain)?;
groups, sgprs, mem, 12, true, chain, None)?;
if out != expected { return Err(format!("replay readback differs for form {variant}")); }
if round >= 3 { samples[variant].extend_from_slice(&times[2..]); }
}
Expand All @@ -1730,7 +1804,7 @@ pub fn dispatch_on(dev: &ID3D12Device, pso: &ID3D12PipelineState, rs: &ID3D12Roo
unsafe {
let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).map_err(e)?;
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).map_err(e)?;
dispatch_with(dev, &queue, &fence, &mut 0, pso, rs, groups, sgprs, mem, 1, false, None).map(|(m, _)| m)
dispatch_with(dev, &queue, &fence, &mut 0, pso, rs, groups, sgprs, mem, 1, false, None, None).map(|(m, _)| m)
}
}

Expand All @@ -1748,6 +1822,7 @@ fn dispatch_with(
reps: usize,
timed: bool,
chain: Option<(&[ID3D12PipelineState], u32, u64)>,
mut record: Option<RecordChain>,
) -> Result<(Vec<u32>, Vec<f64>), String> {
let mut io = sgprs.to_vec();
io.resize(KERNEL_SGPRS, 0);
Expand Down Expand Up @@ -1803,9 +1878,17 @@ fn dispatch_with(
}
let scratch = chain.map(|(_, words, most)| {
let first = super::pso::dispatch_slabs(groups, most)[0].1;
let bytes = first.iter().fold(4 * u64::from(words), |n, &g| n * u64::from(g));
let bytes = if record.is_some() { most * 4 * u64::from(words) } else { first.iter().fold(4 * u64::from(words), |n, &g| n * u64::from(g)) };
buffer(dev, bytes, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true)
}).transpose()?;
// the slabs' constants (`pso::record_dispatch_chain`), the same for every one of the `reps`
let constants = chain.filter(|_| record.is_none()).map(|(_, _, most)| {
let bytes = super::pso::slab_count(groups, most) * u64::from(super::pso::CHAIN_COMMAND_BYTES);
let b = buffer(dev, bytes, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, false)?;
let mut p = std::ptr::null_mut();
b.Map(0, None, Some(&mut p)).map_err(e)?;
Ok::<_, String>((p as *mut u8, b.GetGPUVirtualAddress(), b))
}).transpose()?;
// `reps` dispatches, each between two timestamps when `timed`
let stamps = 2 * reps as u32;
let queries = if timed {
Expand All @@ -1824,8 +1907,10 @@ fn dispatch_with(
if let Some((heap, _)) = &queries {
list.EndQuery(heap, D3D12_QUERY_TYPE_TIMESTAMP, 2 * k);
}
if let (Some((pieces, _, most)), Some(scratch)) = (chain, &scratch) {
super::pso::record_dispatch_chain(&list, groups, most, scratch, k > 0, pieces);
if let (Some(record), Some(scratch)) = (record.as_mut(), &scratch) {
record(&list, scratch, [io_buf.GetGPUVirtualAddress(), res_buf.GetGPUVirtualAddress(), mem_buf.GetGPUVirtualAddress()])?;
} else if let (Some((pieces, _, most)), Some(scratch), Some((cpu, gpu, _))) = (chain, &scratch, &constants) {
super::pso::record_dispatch_chain(&list, groups, most, scratch, k > 0, (*cpu, *gpu), pieces);
} else {
list.Dispatch(groups[0], groups[1], groups[2]);
}
Expand Down
Loading
Loading