diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index f977ffd3b..22780b2c9 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -349,7 +349,11 @@ Calls Metal does not have, for a consumer that emulates a Metal device: encoders Metal would still run and keeping its signals, as a consumer measured on an M3 Max. - `CommandBuffer::set_submission_check` runs a check as the command buffer is submitted, and fails it as Metal fails a submission. It runs on the thread that submits it: the scheduler's, or one - in `commit_and_submit`, so it must take no lock that thread holds and wait for no GPU work. + in `commit_and_submit`, so it must take no lock that thread holds and wait for no GPU work. It + runs once the command buffer's event waits are met as Metal submits it: an `Event` wait for a + signal already submitted is met at once and waits on the `VkQueue`; a `SharedEvent` wait that + only a GPU signal still running reaches is held until the signal lands (measured on an M3 Max, + the conformance case `event-wait-behind-a-running-signal`). - `CommandBuffer::add_submission_handler` is called when the runtime takes the command buffer's last batch for its queue (submits it, or holds it for one kernel submission with later ones); `add_executed_commands_handler` each time an execution's indirect records are read. @@ -367,7 +371,8 @@ Calls Metal does not have, for a consumer that emulates a Metal device: where no other submission pass is under way: the ready command buffers committed first, this one among them, while each needs no host step and no switch of a storage form, up to the pass's bound, their submission checks run inside the call just before each submission (unless one - waits for a GPU signal to land, which the scheduler's thread waits for) and their submission + waits for a shared event's GPU signal to land, which the scheduler's thread waits for; a plain + event's wait goes to the `VkQueue`, as Metal submits an `MTLEvent` wait at once) and their submission handlers after it, in submission order with every other command buffer's. What it leaves goes to the scheduler's thread. One wake and one thread hop fewer per command buffer than `commit`, whose submission is the scheduler thread's, as Metal's is its own thread's diff --git a/crates/metal2vulkan-runtime/src/command_buffer/checks.rs b/crates/metal2vulkan-runtime/src/command_buffer/checks.rs index 0d3882472..9e21058a8 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/checks.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/checks.rs @@ -12,18 +12,27 @@ //! - **When.** The scheduler holds a batch of a command buffer with a check, as it holds one for //! its host step, once it is otherwise ready: its binary semaphores' signals submitted, every //! event value it waits for reached by the host or by a submitted signal, and its host step -//! recorded. It holds it further while a value it waits for is reached only by a GPU signal -//! submitted but not yet landed (an `Event`'s, or a `SharedEvent`'s the GPU signals): Metal -//! submits the submission, and checks it, once the signal happened, so a resource detached -//! meanwhile fails it. A value the host set (or the device reached) needs no wait. On its +//! recorded. Then the event's kind decides, as Metal's submission does (an M3 Max under macOS +//! 27.0.1, the conformance case `event-wait-behind-a-running-signal`: a command buffer waiting +//! for a value only a signal still running on the GPU reaches, its status and scheduled handler +//! read while that signal ran; its `Scheduled` is the proxy for "submitted, and so checked"). +//! An `Event`'s value no host call sets, and Metal submits a command buffer whose `MTLEvent` +//! wait such a signal meets at once: so a wait the batch hands to its `VkQueue` (a signal +//! submitted before it, on its `VkQueue` or one the GPU wait reaches, or reserved by the +//! caller) holds no check, which runs at once and the batch goes with the wait on the `VkQueue`, +//! as a batch without a check does. Metal holds one whose `MTLSharedEvent` wait such a signal +//! meets until the signal lands, as the runtime holds every wait for a value the host may set +//! (`event_timeline.rs`): so a `SharedEvent`'s value reached only by a GPU signal submitted but +//! not yet landed holds the check until it lands, and a resource detached meanwhile fails the +//! submission. A value the host set (or the device reached) needs no wait. On its //! thread, without its own lock or the command buffer's, it then runs the check of each //! submission that begins in the batch, and again of each that a host step of the batch //! recorded commands for (what they reach is known only then), then submits the batch in its //! order. A thread in `CommandBuffer::commit_and_submit` that makes the submission pass itself //! runs those checks instead, the same way, just before it submits the batch, where the batch //! is the one committed first of all, ready but for them, with no host step and no group, and -//! every value it waits for reached on the device or by the host (`scheduler::claim_check`); -//! the rest are the scheduler thread's. Where the scheduler groups batches (`CompletionPolicy::Grouped` on a driver that +//! every value it waits for reached on the device or by the host or one its `VkQueue` waits for +//! (`scheduler::claim_check`); the rest are the scheduler thread's. Where the scheduler groups batches (`CompletionPolicy::Grouped` on a driver that //! merges submissions, `group.rs`), it runs the checks of up to three batches right behind a //! head that goes with the head's, in commit order, each once its batch is otherwise ready and //! its events' values reached on the device or by the host, none committed after another diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 32378bd93..44a078b22 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -135,9 +135,9 @@ //! completes meanwhile. Each waking is counted by what woke the thread, and each sleep //! (`WorkCounts::scheduler_sleeps` and kin). //! - **Waits for a signal to land itself** ([`Nudge`]). Where the heads wait only for event -//! values that submitted GPU signals reach (no gate, none reached yet; a shared event's too, -//! whose wait is held on the host rather than handed to a `VkQueue`, `event_timeline.rs`: -//! [`landing_waits`]), the thread sleeps in one +//! values that submitted GPU signals reach (no gate, none reached yet; a shared event's, whose +//! wait is held on the host rather than handed to a `VkQueue`, `event_timeline.rs`, and a +//! checked head's for a shared event's value: [`landing_waits`]), the thread sleeps in one //! `vkWaitSemaphores` for any of those values, or for the device's nudge semaphore to reach its //! next value, instead of on its condition variable: the signal wakes it from the driver, with //! no completion turn and no waker between, and it observes the values itself @@ -1963,9 +1963,10 @@ fn plain( /// without the lock and right before it submits that head ([`Scheduler::submit_at_commit`]), as /// Metal checks a submission as it submits it: where they are still to run and no other thread /// took them, nothing else holds the head back from that thread ([`plain`]), and every event value -/// it waits for was reached on the device or by the host. A head whose checks wait for a submitted -/// GPU signal to land is left to the scheduler's thread, which waits for it ([`check_heads`]). -/// Marked taken, so that thread leaves them. +/// it waits for was reached on the device or by the host, or is one its `VkQueue` waits for +/// ([`landing_waits`]). A head whose checks wait for a submitted GPU signal to land (a shared +/// event's) is left to the scheduler's thread, which waits for it ([`check_heads`]). Marked +/// taken, so that thread leaves them. fn claim_check(state: &mut State) -> Option { let State { order, @@ -2126,11 +2127,12 @@ type Work = (Hold, usize); /// Look at each line's head of a command buffer with a submission check that is otherwise /// ready (its binary semaphores' signals submitted, its events' values reached, its host step /// recorded, not `behind` an earlier commit) and waits for its checks: once every value it waits -/// for was reached on the device or by the host, not only by a submitted GPU signal (or the device -/// is lost), it goes to `left` as a check, unless its checks were taken already (to `left`, or by -/// a committing thread, [`claim_check`]). What it waits for goes to `awaits` before the events are -/// read again, as [`Scheduler::completions`] says. Whether a head still waits for a GPU signal to -/// land. +/// for was reached on the device or by the host, or is one its `VkQueue` waits for (a plain +/// event's for a submitted or reserved signal, [`landing_waits`]), not one a shared event's +/// submitted GPU signal alone reaches (or the device is lost), it goes to `left` as a check, +/// unless its checks were taken already (to `left`, or by a committing thread, [`claim_check`]). +/// What it waits for goes to `awaits` before the events are read again, as +/// [`Scheduler::completions`] says. Whether a head still waits for a GPU signal to land. /// /// Where the line's batches may go in one kernel submission (`command_buffer/group.rs`), the /// batches right behind a head that goes (its checks in `left` or none due, otherwise ready, its @@ -2321,14 +2323,26 @@ fn events_reachable(held: &Held) -> bool { .all(|(event, value)| event.wait(*value).is_some()) } -/// The waits of `held`, a line's head whose checks wait for its events' values to land, for the -/// signal indices of the submitted GPU signals that reach the values not yet reached, a shared -/// event's whose wait is held on the host included (`EventShared::landing`): what the scheduler's -/// thread may wait for on the GPU itself (the module docs). `None` while a value is reached by -/// neither a submitted signal nor the host. +/// The waits of `held`, a line's head whose checks are due, that its checks wait for to land: +/// the signal indices of the submitted GPU signals that reach the values not yet reached of the +/// waits the batch cannot hand to its `VkQueue` (`EventShared::wait` answers none: a shared +/// event's, whose wait is held on the host, and one a signal behind a merge reaches), from +/// `EventShared::landing`. What the scheduler's thread may wait for on the GPU itself (the module +/// docs). `None` while such a value is reached by neither a submitted signal nor the host. +/// +/// A wait the batch hands to its `VkQueue` (a plain event's, `MTLEvent`, for a signal submitted or +/// reserved before it) holds no check: Metal submits a command buffer whose `MTLEvent` wait only +/// a signal still running on the GPU meets, and so checks it, at once, where it holds one whose +/// `MTLSharedEvent` wait such a signal meets until the signal lands (an M3 Max under macOS 27.0.1: +/// the conformance case `event-wait-behind-a-running-signal`, the waiter's status and scheduled +/// handler read while the signaller still ran). Such a head is checked once otherwise ready, and +/// its waits chain on the `VkQueue` as an unchecked head's do ([`event_waits`]). fn landing_waits(held: &Held) -> Option> { let mut waits = Vec::new(); for (event, value) in &held.waits { + if event.wait(*value).is_some() { + continue; + } match event.landing(*value)? { WaitFor::Reached => {} WaitFor::Index(index) => waits.push((event.semaphore(), index)), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/submission_checks.rs b/crates/metal2vulkan-validation/src/runtime_lane/submission_checks.rs index 2b4d61e80..99e49c5c4 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/submission_checks.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/submission_checks.rs @@ -15,12 +15,16 @@ //! - **Reached through an indirect command alone.** A buffer an indirect command binds, which //! no encoder makes resident, is the submission's from the commit on; detached behind a wait, //! that submission fails. -//! - **A GPU signal in flight**. A submission whose `Event` or `SharedEvent` wait another command -//! buffer's GPU signal meets is checked only once that signal landed (the check reads a companion +//! - **A GPU signal in flight**. A submission whose `SharedEvent` wait another command buffer's +//! GPU signal meets is checked only once that signal landed (the check reads a companion //! `SharedEvent` the signaller signals first); detached while the signaller runs, it fails, //! whether committed before the signaller or with `commit_and_submit` while its signal is in -//! flight (the committing thread leaves that check to the scheduler's). A `SharedEvent` the -//! host signals while a GPU signal of it is in flight is checked without waiting for the GPU. +//! flight (the committing thread leaves that check to the scheduler's). One whose `Event` wait +//! such a signal meets is checked at once and submitted with its wait on the `VkQueue`, while the +//! signal still runs (the companion reads 0 in its check), as Metal submits it +//! (`event-wait-behind-a-running-signal`), no head counted as awaiting its checks' signals. A +//! `SharedEvent` the host signals while a GPU signal of it is in flight is checked without +//! waiting for the GPU. use super::degrade_notices::on_device; use super::failed_commits::{Shape, HELD, KERNEL, SHAPES}; @@ -493,28 +497,32 @@ impl InFlight { } } -/// A submission whose `Event` (then `SharedEvent`) wait another command buffer's GPU signal -/// meets is checked only once that signal landed: the companion event the signaller signals -/// first reads 1 in the check, every try. Its buffer detached once the signaller is scheduled, -/// while its signal is in flight (the companion still reads 0 after the detach), the submission -/// fails with code 8 and its fill does not run. A try whose signaller finished before the -/// detach proves nothing of the detach, and the next doubles the signaller's work; one try at -/// least must catch it in flight. So too for a waiter committed with `commit_and_submit` while -/// the signal is in flight: its check is left to the scheduler's thread, never run on the -/// committing thread before the signal landed. +/// A submission whose `SharedEvent` wait another command buffer's GPU signal meets is checked +/// only once that signal landed, as Metal holds such a command buffer until then +/// (`event-wait-behind-a-running-signal`): the companion event the signaller signals first reads 1 +/// in the check, every try, and a head is counted as awaiting its checks' signals. Its buffer +/// detached once the signaller is scheduled, while its signal is in flight (the companion still +/// reads 0 after the detach), the submission fails with code 8 and its fill does not run. A try +/// whose signaller finished before the detach proves nothing of the detach, and the next doubles +/// the signaller's work; one try at least must catch it in flight. So too for a waiter committed +/// with `commit_and_submit` while the signal is in flight: its check is left to the scheduler's +/// thread, never run on the committing thread before the signal landed. #[test] fn a_submission_detached_while_a_gpu_signal_is_in_flight_fails() { on_device(|device, _| { let caller = std::thread::current().id(); - for (shared, at_commit) in [(false, false), (true, false), (false, true), (true, true)] { + let shared = true; + for at_commit in [false, true] { let mut caught = None; for attempt in 0..ATTEMPTS { let fills = 4u64 << attempt; let waited = Waited::new(device, shared); + let awaiting = device.work_counts().heads_awaiting_check_signals; let run = InFlight::start(device, &waited, fills, at_commit); run.detach.detach(&run.target); let in_flight = run.landed.signaled_value() == 0; let at_check = run.finish(); + let awaited = device.work_counts().heads_awaiting_check_signals - awaiting; assert_eq!( at_check, 1, "shared {shared}, at commit {at_commit}: checked before the GPU signal landed" @@ -526,6 +534,10 @@ fn a_submission_detached_while_a_gpu_signal_is_in_flight_fails() { !run.checked_on.lock().unwrap().contains(&caller), "shared {shared}, at commit {at_commit}: checked on the committing thread" ); + assert!( + awaited > 0, + "at commit {at_commit}: no head counted as awaiting its checks' signals" + ); assert_eq!( run.waiter.status(), CommandBufferStatus::Error, @@ -558,6 +570,68 @@ fn a_submission_detached_while_a_gpu_signal_is_in_flight_fails() { }); } +/// A submission whose `Event` wait another command buffer's GPU signal meets, still running, is +/// checked at once and submitted with its wait on the `VkQueue`, as Metal submits (and so checks) +/// such a command buffer at once (an M3 Max under macOS 27.0.1: the conformance case +/// `event-wait-behind-a-running-signal`, the waiter `Scheduled` and its scheduled handler run while +/// the signaller still ran). In one try at least the waiter reads `Scheduled` while the companion +/// event the signaller signals first still reads 0, and the companion read 0 in its check; in +/// every try no head is counted as awaiting its checks' signals +/// (`WorkCounts::heads_awaiting_check_signals`), its check runs once, and its fill runs, after +/// the signal. So too for a waiter committed with `commit_and_submit` while the signal is in +/// flight. A try whose signaller finished first proves nothing of the order, and the next doubles +/// its work. +#[test] +fn an_event_wait_a_gpu_signal_in_flight_meets_is_checked_at_once() { + on_device(|device, _| { + for at_commit in [false, true] { + let mut caught = None; + for attempt in 0..ATTEMPTS { + let fills = 4u64 << attempt; + let waited = Waited::new(device, false); + let awaiting = device.work_counts().heads_awaiting_check_signals; + let run = InFlight::start(device, &waited, fills, at_commit); + let deadline = std::time::Instant::now() + WAIT; + while run.waiter.status() == CommandBufferStatus::Committed { + assert!( + std::time::Instant::now() < deadline, + "at commit {at_commit}: the waiter was never scheduled" + ); + std::thread::sleep(std::time::Duration::from_micros(200)); + } + // Read after the waiter's status: 0 says the signal had not landed when it was. + let in_flight = run.landed.signaled_value() == 0; + let at_check = run.finish(); + let awaited = device.work_counts().heads_awaiting_check_signals - awaiting; + assert_eq!( + awaited, 0, + "at commit {at_commit}: a head awaited its checks' signals" + ); + assert_eq!(run.waiter.status(), CommandBufferStatus::Completed); + assert_eq!(read_buffer(&run.target, 0, 4).unwrap(), [0x5a; 4]); + if !in_flight { + continue; + } + assert_eq!( + at_check, 0, + "at commit {at_commit}: checked only once the GPU signal landed" + ); + caught = Some(fills); + break; + } + let fills = caught.unwrap_or_else(|| { + panic!( + "at commit {at_commit}: the signaller never ran at the waiter's scheduling \ + in {ATTEMPTS} tries" + ) + }); + println!( + "EVENT-IN-FLIGHT at_commit={at_commit} caught with {fills} fills of {FILL} bytes" + ); + } + }); +} + /// A `SharedEvent` wait that a GPU signal in flight would meet, met by the host first: the check /// runs at the host's signal, without waiting for the GPU (the companion event reads 0 in it, in /// one try at least), and the submission runs. A try whose signaller finished first proves diff --git a/docs/RUNTIME-PORTING.md b/docs/RUNTIME-PORTING.md index 0a12f6636..1b1ad14c4 100644 --- a/docs/RUNTIME-PORTING.md +++ b/docs/RUNTIME-PORTING.md @@ -150,9 +150,9 @@ narrow refusal that the call's rustdoc describes. | `used_resources` | (none: a runtime extension) | `CommandBuffer::used_resources`: the buffers, textures and heaps its ended encoders' commands use, each as what owns its memory, until it completes; what `detach_backing` waits on | | `ended_encoders` | (none: a runtime extension) | `CommandBuffer::ended_encoders`: each ended encoder's kind, the event waits and signals before it, and what its own commands use; with `commit_failed`'s `FailedEncoders::Set`, what fails the one submission that uses a detached resource | | `add_submission_handler` | (none: a runtime extension) | `CommandBuffer::add_submission_handler`: a handler run once the command buffer's last batch reached its `VkQueue`, on the thread that submitted it (the device's scheduler thread, or one in `commit_and_submit`), in submission order, before it is `Scheduled`; never for one that fails before | -| `commit_and_submit` | (none: a runtime extension) | `CommandBuffer::commit_and_submit`: `commit`, then the calling thread submits the ready command buffers committed first, this one among them, where no other pass is under way and each needs no host step or storage-form switch, running their submission checks just before each submission (one that waits for a GPU signal to land is left to the scheduler's thread) and their submission handlers inside the call; the rest goes to the scheduler's thread | +| `commit_and_submit` | (none: a runtime extension) | `CommandBuffer::commit_and_submit`: `commit`, then the calling thread submits the ready command buffers committed first, this one among them, where no other pass is under way and each needs no host step or storage-form switch, running their submission checks just before each submission (one that waits for a shared event's GPU signal to land is left to the scheduler's thread; a plain event's wait goes to the `VkQueue`) and their submission handlers inside the call; the rest goes to the scheduler's thread | | `add_completion_seen_handler` | (none: a runtime extension) | `CommandBuffer::add_completion_seen_handler`: a handler run once the command buffer completed, its status and GPU times final, on the runtime's thread that saw it complete where its transition to `Completed` runs there (else on its queue's handler thread), before its completed handlers; short, and never blocking | -| `set_submission_check` | (none: a runtime extension) | `CommandBuffer::set_submission_check`: a check of each of Metal's submissions of the command buffer, run once its event waits are met, just before it is submitted, on the thread that submits it (the device's scheduler thread, or one in `commit_and_submit`); a code it answers fails that submission alone, as Metal fails one that uses a resource detached by then | +| `set_submission_check` | (none: a runtime extension) | `CommandBuffer::set_submission_check`: a check of each of Metal's submissions of the command buffer, run once its event waits are met (an `Event` wait for a submitted signal at once, its wait on the `VkQueue`; a `SharedEvent` wait only a GPU signal still running reaches once that signal lands, as Metal schedules each), just before it is submitted, on the thread that submits it (the device's scheduler thread, or one in `commit_and_submit`); a code it answers fails that submission alone, as Metal fails one that uses a resource detached by then | | `object_id` | (none: Metal names an object by pointer) | `Buffer::object_id`, `Texture::object_id`, `Heap::object_id`: an `ObjectId` unique for the object's life, never reused (a sparse heap's `gpu_address` is 0) | ### `ComputeCmd`: `MTLComputeCommandEncoder` commands