From 2bcac43a5343d59573a87273d78ca3f324f4d232 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 14:47:02 -0800 Subject: [PATCH] Runtime: what a host wait sends waits for a sharer of another queue that Metal had submitted What: a queue sent through by a host wait (Work::send, send_now) goes through only once no command buffer of another queue, committed before one it sends, the first not yet submitted of a queue that has submitted nothing since a commit found it idle (Fifo::idle_first), with a part ready, and holding a buffer or texture it holds by the ids the hazards track them by (or where either reaches memory outside its resources), is left unsubmitted (WorkState::sends_past). Until then it waits for the device's room, whose order sends that one first. The runtime README says so. Why: on the cut the device's room is shared. With busy work of a third queue filling it, a command buffer that fills, waits for an event and fills again on an idle queue is held up to its wait in a group for room, and a later command buffer of another queue that copies what it filled and signals the event, sent by a host wait, went past the room and ahead of it: its copy read none of the fill. Metal submits the first at its commit and orders the copy after its fill before the wait (the conformance case event-signaller-of-a-wait, M3 Max, job m3-max-68673). Only such a pair is held, and only until the first's ready part is submitted; a command buffer committed behind its own queue's work holds nothing, as on Metal (the README's M3 Max rows). Verified: pending. --- crates/metal2vulkan-runtime/README.md | 7 ++- crates/metal2vulkan-runtime/src/submit.rs | 58 ++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 9281788d8..f9f3ca320 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1889,7 +1889,12 @@ 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. Waiting + `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 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 diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 10996766f..e59c7b3c3 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -1729,7 +1729,7 @@ impl WorkState { true => segment.work.run, false => run.unwrap_or(u64::MAX), }; - let through = fifo.send_through >= head.place && !cut; + let through = fifo.send_through >= head.place && !cut && !self.sends_past(index); signals && !(self.stop || through || self.has_room(index, run)) } } @@ -1793,7 +1793,9 @@ impl WorkState { fn goes(&self, index: usize) -> bool { let fifo = &self.fifos[index]; fifo.group.as_ref().is_some_and(|group| { - self.stop || fifo.send_through >= group.first || self.has_room(index, group.run) + self.stop + || (fifo.send_through >= group.first && !self.sends_past(index)) + || self.has_room(index, group.run) }) } @@ -2231,6 +2233,36 @@ impl WorkState { .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) + }; + (self.fifos.iter().enumerate()).any(|(other, holder)| { + let Some(first) = holder.idle_first().filter(|_| other != index) 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)) + }) + }) + } + /// The FIFO, other than the one at `index`, whose group goes now ([`goes`](Self::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 @@ -2280,6 +2312,28 @@ impl Fifo { (self.entries.front()).and_then(|entry| entry.pending.as_ref()) } + /// 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 + /// nothing ahead of it at its commit (the runtime README's M3 Max rows). One that has ended, + /// holds nothing for the GPU, or waits for an event not yet met has no part ready: Metal's wait + /// stops only its own queue. + fn idle_first(&self) -> Option<&Pending> { + if self.turn != 0 { + return None; + } + match &self.group { + Some(group) => (group.finished.first().map(|(_, pending, _)| pending)) + .or_else(|| self.head().filter(|_| group.partial)), + None => { + let entry = self.entries.front().filter(|entry| entry.error.is_none())?; + let head = entry.pending.as_ref()?; + let next = head.gpu.segments.get(head.next); + (!head.gpu.is_empty() && next.is_some_and(ready)).then_some(head) + } + } + } + /// 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 {