Files
metal2vulkan/crates/metal2vulkan-runtime/src/batch_order.rs
T
Anees Iqbal ca8340b4ee Group ready batches into one kernel submission on Mesa's drivers, and order a queue's batches by their ends
On ANV over i915 every kernel submission lists every VkDeviceMemory of the device, at a cost per
object; the consumer's guest drag on the Iris Xe had 302 entries per execbuffer (260 of them host
imports) at 112 to 128 execbuffers a frame. The runtime now makes fewer kernel submissions and
fewer command buffers per frame, under two new runtime policies.

What changes:
- DevicePolicy::completion, CompletionPolicy { Grouped (default), Exact }; exact_answers()
  selects Exact. Under Grouped, on a driver whose queue merges consecutive submits (Mesa's common
  queue: ANV, RADV, lavapipe, NVK and the rest), the scheduler holds ready batches of one VkQueue
  and submits up to four in one vkQueueSubmit, only the last signalling, so Mesa merges them into
  one kernel submission:
  - a batch whose only signals are events' joins, the group's end signalling each event to the
    highest index held; a batch waiting for a signal the group holds sends the group first;
  - only a group's first member may wait for anything not yet seen complete, since Mesa gathers
    every member's waits before the first member's commands; a later waiter goes alone after the
    group in the same call. Metal never holds one command buffer back for another's wait;
  - batches that cannot join (first work, binary semaphores, fences, sparse updates, wrapped
    images, storage-form switches) submit the held group first;
  - a pass that finds nothing ready keeps the group held while two or more earlier submissions of
    its queue are not seen complete, gated on the second-to-last, so commits arriving meanwhile
    join. It goes once full, when a head waits for the GPU, or when a thread waits for one of its
    command buffers;
  - a held batch that ends its command buffer turns Scheduled, takes its kernel end time and runs
    its submission handlers as it is held, so waitUntilScheduled returns at once, as Metal
    schedules a command buffer committed behind running work within 0.2 ms;
  - submission checks run ahead for up to three batches behind a line's head, so checked command
    buffers are found ready together; a failed group restores its waits, signals its last value
    and fails each member once; the group's submission allocates nothing.
  New degradation kind grouped-completion (Precision), announced once per device: a group's
  members but the last are seen complete late, by at most three later batches' GPU time plus one
  completion turn, and intermediate event values are seen late, never wrong.
- An event-signalling batch's end goes into its work's last command buffer, as every other batch's,
  unless its signal must wait for another queue's earlier signal.
- DevicePolicy::batch_order, BatchOrderPolicy { Ends (default), Exact }; exact_answers() selects
  Exact. On ANV, RADV and NVIDIA's driver, measured to run a queue's submissions one after another,
  each batch ends with a barrier to every later command of its queue, the tracker takes an earlier
  batch of that queue as ordered (no barrier per dependency), and a batch's start timestamp with
  nothing planned before it is written at the bottom of the pipe. Other drivers and Exact keep the
  barriers per dependency.
- WorkCounts: grouped_submissions, batches_grouped, group_ends_full / _wait / _apart /
  _first_work / _drained / _checks / _held, event_waits_met, batches_with_none_pending and kin by
  pending depth, groups_kept_pending and groups_sent_for_waits; frame_costs prints them, with
  kernel submissions and ioctl time where the i915 execbuffer script is loaded, and gains guest-
  shaped legs over 260 and 520 host imports.
