Fill by transfers in sixteenfold steps, three barriers for 256 MiB where doubling took twelve

Where fills go as transfers (DriverFact::FillCompiledPerCommandPool, MoltenVK), a fill updated
its first 64 KiB, then copied all that was filled so far onto the next words past a barrier,
doubling each time: a 256 MiB fill took 12 barriers and 12 copies. MoltenVK ends its blit
encoder at each barrier and makes the next one inside vkQueueSubmit, about 10 us each on an M3
Max, so the conformance case event-wait-behind-a-running-signal (192 fills of 256 MiB in one
command buffer) kept its signaller Committed for tens of milliseconds where Metal schedules the
same fills 0.04 to 0.18 ms after the commit.

Now each step past a barrier is one vkCmdCopyBuffer of up to 15 regions, each a copy of what is
filled onto the next words, so each barrier multiplies what is filled by 16: 256 MiB takes 3
barriers and 3 copy commands (45 regions). The regions of one command write disjoint words that
no earlier step wrote and read only words written before the barrier ahead, so one command's
sources and destinations never overlap, as vkCmdCopyBuffer requires.

Verified: the exhaustive check (a_fill_by_transfers_writes_exactly_its_words) simulates every
step over every word range it sweeps, and now holds each region of a command apart from the
others; a_fill_by_transfers_takes_a_barrier_per_sixteenfold pins 1 MiB (1 barrier, 15 regions),
256 MiB (3 barriers) and a 12-byte tail. 79 fill lanes (test(/fill/) | runtime_lane::blits) pass
on the M3 Max with MoltenVK 1.4.2. Release probe of commit to the signaller leaving Committed
(M3 Max, loaded host): before 26.1 to 27.0 ms (plain-event row) and 9.6 to 10.1 ms (shared-event
row); after 20.1 to 21.2 ms and 7.4 to 20.1 ms. A Time Profiler trace of the after run still
puts the submitting thread in MoltenVK's blit-encoder creation and region encoding.

(cherry picked from commit 9a82c788b579cb1b23b9e1560e92e275fee7d0ee)
This commit is contained in:
Anees Iqbal committed 2026-11-03 17:03:10 -08:00
1 parent 266ed619b7
commit 874bd7f09b
2 files changed
+92 -42

No files matched your search

