Translator: a kernel's floor tail returns at entry again; a render stage's folds into its other pushed fields

Since 059835018a every stage folded its tail's test into each other pushed field through a
select. A kernel's entry also returns invocations before its body where its dispatch passes its
grid (dispatch::emit_entry), so a return at entry is a kernel's own shape, and the selects cost
kernels on three vendors. A fragment stage has no other return at its entry; AMD's open driver
(AMDVLK 2025.Q2.1) died making one whose body ran under the never-taken return, which is what the
fold removed.

Now the stage kind decides it, the same on every device:
- Stage::Kernel loads its push fields in placement order, then returns where the tail holds what
  the consumer never pushes (EmitCx::emit_tail_guard, lower::push::return_never_pushed), as
  before 059835018a. The tail's read is in the first block, and everything the kernel does after
  the return depends on it.
- A render pipeline's stage (vertex, fragment, object, mesh) keeps the fold as shipped
  (EmitCx::fold_tail, lower::push::fold_never_pushed): no branch on the tail.
- EmitCx::tail_test reads the tail and makes its test for both; load_fields loads the fields a
  filter takes, in placement order.
- Docs: lower/push.rs, lower/emit.rs, the arena test's module doc, docs/API.md section 5.

Same pushed bytes, same placement, no interface change.
This commit is contained in:
Anees Iqbal committed 2026-11-23 06:26:25 -08:00
1 parent c2d505c30c
commit 3a4fe0d314
4 files changed
+105 -51

No files matched your search

+67 -34
View File
@@ -184,8 +184,9 @@ pub struct EmitCx<'a> {
entry_block: Option<SBlockId>,
/// 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<SBlockId>,
/// 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<Option<(PushField, SValue)>> {
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<Emitted, Err
let r = em
.cx
.begin_hooks(&fb)
// The push fields, the floor's tail folded into them (`EmitCx::fold_tail`).
// The push fields and the floor's tail: a kernel's returns at entry, a render
// stage's folds into its other fields (`EmitCx::load_push_fields`).
.and_then(|()| em.cx.load_push_fields(&mut fb))
.and_then(|()| dispatch::emit_entry(&mut em.cx, &mut fb))
// What the invocation draws (a merged draw's command and its slot tables) before
+31 -11
View File
@@ -35,13 +35,16 @@
//! A driver that uploads a stage's push bytes up to the highest one it reads then uploads at least
//! 28 bytes past the other fields, which holds every load it widens from them, and the tail's own
//! load, one or two words, is never widened. The stage reads the tail in its first block and tests
//! whether it holds a value the consumer never pushes there ([`tail_never_pushed`]); each other
//! pushed field's value is its member where the test fails and the null of its type where it holds
//! ([`fold_never_pushed`]), so no optimizer drops the tail's read while it keeps the read of any
//! other pushed field. No branch makes the body depend on the tail: AMD's open driver (2025.Q2.1)
//! dies making a fragment stage whose body runs under a never-taken return there and holds a loop
//! of convergent instructions under `MaximallyReconvergesKHR`. A stage that pushes one field of one
//! or two words has no other load to widen and no tail. The hole moves no field the stage reads
//! whether it holds a value the consumer never pushes there ([`tail_never_pushed`]), so no
//! optimizer drops the tail's read while it keeps the read of any other pushed field. A kernel
//! returns where the test holds ([`return_never_pushed`]): its entry also returns invocations
//! before its body where its dispatch passes its grid (`dispatch::emit_entry`). In a render stage
//! each other pushed field's value is its member where the test fails and the null of its type
//! where it holds ([`fold_never_pushed`]), and no branch makes the body depend on the tail: a
//! fragment stage has no other return at its entry, and AMD's open driver (2025.Q2.1) dies making
//! one whose body runs under a never-taken return there and holds a loop of convergent
//! instructions under `MaximallyReconvergesKHR`. A stage that pushes one field of one or two words
//! has no other load to widen and no tail. The hole moves no field the stage reads
//! and adds no read where the tail is a field of its own, so the words a driver keeps in registers
//! are the same.
//!
@@ -436,9 +439,10 @@ pub fn check_floor(placement: &PushPlacement, bytes: u32) -> 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.
@@ -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
+5 -4
View File
@@ -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