diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index e0ed0da3b..ec491551e 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -728,11 +728,12 @@ pub(super) struct State { storage_form: Option>, /// Its units of command predication (`predication.rs`). pub(super) predication: Predication, - /// The predication of an encoder a rebind encodes again now, which was its unit alone + /// The predication of an encoder a rebind encodes again now, which encodes again exactly /// (`Predication::replaying`, `reencode.rs`): its records and dispatches step and gate there. pub(super) replaying_predication: Option, /// An encoder of it used command predication: its commit lets go of the logs of its compute - /// encoders that shared their unit, or laid a loop or left work out (`State::keep_replays`). + /// encoders their unit of predication reaches past, or that laid a loop or left work out + /// (`State::keep_replays`). pub(super) predicated: bool, /// The device's trail mark as it completed (`trails.rs`): its waiters return once no trail /// numbered below is open. 0 before, which waits for none. diff --git a/crates/metal2vulkan-runtime/src/command_buffer/rebound_use.rs b/crates/metal2vulkan-runtime/src/command_buffer/rebound_use.rs index 196c8d597..9343832d5 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/rebound_use.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/rebound_use.rs @@ -18,9 +18,10 @@ pub(crate) enum Unlogged { Segments, /// The command buffer's failure fails it, or a fault of its own, as it commits. Fails, - /// A compute encoder of a command buffer that used command predication whose unit held other - /// encoders, a loop or work left out (`Predication::alone`): a command encoded again after - /// the commit would run outside its block. + /// A compute encoder of a command buffer that used command predication whose unit laid a + /// step before it opened, or one of its own that the unit's later encoders read, or a loop, + /// or left work out (`Predication::alone`): a command encoded again after the commit would + /// run outside its block, or leave a command of the unit's reading a gate nothing writes. Predicated, } @@ -71,7 +72,8 @@ impl fmt::Display for Unlogged { Unlogged::Segments => "it left work for later an encoding again does not record", Unlogged::Fails => "its command buffer fails it as it commits", Unlogged::Predicated => { - "its unit of command predication held other encoders, a loop or work left out" + "its unit of command predication laid a step before it opened, or one of its \ + own that the unit's later encoders read, or a loop, or left work out" } }) } diff --git a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs index f0814e355..4788f04c4 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/reencode.rs @@ -39,8 +39,9 @@ //! commands, `gpu_execute::Rerecord`, or as a route outgrew it, `DeferredPass::outgrown`) //! keeps that step's recording. //! - **What cannot be encoded again stays as before.** An encoder whose log is gone (a compute -//! encoder of a command buffer that used command predication whose unit held other encoders, a -//! loop or work left out; one failed at the commit) or whose second +//! encoder of a command buffer that used command predication whose unit laid a step before it +//! opened, or one of its own that the unit's later encoders read, or a loop, or left work out; +//! one failed at the commit) or whose second //! encoding is refused or records otherwise (`EncoderCore::finish_replay`), its work among //! them where it no longer runs on the GPU alone, keeps its first recording, which the first //! submission finds rebound: `CommandBufferError::EncodedUseRebound`, as before. So does work @@ -128,8 +129,9 @@ impl CommandBufferShared { // The encoders encoded again, whose segments the encoding again recorded. let mut spliced = Vec::new(); for (at, replay) in moved { - // A compute encoder of a command buffer that used command predication, its unit - // alone, encodes again in its unit begun anew (`Predication::replaying`). + // A compute encoder of a command buffer that used command predication, which + // encodes again exactly, does so in its unit begun anew + // (`Predication::replaying`). let residency = { let mut state = lock(&self.state); state.replaying_predication = match state.predicated { @@ -447,8 +449,9 @@ impl State { /// As it commits: the logs of encoders no rebind encodes again go, for the caller to let go /// of without this lock: where an encoder used command predication, those of the compute - /// encoders that shared their unit, or laid a loop or left work out (`Predication::alone`: a - /// command of theirs encoded again after the commit would run outside its block); and those of + /// encoders their unit of predication reaches past, or that laid a loop or left work out + /// (`Predication::alone`: a command of theirs encoded again after the commit would run + /// outside its block, or leave a command of their unit's reading a gate nothing writes); and those of /// encoders the commit's failure fails (they run nothing). The keys of buffers over host pages /// the encoders whose logs stay named, and the work its commit recorded on the GPU alone, /// sorted, each once, and the identities of the rasterization-rate maps its encoders' passes diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 35cbde9af..19a64682a 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -1418,8 +1418,9 @@ degradations! { /// resolved buffer) that was rebound (`rebind_bytes_no_copy`) after the command was /// encoded and before the command buffer was first submitted /// (`CommandBufferError::EncodedUseRebound`, `command_buffer/first_submission.rs`), in an - /// encoder the rebind could not encode again (a command predication's compute encoder - /// that shares its unit of predication with other encoders, holds a loop or leaves work + /// encoder the rebind could not encode again (a compute encoder its unit of command + /// predication reaches past: one whose unit laid a step before it opened, or one of its + /// own that the unit's later encoders read, or whose unit holds a loop or leaves work /// out; a render pass a host step records, or whose end captured its draws' tile reach; work /// recorded on the GPU alone that no longer runs there; a call that answers otherwise than /// at its encoding; `replay.rs`). Its reason names where the first such submission found diff --git a/crates/metal2vulkan-runtime/src/predication.rs b/crates/metal2vulkan-runtime/src/predication.rs index 68c9f5ed2..a10d367a0 100644 --- a/crates/metal2vulkan-runtime/src/predication.rs +++ b/crates/metal2vulkan-runtime/src/predication.rs @@ -133,13 +133,18 @@ //! run again records it again where it lies, as its first run did. A dispatch inside a laying //! whose pipelines are being made waits for them, so the laying is not split for it. //! - **Encoded again at a rebind** (`command_buffer/reencode.rs`), as Metal resolves what a -//! command names as it submits the command buffer: a compute encoder that was its unit alone, -//! laid no loop and left no work out logs its Ifs, Elses and Ends among its calls -//! (`replay.rs`), and is encoded again in its unit begun anew as it opened -//! ([`Predication::replaying`]: the next record's index and the run of compute encoders as -//! they were), over a counter, steps, gates and a check word of its own; the first encoding's -//! go unsubmitted with its recording. Any other compute encoder of a command buffer that used -//! predication keeps no log, and a loop's record ends the log. +//! command names as it submits the command buffer: a compute encoder whose unit laid no loop +//! and left no work out, and of whose unit's predication nothing reached in over its open and +//! nothing of its own reaches out over its end ([`Predication::alone`]), logs its Ifs, Elses +//! and Ends among its calls (`replay.rs`), and is encoded again in its unit begun anew as it +//! opened ([`Predication::replaying`]: the next record's index and the run of compute encoders +//! as they were), over a counter, steps, gates and a check word of its own; the first +//! encoding's go unsubmitted with its recording. So an encoder of a unit that recorded no +//! predication at all, every encoder of a unit before the one that laid its first step, and +//! that one where its unit ended with it. Any other compute encoder of a command buffer that +//! used predication keeps no log — the commands of a unit's encoders after its first step run +//! from gates that step's encoder's recording writes, which an encoding again replaces — and a +//! loop's record ends the log. use super::buffer::Buffer; use super::command_buffer::rerun::{LoopCheck, Piece}; @@ -198,8 +203,9 @@ pub(super) struct Predication { left_out: Option, /// What each compute encoder found as it opened, in order ([`Opened`]). opened: Vec, - /// The places among the command buffer's ended encoders of the compute encoders whose unit - /// they were alone, which laid no loop and left nothing out: a rebind may encode one again + /// The places among the command buffer's ended encoders of the compute encoders that encode + /// again, in their unit begun anew, exactly what they first encoded + /// ([`alone`](Self::alone)): a rebind may encode one again /// ([`replaying`](Self::replaying)). alone: Vec, } @@ -479,6 +485,11 @@ struct Unit { looped: bool, /// An unbalanced record left out what its encoders recorded before it. left: bool, + /// The places among the command buffer's ended encoders of its compute encoders that opened + /// with no step of its laid ([`Unit::fresh`]), in order: its first encoder's, and each + /// later one's until one laid a step, so a prefix of its encoders + /// ([`alone`](Predication::alone)). + opened_fresh: Vec, } /// The range of a unit an unbalanced record drops (the module docs): from the anchor to the next @@ -562,9 +573,38 @@ impl Unit { carried: Vec::new(), looped: false, left: false, + opened_fresh: vec![place], } } + /// Whether no step of its has been laid: its skip counter is made with its first step + /// ([`Predication::step_holding`]), and every other field a record leaves — its open blocks, + /// its kill, its run, its droppable range's anchor and held steps, its check word and its + /// carried loop bodies — is set only through one, which the assertion below holds it to. Its + /// range's `work` alone is not: a dispatch Metal emitted sets it, and it is read only where + /// an unbalanced record with no anchor sets [`left`](Unit::left), which + /// [`alone`](Predication::alone) excludes ([`unbalanced`](Predication::unbalanced) sets the + /// two together, and [`resume`](Predication::resume) asserts it). + fn fresh(&self) -> bool { + let fresh = self.counter.is_none(); + // What `alone` rests on: a unit with no step laid carries nothing a unit begun anew + // would not hold, so an encoder that opened on one encodes again exactly. + debug_assert!( + !fresh + || (self.stack.is_empty() + && !self.dead + && self.run.is_none() + && self.range.anchor.is_none() + && self.range.since.is_empty() + && !self.range.dropping + && self.check.is_none() + && self.carried.is_empty() + && !self.looped), + "a unit of predication with no step laid carries predication state" + ); + fresh + } + /// Kill the outermost open block (the module docs), once. fn kill(&mut self) { if self.dead { @@ -632,6 +672,9 @@ impl Predication { && unit.events == events && unit.dispatch_type == dispatch_type { + if unit.fresh() { + unit.opened_fresh.push(place); + } unit.place = place; return std::mem::take(&mut unit.carried); } @@ -649,13 +692,28 @@ impl Predication { } /// The current unit ends: one with a live block open keeps its check word for the completion, - /// with the index of the record that opened its outermost open block. + /// with the index of the record that opened its outermost open block. Its encoders that a + /// rebind may encode again alone are noted ([`alone`](Self::alone)). fn end_unit(&mut self) { // A loop open where its unit ends is closed by no End: its laying is checked by none. self.laying = None; if let Some(unit) = self.unit.take() { - if unit.first == unit.place && !unit.looped && !unit.left { - self.alone.push(unit.first); + if !unit.looped && !unit.left { + // Each encoder that opened with no step of the unit laid and laid none itself: + // the unit was never gated while it was open, so no command of its reads a gate + // and it leaves no step for a later encoder of the unit to read. Those are + // every one of `opened_fresh` but its last where the unit laid a step, and every + // one where it laid none. + let stepped = !unit.fresh(); + // Where the unit laid a step, the last of `opened_fresh` is the encoder that + // laid its first: that one gates the commands of the unit's later encoders, so + // it encodes again alone only where the unit ended with it. + let first_step_at = unit.opened_fresh.len().saturating_sub(1); + for (at, &place) in unit.opened_fresh.iter().enumerate() { + if !stepped || at < first_step_at || place == unit.place { + self.alone.push(place); + } + } } if let (true, Some(check), Some(outermost)) = (unit.live_open(), unit.check, unit.stack.first()) @@ -685,15 +743,37 @@ impl Predication { self.records += 1; } - /// Whether the compute encoder at `place` among the command buffer's ended encoders was its - /// unit alone, laying no loop and leaving nothing out, as its commit found: a rebind may - /// encode it again (`command_buffer/reencode.rs`). + /// Whether the compute encoder at `place` among the command buffer's ended encoders encodes + /// again, in its unit begun anew ([`replaying`](Self::replaying)), exactly what it first + /// encoded, as its commit found: a rebind may encode it again + /// (`command_buffer/reencode.rs`). It does where its unit laid no loop and left no work out + /// and, of the unit's predication, nothing reached in over its open and nothing of its own + /// reaches out over its end: + /// + /// - **Nothing reached in**: no step of the unit was laid before it opened ([`Unit::fresh`]), + /// so a unit begun anew holds what the unit held then — its counter, open blocks, kill, + /// run, droppable range, check word and carried loop bodies all come of a step. + /// - **Nothing reaches out**: the unit laid no step while the encoder was open, so it was + /// never gated (`Unit::gated`) and left the unit as it found it; or the encoder laid the + /// unit's first step and the unit ended with it, so its steps, gates and check word are + /// read by its own commands alone. + /// + /// So every encoder of a unit that recorded no predication at all, every encoder before the + /// one that laid its unit's first step, and that one where its unit ended with it. A unit + /// whose first step lies in an earlier encoder than its last keeps none of the encoders from + /// that step on: the commands of the unit's later encoders run from gates the step before + /// them writes, which lie in that encoder's recording, and an encoding again lays steps and + /// gates of its own, leaving those written by nothing. A gate holds the dispatch's group + /// counts where the step writes them and nothing where it does not + /// (`PredicationStep::GATE_OUT_OFFSET`, which only a step fills), so such a command would + /// run nothing where Metal runs it. Encoding a run of a unit's encoders again together, + /// which that shape needs, is not done. pub(super) fn alone(&self, place: usize) -> bool { self.alone.contains(&place) } - /// The predication the compute encoder at `place` encodes again with, where it was its unit - /// alone ([`alone`](Self::alone)): its unit begun anew as it opened, the next record's index + /// The predication the compute encoder at `place` encodes again with, where it encodes again + /// exactly ([`alone`](Self::alone)): its unit begun anew as it opened, the next record's index /// and the run of compute encoders as they were then. Its records and dispatches then step /// and gate as the first encoding's did, over a counter, steps and gates of their own; the /// first encoding's go with its recording, never submitted. @@ -864,6 +944,11 @@ impl Predication { if !unit.stack.is_empty() || !unit.range.dropping { return Ok((None, false)); } + // The work this reads is the encoder's own, or a later encoder's of the unit: where no + // anchor reset the range, the unbalanced record that set `dropping` already set `left` + // for any work Metal emitted before it, which `alone` excludes. So an encoder `alone` + // answers for reads only work its own encoding laid down. + debug_assert!(unit.range.anchor.is_some() || unit.left || !unit.range.work); let dead = !unit.range.work; let record = self.step(uploads, device, (None, None), |counter| { PredicationStep::new(counter, PredicationOp::Resume) diff --git a/crates/metal2vulkan-runtime/src/rebind.rs b/crates/metal2vulkan-runtime/src/rebind.rs index 93b60bca6..96d4c3f61 100644 --- a/crates/metal2vulkan-runtime/src/rebind.rs +++ b/crates/metal2vulkan-runtime/src/rebind.rs @@ -23,7 +23,8 @@ //! that names the buffer took the pages as it was encoded (`Taken`), so the rebind encodes its //! compute, blit and render encoders that named the buffer again, in the rebind's call, before //! their group's submission (`replay.rs`, `command_buffer/reencode.rs`); an encoder it cannot -//! encode again (a compute encoder that shared its unit of command predication, among others) fails +//! encode again (a compute encoder its unit of command predication reaches past, among others) +//! fails //! `CommandBufferError::EncodedUseRebound` there, as all did before. Commands encoded after //! the rebind use the new pages. A rebind while a command buffer that is not yet committed has //! encoded a use of the buffer or heap (one of its resources included), or has an encoder open diff --git a/crates/metal2vulkan-runtime/src/replay.rs b/crates/metal2vulkan-runtime/src/replay.rs index 1cfa52f64..15a5c94a6 100644 --- a/crates/metal2vulkan-runtime/src/replay.rs +++ b/crates/metal2vulkan-runtime/src/replay.rs @@ -25,11 +25,13 @@ //! the encoding again records at once on the GPU alone, as its commit did, or refuses where it //! no longer runs there (`EncoderCore::finish_replay`); and it fails nothing as it commits. A //! compute encoder of a command buffer that used command predication keeps its log, its Ifs, -//! Elses and Ends among its calls, where it was its unit of predication alone and laid no loop -//! and left no work out (`Predication::alone`): encoded again, it steps and gates in its unit -//! begun anew, as it did (`Predication::replaying`). Any other compute encoder of such a -//! command buffer keeps none (a command of it encoded again after the commit would run outside -//! its block): it fails `CommandBufferError::EncodedUseRebound` at a rebind as before, as does +//! Elses and Ends among its calls, where its unit laid no loop and left no work out and, of its +//! unit's predication, nothing reached in over its open and nothing of its own reaches out over +//! its end (`Predication::alone`): encoded again, it steps and gates in its unit begun anew, as +//! it did (`Predication::replaying`). Any other compute encoder of such a command buffer keeps +//! none (a command of it encoded again after the commit would run outside its block, or leave a +//! command of its unit's reading a gate nothing writes): it fails +//! `CommandBufferError::EncodedUseRebound` at a rebind as before, as does //! any other encoder (a resource state or acceleration structure encoder's), which logs //! nothing. (Changed:) a //! render encoder logs as these do, with its pass's descriptor (`render/replay.rs`). diff --git a/crates/metal2vulkan-validation/src/runtime_lane/predication.rs b/crates/metal2vulkan-validation/src/runtime_lane/predication.rs index 18cf605df..15eb94976 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/predication.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/predication.rs @@ -105,14 +105,14 @@ const LOG_WORDS: usize = 128; /// Metal's comparison code `==` (0 `==`, 1 `!=`, 2 `>`, 3 `>=`, 4 `<`, 5 `<=`, and `==` for any /// other code). -const EQUAL: u64 = 0; +pub(super) const EQUAL: u64 = 0; /// An offset past the end of a stream's condition words: Metal reads no word there for an /// `encodeEndDoWhile:...` that closes no live DoWhile. pub(crate) const PAST: u64 = 0x3f00; /// Metal's comparison codes `!=` and `<`. -const NOT_EQUAL: u64 = 1; +pub(super) const NOT_EQUAL: u64 = 1; const LESS: u64 = 4; /// The state of kernel `name` on `device`, made for indirect command buffers where `indirect`. diff --git a/crates/metal2vulkan-validation/src/runtime_lane/rebind_reach.rs b/crates/metal2vulkan-validation/src/runtime_lane/rebind_reach.rs index 0e4ae3097..4afe2d424 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/rebind_reach.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/rebind_reach.rs @@ -35,7 +35,7 @@ use super::degrade_notices::{assert_made_once, made_once, on_device as on_device use super::host_memory::{gpu_read, host_page, no_copy, not_importable, on_device, recording}; use super::host_memory::{Mapping, Seen}; use super::no_copy_heaps::{no_copy_heap, shared, HEAP}; -use super::predication::kernel; +use super::predication::{kernel, EQUAL, NOT_EQUAL}; use super::rebinds::{rebind, rebind_heap}; use super::resource_ids::DELAYED_DEALLOCATORS; use super::shared_kinds; @@ -670,15 +670,20 @@ fn an_encoder_with_a_call_refused_at_its_encoding_is_encoded_again_with_it_refus }); } -/// What a rebind cannot encode again keeps the gap: a compute encoder of a command buffer that -/// used command predication whose unit held another encoder keeps no log (`replay.rs`), so its -/// dispatch counting into the member, before the unit's next encoder's empty If block, keeps its -/// first recording, which named the old pages: its submission runs nothing, the command buffer -/// fails `EncodedUseRebound`, announced once, the notice naming why, and its signal fires. The blit -/// encoder before it, which copies the member, is encoded again, and its own submission copies the -/// new pages, as Metal's does. +/// A unit of command predication a rebind encodes again: an encoder of one keeps its log, its +/// Ifs, Elses and Ends among its calls, where its unit laid no loop and left no work out and, of +/// its unit's predication, nothing reached in over its open and nothing of its own reaches out +/// over its end (`Predication::alone`). A plain compute encoder that dispatches `prd_mark` into +/// the member's word 0, and next in its unit a predicated one whose failing If holds a dispatch +/// into its word 1, both naming the member rebound after the commit: the plain encoder laid no +/// step of the unit, and the predicated one laid the unit's first step and the unit ended with +/// it, so both are encoded again, their dispatches reach the new pages, the held dispatch runs +/// nothing and the command buffer completes with no error, its signal fired. Both once failed +/// `EncodedUseRebound` for sharing one unit. The blit encoder before them, its own submission +/// (`checks::metal_submissions`: consecutive compute encoders share one), copies the new pages +/// too. #[test] -fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { +fn a_unit_s_plain_encoder_before_its_predicated_one_is_encoded_again() { on_device_noticed(|device, messages| { let queue = device.new_command_queue().unwrap(); let Some((member, _)) = member_over(device, A) else { @@ -689,6 +694,7 @@ fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { let mark = kernel(device, "prd_mark", false); let event = device.new_shared_event().unwrap(); let done = device.new_shared_event().unwrap(); + let one = Size::new(1, 1, 1); let command_buffer = queue.command_buffer().unwrap(); command_buffer .encode_wait_for_event(EventRef::from(&event), 1) @@ -697,15 +703,23 @@ fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { blit.copy_from_buffer_to_buffer(&member, 0, &copied, 0, 16) .unwrap(); blit.end_encoding().unwrap(); + // The unit's first encoder, which lays no step of it: its dispatch runs ungated. let mut compute = command_buffer.compute_command_encoder().unwrap(); compute.set_compute_pipeline_state(&mark).unwrap(); compute.set_bytes(&0u32.to_le_bytes(), 0).unwrap(); compute.set_buffer(Some(&member), 0, 1).unwrap(); - let one = Size::new(1, 1, 1); compute.dispatch_threads(one, one).unwrap(); compute.end_encoding().unwrap(); + // The unit's last encoder, which lays its every step: `0 != 0` fails, so its block runs + // nothing. let mut compute = command_buffer.compute_command_encoder().unwrap(); - compute.encode_start_if(Some(&word), 0, 0, 0).unwrap(); + compute.set_compute_pipeline_state(&mark).unwrap(); + compute.set_bytes(&1u32.to_le_bytes(), 0).unwrap(); + compute.set_buffer(Some(&member), 0, 1).unwrap(); + compute + .encode_start_if(Some(&word), 0, NOT_EQUAL, 0) + .unwrap(); + compute.dispatch_threads(one, one).unwrap(); assert!(compute.encode_end_if().unwrap()); compute.end_encoding().unwrap(); command_buffer @@ -716,6 +730,102 @@ fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { drop(rebind_to(&member, B)); event.set_signaled_value(1); command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.error(), None); + assert_eq!(command_buffer.status(), CommandBufferStatus::Completed); + let mut marked = node(B); + marked[..4].copy_from_slice(&1u32.to_le_bytes()); + assert_eq!( + read_buffer(&member, 0, 16).unwrap(), + marked, + "the ungated dispatch ran over the new pages, and the held one ran nothing" + ); + assert_eq!( + read_buffer(&copied, 0, 16).unwrap(), + node(B), + "the blit's copy of the new pages" + ); + assert_eq!(done.signaled_value(), 1, "its signal"); + let notices = assert_made_once(messages, &[], &shared_kinds(device)); + assert!(!notices.contains_key(&Degradation::EncodedUseRebound)); + let after = device.work_counts(); + // The blit encoder and the unit's two, each naming the member. + assert_eq!(after.encoders_reencoded, before.encoders_reencoded + 3); + assert_eq!(after.reencodes_refused, before.reencodes_refused); + eprintln!( + "REBIND-REACH {}: a unit's plain encoder before its predicated one, both encoded \ + again", + device.name() + ); + }); +} + +/// What a rebind cannot encode again keeps the gap: a unit of command predication whose first +/// step lies in an earlier encoder than its last keeps no log from that encoder on +/// (`Predication::alone`). The commands of the unit's later encoders run from gates the step +/// before them writes, which lie in that encoder's recording; an encoding again lays steps and +/// gates of its own, leaving those written by nothing -- a gate holds the group counts only +/// where its step writes them -- so such a command would run nothing where Metal runs it. +/// Encoding a run of a unit's encoders again together, which that shape needs, is not done. +/// +/// The unit's first encoder dispatches `prd_mark` into the member rebound after the commit and +/// then opens and closes an If, so the member is named by a dispatch before the unit's first +/// record, which runs ungated; next in its unit a plain encoder dispatches into a buffer of its +/// own, from a gate that first encoder's step writes. The first encoder laid the unit's first +/// step and the unit went on past it, so it keeps no log and keeps its first recording, which +/// named the old pages: its submission (the unit's two encoders) runs nothing, the member keeps +/// the new pages' node, the command buffer fails `EncodedUseRebound`, announced once, the notice +/// naming the encoder and why, and its signal fires. The blit encoder before it, which copies +/// the member, is encoded again, and its own submission +/// (`checks::metal_submissions`: consecutive compute encoders share one) copies the new pages. +#[test] +fn a_unit_s_encoder_that_laid_its_first_step_fails_encoded_use_rebound() { + on_device_noticed(|device, messages| { + let queue = device.new_command_queue().unwrap(); + let Some((member, _)) = member_over(device, A) else { + return; + }; + let copied = buffer_with(device, &[0x5a; 16]).unwrap(); + let word = buffer_with(device, &[0; 4]).unwrap(); + // The later encoder's own log, over no host pages: its gated dispatch names nothing the + // rebind moves. + let log = buffer_with(device, &[0; 16]).unwrap(); + let mark = kernel(device, "prd_mark", false); + let event = device.new_shared_event().unwrap(); + let done = device.new_shared_event().unwrap(); + let one = Size::new(1, 1, 1); + let command_buffer = queue.command_buffer().unwrap(); + command_buffer + .encode_wait_for_event(EventRef::from(&event), 1) + .unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + blit.copy_from_buffer_to_buffer(&member, 0, &copied, 0, 16) + .unwrap(); + blit.end_encoding().unwrap(); + // The unit's first encoder: its dispatch comes before the unit's first record, so it + // runs ungated, and its empty If lays that first step. + let mut compute = command_buffer.compute_command_encoder().unwrap(); + compute.set_compute_pipeline_state(&mark).unwrap(); + compute.set_bytes(&0u32.to_le_bytes(), 0).unwrap(); + compute.set_buffer(Some(&member), 0, 1).unwrap(); + compute.dispatch_threads(one, one).unwrap(); + compute.encode_start_if(Some(&word), 0, EQUAL, 0).unwrap(); + assert!(compute.encode_end_if().unwrap()); + compute.end_encoding().unwrap(); + // Next in the unit, after that step: its dispatch runs from a gate the step writes. + let mut compute = command_buffer.compute_command_encoder().unwrap(); + compute.set_compute_pipeline_state(&mark).unwrap(); + compute.set_bytes(&0u32.to_le_bytes(), 0).unwrap(); + compute.set_buffer(Some(&log), 0, 1).unwrap(); + compute.dispatch_threads(one, one).unwrap(); + compute.end_encoding().unwrap(); + command_buffer + .encode_signal_event(EventRef::from(&done), 1) + .unwrap(); + command_buffer.commit().unwrap(); + let before = device.work_counts(); + drop(rebind_to(&member, B)); + event.set_signaled_value(1); + command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.status(), CommandBufferStatus::Error); assert_eq!( command_buffer.error(), @@ -729,7 +839,12 @@ fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { assert_eq!( read_buffer(&member, 0, 16).unwrap(), node(B), - "the dispatch ran nothing" + "the unit's dispatches ran nothing" + ); + assert_eq!( + read_buffer(&log, 0, 16).unwrap(), + vec![0; 16], + "the gated dispatch of the unit's later encoder ran nothing" ); assert_eq!(done.signaled_value(), 1, "its signal"); let notices = made_once(messages, &REBOUND_USES, &shared_kinds(device)); @@ -740,8 +855,9 @@ fn an_encoder_a_rebind_cannot_encode_again_fails_encoded_use_rebound() { .find(|notice| notice.degradation == Degradation::EncodedUseRebound) .map(|notice| notice.reason.clone()); let found = "(found in its compute encoder 1: a command of its own named it, not encoded \ - again as its unit of command predication held other encoders, a loop or \ - work left out"; + again as its unit of command predication laid a step before it opened, or \ + one of its own that the unit's later encoders read, or a loop, or left work \ + out"; assert!( reason .as_deref() diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 2246b1b79..cb59d612a 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 449; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 1579; +pub const RUNTIME_VERSION: u32 = 1580; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 8378974f1..3e1c6369d 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -398,8 +398,9 @@ driver's. Each is open work. their deallocator waits (announced), until the command buffer completes. Its commands that named the buffer are encoded again over the new pages, as Metal runs them, and fail with code 8 where it is detached by then, as Metal's; those of an encoder that cannot be encoded again (a - compute encoder whose unit of command predication held other encoders, a loop or work left out; - work on the GPU alone) fail `EncodedUseRebound` (announced). + compute encoder its unit of command predication reaches past, which is one whose unit laid a + step before it opened or one of its own that the unit's later encoders read, and one whose unit + laid a loop or left work out; work on the GPU alone) fail `EncodedUseRebound` (announced). - **A runtime sampler's nearest sample of an 8-bit sRGB texture under native sampling.** On a device that samples the format, the sample keeps the device's sRGB decode (announced as `SrgbTexelDecode`), where the table's exact decode of the one texel it reads would cost little: