Files
metal2vulkan/crates/metal2vulkan-runtime/src/warm/mod.rs
T
Anees Iqbal 6e88d181dd Record each pipeline state creation, and warm a device from an earlier process's records
DeviceOptions::pipeline_records (and RawDevice::pipeline_records) hands a
hook an opaque PipelineRecord once per pipeline state creation that made its
state. That covers compute, render, mesh and tile states, the
additional-binary-functions forms, and a render state's draw-time twins (the
one-sample and factor-scaling ones). A failed creation and a binary
archive's add make none. A record holds the descriptor field by field
("M2VPREC\0", RECORD_FORMAT 1, the library table by content hash, SHA-256),
read back and compared at its making. Its libraries are kept once in the
shader cache's content directory. to_bytes and from_bytes carry it between
processes.

Device::warm(records, byte_budget) stands in for Metal's own pipeline caches
across processes:
- One background thread per device (utility QoS on macOS, Urgency::Warm in
  the making budget) runs each record's creation under the default options
  and drops the state. Translations, creation records and driver
  pipeline-cache data are then in place before the program asks.
- The guest sees nothing of it: no identifier, resource ID, sampler number,
  neutral, room, stand-in copy, kept-variant traffic, live-variant sharing,
  announcement or record.
- It stops at byte_budget of added driver data.
- An unread record is counted, and the device's first is announced as the
  warning metal2vulkan-warm-record-unread; nothing fails.
- WarmStatistics and Device::warm_statistics report the pass.

Ports dev-perf 14df1fb5fe and 66fe0ada29 without per-entry stamps.
Development's build directory names the translator, and its creation records
name RUNTIME_VERSION, so no entry of another translator or runtime version
is ever found, and dev-perf's re-translate-and-compare check has nothing to
do. Adapted:
- the memo is asked but never hands a warm thread a live variant the
  program holds;
- a warm thread's waits rank Urgency::Warm, not Program (making.rs), and the
  pipewait creation bound still applies on warm threads;
- unread records are announced.
DeviceOptions' and RawDevice's Debug are hand-written. PipelineRecord is
among the consumer auto-trait checks. api_signatures re-blessed; runtime
README and HOWTO updated. RUNTIME_VERSION 1707.

Verified on the M3 Max (MoltenVK 1.4.2), on development 2e2e238346:
- 140 device tests and 500 plain tests pass.
- Five sabotages each fail their guard: an archive add making a record; a
  pass answering Warmed without creating; no unread announcement; a warm
  state taking an identifier; a warm thread taking a live variant.
- clippy plain and --cfg m2v_gpu, and rustdoc -D warnings, clean.
- Release, 5 runs of the cross-process test, three creations (render,
  compute, indirect compute):
  - after a warm pass: 3.11-3.42 ms, with 0 stages decoded, 0 translated, 0
    directory lookups and 0 driver caches loaded;
  - unwarmed over the same directory: 4.19-4.32 ms (6 lookups, 2 loads);
  - cold: 4.98-5.10 ms.
  Small shaders, and Metal's own cross-process compiler cache serves all
  three legs.

Lane commits: ws/warmrec-lane (8d106f29ab hook, 30de4fc948 warm).

One work packet, landed as 2 commits on archive/development-linear-2026-10-29: 8c709f27a7, then e2f0f2a3d9 (the re-pin).
2026-10-29 15:43:56 +02:00

81 lines
3.8 KiB
Rust

//! Pipeline records and the warm pass: what a device hands
//! [`DeviceOptions::pipeline_records`](crate::DeviceOptions::pipeline_records) at each pipeline
//! state's creation (`record.rs`, `codec.rs`), for a consumer to keep and hand a later process's
//! device ([`Device::warm`]), which makes the same translations and `VkPipeline`s before its
//! program asks for them (`pass.rs`).
pub(crate) mod codec;
mod pass;
mod record;
pub(crate) use codec::Pipeline;
/// A warm pass's progress, which the budget's tests make one of.
#[cfg(test)]
pub(crate) use pass::Pass;
/// The id name of the warning of a device's first unread record, which `tests::warm` listens for.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) use pass::UNREAD;
pub(crate) use pass::{added_pipeline_bytes, pass, translation_only, warming, within, Warmer};
pub use record::PipelineRecord;
pub(crate) use record::{RecordHook, Recording};
use crate::device::Device;
use std::sync::atomic::Ordering;
/// No Metal counterpart: a device's pipeline records and warm passes since it was made
/// ([`Device::warm_statistics`]).
///
/// The record counts of a warm pass add up, once its calls are done, to the records handed to
/// [`Device::warm`]: each was warmed, translated only, failed, unread, not warmable or not run.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct WarmStatistics {
/// Records handed to [`DeviceOptions::pipeline_records`](crate::DeviceOptions::pipeline_records):
/// one per creation that made its state.
pub records_made: u64,
/// Creations the hook was set for whose record was not made: a stage the compile layer had
/// compiled already, a value the record format cannot hold, or bytes that did not read back as
/// the creation's descriptor (`warm/codec.rs`).
pub records_unmade: u64,
/// Libraries those records name that the device wrote to its shader cache directory, each
/// once per directory (`shader_cache/libraries.rs`).
pub libraries_kept: u64,
/// Records a warm pass made the creation of: its translations and its predicted
/// `VkPipeline`s.
pub records_warmed: u64,
/// Records a warm pass made the translations of alone: a layout that bakes an immutable
/// sampler, which a warm pass makes none of.
pub records_translated: u64,
/// Records whose creation failed on the warm pass, dropped.
pub records_failed: u64,
/// Records a warm pass could not read: of another format, damaged, or naming a library the
/// shader cache directory no longer holds. The device's first is announced
/// (`metal2vulkan-warm-record-unread`); the pass goes on with the next record.
pub records_unread: u64,
/// Mesh render pipeline records, which a warm pass does not make: that creation keeps no
/// translation on disk and makes no `VkPipeline`.
pub records_not_warmable: u64,
/// Records a warm pass did not reach: its byte budget was spent, the process began exiting,
/// or no thread could be started.
pub records_not_run: u64,
/// The driver's pipeline cache data the warm passes' makings added, in bytes: what
/// [`Device::warm`]'s `byte_budget` bounds.
pub pipeline_bytes: u64,
}
impl Device {
/// No Metal counterpart: the device's pipeline records and warm passes since it was made.
pub fn warm_statistics(&self) -> WarmStatistics {
let shared = self.shared();
let read = |counter: &std::sync::atomic::AtomicU64| counter.load(Ordering::Relaxed);
let mut statistics = WarmStatistics {
records_made: read(&shared.pipeline_recording.made),
records_unmade: read(&shared.pipeline_recording.unmade),
libraries_kept: shared.shader_cache.libraries_kept(),
..WarmStatistics::default()
};
shared.warmer.read(&mut statistics);
statistics
}
}