diff --git a/crates/metal2vulkan/src/lower/emit.rs b/crates/metal2vulkan/src/lower/emit.rs index 1e775420c..651047d40 100644 --- a/crates/metal2vulkan/src/lower/emit.rs +++ b/crates/metal2vulkan/src/lower/emit.rs @@ -184,8 +184,9 @@ pub struct EmitCx<'a> { entry_block: Option, /// The block the entry hooks end in, where the walk of the body starts: it dominates every /// block of the body, so its memo entries outlive the block that emitted them too. It is the - /// entry block but where a hook closed a construct there: a dispatch past its grid - /// (`dispatch::emit_entry`) returns from an arm, and the hooks after it, the sink's address + /// entry block but where a hook closed a construct there: a kernel's tail guard + /// (`EmitCx::emit_tail_guard`) or a dispatch past its grid (`dispatch::emit_entry`) returns + /// from an arm, and the hooks after it, the sink's address /// among them (`access::dynamic::emit_entry`), run in its merge. hooks_block: Option, /// While a workgroup synchronization of a predicated walk is emitted, the calling invocation's @@ -508,48 +509,56 @@ impl<'a> EmitCx<'a> { /// load, so a read that canonicalization deletes cannot make the others load it again; a placed /// field no instruction reads leaves with the other unused values (`spirv::dead`). /// - /// The fields read from their push members load first, then the tail folds into them - /// ([`EmitCx::fold_tail`]), then the spilled fields and those the function derives, which may - /// read through the folded values (the parameter block's address, a slot table's). + /// A kernel loads its fields in placement order, then returns where its tail holds what the + /// consumer never pushes ([`EmitCx::emit_tail_guard`]). A render stage loads the fields read + /// from their push members first, then folds the tail into them ([`EmitCx::fold_tail`]), then + /// the spilled fields and those the function derives, which may read through the folded values + /// (the parameter block's address, a slot table's). fn load_push_fields(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> { - let plan: &Plan = self.plan; + if self.plan.stage == Stage::Kernel { + self.load_fields(fb, |_, _| true)?; + return self.emit_tail_guard(fb); + } let member = |s: &Self, field: PushField| { !s.derives(field) && s.decl.push.iter().any(|m| m.field == field) }; - for read in [true, false] { - for placed in &plan.push.fields { - if member(self, placed.field) != read { - continue; - } - match placed.field { - PushField::SlotBounds(slot) => { - self.slot_bounds(fb, slot)?; - } - field => { - self.push_field(fb, field)?; - } - } + self.load_fields(fb, member)?; + self.fold_tail(fb)?; + self.load_fields(fb, |s, field| !member(s, field)) + } + + /// Load each field the plan places that `which` takes, in placement order. + fn load_fields( + &mut self, + fb: &mut FunctionBuilder<'_>, + which: impl Fn(&Self, PushField) -> bool, + ) -> BResult<()> { + let plan: &Plan = self.plan; + for placed in &plan.push.fields { + if !which(self, placed.field) { + continue; } - if read { - self.fold_tail(fb)?; + match placed.field { + PushField::SlotBounds(slot) => { + self.slot_bounds(fb, slot)?; + } + field => { + self.push_field(fb, field)?; + } } } Ok(()) } /// Where the plan's placement ends with a tail (`lower::push::tail`): read it from the push - /// block, test whether it holds the value the consumer never pushes there - /// ([`crate::lower::push::tail_never_pushed`]), and make each other field read from its push - /// member, as the function's every use recalls it, the member's value where the test fails and - /// the null of its type where it holds ([`crate::lower::push::fold_never_pushed`]): the tail's - /// read lives while any other push read does, and no branch makes the body depend on it. A - /// tail field the function reads from its member is the load [`EmitCx::load_push_fields`] made - /// in this block, so the member is loaded once; the tail word, and a field the function - /// derives elsewhere, are loaded here. A tail field of the function's own folds into the - /// others alone: its own uses need no select to read it. - fn fold_tail(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> { + /// block, and test whether it holds the value the consumer never pushes there + /// ([`crate::lower::push::tail_never_pushed`]); `None` where there is no tail or no such + /// value. A tail field the function reads from its member is the load + /// [`EmitCx::load_push_fields`] made in this block, so the member is loaded once; the tail + /// word, and a field the function derives elsewhere, are loaded here. + fn tail_test(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult> { let Some(field) = crate::lower::push::tail(&self.plan.push) else { - return Ok(()); + return Ok(None); }; let member = (self.decl.push.iter()).find(|m| m.field == field); let (Some(member), Some(push)) = (member, self.decl.push_global) else { @@ -559,7 +568,30 @@ impl<'a> EmitCx<'a> { true => self.push_field(fb, field)?, false => Self::load_member(fb, push, member.index)?, }; - let Some(never) = crate::lower::push::tail_never_pushed(fb, value, field)? else { + let never = crate::lower::push::tail_never_pushed(fb, value, field)?; + Ok(never.map(|never| (field, never))) + } + + /// A kernel's tail ([`EmitCx::tail_test`]): return from the function where it holds the value + /// the consumer never pushes there ([`crate::lower::push::return_never_pushed`]). The branch + /// is never taken; everything the function does after it depends on the tail's read. A return + /// at entry is a kernel's own shape: its entry also returns invocations before its body where + /// its dispatch passes its grid (`dispatch::emit_entry`). + fn emit_tail_guard(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> { + match self.tail_test(fb)? { + Some((_, never)) => crate::lower::push::return_never_pushed(fb, never), + None => Ok(()), + } + } + + /// A render stage's tail ([`EmitCx::tail_test`]): make each other field read from its push + /// member, as the function's every use recalls it, the member's value where the test fails and + /// the null of its type where it holds ([`crate::lower::push::fold_never_pushed`]): the tail's + /// read lives while any other push read does, and no branch makes the body depend on it + /// (`lower::push`: a fragment stage has no other return at its entry). A tail field of the + /// function's own folds into the others alone: its own uses need no select to read it. + fn fold_tail(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> { + let Some((field, never)) = self.tail_test(fb)? else { return Ok(()); }; let plan: &Plan = self.plan; @@ -1617,7 +1649,8 @@ pub fn emit_module(e: &Entry, plan: &Plan, m: &mut Meter) -> Result Result<(), String> /// In the open block of a function whose push block holds the stage's tail `field` ([`tail`]), /// `value` its member as the block read it: whether it holds the value the consumer never pushes /// there: the tail word non-zero, a two-word field with both words all ones, an address all ones. -/// `None` for a tail of another shape, which has no such value. The function folds the test into -/// each other pushed field's value ([`fold_never_pushed`]), so no optimizer drops the read while -/// it keeps any other push read, and no branch makes the body depend on it. +/// `None` for a tail of another shape, which has no such value. A kernel returns where the test +/// holds ([`return_never_pushed`]); a render stage folds it into each other pushed field's value +/// ([`fold_never_pushed`]). Either way no optimizer drops the read while it keeps any other push +/// read. pub(crate) fn tail_never_pushed( fb: &mut crate::spirv::builder::FunctionBuilder<'_>, value: crate::spirv::ids::Value, @@ -473,7 +477,23 @@ pub(crate) fn tail_never_pushed( Ok(Some(set)) } -/// `value`, a pushed field's value as its member was read, where the stage's tail does not hold +/// A kernel's tail test `never` ([`tail_never_pushed`]): return from the function where it holds. +/// The branch is never taken; the return keeps the read from any optimizer, as everything the +/// function does after it depends on it. +pub(crate) fn return_never_pushed( + fb: &mut crate::spirv::builder::FunctionBuilder<'_>, + never: crate::spirv::ids::Value, +) -> crate::spirv::error::BResult<()> { + use crate::spirv::builder::constructs::{ArmEnd, ArmFn}; + let leave: ArmFn<'_, ()> = Box::new(|fb, _| { + fb.ret(None)?; + Ok(ArmEnd::Exits) + }); + fb.if_(&mut (), never, leave, None)?; + Ok(()) +} + +/// A render stage's `value`, a pushed field's value as its member was read, where the stage's tail does not hold /// the value the consumer never pushes there (`never`, [`tail_never_pushed`]), else the null of /// its type: the value the consumer's push gives, through a select whose condition is the tail's /// test, so the field's every use reads the tail too. diff --git a/crates/metal2vulkan/src/tests/engine_device_arena.rs b/crates/metal2vulkan/src/tests/engine_device_arena.rs index 83e4b0656..fa9b46e5f 100644 --- a/crates/metal2vulkan/src/tests/engine_device_arena.rs +++ b/crates/metal2vulkan/src/tests/engine_device_arena.rs @@ -10,8 +10,8 @@ //! plan keeps the arena in device memory, with the default lowering inputs and as a kernel whose //! threadgroups may be partial, the invocation's slot (`EmitCx::arena_slot`) is derived once in //! the function, and each push-block or `ArgumentTable` member, the arena's address among them, -//! is loaded once: the floor's tail (`EmitCx::fold_tail`) reads the tail from the load the first -//! block made, and a hook after a construct of the entry hooks recalls the first block's loads. +//! is loaded once: the floor's tail guard (`EmitCx::emit_tail_guard`) reads the tail from the load +//! the first block made, and a hook after the guard's construct recalls the first block's loads. //! //! What one access of the arena emits, on a generated module: each shape is two kernels, one that //! makes the access once and one that makes it twice, everything else equal, as in diff --git a/docs/API.md b/docs/API.md index b037fa401..34a955bea 100644 --- a/docs/API.md +++ b/docs/API.md @@ -169,10 +169,11 @@ and so do the command line, the compile worker and every validation tool. It hol zero word `PushField::TailWord` at `PushPlacement::tail_word`, with which its `push_range` ends; a function that pushes one field of 8 bytes or fewer has none. A driver that uploads a stage's push bytes up to the highest one the stage reads then holds every load it widens from the other - fields. The function reads its tail at its start, and each of its other pushed fields reads as - null where the tail holds what the consumer never pushes there (a tail word other than zero, - bounds whose two words are both all ones, a zero-range address of all ones), so the consumer - pushes the tail word zero with the other fields; the bytes of the hole are not read. A kernel that reads a grid builtin reads `PushField::DispatchRegion` on + fields. The function reads its tail at its start and tests whether it holds what the consumer + never pushes there (a tail word other than zero, bounds whose two words are both all ones, a + zero-range address of all ones): a kernel returns there, and in a render pipeline's function + each of its other pushed fields reads as null there, with no branch. So the consumer pushes the + tail word zero with the other fields; the bytes of the hole are not read. A kernel that reads a grid builtin reads `PushField::DispatchRegion` on every dispatch: a dispatch the host knows states Metal's grid and its piece's first threadgroup there, and an indirect dispatch a zero grid, whose kernel takes `NumWorkgroups` times its workgroup size. It reads its first threadgroup from every region (0 where the