diff --git a/crates/metal2vulkan/src/spirv/requirements/writes.rs b/crates/metal2vulkan/src/spirv/requirements/writes.rs index 1daf54ee6..0d14915b3 100644 --- a/crates/metal2vulkan/src/spirv/requirements/writes.rs +++ b/crates/metal2vulkan/src/spirv/requirements/writes.rs @@ -15,14 +15,16 @@ //! (`lower::push`'s `the_status_address_is_always_placed_in_the_push_block`); one of a device that //! does not reaches it through its `Synthetic(ExecutionStatus)` storage buffer, the buffer's //! binding, which counts as the buffer too. A -//! written pointer is traced back through access chains, `OpConvertUToPtr`, and an `OpIAdd` of a -//! constant; it addresses the status buffer exactly when the trace ends at that binding or at a -//! load of that member. A pointer that traces anywhere else, or not at all (a phi, a parameter, a -//! selected value), is the function's: the guest never holds the status buffer's address, so no -//! write of the guest's is taken for the product's. +//! written pointer is traced back through access chains, `OpConvertUToPtr`, an `OpIAdd` of a +//! constant, and an `OpSelect` whose one hand is a constant (the floor's tail folds into each +//! pushed field so: `lower::push::fold_never_pushed`); it addresses the status buffer exactly when +//! the trace ends at that binding or at a load of that member. A pointer that traces anywhere else, +//! or not at all (a phi, a parameter, a selection of two values), is the function's: the guest +//! never holds the status buffer's address, so no write of the guest's is taken for the product's. //! -//! The trace also ends at an `OpIAdd` whose one operand is a load of the address member, whatever -//! the other: the guest does not hold the address, so no sum of the guest's has such an operand. +//! The trace also ends at an `OpIAdd` whose one operand is a load of the address member, folded or +//! not, whatever the other: the guest does not hold the address, so no sum of the guest's has such +//! an operand. //! //! A tile function's color attachment (`TileAttachment`) is the product's too: its writes carry //! Metal's color attachment write, a color output, which no other invocation or command reads but @@ -92,8 +94,8 @@ struct Status<'m> { block: Option, /// The indices of its `ExecutionStatusAddress` members, the product's address. members: Vec, - /// The defining instruction of every access chain, `OpConvertUToPtr`, `OpIAdd`, and load; - /// empty when the module reaches no status buffer. + /// The defining instruction of every access chain, `OpConvertUToPtr`, `OpIAdd`, `OpSelect`, + /// and load; empty when the module reaches no status buffer. defs: BTreeMap, } @@ -155,6 +157,7 @@ impl<'m> Status<'m> { op: BinOp::IAdd, .. } + | Inst::Select { .. } | Inst::Load { .. } ); if let (true, Some(r)) = (traced, n.result) { @@ -201,6 +204,10 @@ impl<'m> Status<'m> { } (true, true) => return Ok(false), }, + Some(Inst::Select { .. }) => match self.unfolded(m, v)? { + Some(other) => other, + None => return Ok(false), + }, Some(Inst::Load { ptr, .. }) => return self.is_member(m, *ptr), _ => return Ok(false), }; @@ -208,14 +215,29 @@ impl<'m> Status<'m> { Ok(false) } - /// Whether `v` is a load of the push block's product address member. + /// Whether `v` is a load of the push block's product address member, or such a load folded + /// ([`Self::unfolded`]). fn loads_member(&self, m: &TypedModule, v: Value) -> BResult { + let v = self.unfolded(m, v)?.unwrap_or(v); match self.defs.get(&v) { Some(Inst::Load { ptr, .. }) => self.is_member(m, *ptr), _ => Ok(false), } } + /// The other hand of `v` where `v` is an `OpSelect` whose one hand is a constant, the form the + /// floor's tail folds a pushed field in. + fn unfolded(&self, m: &TypedModule, v: Value) -> BResult> { + let Some(Inst::Select { t, f, .. }) = self.defs.get(&v) else { + return Ok(None); + }; + Ok(match (constant(m, *t)?, constant(m, *f)?) { + (true, false) => Some(*f), + (false, true) => Some(*t), + _ => None, + }) + } + /// Whether `ptr` points to the push block's `ExecutionStatusAddress` member. fn is_member(&self, m: &TypedModule, ptr: Value) -> BResult { let (Some(block), Some(Inst::AccessChain { base, indices, .. })) = diff --git a/crates/metal2vulkan/src/spirv/requirements/writes/tests.rs b/crates/metal2vulkan/src/spirv/requirements/writes/tests.rs index 0841e27a7..57b8ff2c3 100644 --- a/crates/metal2vulkan/src/spirv/requirements/writes/tests.rs +++ b/crates/metal2vulkan/src/spirv/requirements/writes/tests.rs @@ -65,9 +65,10 @@ fn fragment(mut b: ModuleBuilder, main: FuncId) -> TypedModule { /// A fragment that counts a resident miss as `lower::access::dynamic` does (`ResidentMisses += 1` /// and `Status |= RESIDENT_MISS`, relaxed device atomics at a constant offset from the push /// block's `ExecutionStatusAddress`), then reads through a slot's guest base and writes as `guest` -/// says. Its push members follow the kind order: `RootTableAddress`, `ExecutionStatusAddress`, then -/// the slot's `SlotGuestBase`. -fn miss_counting(guest: Guest) -> TypedModule { +/// says. Its push members follow the kind order: `RootTableAddress`, `ExecutionStatusAddress`, the +/// slot's `SlotGuestBase`, then, where `folded`, the floor's `TailWord`, whose test folds into each +/// other field as lowering folds it (`lower::push::fold_never_pushed`). +fn miss_counting(guest: Guest, folded: bool) -> TypedModule { let mut b = ModuleBuilder::new(); let u32_ = int(&mut b, 32); let u64_ = int(&mut b, 64); @@ -87,10 +88,28 @@ fn miss_counting(guest: Guest) -> TypedModule { let base = b .push_member(PushField::SlotGuestBase(slot), uvec2, 16) .expect("base field"); + let tail = folded.then(|| { + b.push_member(PushField::TailWord, u32_, 24) + .expect("tail field") + }); let push = b.push_global().expect("push block"); let void = b.ty(Type::Void).expect("void"); let mut f = b.function(void).expect("main"); - let at = field(&mut f, push, status.index); + let never = tail.map(|t| { + let word = field(&mut f, push, t.index); + crate::lower::push::tail_never_pushed(&mut f, word, PushField::TailWord) + .expect("test") + .expect("the tail's test") + }); + // Member `index`'s value, folded where the tail is. + let pushed = |f: &mut FunctionBuilder<'_>, index: u32| { + let v = field(f, push, index); + match never { + Some(never) => crate::lower::push::fold_never_pushed(f, never, v).expect("fold"), + None => v, + } + }; + let at = pushed(&mut f, status.index); for (word, op, value) in [ (StatusWord::ResidentMisses, AtomicOp::IAdd, 1), ( @@ -106,7 +125,7 @@ fn miss_counting(guest: Guest) -> TypedModule { f.atomic(op, p, Scope::Device, Semantics::RELAXED, v) .expect("atomic"); } - let words = field(&mut f, push, base.index); + let words = pushed(&mut f, base.index); let addr = f.convert(ConvOp::Bitcast, words, u64_).expect("address"); let sixteen = k(&mut f, u64_, 16); let addr = f.binary(BinOp::IAdd, addr, sixteen).expect("add"); @@ -120,7 +139,7 @@ fn miss_counting(guest: Guest) -> TypedModule { Guest::Reads => {} Guest::Stores => f.store(p, v, access).expect("store"), Guest::StoresThroughAnotherAddress => { - let other = field(&mut f, push, root.index); + let other = pushed(&mut f, root.index); let eight = k(&mut f, u64_, 8); let a = f.binary(BinOp::IAdd, other, eight).expect("add"); let q = f.device_place(a, u32_).expect("place"); @@ -178,12 +197,18 @@ fn status_binding() -> TypedModule { /// `uvec2` push member bitcast to an address, whose trace ends at the bitcast) does, and so does /// one that stores in the count's own shape through the push block's other `u64` address, /// `RootTableAddress`, whose trace reaches the push block's load and is told from the status -/// address by its member index alone. +/// address by its member index alone. Each holds with the floor's tail folded into every field. #[test] fn the_status_words_are_not_the_functions_writes() { - assert!(!writes_memory(&miss_counting(Guest::Reads)).expect("reads")); - assert!(writes_memory(&miss_counting(Guest::Stores)).expect("stores")); - assert!(writes_memory(&miss_counting(Guest::StoresThroughAnotherAddress)).expect("root")); + for folded in [false, true] { + let writes = |guest| writes_memory(&miss_counting(guest, folded)).expect("writes"); + assert!(!writes(Guest::Reads), "reads, folded {folded}"); + assert!(writes(Guest::Stores), "stores, folded {folded}"); + assert!( + writes(Guest::StoresThroughAnotherAddress), + "root, folded {folded}" + ); + } assert!(!writes_memory(&status_binding()).expect("binding")); } diff --git a/crates/metal2vulkan/src/tests/engine_identity_spill.rs b/crates/metal2vulkan/src/tests/engine_identity_spill.rs index d9074fa08..c8710b242 100644 --- a/crates/metal2vulkan/src/tests/engine_identity_spill.rs +++ b/crates/metal2vulkan/src/tests/engine_identity_spill.rs @@ -245,7 +245,9 @@ fn a_vertex_function_past_its_share_spills_the_checks_fields_into_its_block() { } /// The loads through a parameter block's address in disassembly `text`: a `ulong` loaded from the -/// push block, plus a constant (none for the block's first line, where the sum may fold), made a +/// push block (selected against null by the floor's tail's test where the function has the tail, +/// `lower::push::fold_never_pushed`), plus a constant (none for the block's first line, where the +/// sum may fold), made a /// pointer to a one-member block (`OpConvertUToPtr`), whose member is loaded. Their number, how /// many of them lie in a function's first block, and how many read a 16-byte line: a `uvec4`, /// `Aligned 16`, through a block whose member is `NonWritable`. @@ -285,6 +287,9 @@ fn block_loads(text: &str) -> (usize, usize, usize) { [id, "=", "OpLoad", "%ulong", chain, ..] if chains.contains(chain) => { bases.insert(id); } + [id, "=", "OpSelect", "%ulong", _, _, base] if bases.contains(base) => { + bases.insert(id); + } [id, "=", "OpIAdd", "%ulong", a, b] if (bases.contains(a) && constants.contains(b)) || (constants.contains(a) && bases.contains(b)) =>