Files
metal2vulkan/crates/metal2vulkan-runtime/src/indirect_execute/kept.rs
T
Anees Iqbal 45ca084034 Record a step's chunks apart at an inline step's cost: carry what names itself from chunk to chunk, and note the resident uses once
The host step that first finds a page's records unchanged records its chunks apart to keep them
(render/executions/kept.rs). Each chunk's encoder started from nothing but the bind state, so
each worked out again what an execution recorded inline works out once: on the replayed_textured
page (5001 commands, 20 chunks, 5001 textures and 15018 buffers resident) that step took
host_step_ms 82-85 where an inline step takes 27-31. Per chunk:
- the heap parts (draw_heap_parts_made 40 against 2), let go as each chunk's encoder ended;
- the instance's gathering of the 5002 uses of what the encoder made resident
  (resident_uses_gathered 100044 against 5006);
- the chunk core's note of those uses, then its summary, its kept effects and the pass's core
  taking them;
- the resident textures' shadow copies and those a carrier holds (draw_resident_textures_looked_at
  21 against 2), and the heaps of the resident resources.

Now each chunk's encoder starts from what the one before left (kept.rs's Carried), each piece kept
by what names it, so a chunk's draw takes it as the next draw of one encoder does:
- LiveBind::end_chunk keeps the heap parts (HeapOf::holds compares what each is made of) and the
  shadows' copies listed (ResidentShadows, keyed by the residency's generation and the usage
  shadows made);
- the instance's gathering (InstanceShadows, by the identity of the resident uses), the
  unreadable textures found (ResidentUnreadable, by generation) and the unwritten facts
  (RenderLowering::resident_again, by identity) go to the next chunk's encoder;
- Residency::resident_heaps finds the resident heaps once a generation.
The chunks' cores (EncoderCore::capture_apart) note their draws' uses past those of what the
encoder made resident; those go in a ResidentPart, made once a step, which the pass's core notes
once (EncoderCore::absorb), and a kept chunk holds it so a command buffer that runs the kept
chunks again notes it once for them (EncoderCore::run_kept), not once a chunk.

Verified on the M3 Max (MoltenVK 1.4.2), release, the replayed_textured page, 2 rounds of 3 frames
in one job, interleaved, b960f51b05 (this branch's base before commit 1, the code of bfcfa638ee:
every step inline) against this tree:
- frame 1, the step that records the chunks apart: host_step_ms 29.1/28.3 now, 27.4/25.7 inline;
  encoding to completion 60.7/59.3 ms now, 57.9/56.7 inline. resident_uses_gathered 5006 and
  draw_heap_parts_made 2, as inline. At 791b4fbf58 (another job, 4 frames) that step took
  host_step_ms 82.4/84.5 and 112.3/116.6 ms to completion, gathering 100044 uses and making 40
  heap parts.
- frame 2, the first kept step: host_step_ms 3.6/3.5 now, 25.7/24.3 inline (7.4/7.2 at
  791b4fbf58); encoding to completion 33.9/33.8 ms now, 57.6/57.2 inline. resident_uses_gathered
  4, draw_heap_parts_made 0: a kept step's run notes the resident part once, not once a chunk.
- A step that records apart at every frame (a temporary case, since removed, that swaps one
  texture every other frame; a build of this change before Residency's heaps cache went behind an
  Arc, a layout change for clippy's large_enum_variant): host_step_ms 31-33 at each of 8 frames,
  against 85-92 at 791b4fbf58 and 51-53 with the heap parts alone kept.

New checks:
- runtime tests::kept_heap_uses now also compares the step that records the chunks apart: its
  planned uses equal the inline step's, as the kept step's do.
- lane kept_textured: the step that records apart gathers, notes, works heap parts out and looks
  at the resident textures no more often than the inline step before it (steady()).

Negative controls, each fails its test on the M3:
- the gathering not carried: kept_textured's keeping test (frame 1 gathers 1030 uses,
  frame 0 517);
- each chunk's core noting the resident uses itself: the same test (frame 1 notes 1030, frame 0
  517);
- the heap parts let go at each chunk: the same test (frame 1 makes 4 heap parts, frame 0 2);
- the unreadable textures found again at each chunk: the same test (frame 1 looks at
  3 resident textures, frame 0 2);
- absorb not noting the resident part: tests::kept_heap_uses (frame 1 lacks the 512 textures);
- run_kept not noting it: tests::kept_heap_uses (frame 2 lacks the 512 textures).

Checks run on the M3 on this tree: clippy -D warnings with --cfg m2v_gpu over the runtime and
validation crates (all targets, all features); the runtime suite (1660 passed); the residency,
draw and notice lanes (248 passed); the kept, step and replayed lanes (62 passed); the frame-cost
lanes in debug and release (4 and 3 passed); the API signature table (unchanged). The earlier
controls still fail their tests: the old plain rule, run_kept noting none of the recording's uses
(now 520 uses lacking at frame 2), RenderKey's samplers and the resident key left out, commit 1's
hand-back dropped.

