RUNTIME 2480: A dispatch's regions are counted once and walked by count, and its row checks read the pieces

What: Pieces holds the number of chunks its count takes, computed where the axis is cut, so
Regions::len and Regions::iter divide nothing; planning and instantiating a dispatch asked for
each axis's count over its chunk size again at every len() and every walk of the axis.

Why: the regions are numbers since f5b102f317, and an instantiation reads their count and
walks them several times (the product's bound, the emptiness check, the commands' length, the
unit-axes and row checks, the regions' own walk), each time three divisions.

Verified: the bind::dispatch tests in the next msi job.

What: Regions::iter is an iterator of its own (RegionsIter), which counts each axis's piece
from x slowest to z fastest and takes each piece by its number (Pieces::piece), where it was
three nested flat_maps over chained ranges. The unit-axes and row checks (unit_axes,
multi_row) read the widest local size of each axis's pieces (Regions::widest) rather than
walking every region: each piece of an axis is some region's wherever there is a region, so
the answers are the walk's.

Why: msi profile of both binaries in one job (m3-max-37856, taskset -c 0-7, the guest shape):
at the tip of the regions-as-numbers change, unit_axes alone took 122 calling-thread samples
(about 67 ns a dispatch, 1.8% of the dispatch call) and the flat_map walk in instantiate 32
more, where base walked a Vec; the instantiation subtree measured 12.75% of the dispatch call
against base's 12.25%.

Verified (lane instleft): clippy plain 0 and the translator bind::dispatch, bind::plan and bind::graphics tests plus tests::bind 80/80 at 080d04c44a (m3-max-37875). Profile of both binaries in one job (m3-max-37874, msi, taskset -c 0-7, the guest shape), base 542c74ed91 against 080d04c44a: the instantiation subtree 12.87% -> 11.13% of the dispatch call (about 42-52 ns a guest dispatch at a 2.4-3.0 us call; RUNTIME 2478 alone measured about neutral, its translator half costing what its runtime half saved). Clippy m2v_gpu and m2v_cpu_icd and rustdoc ran at f5b102f317 (m3-max-37851, all 0); these commits touch bind/dispatch.rs only, which has no cfg-gated code; they follow on development.
This commit is contained in:
Anees Iqbal committed 2026-11-16 14:09:20 -08:00
1 parent 6bbfc310d3
commit c195086369
2 files changed
+79 -37

No files matched your search

+78 -36
View File
@@ -446,17 +446,21 @@ struct Pieces {
count: u32,
local: u32,
max: u32,
/// How many chunks `count` takes: `count / max`, rounded up.
chunks: u32,
edge: Option<Piece>,
}
impl Pieces {
/// `count` groups of `local` threads from group `base`, in chunks of at most `max`.
fn chunks(base: u32, count: u32, local: u32, max: u32) -> Pieces {
let max = max.max(1);
Pieces {
base,
count,
local,
max: max.max(1),
max,
chunks: count.div_ceil(max),
edge: None,
}
}
@@ -468,31 +472,37 @@ impl Pieces {
count: 0,
local,
max: 1,
chunks: 0,
edge: Some((base, count, local)),
}
}
/// How many pieces the axis has.
fn len(&self) -> u64 {
u64::from(self.count.div_ceil(self.max)) + u64::from(self.edge.is_some())
u64::from(self.chunks) + u64::from(self.edge.is_some())
}
/// The pieces in order: each chunk, then the edge.
fn iter(&self) -> impl Iterator<Item = Piece> + Clone {
let Pieces {
base,
count,
local,
max,
edge,
} = *self;
// Each chunk starts below `count`, so no start overflows.
(0..count.div_ceil(max))
.map(move |k| {
let done = k * max;
(base + done, (count - done).min(max), local)
})
.chain(edge)
/// Piece `k` in order: each chunk, then the edge; `None` past the last.
fn piece(&self, k: u64) -> Option<Piece> {
match u32::try_from(k) {
// Each chunk starts below `count`, so no start overflows.
Ok(k) if k < self.chunks => {
let done = k * self.max;
Some((
self.base + done,
(self.count - done).min(self.max),
self.local,
))
}
Ok(k) if k == self.chunks => self.edge,
_ => None,
}
}
/// The widest local size of its pieces, 0 where it has none.
fn widest(&self) -> u32 {
let chunk = if self.chunks > 0 { self.local } else { 0 };
chunk.max(self.edge.map_or(0, |(_, _, local)| local))
}
}
@@ -532,19 +542,21 @@ impl Regions {
}
/// The regions in order.
pub(crate) fn iter(&self) -> impl Iterator<Item = Region> + Clone + '_ {
let [x, y, z] = &self.pieces;
let multi = self.multi;
x.iter().flat_map(move |(bx, cx, lx)| {
y.iter().flat_map(move |(by, cy, ly)| {
z.iter().map(move |(bz, cz, lz)| Region {
local: [lx, ly, lz],
groups: [cx, cy, cz],
base: [bx, by, bz],
multi,
})
})
})
pub(crate) fn iter(&self) -> RegionsIter {
RegionsIter {
regions: *self,
at: [0; 3],
left: self.len(),
}
}
/// The widest local size along `axis` of any region, 0 where there is none: each piece of an
/// axis is some region's where there is a region.
fn widest(&self, axis: usize) -> u32 {
match self.is_empty() {
true => 0,
false => self.pieces[axis].widest(),
}
}
/// Every region with `MultiRegion` 1: it reads its record.
@@ -553,6 +565,39 @@ impl Regions {
}
}
/// [`Regions::iter`]: each region in order, the piece of each axis it takes counted from x
/// slowest to z fastest.
#[derive(Clone)]
pub(crate) struct RegionsIter {
regions: Regions,
at: [u64; 3],
left: usize,
}
impl Iterator for RegionsIter {
type Item = Region;
fn next(&mut self) -> Option<Region> {
self.left = self.left.checked_sub(1)?;
let pieces = &self.regions.pieces;
let [x, y, z] = [0, 1, 2].map(|i| pieces[i].piece(self.at[i]));
let ((bx, cx, lx), (by, cy, ly), (bz, cz, lz)) = (x?, y?, z?);
for i in [2, 1, 0] {
self.at[i] += 1;
if i == 0 || self.at[i] < pieces[i].len() {
break;
}
self.at[i] = 0;
}
Some(Region {
local: [lx, ly, lz],
groups: [cx, cy, cz],
base: [bx, by, bz],
multi: self.regions.multi,
})
}
}
/// The regions of a direct dispatch whose axes split into `pieces`; `MultiRegion` whenever there
/// is more than one region or `force_multi`.
fn product(pieces: [Pieces; 3], force_multi: bool) -> Result<Regions, BindError> {
@@ -581,7 +626,7 @@ fn product(pieces: [Pieces; 3], force_multi: bool) -> Result<Regions, BindError>
/// 2 `z`): Metal's `threads_per_threadgroup` there is 1, and so is every region's local size.
fn unit_axes(planned: &Planned) -> u8 {
(0..3)
.filter(|&a| planned.nominal[a] == 1 && planned.regions.iter().all(|r| r.local[a] <= 1))
.filter(|&a| planned.nominal[a] == 1 && planned.regions.widest(a) <= 1)
.fold(0, |axes, a| axes | 1 << a)
}
@@ -592,10 +637,7 @@ fn unit_axes_run(axes: u8, planned: &Planned) -> bool {
}
fn multi_row(planned: &Planned) -> bool {
planned
.regions
.iter()
.any(|r| r.local[1] > 1 || r.local[2] > 1)
planned.regions.widest(1) > 1 || planned.regions.widest(2) > 1
}
/// The regions of a dispatch.
+1 -1
View File
@@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 524;
/// changes no behaviour, and then updates that hash alone with the commit saying why, or changes
/// behaviour and also bumps this version. The commit that bumps it says what changed; this comment
/// keeps no history.
pub const RUNTIME_VERSION: u32 = 2479;
pub const RUNTIME_VERSION: u32 = 2480;
/// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex,
/// of the manifest, the build script, and exactly the files the crate roots' module tree compiles