Files
metal2vulkan/crates/metal2vulkan-runtime/src/indirect_command_buffer/carry.rs
T
Anees Iqbal 257bdd7d14 SEMANTICS 2117 / RUNTIME 2319: GPU-written ICB records naming released or reused state IDs run what a Mac runs
What: two changes to how an indirect command's record names a pipeline state the program
released.

1. A record's state word gains a bit, KERNEL_PIPELINE (layout::KERNEL_PIPELINE = 2,
   CommandRecord::kernel_pipeline, RecordView::kernel_pipeline). A kernel's pipeline setter writes
   it wherever the token names a state. A kernel's copy carries it with the pipeline word, and its
   "reports" test now reads RETAINED alone. A kernel's compute reset keeps the bit beside RETAINED.
   Metal's reset leaves the command executing with the pipeline it had (measured on the M3 Max and
   the M4: reported cleared, run by the next dispatch). For a kernel's write, that pipeline is the
   state the write resolved, so the retained pipeline keeps saying the kernel wrote it. The CPU's
   compute reset does the same. The CPU's pipeline setter clears the bit, and a record the CPU
   wrote never carries it. The replay's state-word check and decode accept the bit; either bit
   beside no pipeline is still malformed. Where the bit is set, the runtime's host step, getter and
   identifier ignore the state the CPU set and resolve the ID by its holder as the GPU last could
   write it.
   Case 4 (the CPU set x, x released, y took the ID, then a kernel writes the state) now runs y, as
   the M3 Max and the 5300M do; before, it ran x, announced. Case 5 (the kernel writes only the
   buffers) runs x unannounced, as every Mac does. The host step no longer guesses, so the
   "Unsure" announcement and may_name_another are gone.

2. The device keeps the program of each freed pipeline state ID's last holder (FreedPrograms):
   one per ID, dropped when any state takes the ID. Every live indirect command buffer of the
   device holds the store and the device holds it weakly, since a program holds its device. A
   buffer the GPU may write takes the store's programs at its first such write. Case 1 (x released
   before the write, its ID taken by no one) now runs x, as the M3 Max and the UHD 630 do; the
   Radeon Pro 5300M fails the command buffer instead. Where Macs disagree, the cheaper answer is
   taken: failing the CB would need a failure path decided mid-flight in a host step. Bound: one
   program per freed ID that no state retook, so at most the peak count of live
   supportIndirectCommandBuffers states plus placed IDs, kept only while an ICB of the device
   lives. Still announced (ReleasedCommandState, the command runs nothing): a state released while
   no ICB of the device lived, and one past the ID's last four holders.

SEMANTICS_VERSION 2117: kernels that set pipelines now write the state-word bit, so their SPIR-V
and golden hashes change (re-blessed on development). api_signatures blessed for the new field and
method. No new Degradation kind. RUNTIME_VERSION is left to the landing.