(cherry picked from commit df484df0fe15e38b4c459fe1653baeb066062209)
2026-11-05 11:25:48 -08:00

233 lines
11 KiB
Rust

//! An execution of compute commands the CPU encoded, recorded chunk by chunk so a later one runs
//! the chunks no write reached again (`kept_commands.rs`).
//!
//! - **Which executions.** One `commit` records from the CPU's records (not the host step), of a
//! range in the call, outside a block of command predication, with no check of the caller's
//! that may see something among its commands (one whose needs rule out what the buffer's
//! records may hold calls nothing at `commit` either, `Execute::check_sees`), no inherited
//! table that binds anything, in a command buffer with no executed-commands handler that is
//! not encoded again after its commit; no chunk of its range last recorded otherwise
//! than plainly; and a chunk of its range run before, no write reaching it since
//! (`kept_commands.rs`, **From the second execution**): a range's first execution records as
//! one that keeps nothing.
//! - **The key.** A chunk is recorded from its records and from the inputs [`ComputeKey`] holds:
//! the inherited pipeline state, whether the encoder runs its dispatches in encoded order, and
//! the resources made resident (`Residency::kept_key`, one list an execution, compared whole
//! once a list its kept chunks hold: `kept_commands::ResidentMet`). A kept chunk runs again only
//! under an equal key.
//! - **Each chunk** runs its kept recording again, or records apart into a primary of its own
//! (from a context of its own where an execution ran it before, no write reaching it since,
//! else from its command buffer's own, kept by none), as a segment of its own
//! (`EncoderCore::segment`): after a barrier from everything before,
//! and with one to everything after, so it orders against nothing else of the execution's, as a
//! command's barrier at its start would. Its recording is plain where it noted no shadowed
//! texture's work, named no detached buffer, named no released state, recorded into one command
//! buffer, held only shared objects, and reached nothing its command buffer alone holds
//! (`CommandBuffer::own_reached`); a plain chunk is kept, any other noted, so later executions
//! of it record as before until a write reaches it. The execution's segment is its chunks', in
//! order.
use super::{Execute, ExecutionRange};
use crate::command_buffer::CommandBuffer;
use crate::encoder::{EncoderCore, RecordedSegment};
use crate::error::Error;
use crate::indirect_command_buffer::Reader;
use crate::kept_commands::{
self, Kept, KeptChunk, KeptContext, KeptEffects, ResidentKey, ResidentMet,
};
use crate::pipeline::{BufferForms, ComputePipelineState};
use crate::residency::Residency;
use metal2vulkan::bind::indirect_commands::BufferStage;
use metal2vulkan::bind::ReplayFaults;
use std::ops::Range;
use std::sync::atomic::Ordering;
use std::sync::Arc;
/// What a chunk of an execution's dispatches is recorded from beside its records (the module
/// docs).
#[derive(Clone, PartialEq)]
pub(super) struct ComputeKey {
pipeline: Option<ComputePipelineState>,
ordered: bool,
resident: ResidentKey,
}
/// An execution that keeps its commands (the module docs): its key, what is kept of its range's
/// chunks in order, and the CPU's writes of the buffer as they were looked up.
pub(super) struct Keeping {
key: ComputeKey,
kept: Vec<Kept>,
read_at: u64,
}
impl Execute {
/// Whether the execution keeps its commands (the module docs), recorded by `command_buffer`
/// against `residency`: its key and its chunks.
pub(super) fn keeping(
&self,
command_buffer: &CommandBuffer,
residency: &Residency,
) -> Option<Keeping> {
let ExecutionRange::Fixed(range) = &self.range else {
return None;
};
let inherited = self.icb.descriptor().inherit_buffers;
let keeps = !range.is_empty()
&& !self.icb.gpu_written()
&& !self.check_sees()
&& self.gate.is_none()
&& self.preprocess_gate.is_none()
&& (!inherited || self.table.binds_nothing())
&& !crate::replay::is_replaying()
&& !command_buffer.reports_executed();
if !keeps {
return None;
}
let (kept, read_at) = self.icb.kept_chunks(range.clone())?;
let key = ComputeKey {
pipeline: self.pipeline.clone(),
ordered: self.ordered,
resident: ResidentKey::new(residency.kept_key()),
};
Some(Keeping { key, kept, read_at })
}
/// Record the commands chunk by chunk into a segment of `command_buffer` (the module docs),
/// resolving against `residency`, the command buffer's: `record`'s, where the execution keeps
/// its commands (`keeping`).
pub(super) fn record_kept(
&self,
command_buffer: &CommandBuffer,
residency: &Residency,
keeping: Keeping,
) -> Result<RecordedSegment, Error> {
let (range, faults) = self.range()?;
let Keeping {
mut key,
kept,
read_at,
} = keeping;
// Counted before the records are read: a write meanwhile records it again.
self.read_at.store(read_at, Ordering::Release);
let counts = &command_buffer.device().shared().counts;
let mut met = ResidentMet::default();
let mut parts = Vec::new();
for ((chunk, covered), kept) in kept_commands::chunks(range).zip(kept) {
let (kept, keeps): (Option<Arc<KeptChunk>>, bool) = match kept {
Kept::Recording(kept) => (Some(kept), true),
Kept::Again => (None, true),
Kept::First => (None, false),
};
let again = kept.and_then(|kept| {
let resident = &kept.key::<ComputeKey>()?.resident;
if !met.same(&mut key.resident, resident, counts) {
return None;
}
let held = kept.runs_again(command_buffer, (&key, &covered), residency)?;
Some((kept, held))
});
if let Some((kept, held)) = again {
let mut core = EncoderCore::capture(command_buffer);
core.run_kept(&kept.effects, held)?;
let raw = kept.raw;
let misses = kept.misses;
core.retain_shared(kept);
let mut part = core.finish_capture()?.into_segment(vec![raw])?;
part.resident_misses = misses;
counts.indirect_chunk_kept(u64::from(covered.end - covered.start));
parts.push(part);
continue;
}
// A chunk's first execution records from its command buffer's own.
let context = match keeps {
true => Some(KeptContext::new(command_buffer)?),
false => None,
};
let reached = command_buffer.own_reached();
if let Some(context) = &context {
command_buffer.lend_kept(Some(context));
}
let apart = self.record_apart(command_buffer, residency, covered.clone());
if context.is_some() {
command_buffer.lend_kept(None);
}
let (mut part, resolved, (moved, plain)) = apart?;
let held = part.retained.all_shared();
let keeps = plain && command_buffer.own_reached() == reached && part.raws.len() == 1;
// A first execution's chunk that would not be kept is noted as one that is not.
let Some(context) = context else {
if !keeps || held.is_none() {
self.icb.keep_chunk(chunk, None, read_at);
}
parts.push(part);
continue;
};
match (keeps, held) {
(true, Some(held)) => {
let effects = KeptEffects::new(
(part.summary.clone(), None),
(part.keys.clone(), part.taken.named().clone()),
(&held, &[]),
part.executions.clone(),
);
let kept = KeptChunk::new(
(Arc::clone(&context), part.raws[0]),
covered,
Box::new(key.clone()),
(resolved, moved),
effects,
(part.resident_misses, ReplayFaults::default()),
);
self.icb.keep_chunk(chunk, Some(Arc::new(kept)), read_at);
}
(true, None) | (false, _) => self.icb.keep_chunk(chunk, None, read_at),
}
part.retained.shared(context);
parts.push(part);
}
let mut parts = parts.into_iter();
let mut segment = parts.next().unwrap_or_else(EncoderCore::unrecorded_segment);
for part in parts {
segment.join(part);
}
segment.faults = ReplayFaults(segment.faults.0 | faults.0);
Ok(segment)
}
/// Record commands `covered` apart (the module docs) into a segment of `command_buffer`,
/// resolving against `residency`: the segment, the addresses they named and what they
/// resolved to, the no-copy buffers' moves before it read its records, and whether it is
/// plain but for what its command buffer alone holds.
fn record_apart(
&self,
command_buffer: &CommandBuffer,
residency: &Residency,
covered: Range<u32>,
) -> Result<(RecordedSegment, Vec<kept_commands::Resolution>, (u64, bool)), Error> {
let moved = crate::buffer::host_cells_moved();
let (commands, released) = self.icb.records(covered, Reader::Commit)?;
self.icb.degrade_released(&released);
let descriptor = *self.icb.descriptor();
let addresses = commands
.iter()
.filter(|_| !descriptor.inherit_buffers)
.flat_map(|(record, _)| {
let bindings = record.bindings.buffers(BufferStage::Kernel);
let count = descriptor.max_kernel_buffer_bind_count;
(0..count).zip(bindings).filter_map(|(_, binding)| *binding)
})
.filter(|binding| binding.names_buffer())
.map(|binding| binding.address);
let resolved = kept_commands::resolutions(addresses, residency);
let mut core = EncoderCore::segment(command_buffer, self.ordered)?;
let mut misses = 0u32;
let forms = (residency, BufferForms::Bound);
self.dispatch(&mut core, commands.iter(), forms, &mut misses)?;
let shadows = core.used_shadows();
let mut segment = core.end_segment()?;
segment.resident_misses = misses;
let plain = !shadows && !segment.taken.dead() && released.is_empty();
Ok((segment, resolved, (moved, plain)))
}
}