diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index a51458212..04d5c72f4 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -831,6 +831,12 @@ that touch blocks first. kernel goes once its last state and the map have let it go. Each state numbers its own samplers. `Gauges::kernels` counts the compiled kernels alive and `Gauges::kernel_entries` the map's entries. +- A content the map does not hold is lowered from the device's stage entries (the translator's + `StageEntries`), which every compute and render creation of the device reads: a module is + decoded once for the device and a stage specialized once for its function, constants, links + and call-stack depth, not once a content. They hold at most 32 specialized entries in 64 MiB + and 4 module decodes in 32 MiB, and forget the least recently used past them; + `Gauges::stage_entries` counts what they hold and `Gauges::stage_entry_bytes` its bytes. - A state numbers samplers as Metal's pipeline creation does (`NumberedSampler`, in Metal's order): each constexpr sampler its kernel uses, then one where it reads a texture and one where it writes one. `sampler_ids` takes that list once the kernel is lowered and answers an index diff --git a/crates/metal2vulkan-rt/src/counters.rs b/crates/metal2vulkan-rt/src/counters.rs index 6afd21c05..472cd6eb4 100644 --- a/crates/metal2vulkan-rt/src/counters.rs +++ b/crates/metal2vulkan-rt/src/counters.rs @@ -90,6 +90,10 @@ pub(crate) enum Gauge { CaptureBytes, /// [`Gauges::staging_bytes`]. StagingBytes, + /// [`Gauges::stage_entries`]. + StageEntries, + /// [`Gauges::stage_entry_bytes`]. + StageEntryBytes, } /// No Metal counterpart: a device's counters ([`Device::counters`](crate::Device::counters)). @@ -161,6 +165,8 @@ struct Atomics { private_idle_bytes: AtomicU64, capture_bytes: AtomicU64, staging_bytes: AtomicU64, + stage_entries: AtomicU64, + stage_entry_bytes: AtomicU64, /// Segment ends whose barrier reaches the host, for the tests. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] host_reaches: AtomicU64, @@ -338,6 +344,11 @@ impl Counters { self.gauge(gauge).fetch_sub(amount, Relaxed); } + /// `gauge` at `value`: what an object the device holds counts itself, read after it moved. + pub(crate) fn set(&self, gauge: Gauge, value: u64) { + self.gauge(gauge).store(value, Relaxed); + } + fn gauge(&self, gauge: Gauge) -> &AtomicU64 { let a = &self.atomics; match gauge { @@ -359,6 +370,8 @@ impl Counters { Gauge::PrivateIdleBytes => &a.private_idle_bytes, Gauge::CaptureBytes => &a.capture_bytes, Gauge::StagingBytes => &a.staging_bytes, + Gauge::StageEntries => &a.stage_entries, + Gauge::StageEntryBytes => &a.stage_entry_bytes, } } @@ -430,6 +443,8 @@ impl Counters { private_idle_bytes: a.private_idle_bytes.load(Relaxed), capture_bytes: a.capture_bytes.load(Relaxed), staging_bytes: a.staging_bytes.load(Relaxed), + stage_entries: a.stage_entries.load(Relaxed), + stage_entry_bytes: a.stage_entry_bytes.load(Relaxed), } } } @@ -639,4 +654,10 @@ pub struct Gauges { /// the staging region, once a copy made it, and the staging buffers of their own that copies /// larger than the region hold until the upload that copies through them is gone. pub staging_bytes: u64, + /// The translator's specialized entries and module decodes the device keeps for its pipeline + /// creations: at most 32 entries and 4 decodes, the least recently used going past them. + pub stage_entries: u64, + /// The bytes those entries and decodes hold, each counted as the ledger bytes making it held: + /// at most 64 MiB of entries and 32 MiB of decodes. + pub stage_entry_bytes: u64, } diff --git a/crates/metal2vulkan-rt/src/device.rs b/crates/metal2vulkan-rt/src/device.rs index 78e1e74e6..b27085074 100644 --- a/crates/metal2vulkan-rt/src/device.rs +++ b/crates/metal2vulkan-rt/src/device.rs @@ -202,6 +202,11 @@ pub(crate) struct Core { pub(crate) caches: Caches, /// The compiled kernels by content, which every compute pipeline state of a content shares. pub(crate) kernels: KernelMap, + /// The translator's decodes and specialized entries of the functions the device's pipeline + /// states lowered, which every creation reads, so a module is decoded and a stage + /// specialized once for the device rather than once a state. Bounded by the translator: the + /// least recently used go ([`StageEntries`](metal2vulkan::pipeline::StageEntries)). + pub(crate) entries: metal2vulkan::pipeline::StageEntries, /// Quiesced or torn down: calls that need a thread are refused. closed: AtomicBool, /// [`RUNNING`], [`HALTING`], or the answer of the stop that ran. @@ -474,6 +479,7 @@ impl Device { translator, caches, kernels: KernelMap::new(), + entries: metal2vulkan::pipeline::StageEntries::new(), closed: AtomicBool::new(false), halt: AtomicU8::new(RUNNING), }); @@ -967,6 +973,17 @@ pub(crate) struct ComputeLimits { } impl Core { + /// The gauges of the device's stage entries ([`Gauges::stage_entries`] and + /// [`Gauges::stage_entry_bytes`]), read after a creation moved them. + /// + /// [`Gauges::stage_entries`]: crate::Gauges::stage_entries + /// [`Gauges::stage_entry_bytes`]: crate::Gauges::stage_entry_bytes + pub(crate) fn note_entries(&self) { + let (entries, decodes, bytes) = self.entries.held(); + self.counters.set(Gauge::StageEntries, entries + decodes); + self.counters.set(Gauge::StageEntryBytes, bytes); + } + /// The memory type of a class's blocks. /// What the device makes of an image of `info`'s format, type, tiling, usage and flags; /// `None` where it makes none. @@ -1101,6 +1118,9 @@ impl Drop for Shared { impl Drop for Core { fn drop(&mut self) { self.wait_idle(); + // The stage entries go with the core. + self.counters.set(Gauge::StageEntries, 0); + self.counters.set(Gauge::StageEntryBytes, 0); // The runtime's threads have ended: the core is dropped only once none holds it, and with // it every command buffer and upload, each of which held what it used. The teardown ran // the consumer's unmap hook, so freeing runs none. diff --git a/crates/metal2vulkan-rt/src/pipeline.rs b/crates/metal2vulkan-rt/src/pipeline.rs index 7c12cf5aa..5807861f2 100644 --- a/crates/metal2vulkan-rt/src/pipeline.rs +++ b/crates/metal2vulkan-rt/src/pipeline.rs @@ -499,9 +499,9 @@ impl Compiled { }; let (whole, partial, observes, image_only, source) = match &descriptor.kernel { StageSource::Function(function) => { - let entries = StageEntries::new(); + let entries = &core.entries; let plain = translator - .kernel_inputs_read(function, &descriptor.lowering_inputs(), &budget, &entries) + .kernel_inputs_read(function, &descriptor.lowering_inputs(), &budget, entries) .map_err(Error::Translation)?; let mut emulated = plain.clone(); emulated.emulated_simdgroups = true; @@ -509,7 +509,7 @@ impl Compiled { let mut inputs = plain.clone(); inputs.partial_threadgroups = true; let lower = - |inputs: &LoweringInputs| lower(&core, function, inputs, &budget, &entries); + |inputs: &LoweringInputs| lower(&core, function, inputs, &budget, entries); // The translator's refusal of the variant (emulated simdgroups, a vote word its // workgroup memory cannot hold), kept like a lowering: the kernel keeps its plain // module. @@ -683,7 +683,9 @@ pub(crate) fn lower( return Ok(kept); } core.counters.count(Metric::Translations); - let lowered = translator.compile_function_from_entries(function, inputs, budget, entries)?; + let lowered = translator.compile_function_from_entries(function, inputs, budget, entries); + core.note_entries(); + let lowered = lowered?; core.caches .keep_translation(key.digest(), &lowered.to_bytes(), &core.counters); Ok(lowered) @@ -1165,8 +1167,7 @@ impl Emulated { core.counters.count(Metric::TranslationsLoaded); return Lowered::Refused; } - let entries = StageEntries::new(); - let compiled = match lower(core, &self.function, &self.inputs, &budget, &entries) { + let compiled = match lower(core, &self.function, &self.inputs, &budget, &core.entries) { Ok(compiled) => compiled, Err(TranslationError::Unsupported(_)) => { core.caches.keep_refusal(&key, &core.counters); diff --git a/crates/metal2vulkan-rt/src/render.rs b/crates/metal2vulkan-rt/src/render.rs index d2dcc0944..5d1088fb1 100644 --- a/crates/metal2vulkan-rt/src/render.rs +++ b/crates/metal2vulkan-rt/src/render.rs @@ -162,11 +162,13 @@ impl crate::Device { Some(compiled) => compiled, None => { let budget = Budget::standard(); - let entries = StageEntries::new(); + let entries = &core.entries; let keys = (core.translator) - .render_variant_key(descriptor, &VariantKey::new(), &budget, &entries) + .render_variant_key(descriptor, &VariantKey::new(), &budget, entries) .map_err(Error::Translation)?; - let compiled = RenderCompiled::of(core, descriptor, &keys, &budget, &entries)?; + let compiled = RenderCompiled::of(core, descriptor, &keys, &budget, entries); + core.note_entries(); + let compiled = compiled?; (core.kernels).keep_render(key, Arc::new(compiled), &core.counters) } }; diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 6939a16ea..9000d05c5 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -1674,6 +1674,8 @@ impl Backend for NewRuntime { ("private_idle_bytes", gauges.private_idle_bytes), ("capture_bytes", gauges.capture_bytes), ("staging_bytes", gauges.staging_bytes), + ("stage_entries", gauges.stage_entries), + ("stage_entry_bytes", gauges.stage_entry_bytes), ("shared_block_bytes", held(rt::MemoryClass::Shared)), ("private_block_bytes", held(rt::MemoryClass::Private)), ]) diff --git a/crates/metal2vulkan/src/pipeline/compile.rs b/crates/metal2vulkan/src/pipeline/compile.rs index cb2c51920..e95eec696 100644 --- a/crates/metal2vulkan/src/pipeline/compile.rs +++ b/crates/metal2vulkan/src/pipeline/compile.rs @@ -965,12 +965,12 @@ pub struct StageEntries { /// The most entries one [`StageEntries`] keeps, whatever their sizes. One creation asks for at /// most three (its stages), so this is room for the stages of many states' worth of functions, /// while the search stays a scan of a handful. -const KEPT_ENTRIES: usize = 32; +pub(crate) const KEPT_ENTRIES: usize = 32; /// The most bytes of specialized entries one [`StageEntries`] keeps: 64 MiB, each entry counted /// as the ledger bytes specializing it held. One translation attempt may use 500 MiB, so /// an object shared between creations holds a fraction of one attempt's room. -const KEPT_ENTRY_BYTES: u64 = 64 << 20; +pub(crate) const KEPT_ENTRY_BYTES: u64 = 64 << 20; /// The most module decodes one [`StageEntries`] keeps, whatever their sizes: a render creation's /// three stages and one more, which is the window a sweep of states, or a library's entries @@ -1065,6 +1065,20 @@ impl StageEntries { kept.as_ref().map_or((0, 0), |k| (k.made, k.found)) } + /// What it holds now: its specialized entries, its module decodes, and the bytes both hold + /// (each counted as the ledger bytes making it held, as its bounds count them). Each falls as + /// the least recently used go. + pub fn held(&self) -> (u64, u64, u64) { + let kept = self.kept.lock(); + kept.as_ref().map_or((0, 0, 0), |k| { + ( + k.entries.len() as u64, + k.decodes.len() as u64, + k.bytes + k.decode_bytes, + ) + }) + } + /// How many module decodes it has read of bitcode, and how many asks copied a decode an /// earlier ask had read, over its whole life (neither ever falls). Every entry it specializes /// and every plain answer it reads needs the decode of its module, so the entries of one diff --git a/crates/metal2vulkan/src/tests/library_entries_decode_once.rs b/crates/metal2vulkan/src/tests/library_entries_decode_once.rs index 5cb53ffc6..ac3df96b0 100644 --- a/crates/metal2vulkan/src/tests/library_entries_decode_once.rs +++ b/crates/metal2vulkan/src/tests/library_entries_decode_once.rs @@ -36,6 +36,9 @@ //! `HASH` is verified against its bitcode when the container is parsed //! ([`a_librarys_stated_air_hash_is_the_hash_of_the_bitcode_a_compile_reads`], //! [`a_metallib_whose_stated_hash_is_not_its_bitcodes_is_refused`]). +//! - **Bounded.** Filled past its bounds, it keeps the most recently used entries and no more, +//! and what it holds stays within its byte bounds +//! ([`an_object_filled_past_its_bounds_keeps_only_the_most_recent`]). //! - **What holding it costs.** The headline part reports what one decode charges the ledger and //! holds four of the largest committed module's to the object's byte bound, so a bound that //! could not hold a library's one decode would fail here. @@ -415,3 +418,58 @@ fn a_metallib_whose_stated_hash_is_not_its_bitcodes_is_refused() { "a metallib whose HASH is not its bitcode's must be refused, not parsed" ); } + +/// Filled past its bounds, the object keeps the most recently used and no more: one entry of +/// [`MANY`] asked at forty call-stack depths, each depth its own specialized entry, leaves at most +/// `KEPT_ENTRIES` entries and one decode within the byte bounds ([`StageEntries::held`]); of the +/// depths, the latest is found again, while the first, gone past the bound, is made again. +#[test] +fn an_object_filled_past_its_bounds_keeps_only_the_most_recent() { + use crate::pipeline::compile::{KEPT_DECODE_BYTES, KEPT_ENTRIES, KEPT_ENTRY_BYTES}; + let Some(bitcode) = fixture(MANY) else { return }; + let device = device(); + let library = Library::from_air(&bitcode).expect("library"); + let fd = stages(&library).into_iter().next().expect("an entry"); + let entries = StageEntries::new(); + let ask = |depth: u32| { + let mut inputs = LowerInputs::new(); + inputs.max_call_stack_depth = Some(depth); + let mut meter = Meter::new(&Budget::standard()); + let compiled = compile_stage_from( + &fd, + device.profile(), + InputsFrom::Given(&inputs), + &entries, + &mut meter, + ); + compiled.expect("the entry compiles at every depth"); + }; + for depth in 1..=40 { + ask(depth); + } + let (held, decodes, bytes) = entries.held(); + let kept = u64::try_from(KEPT_ENTRIES).expect("the bound"); + assert!( + (1..=kept).contains(&held), + "40 entries asked must leave between 1 and {kept} held, not {held}" + ); + assert_eq!(decodes, 1, "one module, one decode held"); + assert!( + bytes <= KEPT_ENTRY_BYTES + KEPT_DECODE_BYTES, + "the held bytes, {bytes}, must stay within the bounds" + ); + assert_eq!( + entries.counts(), + (40, 0), + "forty depths, forty entries made" + ); + ask(40); + assert_eq!(entries.counts(), (40, 1), "the latest depth is found again"); + ask(1); + assert_eq!( + entries.counts(), + (41, 1), + "the first depth, gone past the bound, is made again" + ); + assert!(entries.held().0 <= kept, "asking again keeps the bound"); +} diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 1a8e62aa4..0c651b7f1 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2296,6 +2296,8 @@ field metal2vulkan-rt::Gauges.samplers pub samplers: u64 field metal2vulkan-rt::Gauges.shared_idle_bytes pub shared_idle_bytes: u64 field metal2vulkan-rt::Gauges.spilled_bytes pub spilled_bytes: u64 field metal2vulkan-rt::Gauges.staging_bytes pub staging_bytes: u64 +field metal2vulkan-rt::Gauges.stage_entries pub stage_entries: u64 +field metal2vulkan-rt::Gauges.stage_entry_bytes pub stage_entry_bytes: u64 field metal2vulkan-rt::Gauges.textures pub textures: u64 field metal2vulkan-rt::HeapRange.block pub block: BlockId field metal2vulkan-rt::HeapRange.len pub len: u64 @@ -11245,6 +11247,7 @@ method metal2vulkan::pipeline::SpecKey::read_as_data pub fn read_as_data(self) - method metal2vulkan::pipeline::SpecTest::differ pub fn differ(key: SpecKey, mask: u32, bits: u32) -> SpecTest method metal2vulkan::pipeline::SpecTest::equal pub fn equal(key: SpecKey, mask: u32, bits: u32) -> SpecTest method metal2vulkan::pipeline::StageEntries::counts pub fn counts(&self) -> (u64, u64) +method metal2vulkan::pipeline::StageEntries::held pub fn held(&self) -> (u64, u64, u64) method metal2vulkan::pipeline::StageEntries::new pub fn new() -> StageEntries method metal2vulkan::pipeline::StageInputAttributeDescriptor::new pub fn new(format: metal::AttributeFormat, offset: u32, buffer_index: u32) -> Self method metal2vulkan::pipeline::StageInputDescriptor::canonical pub fn canonical(&self) -> StageInputDescriptor