From c6fa6366651d476f4447da2393a2f110f6829fa1 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 15:22:25 -0800 Subject: [PATCH] Hold large pipeline makings to one large compile at a time A guest's run on RADV made kernels of up to 4.5 M SPIR-V words several at once; on the Radeon 780M one such making alone peaks at 3.6 to 10.3 GiB resident (1.9 to 4.5 M words). Nothing bounded how many large modules the driver compiled at once beside the budget's turns, which count makings and not their size. The process now keeps a limit of SPIR-V words in flight (pipeline/compile_memory.rs): a making whose stages hold 65,536 words or more holds its words around its driver call, the counted makings in flight holding at most 4,194,304 words, a larger one alone. One that does not fit waits for those in flight, lending its turn (budget::without_turn) and taking one again after; the waiters go in their turns' rank order and none passes one waiting before it, so a large making never starves. The compute making (objects.rs) holds its kernel's words, a whole render pipeline its stages', a pipeline library its own part's. Each module carries its words. WorkCounts counts the makings that waited (makings_held_for_compile_memory) and their wait (compile_memory_held_nanos); the conformance cost half reads the count with the other making counts. Unit tests: a making below the floor is not held; two that do not fit never hold at once; no counted making passes a waiting one, waiters going in rank order; a waiter lends its turn and takes it again. --- crates/metal2vulkan-conformance/src/cost.rs | 3 +- .../examples/host_ab/runtime.rs | 5 + crates/metal2vulkan-runtime/src/device/mod.rs | 15 + .../src/pipeline/budget.rs | 18 + .../src/pipeline/compile_memory.rs | 319 ++++++++++++++++++ .../src/pipeline/flight.rs | 4 +- .../src/pipeline/graphics.rs | 30 +- .../metal2vulkan-runtime/src/pipeline/mod.rs | 1 + .../src/pipeline/objects.rs | 16 +- .../metal2vulkan-runtime/src/work_counts.rs | 24 ++ 10 files changed, 430 insertions(+), 5 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/pipeline/compile_memory.rs diff --git a/crates/metal2vulkan-conformance/src/cost.rs b/crates/metal2vulkan-conformance/src/cost.rs index 687c56fea..bf2c3990e 100644 --- a/crates/metal2vulkan-conformance/src/cost.rs +++ b/crates/metal2vulkan-conformance/src/cost.rs @@ -850,7 +850,7 @@ impl Region { /// The name prefixes that put a count in [`Region::Translation`]: making a module, a layout or a /// pipeline. -const TRANSLATION: [&str; 20] = [ +const TRANSLATION: [&str; 21] = [ // The AIR decode and specialization a pipeline creation does for its stages, and the question // it asks about a linked function's uses. Translation work by definition -- an entry's // specialization depends on the functions and the constants, never on the pipeline state -- so @@ -891,6 +891,7 @@ const TRANSLATION: [&str; 20] = [ "tile_pipelines_pooled", "variants_taken_back", "flights_followed", + "makings_held_for_compile_memory", "residual_form_makings", ]; diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index b7e3532cf..e374fb216 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -1530,6 +1530,11 @@ impl Gpu for Runtime { ("pipeline_layouts_made", c.pipeline_layouts_made), ("pipeline_layouts_shared", c.pipeline_layouts_shared), ("flights_followed", c.flights_followed), + ( + "makings_held_for_compile_memory", + c.makings_held_for_compile_memory, + ), + ("compile_memory_held_nanos", c.compile_memory_held_nanos), ("predicated_loop_runs_again", c.predicated_loop_runs_again), ("variants_taken_back", c.variants_taken_back), ( diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 68f71985a..440625095 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -790,6 +790,21 @@ impl DeviceShared { }) } + /// The words of the process's limit on large makings in flight that a making of `words` + /// SPIR-V words holds around its driver call (`pipeline/compile_memory.rs`): `None` where it + /// does not count. One that waited for them counts in + /// `WorkCounts::makings_held_for_compile_memory`, its wait in `compile_memory_held_nanos`. + pub(crate) fn compile_memory( + &self, + words: usize, + ) -> Option> { + let (held, waited) = crate::pipeline::compile_memory::hold(words)?; + if let Some(waited) = waited { + self.counts.making_held_for_compile_memory(waited); + } + Some(held) + } + /// The compute pipeline of `info`, made as /// [`make_pipelines_reported`](Self::make_pipelines_reported) makes one under the record /// `making` names, with what its driver reports of it where the device's makings report diff --git a/crates/metal2vulkan-runtime/src/pipeline/budget.rs b/crates/metal2vulkan-runtime/src/pipeline/budget.rs index cab7617f4..f92520c0d 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/budget.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/budget.rs @@ -65,6 +65,9 @@ //! - **A helper's turn before its lock.** A helper pipeline made on its first use takes its turn //! before its helper's lock ([`helper_turn`]), so no thread holds that lock waiting for a turn, //! and the helper's other uses, made already, never wait behind the budget. +//! - **Large makings by their words.** A making of a large module also holds its SPIR-V words of +//! the process's limit on large makings in flight around its driver call, lending its turn +//! while it waits for them (`compile_memory.rs`): two large makings never compile at once. //! - **Not counted:** the device's priming (`device/prime.rs`), which makes three pipelines of //! trivial shaders on the creating thread before the device reaches the program, takes no turn: //! a device's creation never waits behind the process's compiles. @@ -173,6 +176,21 @@ pub(crate) fn holds_turn() -> bool { .unwrap_or(false) } +/// The rank of the turn this thread holds, raised to the rank something that came to wait for its +/// piece asked since (`Place::hasten`), as a lent turn is taken again at ([`without_turn`]): +/// what a wait for compile memory is ordered by (`compile_memory.rs`). `None` where it holds none. +pub(crate) fn held_rank() -> Option { + HELD.try_with(|held| { + held.borrow().as_ref().map(|held| { + let (given, ticket) = held.rank; + let raised = (held.asked.as_ref()).and_then(|asked| asked.get()); + (raised.map_or(given, |raised| raised.min(given)), ticket) + }) + }) + .ok() + .flatten() +} + /// A new ticket: later than every ticket drawn before it. pub(crate) fn ticket() -> u64 { TICKETS.fetch_add(1, Ordering::Relaxed) diff --git a/crates/metal2vulkan-runtime/src/pipeline/compile_memory.rs b/crates/metal2vulkan-runtime/src/pipeline/compile_memory.rs new file mode 100644 index 000000000..1e3ca8a2a --- /dev/null +++ b/crates/metal2vulkan-runtime/src/pipeline/compile_memory.rs @@ -0,0 +1,319 @@ +//! The process's limit on its large pipeline makings in flight, by the SPIR-V words they hand the +//! driver's compiler: two large makings never compile at once. +//! +//! - **What counts.** A program's pipeline making whose stages hold [`COUNTED_WORDS`] SPIR-V +//! words or more: a compute pipeline's kernel (`objects.rs`), a render pipeline's stages and a +//! graphics pipeline library's own (`graphics.rs`). Every other making goes as the budget's +//! turns let it (`budget.rs`), the runtime's own pipelines among them, each far below. +//! - **The limit.** The counted makings in flight hold at most [`WORDS_IN_FLIGHT`] words at once, +//! a single larger one alone. A driver's compiler takes memory as it compiles, more the larger +//! the module: RADV (Mesa 26.2.6, ACO) on the Radeon 780M, one making at a time in a process of +//! its own, peaked at 3.6 GiB resident for a kernel of 1.9 M words, 4.2 GiB for 3.8 M, 8.5 GiB +//! for 2.3 M and 10.3 GiB for 4.5 M (two forms of two kernels), where a guest's own memory +//! shares the host's. +//! - **Waiting** ([`hold`]). A counted making that does not fit waits for the counted makings in +//! flight, holding no turn of the budget's: it lends its thread's for the wait, as a wait for a +//! twin does (`budget::without_turn`), and takes one again after it. The waiters go in the +//! order of their turns' ranks, as they were when they asked (`budget::held_rank`: the most +//! urgent first, then the earlier ticket); no counted making passes one waiting before it, so a +//! large making waits only for those in flight, never for a stream of later ones. +//! - **Only around the driver call.** A making holds its words from just before its +//! `vkCreate*Pipelines` call to just after it, or it may wait for its turn again holding them. +//! No making waits for words holding a turn, so the two never wait on each other, and every +//! holder's driver call ends. +//! - **Nothing seen is decided here.** Which pipelines are made, their keys and everything the +//! program sees stay the callers'; the limit only delays a driver call. +//! - **Counted.** A making that waited counts in `WorkCounts::makings_held_for_compile_memory`, +//! and its wait in `WorkCounts::compile_memory_held_nanos`. + +use super::budget::{self, Rank, Urgency}; +use crate::device::lock; +use std::sync::{Condvar, Mutex, OnceLock}; +use std::time::{Duration, Instant}; + +/// The fewest SPIR-V words a making's stages hold for it to count: 65,536. +pub(crate) const COUNTED_WORDS: usize = 1 << 16; + +/// The most words of counted makings in flight at once, a single larger making alone: +/// 4,194,304, about one large kernel's (the module docs). +pub(crate) const WORDS_IN_FLIGHT: usize = 1 << 22; + +/// The process's limit, which every device's makings share. +static PROCESS: OnceLock = OnceLock::new(); + +/// A limit of words in flight (the module docs): the process's, or a test's own. +pub(crate) struct Limit { + state: Mutex, + freed: Condvar, + most: usize, +} + +struct State { + /// The words the counted makings in flight hold. + held: usize, + /// Those waiting, each by an id of its own, with its rank and words. + waiting: Vec<(u64, Rank, usize)>, + /// The id of the next waiter. + next: u64, +} + +/// The words a making holds, given back when dropped. +pub(crate) struct Words<'a> { + limit: &'a Limit, + words: usize, +} + +impl Drop for Words<'_> { + fn drop(&mut self) { + lock(&self.limit.state).held -= self.words; + self.limit.freed.notify_all(); + } +} + +impl Limit { + /// A limit of `most` words in flight. + pub(crate) fn new(most: usize) -> Limit { + Limit { + state: Mutex::new(State { + held: 0, + waiting: Vec::new(), + next: 0, + }), + freed: Condvar::new(), + most, + } + } + + /// Hold `words` for a making whose stages hold that many, ranked `rank` where it waits: `None` + /// where it does not count ([`COUNTED_WORDS`]); else its words, and how long it waited for + /// them where it did. The wait lends this thread's turn (the module docs). + pub(crate) fn hold(&self, words: usize, rank: Rank) -> Option<(Words<'_>, Option)> { + if words < COUNTED_WORDS { + return None; + } + let mut state = lock(&self.state); + if state.waiting.is_empty() && self.fits(state.held, words) { + state.held += words; + return Some((self.words(words), None)); + } + let id = state.next; + state.next += 1; + state.waiting.push((id, rank, words)); + drop(state); + let asked = Instant::now(); + budget::without_turn(|| self.wait(id)); + Some((self.words(words), Some(asked.elapsed()))) + } + + /// Wait as waiter `id` until it is first among the waiters and fits, then hold its words. + fn wait(&self, id: u64) { + let mut state = lock(&self.state); + loop { + let first = (state.waiting.iter().enumerate()) + .min_by_key(|(_, (at, rank, _))| (*rank, *at)) + .map(|(i, &(at, _, words))| (i, at, words)); + if let Some((i, at, words)) = first { + if at == id && self.fits(state.held, words) { + state.waiting.remove(i); + state.held += words; + drop(state); + // The next waiter may fit too. + self.freed.notify_all(); + return; + } + } + state = self + .freed + .wait(state) + .unwrap_or_else(std::sync::PoisonError::into_inner); + } + } + + /// Whether a making of `words` may go beside makings holding `held`. + fn fits(&self, held: usize, words: usize) -> bool { + held == 0 || held + words <= self.most + } + + fn words(&self, words: usize) -> Words<'_> { + Words { limit: self, words } + } + + /// The words held now and the waiters (the tests). + #[cfg(test)] + fn census(&self) -> (usize, usize) { + let state = lock(&self.state); + (state.held, state.waiting.len()) + } +} + +/// [`Limit::hold`] of the process's limit, ranked by this thread's turn where it holds one, else as +/// a program's making asked now. +pub(crate) fn hold(words: usize) -> Option<(Words<'static>, Option)> { + if words < COUNTED_WORDS { + return None; + } + let rank = budget::held_rank().unwrap_or_else(|| (Urgency::Program, budget::ticket())); + PROCESS + .get_or_init(|| Limit::new(WORDS_IN_FLIGHT)) + .hold(words, rank) +} + +#[cfg(test)] +mod tests { + use super::{Limit, COUNTED_WORDS}; + use crate::pipeline::budget::{self, Budget, Urgency}; + use std::sync::{mpsc, Arc, Mutex}; + use std::time::{Duration, Instant}; + + /// A limit of four counted makings' least words. + const MOST: usize = 4 * COUNTED_WORDS; + + /// Wait until `done` holds, at most a minute (a guard against a hang). + fn until(done: impl Fn() -> bool) { + let deadline = Instant::now() + Duration::from_secs(60); + while !done() { + assert!(Instant::now() < deadline, "timed out"); + std::thread::sleep(Duration::from_millis(5)); + } + } + + /// Hold `words` of `limit` at `rank` on a thread of its own, note `name` in `order` once held, + /// and keep them until `release` is sent. + fn holder( + limit: &Arc, + order: &Arc>>, + (name, words, rank): (&'static str, usize, (Urgency, u64)), + ) -> (std::thread::JoinHandle<()>, mpsc::Sender<()>) { + let (limit, order) = (Arc::clone(limit), Arc::clone(order)); + let (release, released) = mpsc::channel::<()>(); + let thread = std::thread::spawn(move || { + let held = limit.hold(words, rank); + order.lock().unwrap().push(name); + released.recv().unwrap(); + drop(held); + }); + (thread, release) + } + + /// A making below the counted words holds nothing, however much the counted ones in flight + /// hold; a counted one that fits goes at once, without a wait. + #[test] + fn a_making_below_the_counted_words_is_not_held() { + let limit = Limit::new(MOST); + let big = limit.hold(MOST * 2, (Urgency::Program, 0)).unwrap(); + assert!(big.1.is_none(), "alone, a larger making goes at once"); + assert!(limit + .hold(COUNTED_WORDS - 1, (Urgency::Program, 1)) + .is_none()); + assert_eq!(limit.census(), (MOST * 2, 0)); + drop(big); + let (a, waited_a) = limit.hold(COUNTED_WORDS, (Urgency::Ahead, 2)).unwrap(); + let (b, waited_b) = limit.hold(COUNTED_WORDS, (Urgency::Ahead, 3)).unwrap(); + assert!(waited_a.is_none() && waited_b.is_none()); + assert_eq!(limit.census(), (2 * COUNTED_WORDS, 0)); + drop((a, b)); + assert_eq!(limit.census(), (0, 0)); + } + + /// Two makings that do not fit together never hold their words at once: the second waits + /// until the first gives its back, and says how long it waited. + #[test] + fn two_large_makings_never_hold_their_words_at_once() { + let limit = Arc::new(Limit::new(MOST)); + let order = Arc::new(Mutex::new(Vec::new())); + let (first, release_first) = holder(&limit, &order, ("first", MOST, (Urgency::Program, 0))); + until(|| limit.census() == (MOST, 0)); + let (second, release_second) = + holder(&limit, &order, ("second", MOST, (Urgency::Program, 1))); + until(|| limit.census() == (MOST, 1)); + assert_eq!(*order.lock().unwrap(), ["first"]); + release_first.send(()).unwrap(); + first.join().unwrap(); + until(|| order.lock().unwrap().len() == 2); + assert_eq!(limit.census(), (MOST, 0)); + release_second.send(()).unwrap(); + second.join().unwrap(); + assert_eq!(limit.census(), (0, 0)); + } + + /// A smaller making that would fit beside the one in flight does not pass a larger one waiting + /// before it, and the waiters go in rank order, the most urgent first, whatever order they + /// asked in. + #[test] + fn no_counted_making_passes_one_waiting_before_it() { + let limit = Arc::new(Limit::new(MOST)); + let order = Arc::new(Mutex::new(Vec::new())); + let (first, release_first) = holder( + &limit, + &order, + ("in flight", 2 * COUNTED_WORDS, (Urgency::Ahead, 0)), + ); + until(|| limit.census() == (2 * COUNTED_WORDS, 0)); + let (large, release_large) = holder(&limit, &order, ("large", MOST, (Urgency::Ahead, 1))); + until(|| limit.census().1 == 1); + let (small, release_small) = holder( + &limit, + &order, + ("small", COUNTED_WORDS, (Urgency::Ahead, 2)), + ); + until(|| limit.census().1 == 2); + let (urgent, release_urgent) = + holder(&limit, &order, ("committed", MOST, (Urgency::Committed, 3))); + until(|| limit.census().1 == 3); + assert_eq!(*order.lock().unwrap(), ["in flight"]); + release_first.send(()).unwrap(); + first.join().unwrap(); + until(|| order.lock().unwrap().len() == 2); + assert_eq!(*order.lock().unwrap(), ["in flight", "committed"]); + release_urgent.send(()).unwrap(); + urgent.join().unwrap(); + until(|| order.lock().unwrap().len() == 3); + assert_eq!(order.lock().unwrap()[2], "large"); + release_large.send(()).unwrap(); + large.join().unwrap(); + until(|| order.lock().unwrap().len() == 4); + release_small.send(()).unwrap(); + small.join().unwrap(); + assert_eq!( + *order.lock().unwrap(), + ["in flight", "committed", "large", "small"] + ); + assert_eq!(limit.census(), (0, 0)); + } + + /// A making that waits for words gives its turn back for the wait: a making asked for + /// meanwhile takes it, though the budget has that one turn; the waiter takes one again once + /// its words are held. + #[test] + fn a_making_waiting_for_words_lends_its_turn() { + let budget = Arc::new(Budget::new(1, 1)); + let limit = Arc::new(Limit::new(MOST)); + let order = Arc::new(Mutex::new(Vec::new())); + let (first, release_first) = holder(&limit, &order, ("first", MOST, (Urgency::Program, 0))); + until(|| limit.census() == (MOST, 0)); + let (waiting, held) = mpsc::channel(); + let waiter = { + let (budget, limit) = (Arc::clone(&budget), Arc::clone(&limit)); + std::thread::spawn(move || { + let _turn = budget::waited_turn(&budget, budget::ticket()); + let words = limit.hold(MOST, (Urgency::Program, 1)); + assert!(budget::holds_turn(), "its turn is taken again"); + held.send(words.is_some_and(|(_, waited)| waited.is_some())) + .unwrap(); + }) + }; + until(|| limit.census() == (MOST, 1)); + until(|| budget.census() == (0, 0)); + let other = { + let budget = Arc::clone(&budget); + std::thread::spawn(move || drop(budget::waited_turn(&budget, budget::ticket()))) + }; + other.join().unwrap(); + release_first.send(()).unwrap(); + first.join().unwrap(); + assert!(waiting.recv().unwrap(), "the waiter says it waited"); + waiter.join().unwrap(); + until(|| budget.census() == (0, 0)); + assert_eq!(limit.census(), (0, 0)); + } +} diff --git a/crates/metal2vulkan-runtime/src/pipeline/flight.rs b/crates/metal2vulkan-runtime/src/pipeline/flight.rs index d971fb41e..d45daf3f5 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/flight.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/flight.rs @@ -19,8 +19,8 @@ //! once it holds its turn of the process's budget, just before its driver call, and lands when //! that returns ([`Leader`]); a twin waits for it holding no turn: a program's thread before it //! asks for one, a pool's piece by giving its thread's back for the wait -//! (`budget::without_turn`). Every making in the air holds a turn and waits on nothing of ours, -//! so each wait ends. +//! (`budget::without_turn`). Every making in the air holds a turn, or waits for compile memory +//! holding none (`compile_memory.rs`), whose holders wait only for a turn: so each wait ends. //! - **Bounded.** The table holds one key per making in the air, at most the budget's turns. //! - **Counted.** A making that waited for a twin counts in `WorkCounts::flights_followed`. diff --git a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs index 758104aed..cef7f16f3 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs @@ -22,7 +22,7 @@ use super::creation_log; use super::interlocked_writes; use super::kept::Place; use super::libraries::{Linked, Part}; -use super::objects::{RawPipeline, VariantObjects}; +use super::objects::{RawPipeline, StageObject, VariantObjects}; use crate::degradation::BoundFormats; use crate::error::{internal, vulkan, DeadlockCause, Error}; use crate::render_target::{PassShape, RenderPassObject}; @@ -1012,6 +1012,25 @@ pub(crate) fn color_write_masks( } impl VariantObjects { + /// The SPIR-V words of the stages `part`'s library compiles (`compile_memory.rs`): the + /// fragment stage's for the fragment shader part, the others' for the pre-rasterization part. + fn part_words(&self, part: Part) -> usize { + let fragment = |stage: &&StageObject| stage.stage == vk::ShaderStageFlags::FRAGMENT; + match part { + Part::FragmentShader => self + .stages + .iter() + .filter(fragment) + .map(StageObject::words) + .sum(), + Part::PreRasterization => (self.stages.iter()) + .filter(|stage| !fragment(stage)) + .map(StageObject::words) + .sum(), + Part::VertexInput | Part::FragmentOutput => 0, + } + } + /// The state of `pipeline`'s (the variant's compiled pipeline) `VkPipeline` for `key` /// ([`GraphicsContent`]): everything its making takes beside the variant's modules, layout /// and pass. @@ -1298,6 +1317,12 @@ impl VariantObjects { if let Some(at) = awaited { creation_log::note(|| "turn".to_owned(), at.elapsed()); } + // A large pipeline's words of the process's limit, waited for without the turn + // (`compile_memory.rs`); a linked one's libraries hold their own as each is made. + let words = match libraries { + None => shared.compile_memory(self.stages.iter().map(StageObject::words).sum()), + Some(_) => None, + }; let started = std::time::Instant::now(); let (created, withheld) = crate::driver_facts::expecting(expected, || { content.with_infos(self, render_pass.raw, |infos| match libraries { @@ -1357,6 +1382,7 @@ impl VariantObjects { shared.counts.driver_diagnostics_withheld(withheld); let (raw, linked, made_here) = created?; let created_in = started.elapsed(); + drop(words); let kept_at = split.then(std::time::Instant::now); drop(leader); drop(turn); @@ -1458,6 +1484,8 @@ impl VariantObjects { let made = match records.of(part) { Some(record) => { let _ = creation_log::take_record(); + // Its own stages' words (`compile_memory.rs`). + let _words = shared.compile_memory(self.part_words(part)); let made = shared.make_graphics_pipeline( Making::new(part.kind()).digest(record), || self.pipeline_name(Some(part.kind())), diff --git a/crates/metal2vulkan-runtime/src/pipeline/mod.rs b/crates/metal2vulkan-runtime/src/pipeline/mod.rs index b07ce2622..c0f1dc8eb 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/mod.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/mod.rs @@ -41,6 +41,7 @@ mod additions; mod attachment_formats; pub(super) mod budget; +pub(super) mod compile_memory; pub(super) mod content_keyed; pub(super) mod creation_log; mod dispatch_making; diff --git a/crates/metal2vulkan-runtime/src/pipeline/objects.rs b/crates/metal2vulkan-runtime/src/pipeline/objects.rs index 9cf9a95c5..472c8c9eb 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/objects.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/objects.rs @@ -266,6 +266,11 @@ impl StageObject { pub(super) fn module(&self) -> vk::ShaderModule { self.module.raw } + + /// Its module's SPIR-V words (`compile_memory.rs`). + pub(super) fn words(&self) -> usize { + self.module.words + } } /// One `VkShaderModule`: the device's module of its SPIR-V, shared by every variant whose stage @@ -278,6 +283,8 @@ impl StageObject { /// MSL once. pub(crate) struct ShaderModule { raw: vk::ShaderModule, + /// Its SPIR-V's words: what its makings weigh (`compile_memory.rs`). + words: usize, } impl Owned for ShaderModule { @@ -403,7 +410,10 @@ pub(crate) fn shader_module( let info = vk::ShaderModuleCreateInfo::default().code(spirv); // SAFETY: the code is live SPIR-V words for the call. let raw = unsafe { shared.vk().create_shader_module(&info, None) }.map_err(vulkan)?; - Ok::<_, Error>(ShaderModule { raw }) + Ok::<_, Error>(ShaderModule { + raw, + words: spirv.len(), + }) }); let gone = modules.sweep(); drop(modules); @@ -1218,11 +1228,15 @@ impl VariantObjects { if followed { shared.counts.flight_followed(); } + // A large kernel's words of the process's limit, waited for without the turn + // (`compile_memory.rs`). + let words = shared.compile_memory(c.object.words()); let started = std::time::Instant::now(); // With what the driver reports of the making: a pipeline found in its record's cache // reads no data back (`shader_cache/driver.rs`). let raw = shared.make_compute_pipeline(making, || self.pipeline_name(None), info)?; let created_in = started.elapsed(); + drop(words); if let Some(kernel) = &self.logged { creation_log::compute(&self.device, kernel, c.specialization, created_in); } diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index d1044ff04..ee2434d55 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -111,6 +111,8 @@ pub(crate) struct Counters { pipeline_layouts_shared: AtomicU64, variant_pipelines_made: AtomicU64, flights_followed: AtomicU64, + makings_held_for_compile_memory: AtomicU64, + compile_memory_held_nanos: AtomicU64, predicated_loop_runs_again: AtomicU64, variants_taken_back: AtomicU64, unlisted_argument_sampler_buffers: AtomicU64, @@ -2248,6 +2250,17 @@ impl Counters { self.flights_followed.fetch_add(1, Ordering::Relaxed); } + /// A making waited `waited` for the words of the process's limit on large makings in flight + /// (`pipeline/compile_memory.rs`). + pub(crate) fn making_held_for_compile_memory(&self, waited: std::time::Duration) { + self.makings_held_for_compile_memory + .fetch_add(1, Ordering::Relaxed); + self.compile_memory_held_nanos.fetch_add( + u64::try_from(waited.as_nanos()).unwrap_or(u64::MAX), + Ordering::Relaxed, + ); + } + /// A predicated loop's check ran its laying again (`command_buffer/rerun.rs`). pub(crate) fn predicated_loop_ran_again(&self) { self.predicated_loop_runs_again @@ -5474,6 +5487,13 @@ pub struct WorkCounts { /// Of those, the makings that waited for a twin's making in the air, of the same functions /// and specialization, to land before their own (`pipeline/flight.rs`). pub flights_followed: u64, + /// Of those, the makings of a large module that waited for the large makings in flight before + /// their own, so that two never compile at once: each making whose stages hold 65,536 SPIR-V + /// words or more waits while the others in flight hold more than 4,194,304 words with its own + /// (`pipeline/compile_memory.rs`). No Metal counterpart. + pub makings_held_for_compile_memory: u64, + /// The time those makings waited, in nanoseconds, summed. + pub compile_memory_held_nanos: u64, /// The runs again of predicated loops' layings that a check submitted, the loop still running /// past its copies (`command_buffer/rerun.rs`). pub predicated_loop_runs_again: u64, @@ -9375,6 +9395,10 @@ impl Device { shader_cache_off_thread_nanos: directory.off_thread_nanos, shader_cache_writes_on_thread: directory.writes_on_thread, flights_followed: counters.flights_followed.load(Ordering::Relaxed), + makings_held_for_compile_memory: counters + .makings_held_for_compile_memory + .load(Ordering::Relaxed), + compile_memory_held_nanos: counters.compile_memory_held_nanos.load(Ordering::Relaxed), predicated_loop_runs_again: counters.predicated_loop_runs_again.load(Ordering::Relaxed), variants_taken_back: counters.variants_taken_back.load(Ordering::Relaxed), unlisted_argument_sampler_buffers: counters