diff --git a/crates/metal2vulkan-runtime/src/compute.rs b/crates/metal2vulkan-runtime/src/compute.rs index 876c8bae5..5809ec75f 100644 --- a/crates/metal2vulkan-runtime/src/compute.rs +++ b/crates/metal2vulkan-runtime/src/compute.rs @@ -1848,7 +1848,7 @@ fn settle_indirect( /// So a grid narrower than one threadgroup on an axis, which the split runs as one region of the /// grid's own size, a `VkPipeline` of each width, takes the flat form's one `VkPipeline` of the /// call's size too: on a guest's run on an RX 7900 XTX, a kernel dispatched at 64 such widths -/// made 185,566 `VkPipeline`s in 45 s, past the most its variant keeps (`PIPELINES`). +/// made 185,566 `VkPipeline`s in 45 s while its variant kept 32 (`pipeline::REGION_PIPELINES`). /// /// `None`, so the dispatch keeps its exact split, where its grid is already a whole number of /// threadgroups on every axis (the split is one region of the call's size, which the flat form diff --git a/crates/metal2vulkan-runtime/src/pipeline/making.rs b/crates/metal2vulkan-runtime/src/pipeline/making.rs index daaab88d3..4a8d7af4c 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/making.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/making.rs @@ -26,7 +26,7 @@ //! (`budget::Urgency`, `pool.rs`). use super::budget::{Asked, Urgency}; -use super::memo::{At, Lru}; +use super::memo::{At, Evictions, Lru}; use super::pool::Place; use crate::device::lock; use crate::error::{internal, Error}; @@ -206,6 +206,16 @@ impl Making { } } + /// An empty memo of at most `max_entries` entries, telling `evictions` how many it lets go + /// past that bound at each making it begins or keeps. + pub(crate) fn counted_evicting(max_entries: usize, evictions: Evictions) -> Making { + Making { + memo: Arc::new(Mutex::new( + Lru::counted(max_entries).counting_evictions(evictions), + )), + } + } + /// The object of `key`: from the memo, from the making in flight for it, or made now by /// `make` with no lock held, and kept. pub(crate) fn get_or_make( diff --git a/crates/metal2vulkan-runtime/src/pipeline/memo.rs b/crates/metal2vulkan-runtime/src/pipeline/memo.rs index 9a584f04f..0abf1dc92 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/memo.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/memo.rs @@ -34,6 +34,10 @@ use std::sync::{Arc, Mutex, Weak}; /// The most entries the device's memo holds, whatever their sizes. pub(crate) const MEMO_ENTRIES: usize = 4096; +/// What a map tells, at each insert that removed entries past its bounds, of how many it removed +/// ([`Lru::counting_evictions`]). +pub(crate) type Evictions = Box; + /// A map that keeps at most `max_weight` of its entries' weights and at most `max_entries` /// entries, removing the least recently used past either. A lookup or an insert is a use. pub(crate) struct Lru { @@ -50,6 +54,8 @@ pub(crate) struct Lru { weight: u64, max_weight: u64, max_entries: usize, + /// Told of the entries an insert removed past the bounds, where the map counts them. + evictions: Option, /// The uses that moved an entry in `order` ([`used`](Self::used)). #[cfg(test)] reordered: u64, @@ -78,6 +84,7 @@ impl Lru { weight: 0, max_weight, max_entries, + evictions: None, #[cfg(test)] reordered: 0, } @@ -88,6 +95,12 @@ impl Lru { Lru::new(u64::MAX, max_entries) } + /// The map, telling `evictions` how many entries each insert removes past its bounds. + pub(crate) fn counting_evictions(mut self, evictions: Evictions) -> Lru { + self.evictions = Some(evictions); + self + } + /// The value of `key`, now the most recently used. pub(crate) fn get(&mut self, key: &K) -> Option<&V> { self.get_mut(key).map(|value| &*value) @@ -153,6 +166,7 @@ impl Lru { self.entries.insert(stamp, slot); self.order.insert(stamp, stamp); self.weight += weight; + let mut evicted = 0; while self.weight > self.max_weight || self.entries.len() > self.max_entries { let Some((_, oldest)) = self.order.pop_first() else { break; @@ -160,8 +174,12 @@ impl Lru { if let Some(slot) = self.entries.remove(&oldest) { self.numbers.remove(&slot.key); self.weight -= slot.weight; + evicted += 1; } } + if let Some(evictions) = self.evictions.as_ref().filter(|_| evicted > 0) { + evictions(evicted); + } } /// Remove `key`'s entry, if any. diff --git a/crates/metal2vulkan-runtime/src/pipeline/mod.rs b/crates/metal2vulkan-runtime/src/pipeline/mod.rs index 0a88a60cb..dce7445f9 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/mod.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/mod.rs @@ -211,8 +211,26 @@ pub(super) fn specialization_values( /// The most plans a variant keeps. const PLANS: usize = 64; -/// The most `VkPipeline`s a variant keeps. +/// The most `VkPipeline`s a render variant keeps, and a tile function's variant of its regions. const PIPELINES: usize = 32; +/// The most region `VkPipeline`s a kernel's variant keeps, the least recently used going first +/// (`WorkCounts::region_pipelines_evicted` counts those let go). +/// +/// A region's local size and `MultiRegion` are specializations, so a kernel takes a `VkPipeline` +/// per threadgroup width its program's calls ask for, and with `dispatchThreads`, where the +/// kernel observes its partition (no flat variant runs it, `compute::flat_direct`), one per +/// remainder that is an edge region and one more per width whose interior has an edge. Measured +/// on a guest's run on an RX 7900 XTX: one kernel dispatched as `dispatchThreads(n, min(n, 64))` +/// took 128 keys of one variant (each width from 1 to 64 alone, and the interior of 64 with each +/// edge from 1 to 63), and with the 32 a variant kept before, each was made again about 6,300 times +/// in 146 s, every such dispatch's command buffer waiting for a making on the pool. 256 holds that +/// working set twice over (`tests/region_widths.rs` makes each of the 128 once). +/// +/// What the bound costs where it is full: a compute `VkPipeline` of a small kernel is its shader +/// binary in the driver's arena and the driver's own records of it, a few KiB each +/// (`tests/region_widths.rs` prints the process's memory across 128 makings); a variant whose +/// dispatches take no more keys than they did holds no more than before. +const REGION_PIPELINES: usize = 256; /// The most variants besides the first a state keeps. const VARIANTS: usize = 8; /// The most plans a state keeps of its latest direct dispatches that planned ([`Planned`]): a @@ -1854,12 +1872,18 @@ impl Variant { None, )?; objects.log_kernel(function, &pipeline.stage); + // A tile function's dispatch is one region of its pass's threads per tile; a kernel's + // takes the call's threadgroup and its grid's edges (`REGION_PIPELINES`). + let tile = (pipeline.dependencies.iter()).any(|d| d.fact == RuntimeFact::TileFunction); + let bound = if tile { PIPELINES } else { REGION_PIPELINES }; + let counts = Arc::clone(&device.shared().counts); + let evicted = move |evicted| counts.region_pipelines_evicted(evicted); let variant = Arc::new(Variant { key, pipeline, objects: Arc::new(objects), plans: Mutex::new(Lru::counted(PLANS)), - pipelines: making::Making::counted(PIPELINES), + pipelines: making::Making::counted_evicting(bound, Box::new(evicted)), }); for (place, made) in kept { if let kept::Place::Region(region) = place { diff --git a/crates/metal2vulkan-runtime/src/tests/grid_widths.rs b/crates/metal2vulkan-runtime/src/tests/grid_widths.rs index 12f8d0091..70a9dc4f2 100644 --- a/crates/metal2vulkan-runtime/src/tests/grid_widths.rs +++ b/crates/metal2vulkan-runtime/src/tests/grid_widths.rs @@ -1,9 +1,8 @@ //! Direct `dispatchThreads:` of grids narrower than one threadgroup, at many widths: a kernel that //! does not observe its threadgroup partition runs each on its flat variant's one `VkPipeline` of //! the call's threadgroup size (`compute.rs`'s `flat_direct`), so a second pass over the same -//! widths makes none. Split instead, each width was a region of its own local size, a -//! `VkPipeline` each, past the most a variant keeps (`pipeline/mod.rs`'s `PIPELINES`): the second -//! pass made them all again. +//! widths makes none. Split instead, each width is a region of its own local size, a +//! `VkPipeline` each: the first pass makes 100. //! //! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. The kernel is the owned fixture's diff --git a/crates/metal2vulkan-runtime/src/tests/memo.rs b/crates/metal2vulkan-runtime/src/tests/memo.rs index df0f04aff..854cabd25 100644 --- a/crates/metal2vulkan-runtime/src/tests/memo.rs +++ b/crates/metal2vulkan-runtime/src/tests/memo.rs @@ -77,6 +77,39 @@ fn the_least_recently_used_goes_first() { assert!(lru.get(&2).is_none() && lru.get(&3).is_some() && lru.get(&5).is_some()); } +/// A map counting its evictions is told of each entry an insert removed past its bounds, and of +/// nothing else: not an insert within them, not a key's replacement, not a removal by key. +#[test] +fn a_counting_map_tells_each_eviction_and_nothing_else() { + let told = Arc::new(std::sync::atomic::AtomicU64::new(0)); + let counted = Arc::clone(&told); + let tell = move |n| { + counted.fetch_add(n, std::sync::atomic::Ordering::Relaxed); + }; + let evicted = || told.load(std::sync::atomic::Ordering::Relaxed); + let mut lru: Lru = Lru::new(10, 3).counting_evictions(Box::new(tell)); + for key in 0..3 { + lru.insert(key, (), 1); + } + lru.insert(1, (), 1); + lru.remove(&2); + assert_eq!( + evicted(), + 0, + "within the bounds, a replacement and a removal" + ); + lru.insert(3, (), 1); + lru.insert(4, (), 1); + assert_eq!(evicted(), 1, "past the entries' bound: the oldest"); + lru.insert(5, (), 9); + assert_eq!( + evicted(), + 3, + "past the weight's bound: the two oldest in one insert" + ); + assert_eq!(lru.footprint(), (10, 2)); +} + #[test] fn replacing_counts_once_and_nothing_heavier_than_the_bound_is_kept() { let mut lru: Lru = Lru::new(10, 4); diff --git a/crates/metal2vulkan-runtime/src/tests/region_widths.rs b/crates/metal2vulkan-runtime/src/tests/region_widths.rs index 0a6e28221..7dfdbdd57 100644 --- a/crates/metal2vulkan-runtime/src/tests/region_widths.rs +++ b/crates/metal2vulkan-runtime/src/tests/region_widths.rs @@ -106,6 +106,8 @@ fn a_kernel_dispatched_at_every_width_makes_each_region_pipeline_once() { let device = device(&scratch.0); let state = state(&device); let made = || device.work_counts().variant_pipelines_made; + let evicted = || device.work_counts().region_pipelines_evicted; + let evicted_before = evicted(); let empty = process_memory(); let mut passes = Vec::new(); let mut times = Vec::new(); @@ -122,9 +124,10 @@ fn a_kernel_dispatched_at_every_width_makes_each_region_pipeline_once() { } println!( "REGION-WIDTHS {}: {GRIDS} grids in threadgroups of up to {WIDEST}, twice: VkPipelines \ - made {passes:?}, passes took {times:?}; memory before {empty:?}, after the first pass \ - {full:?}", - device.name() + made {passes:?}, evicted {}, passes took {times:?}; memory before {empty:?}, after the \ + first pass {full:?}", + device.name(), + evicted() - evicted_before ); assert!( passes[0] <= KEYS, @@ -132,4 +135,9 @@ fn a_kernel_dispatched_at_every_width_makes_each_region_pipeline_once() { passes[0] ); assert_eq!(passes[1], 0, "the second pass made VkPipelines again"); + assert_eq!( + evicted(), + evicted_before, + "the variant's memo let a region pipeline go" + ); } diff --git a/crates/metal2vulkan-runtime/src/tests/tile_rebind_making_wait.rs b/crates/metal2vulkan-runtime/src/tests/tile_rebind_making_wait.rs index 4ddd760c3..c82507f3c 100644 --- a/crates/metal2vulkan-runtime/src/tests/tile_rebind_making_wait.rs +++ b/crates/metal2vulkan-runtime/src/tests/tile_rebind_making_wait.rs @@ -7,8 +7,8 @@ //! Metal makes a tile state's pipeline at `newRenderPipelineStateWithTileDescriptor`, so no Metal //! call of the program's blocks on a shader compile afterwards: a tile dispatch waits for none //! (`tile_pending.rs`), and neither does a replace of a buffer's bytes. The runtime makes a -//! region's `VkPipeline` on demand instead, in the device's pool, and keeps at most 32 of a -//! variant's (`pipeline::PIPELINES`): a rebind's encoding again of a dispatch whose region key +//! region's `VkPipeline` on demand instead, in the device's pool, and keeps at most 32 of a tile +//! function's variant's (`pipeline::PIPELINES`): a rebind's encoding again of a dispatch whose region key //! the memo let go of must answer as the first encoding did -- hand the making to the pool and //! leave the pass to record later -- and never make the pipeline on the program's thread, nor //! wait for a making another encoder began. diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index ab606bc87..8d9ef71b9 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -621,6 +621,7 @@ pub(crate) struct Counters { pipelines_linked: AtomicU64, linked_pipelines_shared: AtomicU64, dispatch_pipelines_pooled: AtomicU64, + region_pipelines_evicted: AtomicU64, tile_pipelines_pooled: AtomicU64, upload_chunks_in_device_memory: AtomicU64, render_log_batches_by_thread: AtomicU64, @@ -2223,6 +2224,13 @@ impl Counters { .fetch_add(1, Ordering::Relaxed); } + /// A compute variant's memo of region `VkPipeline`s let `evicted` of them go past its bound + /// (`pipeline/mod.rs`, `REGION_PIPELINES`). + pub(crate) fn region_pipelines_evicted(&self, evicted: u64) { + self.region_pipelines_evicted + .fetch_add(evicted, Ordering::Relaxed); + } + /// A render encoder's tile dispatch handed the making of a region's `VkPipeline` to the /// device's pool and went on (`render/pending_tile.rs`). pub(crate) fn tile_pipeline_pooled(&self) { @@ -8943,6 +8951,10 @@ pub struct WorkCounts { /// device's pool and went on, rather than make it at the dispatch: each counted once per /// region it handed. pub dispatch_pipelines_pooled: u64, + /// Region `VkPipeline`s compute variants' memos let go past their bound, the least recently + /// used first (`pipeline/mod.rs`, `REGION_PIPELINES`): each one a dispatch takes again is made + /// again. None while each variant's dispatches take no more keys than the bound. + pub region_pipelines_evicted: u64, /// Render encoders' tile dispatches that handed the making of a region's `VkPipeline` to the /// device's pool and went on, their passes recorded once it is made, rather than make it at /// the dispatch: each counted once per region it handed. @@ -10339,6 +10351,7 @@ impl Device { pipelines_linked: counters.pipelines_linked.load(Ordering::Relaxed), linked_pipelines_shared: counters.linked_pipelines_shared.load(Ordering::Relaxed), dispatch_pipelines_pooled: counters.dispatch_pipelines_pooled.load(Ordering::Relaxed), + region_pipelines_evicted: counters.region_pipelines_evicted.load(Ordering::Relaxed), tile_pipelines_pooled: counters.tile_pipelines_pooled.load(Ordering::Relaxed), upload_chunks_in_device_memory: (counters.upload_chunks_in_device_memory) .load(Ordering::Relaxed), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 18d8ee810..85a07ac2a 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -3204,6 +3204,7 @@ field metal2vulkan-runtime::WorkCounts.reading_draws_split_variant_refused pub r field metal2vulkan-runtime::WorkCounts.reading_draws_split_views pub reading_draws_split_views: u64 field metal2vulkan-runtime::WorkCounts.reading_draws_split_visibility_query pub reading_draws_split_visibility_query: u64 field metal2vulkan-runtime::WorkCounts.reencodes_refused pub reencodes_refused: u64 +field metal2vulkan-runtime::WorkCounts.region_pipelines_evicted pub region_pipelines_evicted: u64 field metal2vulkan-runtime::WorkCounts.registrations_waited pub registrations_waited: u64 field metal2vulkan-runtime::WorkCounts.render_encoders_ended pub render_encoders_ended: u64 field metal2vulkan-runtime::WorkCounts.render_log_batches_by_program pub render_log_batches_by_program: u64