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.
This commit is contained in:
Anees Iqbal committed 2026-10-19 00:16:00 +03:00
1 parent ed3ca3aa1f
commit ca8340b4ee
41 files changed
+3614 -346

No files matched your search

+30 -1
View File
@@ -103,6 +103,18 @@ compile does. `DevicePolicy::default()` is `DevicePolicy::UNTRUSTED`:
`tile_store = TileStorePolicy::Exact`, which `exact_answers()` selects, rewrites them before
the pass as Metal's store leaves them, through the extended-range attachments' load pass. On
MoltenVK the pass is a Metal pass and stores them as Metal's under either value.
- **Batches of a queue ordered by their ends** (`BatchOrderPolicy::Ends`, the runtime's choice; no
pipeline differs). On ANV, RADV and NVIDIA's driver, each measured to start no kernel submission
of a `VkQueue` before the one before it completed, each batch of command buffers ends with a
barrier ordering its work before every later command of its `VkQueue`: a batch that depends on
an earlier one there begins with no barrier of its own, and a command buffer's GPU start is
taken once the work before it completed, which is when such a driver starts it. Nothing is
announced: on those drivers the work's order, its results and its GPU times are the same under
either value. `batch_order = BatchOrderPolicy::Exact`, which `exact_answers()` selects, orders
each batch after the earlier ones of its queue only by its work's dependencies on theirs, as
Metal does (an M3 Max starts an independent command buffer committed behind running work while
that work runs), and takes a command buffer's GPU start as its work begins. Every other driver
takes that form under either value.
Loops run as written: no execution bound is translated in.
@@ -1326,7 +1338,9 @@ fast forms record, and each `PrecisionNote`'s rustdoc states its bound.
the texture's own image before its store action was known, the pass executes render commands,
or the device copies no image of the format) does the pass render in place, announced so. Cost:
the attachment's texels after the pass are each its earlier texel or the one the pass rendered,
both Metal's answers, but not always the one Metal's tiler keeps.
both Metal's answers, but not always the one Metal's tiler keeps. Within Metal's contract:
`MTLStoreAction.dontCare` lets the GPU leave arbitrary data ("You need to assume that GPU
discarded the texture's contents").
- `metal2vulkan-degrade-dont-care-load-tiles` (precision). `DontCareLoadTiles`: An attachment loaded
with `DontCare` and stored, in a pass that draws, is cleared before the draws to zero (a color) or
its clear value (depth, stencil) over the render area and the tiles past it a clear reaches, where
@@ -1966,6 +1980,21 @@ fast forms record, and each `PrecisionNote`'s rustdoc states its bound.
it is freed: not announced.
Cost: the deallocator runs once every such command buffer lets go of the pages, or those batches
complete, where Metal's runs at the release; the pages stay mapped until then.
- `metal2vulkan-degrade-grouped-completion` (precision). `GroupedCompletion`: Batches of command
buffers committed on one queue that the scheduler found ready together, or held while two or more
earlier submissions of their queue ran, went to the driver in one `vkQueueSubmit`, only the last
signalling its queue's timeline and their events (`CompletionPolicy::Grouped`, the default, on a
driver whose queue merges a submission that signals nothing into the next one: Mesa's drivers):
one kernel submission for up to four batches, where each kernel submission costs CPU time for
every `VkDeviceMemory` of the device. Announced once per device, on the scheduler's thread. Cost:
each command buffer of the submission but the last is seen complete (its status, completed
handlers and waits), and the event values it signals are reached, once the last completed, late by
the GPU time of the batches after it, at most three, never early and in commit order, and a
submission held while earlier ones of its queue ran late besides by any gap between the last of
those and its own start on the GPU, at most a completion turn's reaction and its vkQueueSubmit;
and the submission checks of the batches behind a queue's next one run with its own, before the
batch before them is submitted; their commands, barriers and GPU times are unchanged;
`CompletionPolicy::Exact` submits and checks each batch alone.
- `metal2vulkan-degrade-misplaced-notice` (performance). `MisplacedNotice`: In a build without debug
assertions, a notice announced where the runtime's rule announces none (`DegradeNotice::misplaced`
names its kind and `NoticePlace`): inside a pipeline pool's piece, or, for a kind announced at