diff --git a/crates/metal2vulkan-runtime/src/device/prime.rs b/crates/metal2vulkan-runtime/src/device/prime.rs index 4caf3f73d..41f9a68c2 100644 --- a/crates/metal2vulkan-runtime/src/device/prime.rs +++ b/crates/metal2vulkan-runtime/src/device/prime.rs @@ -73,7 +73,7 @@ use crate::draw_source::SizedIndexBind; use crate::dynamic_state::{ColorWriteMasks, ExtendedDynamicState}; use crate::error::{vulkan, Error}; -use crate::exit_guard::Call; +use crate::exit_guard::{Call, Calls}; use crate::generated_commands::{ Commands, ExecutionSetCreateInfo, ExecutionSetPipelineInfo, ExecutionSetToken, LayoutCreateInfo, LayoutToken, EXECUTION_SET_PIPELINES, INDIRECT_BINDABLE, TOKEN_DISPATCH, @@ -441,7 +441,7 @@ pub(super) unsafe fn prime( drawn, // SAFETY: the caller's. memory: unsafe { instance.get_physical_device_memory_properties(physical) }, - calls: 0, + calls: Calls::NONE, held: Vec::new(), }; // SAFETY: the caller's. @@ -487,12 +487,12 @@ pub(super) unsafe fn prime( /// [`COLOR_WRITE_MASK_CALLS`] and [`SIZED_INDEX_BIND_CALLS`] where it has /// not their commands, and those of [`YCBCR_CONVERSION_CALLS`] where it converts no YCbCr format, /// that `calls` (one bit each) does not hold. -pub(crate) fn missing(calls: u128, has: Has) -> Vec { +pub(crate) fn missing(calls: Calls, has: Has) -> Vec { Call::ALL .iter() .copied() .filter(|&call| { - calls & call.bit() == 0 + !calls.holds(call) && !UNPRIMED.contains(&call) && (has.timestamps || !TIMESTAMP_CALLS.contains(&call)) && ((has.timestamps && has.host_query_reset) @@ -545,7 +545,7 @@ struct Primer<'a> { drawn: PrimedDraws, memory: vk::PhysicalDeviceMemoryProperties, /// The table's entry points called, one bit each. - calls: u128, + calls: Calls, /// What was made, released last first. held: Vec, } @@ -553,7 +553,7 @@ struct Primer<'a> { impl Primer<'_> { /// `call` was made. fn made(&mut self, call: Call) { - self.calls |= call.bit(); + self.calls = self.calls.with(call); } /// The command buffer `vkAllocateCommandBuffers` allocates of `info`, the device's loader diff --git a/crates/metal2vulkan-runtime/src/exit_guard.rs b/crates/metal2vulkan-runtime/src/exit_guard.rs index 1f2dc8fc8..112649c96 100644 --- a/crates/metal2vulkan-runtime/src/exit_guard.rs +++ b/crates/metal2vulkan-runtime/src/exit_guard.rs @@ -338,33 +338,75 @@ static SECTIONS: AtomicU64 = AtomicU64::new(0); /// The entry points ([`Call`]) called in sections that ended since the hook was last registered /// at a device after a load of a shared object ([`register_device`], they were the process's), one /// bit each. -static CALLED: CalledWord = CalledWord([AtomicU64::new(0), AtomicU64::new(0)]); +static CALLED: CalledWords = CalledWords([const { AtomicU64::new(0) }; CALL_WORDS]); -/// One bit per entry point of the table ([`Call::bit`]), as two atomic halves: the table -/// outgrew 64 bits with the render host step's entry points. -struct CalledWord([AtomicU64; 2]); +/// A set of [`Calls`] as atomic words, one bit per entry point of the table ([`Call::bit`]). +struct CalledWords([AtomicU64; CALL_WORDS]); -impl CalledWord { +impl CalledWords { /// Set `calls`; the bits set before. - fn fetch_or(&self, calls: u128) -> u128 { - let low = self.0[0].fetch_or(calls as u64, Ordering::Relaxed); - let high = self.0[1].fetch_or((calls >> 64) as u64, Ordering::Relaxed); - u128::from(low) | (u128::from(high) << 64) + fn fetch_or(&self, calls: Calls) -> Calls { + Calls(std::array::from_fn(|at| { + self.0[at].fetch_or(calls.0[at], Ordering::Relaxed) + })) } /// Clear every bit: a load of a shared object since (a driver or layer loaded again makes /// its statics again at each entry point's first call). fn clear(&self) { - self.0[0].store(0, Ordering::Relaxed); - self.0[1].store(0, Ordering::Relaxed); + for word in &self.0 { + word.store(0, Ordering::Relaxed); + } } /// The bits set. #[cfg(test)] - fn load(&self) -> u128 { - let low = self.0[0].load(Ordering::Relaxed); - let high = self.0[1].load(Ordering::Relaxed); - u128::from(low) | (u128::from(high) << 64) + fn load(&self) -> Calls { + Calls(std::array::from_fn(|at| self.0[at].load(Ordering::Relaxed))) + } +} + +/// The words of 64 bits a set of the table's entry points takes ([`Calls`]): as many as the +/// table needs, so it grows with the table. +const CALL_WORDS: usize = Call::ALL.len().div_ceil(64); + +/// A set of the table's entry points, one bit each ([`Call::bit`]). +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) struct Calls([u64; CALL_WORDS]); + +impl Calls { + /// No entry point. + pub(crate) const NONE: Calls = Calls([0; CALL_WORDS]); + + /// It and `call`. + pub(crate) fn with(self, call: Call) -> Calls { + let (at, bit) = call.bit(); + let mut words = self.0; + words[at] |= bit; + Calls(words) + } + + /// Whether it holds `call`. + pub(crate) fn holds(self, call: Call) -> bool { + let (at, bit) = call.bit(); + self.0[at] & bit != 0 + } + + /// It without `call`. + #[cfg(test)] + pub(crate) fn without(self, call: Call) -> Calls { + let (at, bit) = call.bit(); + let mut words = self.0; + words[at] &= !bit; + Calls(words) + } + + /// Whether it holds an entry point `other` does not. + fn beyond(self, other: Calls) -> bool { + self.0 + .iter() + .zip(other.0) + .any(|(&own, other)| own & !other != 0) } } @@ -685,11 +727,10 @@ pub(crate) const HOST_STEP: &[Call] = &[ ]; impl Call { - /// Its bit. The table fills 113 of the word's 128 (widened from 64): when it next outgrows the - /// word, widen the word (here, [`CALLED`], `PENDING` and the priming's calls); do not add a - /// second table. - pub(crate) fn bit(self) -> u128 { - 1 << self as u32 + /// Its word of a set ([`Calls`]) and its bit in that word. + fn bit(self) -> (usize, u64) { + let at = self as usize; + (at / 64, 1 << (at % 64)) } } @@ -697,7 +738,7 @@ thread_local! { /// The sections in progress on this thread (of every guard). static OWN: Cell = const { Cell::new(0) }; /// The entry points ([`Call`]) this thread called since its last section ended. - static PENDING: Cell = const { Cell::new(0) }; + static PENDING: Cell = const { Cell::new(Calls::NONE) }; /// This thread is one of the runtime's own ([`spawn`]). static RUNTIME: Cell = const { Cell::new(false) }; /// The program's code this thread is running ([`program`]). @@ -930,10 +971,10 @@ impl Drop for Busy<'_> { // After the section's Vulkan calls, which may have registered a destructor. let calls = PENDING.with(|pending| { let calls = pending.get(); - pending.set(0); + pending.set(Calls::NONE); calls }); - let first = calls & !CALLED.fetch_or(calls) != 0; + let first = calls.beyond(CALLED.fetch_or(calls)); let ended = SECTIONS.fetch_add(1, Ordering::Relaxed) + 1; if (first || ended.is_power_of_two()) && !self.guard.is_closed() { register_again(); @@ -1036,7 +1077,7 @@ pub(crate) fn pipelines_made() { /// This thread called `call`; the section it is in registers the hook again as it ends if no /// section that ended had called it. pub(crate) fn called(call: Call) { - PENDING.with(|pending| pending.set(pending.get() | call.bit())); + PENDING.with(|pending| pending.set(pending.get().with(call))); } /// Whether the process is exiting: its guard closed. @@ -1785,5 +1826,5 @@ pub(crate) fn sections() -> u64 { /// Whether a section that ended called `call`. #[cfg(test)] pub(crate) fn was_called(call: Call) -> bool { - CALLED.load() & call.bit() != 0 + CALLED.load().holds(call) } diff --git a/crates/metal2vulkan-runtime/src/tests/exit_guard.rs b/crates/metal2vulkan-runtime/src/tests/exit_guard.rs index a270b5102..5ec186d45 100644 --- a/crates/metal2vulkan-runtime/src/tests/exit_guard.rs +++ b/crates/metal2vulkan-runtime/src/tests/exit_guard.rs @@ -6,7 +6,7 @@ //! the hook learns it (Linux). Each test uses a guard of its own; only the fork tests use the //! process's, which nothing here closes. -use crate::exit_guard::{self, hook, Busy, Call, Guard, InProcess, InProcessDevices}; +use crate::exit_guard::{self, hook, Busy, Call, Calls, Guard, InProcess, InProcessDevices}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{mpsc, Arc, Barrier, Mutex, Weak}; use std::time::{Duration, Instant}; @@ -675,12 +675,15 @@ fn a_forked_child_s_exit_does_not_wait_for_its_parent_s_sections() { /// Each entry point has a bit of its own. #[test] fn every_entry_point_has_a_bit_of_its_own() { - let mut bits = 0_u128; for &call in Call::ALL { - let bit = call.bit(); - assert_eq!(bit.count_ones(), 1, "{call:?} has no bit of the word"); - assert_eq!(bits & bit, 0, "{call:?} shares a bit"); - bits |= bit; + let alone = Calls::NONE.with(call); + let held: Vec = Call::ALL + .iter() + .copied() + .filter(|&each| alone.holds(each)) + .collect(); + assert_eq!(held, [call], "{call:?} shares a bit"); + assert!(!Calls::NONE.with(call).without(call).holds(call)); } } @@ -2429,26 +2432,28 @@ fn the_priming_check_names_what_was_not_made() { sized_index_bind: true, ycbcr_conversion: true, }; - let all = Call::ALL.iter().fold(0, |bits, call| bits | call.bit()); + let all = Call::ALL + .iter() + .fold(Calls::NONE, |bits, &call| bits.with(call)); assert!(missing(all, has(true, true)).is_empty()); let made = Call::ALL .iter() .filter(|call| !UNPRIMED.contains(call)) - .fold(0, |bits, call| bits | call.bit()); + .fold(Calls::NONE, |bits, &call| bits.with(call)); assert!(missing(made, has(true, true)).is_empty()); assert_eq!( - missing(0, has(true, true)).len(), + missing(Calls::NONE, has(true, true)).len(), Call::ALL.len() - UNPRIMED.len() ); for &call in Call::ALL { if !UNPRIMED.contains(&call) { - assert_eq!(missing(made & !call.bit(), has(true, true)), [call]); + assert_eq!(missing(made.without(call), has(true, true)), [call]); } } // Without timestamps, their commands are neither made nor missed, nor is their reset on the // host. let untimed = (TIMESTAMP_CALLS.iter().chain(&HOST_QUERY_RESET_CALLS)) - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); assert!(missing(untimed, has(false, true)).is_empty()); let mut timed = TIMESTAMP_CALLS.to_vec(); timed.extend(HOST_QUERY_RESET_CALLS); @@ -2457,7 +2462,7 @@ fn the_priming_check_names_what_was_not_made() { // Without `hostQueryReset`, nor is the reset on the host. let unreset = HOST_QUERY_RESET_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { host_query_reset: false, ..has(true, true) @@ -2467,13 +2472,13 @@ fn the_priming_check_names_what_was_not_made() { // Without device addresses, nor is a buffer's address. let unaddressed = DEVICE_ADDRESS_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); assert!(missing(unaddressed, has(true, false)).is_empty()); assert_eq!(missing(unaddressed, has(true, true)), DEVICE_ADDRESS_CALLS); // Without generated commands, nor is the release of their objects. let ungenerated = GENERATED_COMMANDS_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { generated_commands: false, ..has(true, true) @@ -2486,7 +2491,7 @@ fn the_priming_check_names_what_was_not_made() { // Without sparse binding, nor is a batch's bind. let unbound = SPARSE_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { sparse_binding: false, ..has(true, true) @@ -2496,7 +2501,7 @@ fn the_priming_check_names_what_was_not_made() { // Without the push command, nor is a host step's push. let unpushed = PUSH_DESCRIPTOR_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { push_descriptor: false, ..has(true, true) @@ -2506,7 +2511,7 @@ fn the_priming_check_names_what_was_not_made() { // Without calibrated timestamps, nor is a completion turn's calibration reading. let uncalibrated = CALIBRATION_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { calibration: false, ..has(true, true) @@ -2516,7 +2521,7 @@ fn the_priming_check_names_what_was_not_made() { // Without messages reported, nor is a pipeline's debug name. let unnamed = NAMING_CALLS .iter() - .fold(made, |bits, call| bits & !call.bit()); + .fold(made, |bits, &call| bits.without(call)); let without = Has { object_names: false, ..has(true, true) @@ -2548,7 +2553,7 @@ fn the_priming_check_names_what_was_not_made() { }), ]; for (calls, without) in groups { - let unmade = calls.iter().fold(made, |bits, call| bits & !call.bit()); + let unmade = calls.iter().fold(made, |bits, &call| bits.without(call)); assert!(missing(unmade, without(has(true, true))).is_empty()); assert_eq!(missing(unmade, has(true, true)), calls); }