@@ -1,12 +1,13 @@
//! A fill by transfers (`word_fill.rs`): exact, and every copy reads only words the fill wrote
//! before a barrier and writes words nothing in the fill wrote yet.
use super::super::word_fill::{steps, Step};
use super::super::word_fill::{steps, Copy, Step};
/// The fill of `words` by its steps, on a buffer of `len` bytes: every byte of `words` ends as the
/// fill's, every other byte untouched; each update writes at most 65536 bytes, a multiple of 4 at
/// a multiple of 4; each copy's source lies in words written before the barrier ahead of it, and
/// its destination in words not yet written, so they never overlap.
/// its destination in words not yet written, not even by another region of its command, so one
/// command's sources and destinations never overlap.
fn check(words: std::ops::Range<u64>, len: u64) {
const FILL: u8 = 0xa5;
const UNTOUCHED: u8 = 0x5a;
@@ -27,17 +28,20 @@ fn check(words: std::ops::Range<u64>, len: u64) {
}
}
Step::Barrier => visible.clone_from(&written),
Step::Copy { from, to, len: n } => {
assert!(
from + n <= to || to + n <= from,
"{words:?}: overlapping copy"
);
for i in 0..n {
let (src, dst) = ((from + i) as usize, (to + i) as usize);
assert!(visible[src], "{words:?}: copy reads {src}, not yet visible");
assert!(!written[dst], "{words:?}: copy writes {dst} twice");
bytes[dst] = bytes[src];
written[dst] = true;
Step::Copies(copies) => {
assert!(!copies.is_empty(), "{words:?}: a command of no region");
for Copy { from, to, len: n } in copies {
assert!(
from + n <= to || to + n <= from,
"{words:?}: overlapping copy"
);
for i in 0..n {
let (src, dst) = ((from + i) as usize, (to + i) as usize);
assert!(visible[src], "{words:?}: copy reads {src}, not yet visible");
assert!(!written[dst], "{words:?}: copy writes {dst} twice");
bytes[dst] = bytes[src];
written[dst] = true;
}
}
}
}
@@ -69,18 +73,31 @@ fn a_fill_by_transfers_writes_exactly_its_words() {
}
#[test]
fn a_fill_by_transfers_takes_a_barrier_per_doubling() {
fn a_fill_by_transfers_takes_a_barrier_per_sixteenfold() {
assert!(steps(0..0).is_empty());
assert_eq!(steps(4..260), [Step::Update(4..260)]);
// 1 MiB: 64 KiB updated, then four doublings to 128, 256, 512 KiB and 1 MiB.
// 1 MiB: 64 KiB updated, then fifteen copies of it in one command.
let s = steps(0..1 << 20);
assert_eq!(s.iter().filter(|s| **s == Step::Barrier).count(), 4);
assert_eq!(s.iter().filter(|s| **s == Step::Barrier).count(), 1);
let copies: Vec<Copy> = (1..16)
.map(|at| Copy {
from: 0,
to: at << 16,
len: 1 << 16,
})
.collect();
assert_eq!(s.last(), Some(&Step::Copies(copies)));
// 256 MiB: to 1, 16 and 256 MiB, three barriers where doubling took twelve.
let s = steps(0..256 << 20);
assert_eq!(s.iter().filter(|s| **s == Step::Barrier).count(), 3);
// A tail shorter than what is filled is the last region's.
let s = steps(0..(1 << 20) + 12);
assert_eq!(
s.last(),
Some(&Step::Copy {
Some(&Step::Copies(vec![Copy {
from: 0,
to: 1 << 19,
len: 1 << 19
})
to: 1 << 20,
len: 12
}]))
);
}
+55 -22
View File
@@ -9,11 +9,21 @@
//! 250 to 330 ms in that submit on an M4. On a release measured so
//! (`DriverFact::FillCompiledPerCommandPool`, MoltenVK 1.4.1), a fill goes as transfers, which
//! MoltenVK runs as blits and compiles nothing for:
//! `vkCmdUpdateBuffer` of the first words (at most 65536 bytes, its limit), then copies of the
//! filled words onto the rest, each copying all that is filled so far, past a barrier.
//! `vkCmdUpdateBuffer` of the first words (at most 65536 bytes, its limit), then, past each
//! barrier, one `vkCmdCopyBuffer` of up to [`FAN_OUT`] regions, each a copy of all that is
//! filled so far onto the next words, so each barrier multiplies what is filled by
//! `FAN_OUT + 1`.
//! - **Few barriers.** MoltenVK ends its blit encoder at each barrier and makes a new one for the
//! next copy, and encodes it inside `vkQueueSubmit`: an encoder costs about 10 us of the
//! submitting thread on an M3 Max, where one region more in an encoder costs a blit. A fill of
//! 256 MiB took 12 barriers when each copy doubled what was filled, and 192 such fills kept their
//! command buffer's `vkQueueSubmit` 10 to 27 ms (an M3 Max, MoltenVK 1.4.2, the conformance case
//! `event-wait-behind-a-running-signal`, where Metal schedules the same fills 0.04 to 0.18 ms
//! after the commit); with sixteenfold steps it takes 3.
//! - **Exact.** Each copy reads only words the fill wrote before the barrier ahead of it, and
//! writes words no earlier step of the fill wrote, so its source and destination never overlap
//! (as `vkCmdCopyBuffer` requires), and every word ends as the fill's.
//! writes words no earlier step of the fill wrote, nor another copy of its step, so one
//! command's sources and destinations never overlap (as `vkCmdCopyBuffer` requires), and every
//! word ends as the fill's.
//! - **Elsewhere** `vkCmdFillBuffer`, unchanged, on every other driver and release.
//! - **Counted.** Each fill counts as one of the two forms (`WorkCounts::fills_recorded` and
//! `fills_by_transfers`), so a run shows which its device took; a new buffer's first zeros
@@ -30,6 +40,10 @@ use std::ops::Range;
/// The most bytes one `vkCmdUpdateBuffer` writes.
const UPDATE_LIMIT: u64 = 65536;
/// The most regions one step's `vkCmdCopyBuffer` copies what is filled onto (the module docs):
/// each step multiplies what is filled by one more than this.
const FAN_OUT: u64 = 15;
/// One step of a fill by transfers ([`steps`]).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Step {
@@ -37,12 +51,21 @@ pub(crate) enum Step {
Update(Range<u64>),
/// The transfers before it have written before the ones after it read.
Barrier,
/// `vkCmdCopyBuffer` of `len` bytes at `from` to `to`, within the buffer.
Copy { from: u64, to: u64, len: u64 },
/// One `vkCmdCopyBuffer` of these regions, within the buffer.
Copies(Vec<Copy>),
}
/// One region of a [`Step::Copies`]: `len` bytes at `from` to `to`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Copy {
pub(crate) from: u64,
pub(crate) to: u64,
pub(crate) len: u64,
}
/// The steps that fill `words` (its ends multiples of 4) by transfers: the first words updated,
/// then each copy doubles what is filled, from the filled words at its start, past a barrier.
/// then, past each barrier, up to [`FAN_OUT`] copies of what is filled, from the filled words at
/// its start, onto the words after it.
pub(crate) fn steps(words: Range<u64>) -> Vec<Step> {
let total = words.end.saturating_sub(words.start);
if total == 0 {
@@ -52,14 +75,20 @@ pub(crate) fn steps(words: Range<u64>) -> Vec<Step> {
let mut steps = vec![Step::Update(words.start..words.start + first)];
let mut filled = first;
while filled < total {
let len = filled.min(total - filled);
let mut copies = Vec::new();
let mut to = filled;
while to < total && (copies.len() as u64) < FAN_OUT {
let len = filled.min(total - to);
copies.push(Copy {
from: words.start,
to: words.start + to,
len,
});
to += len;
}
steps.push(Step::Barrier);
steps.push(Step::Copy {
from: words.start,
to: words.start + filled,
len,
});
filled += len;
steps.push(Step::Copies(copies));
filled = to;
}
steps
}
@@ -96,8 +125,8 @@ pub(crate) unsafe fn record(
let bytes: Vec<u8> = (0..first).flat_map(|_| word.to_ne_bytes()).collect();
for step in steps(words) {
// SAFETY: the caller's contract; each update writes at most 65536 bytes of `bytes`, a
// multiple of 4 at a multiple of 4, and each copy's regions lie in `words` and do not
// overlap (the module docs).
// multiple of 4 at a multiple of 4, and each copy's regions lie in `words`, its sources'
// union apart from its destinations' (the module docs).
unsafe {
match step {
Step::Update(range) => device.cmd_update_buffer(
@@ -120,12 +149,16 @@ pub(crate) unsafe fn record(
&[],
);
}
Step::Copy { from, to, len } => {
let region = vk::BufferCopy::default()
.src_offset(from)
.dst_offset(to)
.size(len);
device.cmd_copy_buffer(command_buffer, buffer, buffer, &[region]);
Step::Copies(copies) => {
let regions: Vec<vk::BufferCopy> = (copies.iter())
.map(|copy| {
vk::BufferCopy::default()
.src_offset(copy.from)
.dst_offset(copy.to)
.size(copy.len)
})
.collect();
device.cmd_copy_buffer(command_buffer, buffer, buffer, &regions);
}
}
}