Runtime: of the groups that go at once, the one committed first goes first

What: before the submit thread closes a group, each other queue's group that goes now and whose
first command buffer was committed before every one it holds goes first (WorkState::goes_before,
Fifo::first_committed), and the group right after, before anything more is staged.

Why: a command buffer that fills, waits for an event and fills again, on an idle queue, is staged
up to its wait into a group that goes only at the end of the pass, as that segment holds no
signal. A command buffer of another queue committed after it, which copies what it filled and
signals the event, went at its signal, ahead of that group, so its copy read none of the fill.
Metal submits the first at its commit and its hazard tracking orders the copy after the fill
before the wait (the conformance case event-signaller-of-a-wait, M3 Max, job m3-max-68673). Both
groups go now either way; only their order changes, and nothing waits longer.

Verified: pending.
This commit is contained in:
Anees Iqbal committed 2026-11-22 14:09:06 -08:00
1 parent d271b7523d
commit 53bf3a3f3c
2 files changed
+53 -3

No files matched your search

+6 -2
View File
@@ -1873,8 +1873,12 @@ made, with no lock, and never rewritten for layouts.
command buffers behind it, while the segments before it run, so the host can see a signal the
command buffer makes before its wait. Other queues go on. Each queue groups on its own count:
a command buffer of a queue with nothing running is submitted at once, ahead of every command
buffer committed after it on any queue, while one its queue holds, in a group or for room, may
be passed by another queue's command buffer committed after it, which only a fence or an event
buffer committed after it on any queue, its part before an event wait included (of the groups
that go at once, the one committed first goes first: on an M3 Max, a copy on another queue,
committed after a command buffer that fills, waits for the event the copy then signals, and
fills again, read the first fill and not the second, job m3-max-68673), while one its queue
holds, in a group or for room, may be passed by another queue's command buffer committed after
it, which only a fence or an event
then orders after it. Metal's hazard tracking orders such a pair against work already submitted, and no
more. Recorded on an M3 Max with macOS 27.0.1, with tracked buffers and no fence, a blit fill on
one queue and a copy of what it filled on another, committed after it, five runs each: where the
+47 -1
View File
@@ -2004,6 +2004,26 @@ impl WorkState {
fn unheld(&self) -> Option<usize> {
(self.fifos.iter()).position(|fifo| fifo.goes(self.stop))
}
/// The FIFO, other than the one at `index`, whose group goes now ([`Fifo::goes`]) and whose
/// first command buffer was committed first, before every command buffer the group at `index`
/// holds ([`Fifo::first_committed`]): it goes before that group. So a group that goes at a
/// signal, as a host wait sends it, or at the end of a pass goes no sooner than another
/// queue's group that goes now and was committed before it, the part of a command buffer
/// before its event wait included: Metal submits a command buffer committed to a queue with
/// nothing ahead of it at its commit, and its hazard tracking orders after that part a command
/// buffer of another queue, committed later, that shares a tracked resource with it (the
/// runtime README's M3 Max rows, and the conformance case event-signaller-of-a-wait). Both go
/// now either way: neither waits for anything more.
fn goes_before(&self, index: usize) -> Option<usize> {
let before = self.fifos[index].first_committed();
(0..self.fifos.len())
.filter(|&other| other != index && self.fifos[other].goes(self.stop))
.map(|other| (other, self.fifos[other].first_committed()))
.filter(|&(_, first)| first < before)
.min_by_key(|&(_, first)| first)
.map(|(other, _)| other)
}
}
impl Batch {
@@ -2044,6 +2064,17 @@ impl Fifo {
})
}
/// The place in the commit order of the first command buffer its group holds: the first that
/// finished in it, else its head, whose segments it holds; `u64::MAX` with no group.
fn first_committed(&self) -> u64 {
let Some(group) = &self.group else {
return u64::MAX;
};
let finished = (group.finished.first()).map(|(_, pending, _)| pending.seq);
let partial = self.head().filter(|_| group.partial).map(|head| head.seq);
finished.or(partial).unwrap_or(u64::MAX)
}
/// Ends `entry` with `error` (its own, where it has one): behind the group held, which it
/// joins to retire with; else once the queue's submissions in flight and the command buffers
/// it ended before complete, at once where none is. One not committed ends at its commit.
@@ -2242,9 +2273,17 @@ fn pass<'a>(core: &'a Core, mut state: MutexGuard<'a, WorkState>) -> MutexGuard<
let mut joined: Vec<Joined> = Vec::new();
let mut ended: Vec<Ended> = Vec::new();
let mut sent = false;
// The group that goes once the groups committed before it that go now have gone
// (`WorkState::goes_before`), before anything more is staged: nothing staged after it
// passes it, so no waiter goes ahead of a signal it staged.
let mut then: Option<usize> = None;
loop {
let now = times::host_time();
let index = match state.step(now, &core.counters, &mut joined, &mut ended) {
let step = match then.take() {
Some(index) => Step::Goes(index),
None => state.step(now, &core.counters, &mut joined, &mut ended),
};
let index = match step {
Step::Idle => match state.unheld() {
Some(index) => index,
None => {
@@ -2259,6 +2298,13 @@ fn pass<'a>(core: &'a Core, mut state: MutexGuard<'a, WorkState>) -> MutexGuard<
Step::Staged => continue,
Step::Goes(index) => index,
};
let index = match state.goes_before(index) {
Some(before) => {
then = Some(index);
before
}
None => index,
};
let Some((value, uploads, batch)) = state.close(index, core, &work.released) else {
continue;
};