Runtime: a piece names its kernels without a count or an allocation of its own

WIP for the planner's fix 2, to be measured: the piece's kernels go in a list the
command buffer's kept objects hold (capacity kept for the queue's next recording,
within KEPT_BOUND), each its content taken with no count (Uncount), since the
command buffer keeps the state it bound until it retires, after the rate
update. Verified: cargo fmt only.
This commit is contained in:
Anees Iqbal committed 2026-11-22 12:25:53 -08:00
1 parent 2ec8680181
commit eaba828dcd
3 files changed
+88 -33

No files matched your search

+16 -6
View File
@@ -13,7 +13,7 @@ use crate::hazard::{Access, Hazards};
use crate::kept::{Lent, Objects};
use crate::memory::Block;
use crate::over::{self, Overs, Reached};
use crate::piece::Work;
use crate::piece::{Kernels, Work};
use crate::pipeline::{Compiled, ComputePipelineState};
use crate::queue::QueueInner;
use crate::record::{self, Reach, Recorder};
@@ -1114,13 +1114,13 @@ impl Building {
kernel: Option<&Arc<Compiled>>,
run: Option<u64>,
) -> Result<(), Error> {
let last = self.last();
let (last, kernels) = self.piece();
// Commands after a signal go in a new segment anyway (`current`).
if !last.signals.is_empty() {
return self.split();
}
let overs = last.overs.ahead() || last.overs.leaning().next().is_some();
if !last.work.full(kernel, run) || overs || !last.managed_wrote.is_empty() {
if !last.work.full(kernels, kernel, run) || overs || !last.managed_wrote.is_empty() {
return Ok(());
}
self.split()?;
@@ -1139,7 +1139,15 @@ impl Building {
unbound: bool,
) {
self.gpu.unbound |= unbound;
self.last().work.add(kernel, groups);
let (last, kernels) = self.piece();
last.work.add(kernels, kernel, groups);
}
/// The last segment, and the list its piece's kernels are in (`crate::piece::Kernels`).
fn piece(&mut self) -> (&mut Segment, &mut Kernels) {
let Gpu { segments, kept, .. } = &mut self.gpu;
let last = segments.len() - 1;
(&mut segments[last], &mut kept.kernels)
}
/// Commands of a blit or render encoder follow: the segment's run tells no rate.
@@ -1774,7 +1782,9 @@ pub(crate) fn retire(
let mut pieces = 0;
if let ([segment], Some((start, end)), None) = (&gpu.segments[..], ticks, error) {
if segment.stamps.is_none() && !segment.work.led() {
segment.work.ran(core.ticks.span(start, end));
segment
.work
.ran(&gpu.kept.kernels, core.ticks.span(start, end));
}
}
for (index, segment) in gpu.segments.iter().enumerate() {
@@ -1791,7 +1801,7 @@ pub(crate) fn retire(
};
if let Some((start, end)) = read.and_then(|(own, end)| Some((start(own)?, end))) {
let run = core.ticks.span(start, end);
segment.work.ran(run);
segment.work.ran(&gpu.kept.kernels, run);
pieces += run;
}
}
+7
View File
@@ -11,6 +11,7 @@
//! buffers ([`Objects`]), so a command buffer allocates and frees none of it.
use crate::buffer::{Buffer, BufferObject};
use crate::piece::Kernels;
use crate::pipeline::ComputePipelineState;
use crate::render::RenderPipelineState;
use crate::sampler::SamplerState;
@@ -234,6 +235,8 @@ pub(crate) struct ByKind {
pub(crate) samplers: Kept<SamplerState>,
pub(crate) pipelines: Kept<ComputePipelineState>,
pub(crate) render_pipelines: Kept<RenderPipelineState>,
/// Its pieces' kernels, of the states `pipelines` keeps (`crate::piece::Kernels`).
pub(crate) kernels: Kernels,
}
impl ByKind {
@@ -264,6 +267,10 @@ impl ByKind {
self.samplers.clear();
self.pipelines.clear();
self.render_pipelines.clear();
match self.kernels.capacity() > KEPT_BOUND {
true => self.kernels = Kernels::new(),
false => self.kernels.clear(),
}
}
}
+65 -27
View File
@@ -12,9 +12,12 @@
//! expected run is (`QueueInner::expected_run`): it bounds where a piece ends, and never chooses
//! between two forms of the work.
use crate::kept::Uncount;
use crate::pipeline::Compiled;
use crate::queue::next_run;
use crate::submit::GROUP_RUN;
use std::mem::ManuallyDrop;
use std::ops::Range;
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::sync::Arc;
@@ -53,11 +56,25 @@ impl Rate {
}
}
/// The dispatches of one piece: each kernel once, with its threadgroups summed, and their expected
/// run.
/// A kernel of a piece, with its threadgroups summed: its content as its state holds it, taking
/// no count ([`Uncount`]), since the command buffer keeps the state it bound until it retires,
/// after its pieces' runs set their rates (`crate::command::retire`).
pub(crate) struct Kernel {
compiled: ManuallyDrop<Arc<Compiled>>,
groups: u64,
}
/// A command buffer's pieces' kernels, piece by piece: a list its kept objects hold
/// (`crate::kept::ByKind`), whose capacity goes on to the queue's next recording, so a recording
/// allocates for them only past what an earlier one left, and frees none.
pub(crate) type Kernels = Vec<Kernel>;
/// The dispatches of one piece: each kernel once, with its threadgroups summed, in its command
/// buffer's list ([`Kernels`]), and their expected run.
#[derive(Default)]
pub(crate) struct Work {
kernels: Vec<(Arc<Compiled>, u64)>,
/// Its kernels' places in the list: the last piece's run to the list's end.
kernels: Range<usize>,
/// The expected GPU run of its dispatches, in nanoseconds, summed.
pub(crate) run: u64,
/// It holds dispatches of a kernel whose rate was unknown, and of no other kernel, so its run
@@ -82,10 +99,13 @@ impl Work {
/// where an encoder opens, no kernel and `Some(0)`) begins a new piece: this one holds a
/// dispatch, and holds a kernel of unknown rate (unless the dispatch is that kernel's too, of
/// a rate still unknown), or the dispatch's rate is unknown, or the two would pass the bound.
pub(crate) fn full(&self, kernel: Option<&Arc<Compiled>>, run: Option<u64>) -> bool {
let own = |kernel: &Arc<Compiled>| {
(self.kernels.iter()).all(|(kept, _)| Arc::ptr_eq(kept, kernel))
};
pub(crate) fn full(
&self,
list: &[Kernel],
kernel: Option<&Arc<Compiled>>,
run: Option<u64>,
) -> bool {
let own = |kernel: &Arc<Compiled>| self.of(list).iter().all(|kept| kept.is(kernel));
!self.is_empty()
&& match (self.alone, run) {
(true, None) => !kernel.is_some_and(own),
@@ -104,24 +124,36 @@ impl Work {
}
}
/// Adds a dispatch of `kernel`: `groups` threadgroups, None for an indirect dispatch.
pub(crate) fn add(&mut self, kernel: &Arc<Compiled>, groups: Option<u64>) {
/// Adds a dispatch of `kernel`, of the state its command buffer keeps, to the piece, the last
/// of `list`'s: `groups` threadgroups, None for an indirect dispatch.
pub(crate) fn add(&mut self, list: &mut Kernels, kernel: &Arc<Compiled>, groups: Option<u64>) {
let expected = Work::expected(kernel, groups);
let own = (self.kernels.iter()).all(|(kept, _)| Arc::ptr_eq(kept, kernel));
let own = self.of(list).iter().all(|kept| kept.is(kernel));
self.blind |= groups.is_none() || ((expected.is_none() || self.alone) && !own);
self.alone |= expected.is_none();
self.run = self.run.saturating_add(expected.unwrap_or(0));
let groups = groups.unwrap_or(1);
match self
.kernels
.iter_mut()
.find(|(kept, _)| Arc::ptr_eq(kept, kernel))
{
Some((_, summed)) => *summed = summed.saturating_add(groups),
None => self.kernels.push((Arc::clone(kernel), groups)),
let range = self.kernels.clone();
match (list.get_mut(range)).and_then(|mine| mine.iter_mut().find(|kept| kept.is(kernel))) {
Some(kept) => kept.groups = kept.groups.saturating_add(groups),
None => {
if self.kernels.is_empty() {
self.kernels = list.len()..list.len();
}
// SAFETY: the command buffer keeps the state `kernel` is of until it retires,
// after the list's last use (`crate::command::retire`).
let compiled = unsafe { kernel.uncounted() };
list.push(Kernel { compiled, groups });
self.kernels.end = list.len();
}
}
}
/// Its kernels in `list`, its command buffer's.
fn of<'a>(&self, list: &'a [Kernel]) -> &'a [Kernel] {
list.get(self.kernels.clone()).unwrap_or_default()
}
/// Commands of another encoder go in the piece: after its dispatches, or ahead of them.
pub(crate) fn blind(&mut self) {
self.blind |= !self.is_empty();
@@ -135,24 +167,30 @@ impl Work {
/// The piece ran `nanos` on the GPU: a kernel alone in it takes its rate from that run, and
/// several each scale theirs by the run against what they expected.
pub(crate) fn ran(&self, nanos: u64) {
pub(crate) fn ran(&self, list: &[Kernel], nanos: u64) {
if self.blind {
return;
}
let kernels = self.of(list);
let picos = u128::from(nanos) * 1000;
let rate = |kernel: &Compiled| u128::from(kernel.rate.0.load(Relaxed));
let expected: u128 = (self.kernels.iter())
.map(|(kernel, groups)| rate(kernel) * u128::from(*groups))
let expected: u128 = (kernels.iter())
.map(|kept| rate(&kept.compiled) * u128::from(kept.groups))
.sum();
for (kernel, groups) in &self.kernels {
let picos = match self.kernels.len() {
1 => picos / u128::from((*groups).max(1)),
for kept in kernels {
let picos = match kernels.len() {
1 => picos / u128::from(kept.groups.max(1)),
_ if expected == 0 => continue,
_ => rate(kernel).saturating_mul(picos) / expected,
_ => rate(&kept.compiled).saturating_mul(picos) / expected,
};
kernel
.rate
.ran(u64::try_from(picos).unwrap_or(u64::MAX - 1));
(kept.compiled.rate).ran(u64::try_from(picos).unwrap_or(u64::MAX - 1));
}
}
}
impl Kernel {
/// Whether it is `kernel`.
fn is(&self, kernel: &Arc<Compiled>) -> bool {
Arc::ptr_eq(&self.compiled, kernel)
}
}