diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 3f3b43457..82e18e81f 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -339,8 +339,9 @@ pub(super) struct EncoderCore { /// The uses of the command [`note_reaching`](Self::note_reaching) notes, with those of the /// host memory beside them ([`aliased`](Self::aliased)): kept, so a draw makes no vector. aliased_uses: Vec>, - /// The keys its commands' uses named that took no use of the host memory's, while nothing - /// joined, moved or went since (`host_aliases::Quiet`): a command over those looks nothing up. + /// The keys its commands' uses named, each with the uses of the host memory's it takes, while + /// nothing joined, moved or went since (`host_aliases::Quiet`): a command over those takes no + /// lock. aliases_quiet: Quiet, /// The open runs of the index gather pass ([`gather`](Self::gather)), which record before /// the encoder's next command, and before its `VkCommandBuffer` ends. diff --git a/crates/metal2vulkan-runtime/src/host_aliases.rs b/crates/metal2vulkan-runtime/src/host_aliases.rs index 0138fadad..48d79a897 100644 --- a/crates/metal2vulkan-runtime/src/host_aliases.rs +++ b/crates/metal2vulkan-runtime/src/host_aliases.rs @@ -68,8 +68,9 @@ //! bytes overlap, clipped to them ([`HostAliases::augment`]). So two keys over overlapping bytes //! are ordered where they overlap, whatever made them, at a cost of one lookup a use while no two //! ranges meet, and of one use added per range met otherwise. An encoder keeps the keys it -//! found to take no other use ([`Quiet`]): while no buffer joins a memory, is placed, moves or -//! goes, its commands whose uses of bytes name only those look nothing up and take no lock. +//! found, each with what a use of its bytes takes ([`Quiet`]): while no buffer joins a memory, +//! is placed, moves or goes, its commands whose uses of bytes name only those take no lock and +//! work the uses they take out from what it keeps. //! Each range keeps the ranges that meet it, so a use reads them there rather than looking them //! up by address, but where they outnumber the classes of length that lookup visits //! ([`Placements::sharing`]). A remap reported keeps a placement: it orders more than the pages @@ -145,16 +146,115 @@ pub(super) struct HostAliases { inner: Mutex>, } -/// The keys found to take no other use while the host aliases stood at `version` -/// ([`HostAliases::augment_into`]): no buffer joined another's memory under them, and the range -/// of bytes each lies over, if any, holds no other key and meets no other range -/// ([`Placed::crowded`]). An encoder keeps them, so a command whose uses of bytes name only such -/// keys looks nothing up and takes no lock while no buffer joins a memory, is placed, moves or -/// goes; a change of the version forgets them. +/// The keys found while the host aliases stood at `version` ([`HostAliases::augment_into`]), each +/// with the uses a use of its bytes takes then ([`Takes`]): none for a key over which no buffer +/// joined another's memory and whose range of bytes, if any, holds no other key and meets no +/// other range ([`Placed::crowded`]); else its memory's key, its range's first key and the keys of +/// the ranges its own meets, where its range keeps those ([`Placements::sharing`]). An encoder +/// keeps them, so a command whose uses of bytes name only such keys takes no lock and reads +/// nothing the device's other encoders write while no buffer joins a memory, is placed, moves or +/// goes: each use takes the uses it took when its key was found, worked out from what the key +/// keeps (a draw's uses of a buffer placed in a heap over host pages that another range meets, +/// as the consumer's upload chunks lie in its guest RAM, the bytes moving draw after draw); a +/// change of the version forgets them. #[derive(Default)] pub(super) struct Quiet { version: u64, - keys: KeyMap, + keys: KeyMap, +} + +/// What a use of a key's bytes takes while the host aliases stand ([`Quiet`]): a use of its +/// memory's key, where it joined one, and those its range of bytes gives +/// ([`Placements::sharing`]), as [`augmented`] adds them. +struct Takes { + /// The key of the memory it joined. + joined: Option, + /// Its range's, where its range is crowded: none where it takes nothing of it. + range: Option, +} + +/// A crowded range of bytes a key lies over, as its uses read it ([`Placements::sharing`]): its +/// first byte's host address and its length, the key its keys' uses share where it is another, +/// and the ranges it keeps that meet it, each with its key. +struct Kept { + at: (u64, u64), + first: Option, + meets: Vec<((u64, u64), ResourceKey)>, +} + +impl Takes { + /// Whether a use takes nothing. + fn nothing(&self) -> bool { + self.joined.is_none() && self.range.as_ref().is_none_or(Kept::nothing) + } + + /// Add to `add` the uses `used`, a use of `bytes` of the key, takes, in [`augmented`]'s order. + fn add_to( + &self, + used: &Use, + bytes: &std::ops::Range, + add: &mut impl FnMut(Use), + ) { + if let Some(key) = self.joined { + add(Use { + key, + ..used.clone() + }); + } + if let Some(range) = &self.range { + range.share(bytes, |key, shared| { + // The memory's key carries the same bytes already. + if Some(key) != self.joined || shared != *bytes { + add(Use { + key, + range: UseRange::Bytes(shared), + ..used.clone() + }); + } + }); + } + } +} + +impl Kept { + /// Whether a use of its bytes takes nothing of it: its key is its first, and none meets it. + fn nothing(&self) -> bool { + self.first.is_none() && self.meets.is_empty() + } + + /// For a use of `bytes` of a key over it, each other key those bytes share and the bytes of + /// it they are, to `each`, as [`Placements::sharing`] gives them from what a range keeps. + fn share( + &self, + bytes: &std::ops::Range, + mut each: impl FnMut(ResourceKey, std::ops::Range), + ) { + if let Some(first) = self.first { + each(first, bytes.clone()); + } + share_met(self.at, &self.meets, bytes, each); + } +} + +/// For a use of `bytes` (counted from the first byte of `at`, the range of host bytes its key lies +/// over), the ranges of `meets` those bytes meet, each with its key and clipped to it (counted +/// from its first byte), to `each`, in order. +fn share_met( + at: (u64, u64), + meets: &[((u64, u64), ResourceKey)], + bytes: &std::ops::Range, + mut each: impl FnMut(ResourceKey, std::ops::Range), +) { + // The host bytes the use covers, within the key's own. + let start = at.0.saturating_add(bytes.start.min(at.1)); + let end = at.0.saturating_add(bytes.end.min(at.1)); + for &(other, key) in meets { + let from = start.max(other.0); + let to = end.min(other.0.saturating_add(other.1)); + if from < to { + each(key, from - other.0..to - other.0); + } + } } impl Quiet { @@ -168,6 +268,12 @@ impl Quiet { /// Whether `used` takes no other use as it is: it is of no bytes, or of a key found quiet. fn holds(&self, used: &Use) -> bool { + !matches!(used.range, UseRange::Bytes(_)) + || self.keys.get(&used.key).is_some_and(Takes::nothing) + } + + /// Whether what `used` takes is known: it is of no bytes, or of a key found. + fn knows(&self, used: &Use) -> bool { !matches!(used.range, UseRange::Bytes(_)) || self.keys.contains_key(&used.key) } } @@ -486,12 +592,21 @@ impl HostAliases { if uses.iter().all(|used| quiet.holds(used)) { return false; } + // Every key found: the uses each took then, with no lock. + if uses.iter().all(|used| quiet.knows(used)) { + return augmented_known(quiet, uses, all); + } } self.lookups.fetch_add(1, Ordering::Relaxed); let inner = lock(&self.inner); - let aliases = joined.then_some(&*inner.aliases); - let placements = crowded.then_some(&inner.placements); - augmented((aliases, placements), uses, all, quiet) + // What lies under the lock now, not the flags read before it: a key found here holds + // while the version read before them stands, and a change since then forgets it. + augmented( + (Some(&inner.aliases), Some(&inner.placements)), + uses, + all, + quiet, + ) } /// How many times [`augment_into`](Self::augment_into) looked uses up under the lock @@ -518,8 +633,9 @@ fn release(memories: &mut Memories>, over: Over) { /// [`HostAliases::augment_into`] over, where given, `aliases` (the buffers that joined a memory) /// and `placements`, into `all`, empty: the uses added follow `uses`, which go in at the first. -/// Whether any was added. The uses of keys `quiet` holds are passed over, and the keys of the -/// others that take no use, whatever their bytes, join them. +/// Whether any was added. The uses of keys `quiet` knows take what it keeps of them; the others' +/// keys join it with what they take, where their ranges keep what meets them (a range met by more +/// is looked up at each use). pub(super) fn augmented( (aliases, placements): (Option<&KeyMap>, Option<&Placements>), uses: &[Use], @@ -537,39 +653,82 @@ pub(super) fn augmented( let UseRange::Bytes(bytes) = &used.range else { continue; }; - if quiet.as_deref().is_some_and(|quiet| quiet.holds(used)) { + if let Some(takes) = quiet.as_deref().and_then(|quiet| quiet.keys.get(&used.key)) { + takes.add_to(used, bytes, &mut add); continue; } let joined = aliases .and_then(|aliases| aliases.get(&used.key)) .map(|alias| alias.key); - if let Some(key) = joined { - add(Use { - key, - ..used.clone() - }); - } - let shares = placements.is_some_and(|placements| { - placements.sharing(used.key, bytes, |key, range| { - // The memory's key carries the same bytes already. - if Some(key) != joined || range != *bytes { - add(Use { - key, - range: UseRange::Bytes(range), - ..used.clone() + let found = placements.map_or(Found::Alone, |placements| placements.found(used.key)); + let range = match found { + Found::Alone => None, + Found::Kept(range) => Some(range), + // Met by more than its range keeps: looked up by address at each use. + Found::LookedUp => { + let takes = Takes { + joined, + range: None, + }; + takes.add_to(used, bytes, &mut add); + if let Some(placements) = placements { + placements.sharing(used.key, bytes, |key, range| { + if Some(key) != joined || range != *bytes { + add(Use { + key, + range: UseRange::Bytes(range), + ..used.clone() + }); + } }); } - }) - }); - if joined.is_none() && !shares { - if let Some(quiet) = quiet.as_deref_mut() { - quiet.keys.insert(used.key, ()); + continue; } + }; + let takes = Takes { joined, range }; + takes.add_to(used, bytes, &mut add); + if let Some(quiet) = quiet.as_deref_mut() { + quiet.keys.insert(used.key, takes); } } !all.is_empty() } +/// [`augmented`] of `uses`, whose keys `quiet` knows every one of, into `all`, empty: whether any +/// was added. +fn augmented_known( + quiet: &Quiet, + uses: &[Use], + all: &mut Vec>, +) -> bool { + let mut add = |added: Use| { + if all.is_empty() { + all.reserve(2 * uses.len()); + all.extend_from_slice(uses); + } + all.push(added); + }; + for used in uses { + let UseRange::Bytes(bytes) = &used.range else { + continue; + }; + if let Some(takes) = quiet.keys.get(&used.key) { + takes.add_to(used, bytes, &mut add); + } + } + !all.is_empty() +} + +/// What a use of a key's bytes takes of its range ([`Placements::found`]). +enum Found { + /// Nothing: it lies over no bytes, or over a range that is not crowded. + Alone, + /// What its crowded range keeps. + Kept(Kept), + /// Its range is met by more ranges than it keeps: its uses look those up by address. + LookedUp, +} + /// The bytes each buffer over host pages lies over now, by key, and the ranges of bytes some key /// lies over, each with the key its keys' uses share: the first placed there, while any key lies /// there (the module docs). @@ -730,33 +889,51 @@ impl Placements { if own.meets.is_empty() { return true; } - // The host bytes the use covers, within the key's own. - let start = at.0.saturating_add(bytes.start.min(at.1)); - let end = at.0.saturating_add(bytes.end.min(at.1)); - let mut clipped = |other: (u64, u64), key: ResourceKey| { - let from = start.max(other.0); - let to = end.min(other.0.saturating_add(other.1)); - if from < to { - each(key, from - other.0..to - other.0); - } - }; if own.meets.len() <= self.ranges.held_classes() { - for &(other, key) in &own.meets { - clipped(other, key); - } + share_met(at, &own.meets, bytes, each); return true; } self.looked_up.set(self.looked_up.get() + 1); + // The host bytes the use covers, within the key's own. + let start = at.0.saturating_add(bytes.start.min(at.1)); + let end = at.0.saturating_add(bytes.end.min(at.1)); for other in self.ranges.overlapping(start, end) { if other == at { continue; } if let Some(range) = self.ranges.get(other) { - clipped(other, range.key); + let from = start.max(other.0); + let to = end.min(other.0.saturating_add(other.1)); + if from < to { + each(range.key, from - other.0..to - other.0); + } } } true } + + /// What a use of `key`'s bytes takes of the range it lies over, whatever the bytes, as + /// [`sharing`](Self::sharing) gives it: nothing, what its range keeps, or a lookup by + /// address at each use. + fn found(&self, key: ResourceKey) -> Found { + let Some(&at) = self.at.get(&key) else { + return Found::Alone; + }; + let Some(own) = self.ranges.get(at) else { + return Found::Alone; + }; + if !own.crowded() { + return Found::Alone; + } + if own.meets.len() > self.ranges.held_classes() { + return Found::LookedUp; + } + Found::Kept(Kept { + at, + first: (own.key != key).then_some(own.key), + meets: own.meets.clone(), + }) + } } /// Values by the host bytes they are over (their first byte's address and their length), found diff --git a/crates/metal2vulkan-runtime/src/tests/host_aliases.rs b/crates/metal2vulkan-runtime/src/tests/host_aliases.rs index 3f1c59e71..52b7c2382 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_aliases.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_aliases.rs @@ -788,11 +788,10 @@ fn the_version_counts_each_change_augment_reads() { changed("forgotten"); } -/// An encoder's commands whose uses of bytes name only keys an earlier one found to take no other -/// use look nothing up (`host_aliases::Quiet`, `WorkCounts::host_alias_lookups`), while a key -/// that takes one is looked up at each command; a buffer placed over bytes a quiet key lies -/// over, or a quiet key joining another's memory, between two commands makes the second carry -/// the use it takes now. +/// An encoder's commands whose uses of bytes name only keys an earlier one found look nothing up +/// (`host_aliases::Quiet`, `WorkCounts::host_alias_lookups`), a key that takes no other use and +/// one that takes some alike; a buffer placed over bytes a quiet key lies over, or a quiet key +/// joining another's memory, between two commands makes the second carry the use it takes now. #[test] fn a_quiet_key_is_looked_up_again_once_a_buffer_moves_or_joins() { let aliases = Aliases::default(); @@ -818,17 +817,19 @@ fn a_quiet_key_is_looked_up_again_once_a_buffer_moves_or_joins() { assert_eq!(augment(&used), None); } assert_eq!(aliases.lookups(), 1, "a quiet key is looked up once"); - let crowded = [write(second, 0..0x40)]; - for _ in 0..3 { + // A key that takes a use is looked up once too, and its uses at other bytes take the same + // key's, at their own bytes, from what the encoder kept of it. + for bytes in [0..0x40, 0x40..0x80, 0..0x40] { + let crowded = [write(second, bytes.clone())]; assert_eq!( augment(&crowded), - Some(vec![crowded[0].clone(), write(first, 0..0x40)]) + Some(vec![crowded[0].clone(), write(first, bytes)]) ); } assert_eq!( aliases.lookups(), - 4, - "a key that takes a use is looked up each time" + 2, + "a key that takes a use is looked up once" ); // A buffer placed over the second half of the lone key's bytes. aliases.place(moved, (0x40_0800, 0x1000)); @@ -850,3 +851,74 @@ fn a_quiet_key_is_looked_up_again_once_a_buffer_moves_or_joins() { Some(vec![joined[0].clone(), write(owner, 0..0x40)]) ); } + +/// Keys placed, rebound, removed and joined at random, and uses of random bytes of them: an +/// encoder that keeps what it found of each key (`host_aliases::Quiet`) carries exactly the uses +/// a lookup of every use under the lock gives (`HostAliases::augment`), and looks a command up +/// again only once something changed since or a key is new to it, or its range is met by more +/// ranges than it keeps. +#[test] +fn what_an_encoder_keeps_of_a_key_takes_what_a_lookup_takes() { + const PAGE: u64 = 0x1000; + const BASE: u64 = 0x7000_0000_0000; + let mut state = 0x9e37_79b9_7f4a_7c15_u64; + let mut next = move |below: u64| { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + state % below + }; + let aliases = Aliases::default(); + let keys: Vec = (0..24).map(|_| ResourceKey::new()).collect(); + let mut at: Vec> = vec![None; keys.len()]; + let mut quiet = Quiet::default(); + let mut all = Vec::new(); + let (mut kept, mut found) = (0, 0); + for round in 0..6000 { + let index = next(keys.len() as u64) as usize; + let key = keys[index]; + match next(16) { + 0 => { + at[index] = None; + aliases.unplace(key); + } + 1 => { + let length = (1 + next(1 << next(6))) * PAGE; + let place = (BASE + next(96) * PAGE, length); + at[index] = Some(place); + aliases.place(key, place); + } + 2 => { + // Over the same bytes as another live key's: it joins that one's memory. + let other = next(keys.len() as u64) as usize; + if let Some(memory) = at[other] { + join(&aliases, key, memory); + } + } + _ => {} + } + // A command of one or two uses, of keys placed or not. + let mut uses = Vec::new(); + for _ in 0..1 + next(2) { + let index = next(keys.len() as u64) as usize; + let length = at[index].map_or(PAGE, |at| at.1); + let start = next(length + PAGE); + let bytes = start..start + 1 + next(length + PAGE); + uses.push(match next(2) { + 0 => write(keys[index], bytes), + _ => sample(keys[index], bytes), + }); + } + let looked = aliases.augment(&uses); + let before = aliases.lookups(); + let took = aliases + .augment_into(&uses, &mut all, Some(&mut quiet)) + .then(|| all.clone()); + assert_eq!(took, looked, "round {round}: {uses:?}"); + match aliases.lookups() - before { + 0 => kept += 1, + _ => found += 1, + } + } + assert!(kept > 1000 && found > 1000, "kept {kept}, found {found}"); +}