Translator: the floor's tail folds into each other pushed field through a select, not a never-taken return
The floor's tail guard read the tail in the first block and returned where it held a value the consumer never pushes, so every stage that pushes more than one field ran its whole body under a never-taken branch. AMD's open driver (AMDVLK 2025.Q2.1, LLPC) dies in vkCreateGraphicsPipelines making a fragment stage of that shape whose loop holds convergent instructions under MaximallyReconvergesKHR (an implicit-LOD sample and an f32-f16-f32 round trip): the setup streams' render content 295d5723 ([65] RG16Float, 1 sample) died on the Radeon 780M, and the owner's Windows driver died making the same stream's states. Now the stage reads the tail in its first block, tests it as before (lower/push.rs tail_never_pushed), and each other field read from its push member is, as every use of it recalls it, its member's value where the test fails and the null of its type where it holds (fold_never_pushed, EmitCx::fold_tail): a select per field, no branch, no return. The tail's load lives while any other push read does, which is what the tail is for (a driver that uploads push bytes up to the highest one a stage reads). A tail field of the stage's own folds into the others alone. The fields read from their members load first, then the tail folds, then the spilled and derived fields, which may read through the folded parameter block's address. Same pushed bytes, same placement, no interface change. - lower/push.rs: emit_tail_guard becomes tail_never_pushed (the test) and fold_never_pushed. - lower/emit.rs: load_push_fields loads, folds (fold_tail), then loads the rest; emit_module no longer calls a guard. - SEMANTICS_VERSION 2280. - Test: the_floor_s_tail_folds_into_the_other_pushed_fields_with_no_branch (one select per other pushed field, no branch on the tail's test, one return), which fails on the guard. - Docs: lower/push.rs, PushField docs, docs/API.md section 5.
This commit is contained in:
1 parent
b3fd1a568c
commit
059835018a
7 files changed
+184
-47
No files matched your search
Vendored
+1
-1
@@ -60,7 +60,7 @@ mod tests;
|
||||
/// unchanged version, and its bless records the version. Independent of the crate's version and of
|
||||
/// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no
|
||||
/// history.
|
||||
pub const SEMANTICS_VERSION: u32 = 2279;
|
||||
pub const SEMANTICS_VERSION: u32 = 2280;
|
||||
|
||||
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
|
||||
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
|
||||
|
||||
@@ -184,8 +184,8 @@ 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: the floor's tail guard
|
||||
/// (`push::emit_tail_guard`) returns from an arm, and the hooks after it, the sink's address
|
||||
/// 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
|
||||
/// 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
|
||||
@@ -507,27 +507,47 @@ impl<'a> EmitCx<'a> {
|
||||
/// block, where it dominates every block. Where the body first reads a field never moves its
|
||||
/// 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).
|
||||
fn load_push_fields(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> {
|
||||
let plan: &Plan = self.plan;
|
||||
for placed in &plan.push.fields {
|
||||
match placed.field {
|
||||
PushField::SlotBounds(slot) => {
|
||||
self.slot_bounds(fb, slot)?;
|
||||
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;
|
||||
}
|
||||
field => {
|
||||
self.push_field(fb, field)?;
|
||||
match placed.field {
|
||||
PushField::SlotBounds(slot) => {
|
||||
self.slot_bounds(fb, slot)?;
|
||||
}
|
||||
field => {
|
||||
self.push_field(fb, field)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if read {
|
||||
self.fold_tail(fb)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Where the plan's placement ends with a tail (`lower::push::tail`): read it from the push
|
||||
/// block, and return where it holds the value the consumer never pushes there
|
||||
/// ([`crate::lower::push::emit_tail_guard`]). 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 emit_tail_guard(&mut self, fb: &mut FunctionBuilder<'_>) -> BResult<()> {
|
||||
/// 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<()> {
|
||||
let Some(field) = crate::lower::push::tail(&self.plan.push) else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -539,7 +559,35 @@ impl<'a> EmitCx<'a> {
|
||||
true => self.push_field(fb, field)?,
|
||||
false => Self::load_member(fb, push, member.index)?,
|
||||
};
|
||||
crate::lower::push::emit_tail_guard(fb, value, field)
|
||||
let Some(never) = crate::lower::push::tail_never_pushed(fb, value, field)? else {
|
||||
return Ok(());
|
||||
};
|
||||
let plan: &Plan = self.plan;
|
||||
for placed in &plan.push.fields {
|
||||
let other = placed.field;
|
||||
if other == field
|
||||
|| self.derives(other)
|
||||
|| !self.decl.push.iter().any(|m| m.field == other)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let keys = match other {
|
||||
PushField::SlotBounds(slot) => vec![
|
||||
Invariant::Push(other),
|
||||
Invariant::Bounds(slot, false),
|
||||
Invariant::Bounds(slot, true),
|
||||
],
|
||||
_ => vec![Invariant::Push(other)],
|
||||
};
|
||||
for key in keys {
|
||||
let Some(v) = self.recall(fb, key) else {
|
||||
return Err(self.fail("a pushed field not loaded before the tail folds"));
|
||||
};
|
||||
let folded = crate::lower::push::fold_never_pushed(fb, never, v)?;
|
||||
self.remember(fb, key, folded);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Checked slot `slot`'s bounds (`PushField::SlotBounds`), split: its buffer's bytes before
|
||||
@@ -1569,9 +1617,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`).
|
||||
.and_then(|()| em.cx.load_push_fields(&mut fb))
|
||||
// The floor's tail, read in the first block (`lower::push::emit_tail_guard`).
|
||||
.and_then(|()| em.cx.emit_tail_guard(&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
|
||||
// any hook reads a slot table.
|
||||
|
||||
@@ -34,11 +34,16 @@
|
||||
//! (`PushPlacement::tail_word`).
|
||||
//! 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
|
||||
//! returns where it holds a value the consumer never pushes there ([`emit_tail_guard`]), so no
|
||||
//! optimizer drops the read. 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.
|
||||
//! 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
|
||||
//! and adds no read where the tail is a field of its own, so the words a driver keeps in registers
|
||||
//! are the same.
|
||||
//!
|
||||
//! Field shapes are chosen so that the `ArgumentTable`'s std140 and std430 layouts agree (scalars,
|
||||
//! 2-vectors, and 16-byte-strided arrays only), so the table is a valid uniform block on every
|
||||
@@ -120,7 +125,7 @@ pub const fn vertex_end(bytes: u32) -> u32 {
|
||||
pub const HOLE_BYTES: u32 = 24;
|
||||
|
||||
/// Whether a field of kind `field` can be a floor stage's tail: one or two words whose value the
|
||||
/// consumer never pushes with every bit set, which the stage returns on ([`emit_tail_guard`]). A
|
||||
/// consumer never pushes with every bit set, which the stage tests for ([`tail_never_pushed`]). A
|
||||
/// slot's bounds hold a length of at most 2 GiB, and the zero range is at a multiple of 16
|
||||
/// (`PushField::SlotBounds`, `PushField::ZeroAddress`).
|
||||
pub const fn tail_kind(field: PushField) -> bool {
|
||||
@@ -429,16 +434,16 @@ 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: return from the function where 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. 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 emit_tail_guard(
|
||||
/// `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.
|
||||
pub(crate) fn tail_never_pushed(
|
||||
fb: &mut crate::spirv::builder::FunctionBuilder<'_>,
|
||||
value: crate::spirv::ids::Value,
|
||||
field: PushField,
|
||||
) -> crate::spirv::error::BResult<()> {
|
||||
use crate::spirv::builder::constructs::{ArmEnd, ArmFn};
|
||||
) -> crate::spirv::error::BResult<Option<crate::spirv::ids::Value>> {
|
||||
use crate::spirv::ir::{BinOp, ConstBits, Constant};
|
||||
let set = match (field, shape(field)) {
|
||||
(PushField::TailWord, _) => {
|
||||
@@ -463,14 +468,25 @@ pub(crate) fn emit_tail_guard(
|
||||
let ones = fb.module().const_value(ones)?;
|
||||
fb.binary(BinOp::IEqual, value, ones)?
|
||||
}
|
||||
(_, FieldShape::U32 | FieldShape::UVec4Array(_)) => return Ok(()),
|
||||
(_, FieldShape::U32 | FieldShape::UVec4Array(_)) => return Ok(None),
|
||||
};
|
||||
let leave: ArmFn<'_, ()> = Box::new(|fb, _| {
|
||||
fb.ret(None)?;
|
||||
Ok(ArmEnd::Exits)
|
||||
});
|
||||
fb.if_(&mut (), set, leave, None)?;
|
||||
Ok(())
|
||||
Ok(Some(set))
|
||||
}
|
||||
|
||||
/// `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.
|
||||
pub(crate) fn fold_never_pushed(
|
||||
fb: &mut crate::spirv::builder::FunctionBuilder<'_>,
|
||||
never: crate::spirv::ids::Value,
|
||||
value: crate::spirv::ids::Value,
|
||||
) -> crate::spirv::error::BResult<crate::spirv::ids::Value> {
|
||||
use crate::spirv::ir::Constant;
|
||||
let ty = fb.value_ty(value)?;
|
||||
let null = fb.module().constant(Constant::Null { ty })?;
|
||||
let null = fb.module().const_value(null)?;
|
||||
fb.select(never, null, value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -987,7 +987,7 @@ pub enum PushField {
|
||||
/// of 16, that nothing writes: a load of a buffer slot checked against its `SlotBounds`
|
||||
/// reads each of its units there where the check fails, so it reads zero (`docs/API.md`,
|
||||
/// section 5). Being a multiple of 16 it is never `u64::MAX`, the value a floor stage whose
|
||||
/// tail it is returns on (`lower::push::emit_tail_guard`).
|
||||
/// tail it is tests for (`lower::push::tail_never_pushed`).
|
||||
ZeroAddress,
|
||||
/// Under the identity domains, a buffer slot's buffer (`uvec2`): the bytes of the buffer before
|
||||
/// the slot's bind offset (the bind offset), then the buffer's length; for a `set*Bytes` copy 0
|
||||
@@ -997,13 +997,13 @@ pub enum PushField {
|
||||
/// len - min(len, n - 1)` with the limit made once per slot and size: outside, a load reads
|
||||
/// zero at `ZeroAddress` and a store or atomic runs nothing (`docs/API.md`, section 5). A
|
||||
/// buffer is at most 2 GiB, so the length is never `u32::MAX`: a floor stage whose tail it is
|
||||
/// returns where both words are all ones (`lower::push::emit_tail_guard`), which the consumer
|
||||
/// tests whether both words are all ones (`lower::push::tail_never_pushed`), which the consumer
|
||||
/// never pushes.
|
||||
SlotBounds(SlotRef),
|
||||
/// On `Device::from_queries`' profile, the zero word (`u32`) a stage
|
||||
/// whose pushed fields include no tail kind (`lower::push::tail_kind`) pushes as its tail,
|
||||
/// `lower::push::HOLE_BYTES` above them, and returns where it is non-zero: the consumer pushes
|
||||
/// it zero with the stage's other fields, so the branch is never taken. No placement lists it
|
||||
/// `lower::push::HOLE_BYTES` above them, and tests whether it is non-zero: the consumer pushes
|
||||
/// it zero with the stage's other fields, so the test never holds. No placement lists it
|
||||
/// among its fields: it is the push-block member at `PushPlacement::tail_word`.
|
||||
TailWord,
|
||||
}
|
||||
|
||||
@@ -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 guard (`push::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.
|
||||
//! 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.
|
||||
//!
|
||||
//! 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
|
||||
|
||||
@@ -2568,6 +2568,80 @@ fn the_window_pushes_a_kernel_s_slot_addresses_below_its_tail_word() {
|
||||
}
|
||||
}
|
||||
|
||||
/// On `PROFILES` and in `runtime-floor`'s window, [`many_of`]`(3)` pushes its 4 slot addresses
|
||||
/// below its tail word, and its straight-line module returns once: the tail's test, a compare of
|
||||
/// the word's load, is the condition of one select per other pushed field and of no branch
|
||||
/// (`EmitCx::fold_tail`), so the body depends on the tail through those values alone. 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`.
|
||||
#[test]
|
||||
fn the_floor_s_tail_folds_into_the_other_pushed_fields_with_no_branch() {
|
||||
use crate::spirv::ir::{BinOp, Inst, Terminator, Type};
|
||||
let reads = 3;
|
||||
let Some(bc) = assemble_text("many", &many_of(reads)) else {
|
||||
return;
|
||||
};
|
||||
for stem in PROFILES.iter().copied().chain(["runtime-floor"]) {
|
||||
let desc = crate::pipeline::testing::description(stem).unwrap_or_else(|e| panic!("{e}"));
|
||||
let device = Device::from_queries(desc, identity()).unwrap_or_else(|e| panic!("{e}"));
|
||||
let p = device.profile();
|
||||
let (a, plan, m) = compile_kept(&bc, "many", p).unwrap_or_else(|e| panic!("{stem}: {e}"));
|
||||
link_and_validate(&a, p).unwrap_or_else(|e| panic!("{stem}: L3: {e}"));
|
||||
assert!(plan.push.tail_word.is_some(), "{stem}: no tail word");
|
||||
let pushed = (plan.push.fields.iter())
|
||||
.filter(|f| matches!(f.loc, PushLoc::Push { .. }))
|
||||
.count();
|
||||
assert_eq!(pushed, (reads + 1) as usize, "{stem}");
|
||||
let insts: Vec<_> = m.blocks().iter().flat_map(|b| &b.insts).collect();
|
||||
let push_load = |v: crate::spirv::ids::Value| {
|
||||
insts.iter().any(|n| match (&n.inst, n.result) {
|
||||
(Inst::Load { ptr, .. }, Some(r)) if r == v => {
|
||||
matches!(
|
||||
m.value(*ptr).and_then(|d| m.types().get(d.ty)),
|
||||
Ok(Type::Pointer {
|
||||
class: crate::spirv::ir::StorageClass::PushConstant,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
};
|
||||
// The tail's tests: a compare of a push load that some select takes as its condition.
|
||||
let tests: Vec<_> = (insts.iter())
|
||||
.filter_map(|n| match (&n.inst, n.result) {
|
||||
(
|
||||
Inst::Binary {
|
||||
op: BinOp::INotEqual,
|
||||
a,
|
||||
..
|
||||
},
|
||||
Some(r),
|
||||
) if push_load(*a) => Some(r),
|
||||
_ => None,
|
||||
})
|
||||
.filter(|c| {
|
||||
(insts.iter()).any(|n| matches!(n.inst, Inst::Select { cond, .. } if cond == *c))
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(tests.len(), 1, "{stem}: the tail's tests");
|
||||
let test = tests[0];
|
||||
let selects = (insts.iter())
|
||||
.filter(|n| matches!(n.inst, Inst::Select { cond, .. } if cond == test))
|
||||
.count();
|
||||
assert_eq!(selects, pushed, "{stem}: one select per other pushed field");
|
||||
let branches = (m.blocks().iter())
|
||||
.filter(|b| matches!(b.term, Terminator::CondBranch { cond, .. } if cond == test))
|
||||
.count();
|
||||
assert_eq!(branches, 0, "{stem}: a branch on the tail");
|
||||
let returns = (m.blocks().iter())
|
||||
.filter(|b| matches!(b.term, Terminator::Return))
|
||||
.count();
|
||||
assert_eq!(returns, 1, "{stem}: the module's returns");
|
||||
println!("{stem}: the tail folds into {selects} pushed fields; one return");
|
||||
}
|
||||
}
|
||||
|
||||
/// A kernel storing, at its position in the grid `y · grid.x + x`, the grid's threads along `x` and
|
||||
/// `y` (bits 0-7, 8-15) and its threadgroups along `x` and `y` (bits 16-23, 24-31).
|
||||
fn grid_kernel(name: &str) -> String {
|
||||
|
||||
+4
-4
@@ -169,10 +169,10 @@ 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 returns at its start where its 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 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
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user