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