Runtime: no work goes before an earlier sharer of another queue that Metal submitted at its commit

What: one predicate in submit.rs, WorkState::sends_past(index, pending). It answers whether
`pending`, a command buffer of the FIFO at `index`, would go before an earlier commit of another
queue that Metal had submitted at its commit (Fifo::idle_first: the first command buffer not yet
submitted of a queue committed to it idle, with a part ready) and that shares a tracked resource
with it (by the hazards' ids), or where either reaches memory outside its resources. It replaces
the host-wait-only sends_past and is asked at every place that lets work go:
- next_ready: such a head is not staged, nor a head that would join a group whose first is such
  a one (the head choice);
- has_room and wants: such a FIFO has no room and wants none, so it holds no other queue back by
  rank (the FIFO choice, and the groups that go at once, which stage only with room);
- goes and waits_for_room: no host wait sends it through (the host-wait send).
A group is asked by its first command buffer (Fifo::sends); those that joined it go with it.
wants takes the FIFO's index in place of the FIFO, for the predicate.

Why: the planner's (h3) shape, a regression of the cut. C, committed to an idle queue, is held by
block_shared behind X, a cut command buffer of another queue it shares with. While held, C wants
nothing. D, a sharer of C on a third queue committed after C and sent by no host wait, then took
the room between X's pieces and copied C's buffer before C wrote it. Metal submits C at its
commit, so its hazard tracking orders D after C.

goes_before stays separate. It orders the closes of groups that all go now within one pass and
holds nothing. sends_past decides whether work goes at all. They overlap only for sharers, and
there goes_before still orders the groups that go without a room check: at teardown (stop), and a
fence updater sent through by its waiter (updater_held).

The over-hold row: the holder is still read when the work is staged or takes the room, not at
its commit, as the host-wait rule read it. That is unchanged in kind, but it now covers every send,
not only a host wait's. Two readers of one resource share it here, as in the cut rule (no read or
write mode is kept per resource), so a read waits behind an idle queue's earlier read of the same
buffer, which Metal does not order: an over-hold, now written in the README. No more is held:
an earlier commit not submitted at its commit (its queue's work ahead of it), or one whose next
part waits for an event not yet met, holds nothing, as before.

Seen, not patched (each a shape of its own): updater_held sends a fence updater through to its
waiter with no room check, so an updater held by this rule would still go.

README: the cross-queue paragraph's host-wait sentence now states the rule for every send, the
(h3) case and the over-hold.

Verified: not run when committed; its jobs follow.
This commit is contained in:
Anees Iqbal committed 2026-11-22 15:37:40 -08:00
1 parent daaccb7bb0
commit 8828e8ad6c
2 files changed
+72 -42

No files matched your search

+11 -6
View File
@@ -1889,12 +1889,17 @@ made, with no lock, and never rewritten for layouts.
buffers. The held work goes when a completion leaves room (the completion thread wakes the
submit thread as it retires; no commit or
wait is needed), when the host is to wait for one of them (`wait_until_completed`, or
`CommandQueue::send_now`, the hint for a wait the runtime does not see), or at teardown. What a
host wait sends does not go past a command buffer of another queue committed before it, to a
queue with nothing queued or running, not yet submitted, with a part ready, that holds a buffer
or texture it holds (or where either reaches memory outside its resources): it waits for the
device's room, whose order sends that one first, as Metal submitted that one at its commit and
orders it after (job m3-max-68673). Waiting
`CommandQueue::send_now`, the hint for a wait the runtime does not see), or at teardown. No
command buffer goes past one of another queue committed before it, to a queue with nothing
queued or running, not yet submitted, with a part ready, that holds a buffer or texture it holds
(or where either reaches memory outside its resources), whether the device's room or a host
wait would send it: until that one is submitted it is not staged, and its queue neither wants
nor takes the room, whose order then sends that one first, as Metal submitted that one at its
commit and orders it after (job m3-max-68673). So where that one waits behind a cut command
buffer it shares with (below), a later sharer of a third queue waits behind both; without the
rule it went between the cut one's pieces and read the buffer before that one wrote it (on
lavapipe, job m3-max-68970). A group is asked by its first command buffer, and those that joined
it go with it. Two readers of one resource share it here too: an over-hold. Waiting
for a place in the queue sends nothing. A segment that signals an event ends its group, which goes
at once as it is staged, and so does a presenting command buffer's last (see "Presenting"); so a
command buffer that signals an event, in any segment, is staged only once the device has room
+61 -36
View File
@@ -1650,6 +1650,17 @@ impl WorkState {
if fifo.blocked && !waits {
continue;
}
// A head that would go before an earlier commit of another queue Metal submitted, or
// join a group that would, is not staged until that one is submitted
// ([`sends_past`](Self::sends_past)).
let past = |pending: &Pending| self.sends_past(index, pending);
let past = (fifo.group.is_some() && fifo.sends().is_some_and(past))
|| (fifo.entries.front()).is_some_and(|head| {
head.error.is_none() && head.pending.as_ref().is_some_and(past)
});
if past && !waits {
continue;
}
if let (false, Some(at)) = (waits, fifo.entries.front().and_then(|head| head.held)) {
held = held.filter(|&(first, _)| first < at).or(Some((at, index)));
continue;
@@ -1729,7 +1740,8 @@ impl WorkState {
true => segment.work.run,
false => run.unwrap_or(u64::MAX),
};
let through = fifo.send_through >= head.place && !cut && !self.sends_past(index);
let through =
fifo.send_through >= head.place && !cut && !self.sends_past(index, pending);
signals && !(self.stop || through || self.has_room(index, run))
}
}
@@ -1739,14 +1751,17 @@ impl WorkState {
/// ([`room`]), no FIFO ranked before it ([`rank`](Self::rank)) wanting room that fits
/// ([`wants`](Self::wants)): so the queue that waited longest goes first, and a command
/// buffer passed by another queue's goes before that queue's next. A queue the
/// shared-resource rule holds wants nothing, so it holds no other queue back.
/// shared-resource rule holds wants nothing, so it holds no other queue back, and one whose
/// next submission would go before an earlier commit of another queue Metal submitted has no
/// room ([`sends_past`](Self::sends_past)).
fn has_room(&self, index: usize, run: u64) -> bool {
let fifo = &self.fifos[index];
let me = Self::rank(fifo, self.wants(fifo).map_or(u64::MAX, |(_, seq)| seq));
room(&self.in_flight, run)
let me = Self::rank(fifo, self.wants(index).map_or(u64::MAX, |(_, seq)| seq));
!(fifo.sends()).is_some_and(|first| self.sends_past(index, first))
&& room(&self.in_flight, run)
&& !(self.fifos.iter().enumerate()).any(|(other, fifo)| {
other != index
&& (self.wants(fifo)).is_some_and(|(run, seq)| {
&& (self.wants(other)).is_some_and(|(run, seq)| {
Self::rank(fifo, seq) < me && room(&self.in_flight, run)
})
})
@@ -1762,11 +1777,17 @@ impl WorkState {
(fifo.turn, seq)
}
/// The expected run of what `fifo` would submit with room, and the commit number of its first
/// command buffer: its group held, else its head's next piece or the command buffer, where it
/// is committed, with something for the GPU, its next segment's waits met, and not held behind
/// a cut command buffer it shares with.
fn wants(&self, fifo: &Fifo) -> Option<(u64, u64)> {
/// The expected run of what the FIFO at `index` would submit with room, and the commit number
/// of its first command buffer: its group held, else its head's next piece or the command
/// buffer, where it is committed, with something for the GPU, its next segment's waits met,
/// and not held behind a cut command buffer it shares with. Nothing where that would go before
/// an earlier commit of another queue Metal submitted ([`sends_past`](Self::sends_past)), so
/// it holds no queue back.
fn wants(&self, index: usize) -> Option<(u64, u64)> {
let fifo = &self.fifos[index];
if (fifo.sends()).is_some_and(|first| self.sends_past(index, first)) {
return None;
}
let head = fifo.head();
if let Some(group) = &fifo.group {
let first = (group.leads.first().copied()).or(head.map(|head| head.seq));
@@ -1792,9 +1813,10 @@ impl WorkState {
/// it saw complete when it began, so held work waits for the round to end.
fn goes(&self, index: usize) -> bool {
let fifo = &self.fifos[index];
let past = || (fifo.sends()).is_some_and(|first| self.sends_past(index, first));
fifo.group.as_ref().is_some_and(|group| {
self.stop
|| (fifo.send_through >= group.first && !self.sends_past(index))
|| (fifo.send_through >= group.first && !past())
|| self.has_room(index, group.run)
})
}
@@ -2226,40 +2248,34 @@ impl WorkState {
fn unheld(&self) -> Option<usize> {
let rank = |index: usize| {
let fifo = &self.fifos[index];
Self::rank(fifo, self.wants(fifo).map_or(u64::MAX, |(_, seq)| seq))
Self::rank(fifo, self.wants(index).map_or(u64::MAX, |(_, seq)| seq))
};
(0..self.fifos.len())
.filter(|&index| self.goes(index))
.min_by_key(|&index| rank(index))
}
/// Whether the FIFO at `index`, sent through by a host wait ([`Work::send`]), sends a command
/// buffer, in its group held or as its head, committed after the first command buffer not yet
/// submitted of another queue committed to it idle ([`Fifo::idle_first`]), that holds a buffer
/// or texture that one holds, by the ids the hazards track them by, or where either reaches
/// memory outside its resources. Metal submits such a one at its commit and its hazard
/// tracking orders the later after it, so the FIFO then goes through only once that one is
/// submitted: until then it waits for the device's room, whose order ([`rank`](Self::rank))
/// sends that one first. Nothing else the host wait sends waits for it.
fn sends_past(&self, index: usize) -> bool {
let fifo = &self.fifos[index];
let head = (fifo.entries.front())
.filter(|entry| entry.place <= fifo.send_through)
.and_then(|entry| entry.pending.as_ref());
let sent = || {
let grouped = (fifo.group.iter())
.flat_map(|group| group.finished.iter().map(|(_, pending, _)| pending));
grouped.chain(head)
};
/// Whether `pending`, a command buffer of the FIFO at `index`, would go before an earlier
/// commit of another queue that Metal had submitted at its commit, the first command buffer
/// not yet submitted of a queue committed to it idle ([`Fifo::idle_first`]), that holds a
/// buffer or texture `pending` holds, by the ids the hazards track them by, or where either
/// reaches memory outside its resources. Metal's hazard tracking orders `pending` after that
/// one, so `pending` waits for its submission: it is not staged
/// ([`next_ready`](Self::next_ready)), its queue wants no room and has none
/// ([`wants`](Self::wants), [`has_room`](Self::has_room)), and no host wait sends it through
/// ([`goes`](Self::goes), [`waits_for_room`](Self::waits_for_room)). The room's order
/// ([`rank`](Self::rank)) then sends that one first. A group is asked by its first command
/// buffer ([`Fifo::sends`]): what joined it after that goes with it.
fn sends_past(&self, index: usize, pending: &Pending) -> bool {
(self.fifos.iter().enumerate()).any(|(other, holder)| {
let Some(first) = holder.idle_first().filter(|_| other != index) else {
let Some(first) =
(holder.idle_first()).filter(|first| other != index && first.seq < pending.seq)
else {
return false;
};
let ids = first.gpu.kept.tracked();
sent().any(|pending| {
pending.seq > first.seq
&& (first.gpu.unbound || pending.gpu.unbound || pending.gpu.kept.meets(&ids))
})
first.gpu.unbound
|| pending.gpu.unbound
|| pending.gpu.kept.meets(&first.gpu.kept.tracked())
})
}
@@ -2312,6 +2328,15 @@ impl Fifo {
(self.entries.front()).and_then(|entry| entry.pending.as_ref())
}
/// The first command buffer of its next submission: its group's first, else its head.
fn sends(&self) -> Option<&Pending> {
match &self.group {
Some(group) => (group.finished.first().map(|(_, pending, _)| pending))
.or_else(|| self.head().filter(|_| group.partial)),
None => self.head(),
}
}
/// Its first command buffer not yet submitted with a part ready for the device, where the
/// queue has submitted nothing since a commit found it idle (its turn 0): staged in its group,
/// or its next segment's waits met. Metal submits a command buffer committed to a queue with