From 273ea7efe4433170807cf3cf8597f5dcf09d1111 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 13 Nov 2026 03:10:17 -0800 Subject: [PATCH] A device keeps its compiled pipelines' stage digests past their variants What: a variant whose objects are made new, with no kept instance to give them back, names its modules by the SHA-256 of each stage's words (words_digest). That's one pass over every word. The device now keeps the digests a variant took in WordsDigests (DeviceShared::words_digests). It's an LRU of at most 16384 compiled pipelines, keyed like the memo by the compiled pipeline (kept::Compiled), holding one 32-byte digest per stage and no Vulkan object. A later variant of the same compiled pipeline that gets no digests from its caller takes them from there and hashes nothing. A caller's own digests are used as before (a render creation that linked its pipeline asks its functions). Why: a warm pass makes each record's variant and keeps none of its objects (warm/pass.rs), so the program's state of the same pipeline makes its variant's objects again. Until now it also hashed every word of every stage again, the same digest the pass had just taken. So does any variant made again after its kept instance went, as when the memo let its pipeline go. For ccb_conv_array's 16348 words that hash takes 17.1 us on lavapipe on msi (COMPUTE-CREATE-PARTS), on every such creation. A key names one compiled pipeline in a process, which the memo already trusts, so it names those words. Verified: the_program_s_state_after_a_pass_hashes_no_stage_words (tests/warm.rs) warms a recorded compute state's record, then has the program create that state. The program's creation makes its variant's objects (the shader-module counts move) and derives no digest (words_digests_derived stays still). The pass itself derived one at least, so the test fails without the map. It passed with the warm, render_stage_digests, kept_variants and functions_translated tests, 19 of 19, on lavapipe on msi at the tree of this commit (9262ddd00a). I didn't run it with the map removed, so the failure without the fix is by construction, not observed. compute_create_bench's new round shows the time: for ccb_conv_array on lavapipe, a program's states after a warm pass, memo of 1 byte, take 115.1 us at the median, against 133.6 us on a new device with no pass, which hashes. The gap matches the 17.1 us digest. --- crates/metal2vulkan-runtime/src/device/mod.rs | 6 +++ .../metal2vulkan-runtime/src/pipeline/mod.rs | 2 +- .../src/pipeline/objects.rs | 23 ++++++++++- crates/metal2vulkan-runtime/src/tests/warm.rs | 40 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 445528039..4cbbbae04 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -545,6 +545,9 @@ pub(super) struct DeviceShared { /// The variants' shader modules by their exact SPIR-V, shared device-wide /// (`pipeline/content_keyed.rs`). pub(super) shader_modules: Mutex, + /// The compiled pipelines' stage words' digests, kept past their variants + /// (`pipeline/objects.rs`, `WordsDigests`). + pub(super) words_digests: Mutex, /// The variants' pipeline layouts by their exact definition, shared device-wide /// (`pipeline/shared_layouts.rs`). pub(crate) pipeline_layouts: Mutex, @@ -3018,6 +3021,9 @@ impl Device { kept: Mutex::default(), region_ledger, shader_modules: Mutex::default(), + words_digests: Mutex::new(crate::pipeline::WordsDigests::counted( + crate::pipeline::WORDS_DIGESTS, + )), pipeline_layouts: Mutex::default(), flights: crate::pipeline::flight::Flights::default(), color_resolves: Mutex::new(ColorResolveObjects::default()), diff --git a/crates/metal2vulkan-runtime/src/pipeline/mod.rs b/crates/metal2vulkan-runtime/src/pipeline/mod.rs index d7388f172..1428d8bde 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/mod.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/mod.rs @@ -87,8 +87,8 @@ pub(super) use graphics::{ color_write_masks, keyed_assembly, DepthRange, DrawKey, FixedState, GraphicsKey, }; pub(super) use making::Pending; -pub(super) use objects::ShaderModules; pub(super) use objects::{create_set_layout, set_needs, RawPipeline, VariantObjects}; +pub(super) use objects::{ShaderModules, WordsDigests, WORDS_DIGESTS}; pub use options::{PipelineOption, PipelineOptions}; /// The general form of a key (`render/residual.rs`), for `tests::general_form`. #[cfg(test)] diff --git a/crates/metal2vulkan-runtime/src/pipeline/objects.rs b/crates/metal2vulkan-runtime/src/pipeline/objects.rs index 9212412f8..fff7f393b 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/objects.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/objects.rs @@ -18,6 +18,7 @@ use super::graphics::Notices; use super::interlocked_writes; use super::kept::{Compiled, Instance, KeptObjects, KeptPipeline, Place}; use super::libraries::Linked; +use super::memo::Lru; use super::shared_layouts::{settle_layouts, shared_layout, SharedLayout}; use super::RegionKey; use crate::binding::{Info, SetWrite}; @@ -301,8 +302,21 @@ impl Owned for ShaderModule { /// `DeviceShared::shader_modules`). pub(crate) type ShaderModules = ContentKeyed; +/// The most compiled pipelines whose stages' digests a device keeps ([`WordsDigests`]). +pub(crate) const WORDS_DIGESTS: usize = 16384; + +/// The digests of each compiled pipeline's stage words ([`words_digest`]), in stage order, by the +/// compiled pipeline (the memo's key), kept past its variants and past the memo, which let it go +/// (`DeviceShared::words_digests`). A variant whose objects are made again, where no kept +/// instance gives them back (`kept.rs`), names its modules by these and hashes no word: a +/// program's state after the warm pass made the same pipeline, or after the memo let it go. A key +/// names one compiled pipeline in a process, as the memo trusts, so it names those words. +/// Holds no Vulkan object; at most [`WORDS_DIGESTS`], the least recently used going first. +pub(crate) type WordsDigests = Lru>; + /// The SHA-256 of SPIR-V `words` (their length, then their bytes): what names a stage's module in -/// the device's map and its words in its makings' records, taken once per stage of a variant. +/// the device's map and its words in its makings' records, taken once per stage of a variant of a +/// pipeline the device kept no digests of ([`WordsDigests`]). /// /// The only derivation of such a digest there is. A creation that holds the compiled function a /// stage's words came from asks the function for the digest it already took with this very @@ -817,7 +831,14 @@ impl VariantObjects { // order. settle_device_maps(device); let Some(back) = back else { + // The digests an earlier variant of the pipeline took, where the caller has none. + let known = match digests { + Some(_) => None, + None => lock(&shared.words_digests).get(&compiled).cloned(), + }; + let digests = digests.or(known.as_deref()); let mut objects = VariantObjects::create(device, layout, stages, pipeline, digests)?; + lock(&shared.words_digests).insert(compiled, Arc::clone(&objects.words), 1); objects.keep = (!warming).then(|| Instance::new(compiled)); return Ok((objects, Vec::new())); }; diff --git a/crates/metal2vulkan-runtime/src/tests/warm.rs b/crates/metal2vulkan-runtime/src/tests/warm.rs index 1b7e7442b..65962bee2 100644 --- a/crates/metal2vulkan-runtime/src/tests/warm.rs +++ b/crates/metal2vulkan-runtime/src/tests/warm.rs @@ -530,6 +530,46 @@ fn a_warm_pass_takes_nothing_the_program_sees() { println!("the creations announced {warm_notices} notice(s) on both devices"); } +/// The program's compute state of a pipeline a warm pass made makes its variant's objects again +/// (the pass kept none), and hashes none of its stage's words: the device kept the digests the +/// pass's variants took (`pipeline/objects.rs`, `WordsDigests`). Paired: the pass itself, on a +/// device that knew none of them, took one at least. +#[test] +fn the_program_s_state_after_a_pass_hashes_no_stage_words() { + let recording = Scratch::new("warm-digests-recording"); + let records = recorded(&recording.0); + let scratch = Scratch::new("warm-digests"); + let warm = device(&scratch.0, None, None); + let kernel = compute(&warm); + let derived = crate::pipeline::objects::words_digests_derived; + let before_pass = derived(); + warm.warm(records, u64::MAX); + let s = warmed(&warm, STATES); + assert_eq!(s.records_warmed, STATES, "{s:?}"); + assert!( + derived() > before_pass, + "the pass, on a device that knew no pipeline, hashed no stage's words" + ); + let stages = |device: &Device| { + let counts = device.work_counts(); + counts.shader_modules_made + counts.shader_modules_shared + }; + let (before, stages_before) = (derived(), stages(&warm)); + let state = warm + .new_compute_pipeline_state(&kernel, &PipelineOptions::default()) + .unwrap(); + assert!( + stages(&warm) > stages_before, + "the program's state made no variant's objects: nothing here asked for a digest" + ); + assert_eq!( + derived() - before, + 0, + "the program's state hashed the stage words the pass's variant hashed" + ); + drop(state); +} + /// A pass stops before its next record once the driver data its makings added reaches its byte /// budget: none is made under a budget of 0 bytes, and only the first under a budget of 1 byte. /// The 1-byte pass runs over a directory of its own, which holds none of the records' driver