Tests: indirect_commands::tests::records (a_kernel_s_pipeline_is_told_from_the_cpu_s, plus the
random field round trip with the bit). The engine_indirect_commands expectations of kernel writes
carry the bit, and the kernel reset keeps it. Runtime: released_programs (the freed store's one
program and its release on take) and notice_command_buffers (a host-step Unrun now reached through
a kernel's write of a state released while no ICB lived). Validation runtime_lane:
indirect_host_steps (case 1 runs the template; the forced notice past four holders and while no
ICB lived; case 4 runs the flat template; case 5 runs the template unannounced) and
indirect_executions (the GPU-writable copy case no longer announces). Run on lavapipe in the
lane's msi job; the results go in the lane report.

Also: Pin the notice of a state released while no buffer of commands lived; Replay tests: the kernel's pipeline bit runs, an unknown bit faults.

Verified (lane relstate, msi m3-max-30537): clippy --cfg m2v_cpu_icd exit 0; runtime tests
24/24; validation tests 113/113 on lavapipe, the new released-state cases 1, 4 and 5 among
them. Landed (main) at once: the plain clippy and translator rerun (m3-max-30546) and the
hand negative control (m3-max-30555, steelbrain/relstate-nc) run after landing; the engine
golden hashes of kernels that set pipelines are re-blessed on development.
RUNTIME 2319: what a GPU-written record naming a released or reused state runs changes.
2026-11-14 16:21:41 -08:00

149 lines
7.9 KiB
Rust

//! What a device's recovery carries of the program's indirect command buffers
//! (`device/carry.rs`): each the same object on the new `VkDevice`, as Metal keeps its objects
//! across its GPU's recovery.
//!
//! - **The records.** Its storage, and the buffer of the slots past its records ([`Overflow`]),
//! are buffers of the runtime's own whose `contents` pointer no program holds, carried as such
//! (`Buffer::carry_runtime`): in the runtime's own pages, where every such buffer lies on a
//! device that imports host pointers (`fresh_pages.rs`) but one whose held pointer import
//! costs every submission under the default `IndirectRecordsPolicy::Untaxed`, the same pages
//! there, the records as the CPU and the GPU left them, as Metal keeps them across its GPU's
//! restart; in pooled host-visible memory, on a device that imports none, where the import
//! failed, or on such a device under that default, new memory there, the records lost with the
//! `VkDevice`, whose memory Vulkan makes undefined (vkspec 5.2.3, "Lost Device"), written again
//! as a new buffer's: its header, every command reset, its slots past them zero. The CPU's writes of the records under `pipelines` wait for the carry
//! and read the records' bytes under it. A setter's write without it (`Batch`: one that adds
//! nothing to what the records may hold) in the runtime's own pages writes the pages the carry
//! keeps, whenever it lands; in pooled memory it is waited out before the carry begins, and one
//! begun meanwhile goes under the lock (`writers.rs`): each writes the records the carry leaves
//! or those it carries, never the lost generation's memory. A host read outside a command
//! buffer's work holds the storage's parts it reads.
//! - **What stays.** The object, its records ID, address, `gpuResourceID`, `uniqueIdentifier` and
//! label, which are the device's line's rather than a generation's (`device/line.rs`'s
//! `Carried`); the threadgroup lengths past the last command's, which the host keeps; what its
//! records may hold (`written_commands.rs`) and the CPU's writes counted.
//! - **What is made again.** The program each state the CPU set on a command runs: a live state's
//! its program there, which the state's carry made before (`pipeline/carry.rs`); a released
//! state's made again there from its compiled pipeline, as is each program the buffer keeps of
//! the released states its GPU-written records may name (`released_programs.rs`). One whose
//! making there fails stays the lost generation's, refused where bound
//! (`RuntimeGap::MadeBeforeRecovery`). Its kept recordings of chunks (`kept_commands.rs`), the
//! lost generation's command buffers, are let go of; its executions there record them again.
//! Where the GPU may write its records, the new generation's keepers list it
//! (`Keepers::add`), so a state released there is kept for it too.
//! - **Records in device-local memory** (`home.rs`) are lost with the `VkDevice`, as pooled
//! memory's are: the host's buffer is written again as a new buffer's, its header and every
//! command reset, and the records lie there for good.
//! - **Left** the lost generation's, refused where it meets the next: a buffer whose storage's
//! carry fails (the making of its memory there).
//!
//! [`Overflow`]: super::Overflow
use super::{CommandState, IcbShared, IndirectCommandBuffer};
use crate::device::carry::{Contents, Left};
use crate::device::lock;
use crate::Device;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Weak};
/// An indirect command buffer, held weakly by its generation's registry for a recovery to carry
/// (`Carry::indirect_command_buffer`).
pub(crate) struct WeakIndirectCommandBuffer(Weak<IcbShared>);
impl WeakIndirectCommandBuffer {
/// The buffer, while the program holds it.
pub(crate) fn upgrade(&self) -> Option<IndirectCommandBuffer> {
self.0.upgrade().map(IndirectCommandBuffer)
}
/// Whether the buffer lives.
pub(crate) fn lives(&self) -> bool {
self.0.strong_count() > 0
}
}
impl IndirectCommandBuffer {
/// The buffer, held weakly.
pub(crate) fn downgrade(&self) -> WeakIndirectCommandBuffer {
WeakIndirectCommandBuffer(Arc::downgrade(&self.0))
}
/// Carry the buffer, made on a generation of its device that a recovery leaves, to `to`, the
/// generation that recovery made, after its pipeline states (the module docs): what became of
/// its records. `Kept` where it was carried there already.
pub(crate) fn carry(&self, to: &Device) -> Result<Contents, Left> {
let shared = &self.0;
if shared.storage.generation().same(to) {
return Ok(Contents::Kept);
}
// Records in device-local memory are lost with it: the host's buffer holds them from now
// on, written again below. Decided before `pipelines` is taken, as a staged access takes
// the two (`home.rs`).
let device_lost = shared.home.leave_device();
// In pooled memory, the setters' writes without the lock under way end first, and none
// begins until the records and the keys are the next generation's (`writers.rs`).
let stopped = shared.writers.stop();
let mut commands = lock(&shared.pipelines);
// Each command's next set of its state checked again against the generation it meets,
// under the lock, whether the carry succeeds or not.
commands.states.forget_keys();
// The slots past the records first: a buffer whose storage stays the lost generation's
// stays whole there, its device that of its storage (`generation`).
if let Some(overflow) = &shared.overflow {
overflow.storage.carry_runtime(to)?;
}
let contents = match (shared.storage.carry_runtime(to)?, device_lost) {
(Contents::Kept, true) => Contents::Lost(super::storage_length(shared.size)),
(contents, _) => contents,
};
if let Contents::Lost(_) = contents {
// Every command reset, as a new buffer's: the states the CPU set on them go.
self.write_header().map_err(|_| Left::Failed)?;
commands.states.clear_all();
}
for state in commands.states.entries_mut() {
*state = state.carried(to);
}
commands.kept.clear();
commands.step_kept.clear();
drop(commands);
drop(stopped);
lock(&shared.kept).carry(to);
// The new generation's freed IDs' programs: the old one's stay with it.
let freed = to.shared().keepers.freed();
*lock(&shared.freed) = Arc::clone(&freed);
if shared.keeping.load(Ordering::Acquire) {
to.shared().keepers.add(&shared.kept, &freed);
}
// Kept there for a later recovery to carry on.
if to.shared().carry.indirect_command_buffer(self) {
to.carry_late(|serving| self.carry(serving));
}
Ok(contents)
}
}
impl CommandState {
/// The state on `to`, the generation a recovery made: a live state's program there, a
/// released state's made again there, or the lost one where its making there fails (the
/// module docs).
fn carried(&self, to: &Device) -> CommandState {
match self {
CommandState::Compute(named, program, stamp, can) => {
let program = match named.upgrade() {
Some(state) => state.program(),
None => program.made_again(to).unwrap_or_else(|_| program.clone()),
};
CommandState::Compute(named.clone(), program, *stamp, *can)
}
CommandState::Render(named, program, stamp, can) => {
let program = match named.upgrade().and_then(|state| state.program()) {
Some(program) => program,
None => program.made_again(to).unwrap_or_else(|_| program.clone()),
};
CommandState::Render(named.clone(), program, *stamp, *can)
}
}
}
}