- Docs: FAST-PATHS rows for both policies (the grouping row with the guest's frame rates), API.md
  entries, add_submission_handler's doc, the runtime README and the scheduler modules' docs.

Why: each fewer kernel submission saves the scheduler thread an ioctl of about 80 to 130 us there,
and each barrier per dependency buys nothing on drivers that run a queue's submissions in order.
The bound of four was kept: eight measured about three fewer submissions a frame for twice the
completion lateness and no frame-time change.

Verified:
- Iris Xe, guest shape over 520 host imports, Grouped against Exact: 26.2-26.7 execbuffers a frame
  against 67.0, ioctl 2.77-2.81 against 6.89-6.96 ms, scheduler 52-53 against 122-123 us per
  command buffer, through completion 5.98 against 8.70 ms a frame.
- UHD 770, the held group at a 1 ms commit pace: 25.0 kernel submissions a frame against 58.1,
  scheduler 67-82 against 113-124 us per command buffer, the same frame time and GPU idle;
  completion seen 1.82-1.86 ms after GPU end against 0.43-0.46 ms.
- In the guest on the Iris Xe, Grouped made 24.4-27.5 kernel submissions a frame against Exact's
  42.4-43.8 and saved 0.4-1.7 ms of scheduler CPU a frame; the frame rate was alike within
  boot-to-boot noise (15.7-17.3 against 16.4-17.6 fps).
- runtime_lane::grouped_submissions (held signals on one and two queues, waiters leading, groups
  held behind running work with and without submission handlers and checks, both policies, the
  validation layer silent): 19 of 19 on ANV and lavapipe; the held-handler case fails without its
  change. commit_work, gpu_times, grouped_submissions, queue_order, events and first-use lanes 77 of
  77 on the Radeon 780M, RX 7900 XTX and RTX 5080; under synchronization validation 34 of 34 on
  the UHD 770. The runtime's device tests 1066 of 1066 on ANV; the unit suites 4534 of 4534.
- clippy -D warnings (plain, m2v_gpu, m2v_cpu_icd) and rustdoc.
2026-10-19 00:16:00 +03:00

74 lines
4.2 KiB
Rust

//! How a batch of command buffers on one `VkQueue` begins after the batches before it there
//! (`DevicePolicy::batch_order`).
//!
//! Metal orders a command buffer after the earlier ones of its queue only where its work depends
//! on theirs: an independent fill committed behind a running fill of other bytes started while
//! the first ran, on an M3 Max under macOS 27.0.1 (probe `gpustart`). The exact form copies that:
//! a batch begins with a barrier for each access that depends on an earlier batch's of its
//! `VkQueue` (`tracker.rs`), its first encoder's GPU start written at the top of the pipe, and
//! ends with [`Barrier::to_host`], which orders only the host's reads and writes after it.
//!
//! Some drivers run no two kernel submissions of one queue side by side anyway. Measured with a
//! probe outside the runtime: batches A and B, each a command buffer of a start timestamp, one of
//! work and one of an end barrier and an end timestamp, signalling the queue's timeline, B's work
//! independent of A's; work of one workgroup of 64 invocations running a dependent multiply-add
//! chain of about 3 to 10 ms (so two run side by side in about one's time), or fills of other
//! buffers; with no end barrier, `to_host`'s or [`Barrier::to_all`]'s; in two `vkQueueSubmit`
//! calls or one; with and without the timestamps, the queries reset on the host or the GPU, and
//! with only the last submission signalling. On each of these, every shape ran B after A, in
//! about the sum of their times (20.0 ms for two 10 ms batches on the RTX 5080 Laptop GPU, 18.5
//! on the RX 7900 XTX, 20.0 on the UHD 770), where the same two dispatches recorded in one command
//! buffer ran side by side (10.0, 9.3 and 10.0 ms):
//! - ANV (Mesa 26.2.4) on the UHD 770 of RPL-S;
//! - RADV (Mesa 26.0.8) on the Radeon 780M and the RX 7900 XTX;
//! - NVIDIA's driver (615.71.09) on the RTX 5080 Laptop GPU.
//!
//! On those drivers ([`ends_order`]) the default form orders batches by their ends: every batch
//! ends with [`Barrier::to_all`], every write before every access of every later command of its
//! `VkQueue`, so the tracker plans no barrier after an access of an earlier batch there
//! ([`Tracker::by_ends`](crate::tracker::Tracker)), and a batch's start timestamp is written at the
//! bottom of the pipe ([`start_stage`]): once everything before it in submission order completed,
//! which is when such a driver starts it. A batch no longer needs a command buffer of barriers
//! for a dependency on the batch before it (on the UHD 770, with the consumer's frames over 520
//! host imports, 67.0 command buffers submitted a frame against 129.3 to 129.7), and runs as the
//! driver ran it before. Elsewhere, and under `BatchOrderPolicy::Exact`, the exact form.
use crate::tracker::Barrier;
use ash::vk;
use metal2vulkan::device::{BatchOrderPolicy, DevicePolicy};
use metal2vulkan::vk::DriverId;
/// Whether a device of `policy` and `driver` orders its batches by their ends (the module docs):
/// its policy asks for it and its driver is measured to run no two kernel submissions of one
/// queue side by side.
pub(crate) fn by_ends(policy: &DevicePolicy, driver: Option<DriverId>) -> bool {
policy.batch_order == BatchOrderPolicy::Ends && ends_order(driver)
}
/// Whether the driver is measured to run the kernel submissions of one queue one after another,
/// with or without a barrier between them (the module docs).
fn ends_order(driver: Option<DriverId>) -> bool {
matches!(
driver,
Some(DriverId::INTEL_OPEN_SOURCE_MESA | DriverId::MESA_RADV | DriverId::NVIDIA_PROPRIETARY)
)
}
/// The barrier that ends each batch of a device that orders its batches `by_ends` or not.
pub(crate) fn end(by_ends: bool) -> Barrier {
match by_ends {
true => Barrier::to_all(),
false => Barrier::to_host(),
}
}
/// The stage a batch's start timestamp is written at where nothing is planned before it: the
/// top of the pipe, as its work begins, or, where batches are ordered `by_ends`, the bottom,
/// once everything before it completed.
pub(crate) fn start_stage(by_ends: bool) -> vk::PipelineStageFlags {
match by_ends {
true => vk::PipelineStageFlags::BOTTOM_OF_PIPE,
false => vk::PipelineStageFlags::TOP_OF_PIPE,
}
}