From 8f117956efcbeeefc2956f6604246ec71586b2c9 Mon Sep 17 00:00:00 2001 From: Jacob McSwain Date: Thu, 3 Dec 2026 21:43:06 -0600 Subject: [PATCH] Translator: split whole-struct stack loads and stores into members Vertex functions that build their return value on the stack couldn't be translated. A whole struct stored to or loaded from an alloca reached lowering, which handles only scalars and vectors there, and was refused as Lowering(Access) AccessType. The shape involved, seen in modules Apple's Metal shader converter wrote: - a store of zeroinitializer to an alloca of the return struct; - stores to its members; - one load of the whole struct. What changes: the split that already turns a whole-aggregate access of device, constant and threadgroup memory into its members' accesses now covers stack memory too. Still kept whole, and refused by lowering as before: - a volatile access; - an aggregate that holds a pointer; - an aggregate with more members than the split takes. No public API changes. Evidence: - a_stack_aggregate_is_split checks the member stores and loads, their alignments and byte offsets, the two insertvalues, and that no whole-struct access is left. - what_is_not_split_stays_whole gains the three stack cases above. - The new fixture compiles, links and validates. - a_stack_aggregate_is_split and a_return_value_built_in_a_stack_slot_compiles fail without this change. - No release-mode timing was taken; the split adds a bounded walk per whole stack access. Version: SEMANTICS_VERSION 2389 -> 2390, because inputs refused before can now compile. Golden table: left for the bless on development; golden_hashes_match_the_table lists the new fixture's cells. The fixture's expectations row is included. Not run: the GPU device tests, the Windows clippy legs and the CI matrix. --- .../metal2vulkan-air/src/canon/aggregates.rs | 22 ++-- crates/metal2vulkan-air/src/canon/mod.rs | 4 +- .../src/canon/tests/aggregates.rs | 117 +++++++++++++++++- .../src/canon/tests/private_memory.rs | 13 +- .../public/vertex_stack_return_aggregate.ll | 29 +++++ crates/metal2vulkan/src/cache/mod.rs | 2 +- .../tests/engine_stack_return_aggregate.rs | 27 ++++ crates/metal2vulkan/src/tests/mod.rs | 1 + .../public_fixture_expectations_engine.tsv | 1 + docs/ARCHITECTURE.md | 2 +- 10 files changed, 203 insertions(+), 15 deletions(-) create mode 100644 crates/metal2vulkan-validation/fixtures/public/vertex_stack_return_aggregate.ll create mode 100644 crates/metal2vulkan/src/tests/engine_stack_return_aggregate.rs diff --git a/crates/metal2vulkan-air/src/canon/aggregates.rs b/crates/metal2vulkan-air/src/canon/aggregates.rs index 9cc7073e7..dd84809d7 100644 --- a/crates/metal2vulkan-air/src/canon/aggregates.rs +++ b/crates/metal2vulkan-air/src/canon/aggregates.rs @@ -2,8 +2,9 @@ //! //! An LLVM `load` or `store` may move a whole array or struct (`load [4 x <4 x float>], ptr //! addrspace(2) %p`): Metal's own linker emits one where it passes a field of a buffer's struct to -//! a function by value. Lowering reads and writes buffer and threadgroup memory a scalar or vector -//! at a time. After SCCP, [`expand`] replaces each such access, in its place, by its members' (the +//! a function by value, and a converter-built vertex function's return value is one stored to and +//! loaded from a stack allocation. Lowering reads and writes buffer, threadgroup and stack memory +//! a scalar or vector at a time. After SCCP, [`expand`] replaces each such access, in its place, by its members' (the //! scalars and vectors the type holds, in layout order): a `getelementptr i8` at the member's //! constant byte distance from the access's pointer (none at distance 0), and a load chained into //! the aggregate by `insertvalue`, starting from `undef`; a store's members are each an @@ -11,11 +12,13 @@ //! of two dividing its distance; an access that states none leaves its members' unstated (each //! member's ABI alignment, which its offset in the layout honours). //! -//! What is split: a non-volatile, non-atomic access in `device`, `constant` or `threadgroup` -//! memory of an array or struct that holds no pointer, with at least one and at most -//! [`MAX_MEMBERS`] members, nested at most [`MAX_DEPTH`] deep. The rest stays whole: `thread` -//! memory's allocations the memory planner takes whole, a pointer member (a handle or a resource) -//! is lowering's to follow, and lowering holds no larger aggregate, so it refuses the access. +//! What is split: a non-volatile, non-atomic access in `device`, `constant`, `threadgroup` or +//! `thread` memory of an array or struct that holds no pointer, with at least one and at most +//! [`MAX_MEMBERS`] members, nested at most [`MAX_DEPTH`] deep. The rest stays whole: a pointer +//! member (a handle or a resource) is lowering's to follow, and lowering holds no larger +//! aggregate, so it refuses the access. A stack allocation's whole-aggregate access is split like +//! the rest because lowering holds only scalars and vectors there too: left whole, it was refused +//! as `Lowering(Access)` `AccessType`. use crate::budget::Meter; use crate::error::Error; @@ -213,7 +216,10 @@ fn split_of( let split = matches!( tl.0.get(ptr_ty), Some(Type::Ptr { - space: AddrSpace::Device | AddrSpace::Constant | AddrSpace::Threadgroup, + space: AddrSpace::Device + | AddrSpace::Constant + | AddrSpace::Threadgroup + | AddrSpace::Thread, }) ); if !split { diff --git a/crates/metal2vulkan-air/src/canon/mod.rs b/crates/metal2vulkan-air/src/canon/mod.rs index 10259558a..880e1488e 100644 --- a/crates/metal2vulkan-air/src/canon/mod.rs +++ b/crates/metal2vulkan-air/src/canon/mod.rs @@ -84,7 +84,7 @@ //! through the call's map pointer and Apple's arithmetic. **AIR 2.7 simdgroup-matrix block loads and //! stores** take their AIR 2.8 form, their extent, stride and origin built from the elements per //! row, the origin and the constant transpose flag (`legacy_matrix`). **Aggregate accesses**: a -//! load or store of a whole array or struct in buffer or threadgroup memory becomes its scalar +//! load or store of a whole array or struct in buffer, threadgroup or stack memory becomes its scalar //! and vector members' accesses, joined by `insertvalue` or split by `extractvalue` //! (`aggregates`). **Constant references**: in a kernel, each plain load of a scalar or vector //! from a `constant T&` parameter's object, at an offset fixed at translation time, is made in @@ -764,7 +764,7 @@ pub fn specialize_dynamic( // in plain AIR, each OpenCL C `mad` call its multiply and addition, and each `mul24` and // `mad24` call the 32-bit product of its operands' low 24 bits (and the addition). let body = routine::expand(body, &mut parts, m)?; - // Aggregate loads and stores of buffer and threadgroup memory as their members' accesses. + // Aggregate loads and stores of buffer, threadgroup and stack memory as their members' accesses. let body = aggregates::expand(body, &mut parts, func, m)?; // A kernel's plain loads of its constant references, made in its entry block where nothing // before them may write device memory. diff --git a/crates/metal2vulkan-air/src/canon/tests/aggregates.rs b/crates/metal2vulkan-air/src/canon/tests/aggregates.rs index 310f2b400..1719caa72 100644 --- a/crates/metal2vulkan-air/src/canon/tests/aggregates.rs +++ b/crates/metal2vulkan-air/src/canon/tests/aggregates.rs @@ -1,4 +1,4 @@ -//! Aggregate loads and stores of buffer and threadgroup memory, split into their members' accesses +//! Aggregate loads and stores of buffer, threadgroup and stack memory, split into their members' accesses //! (`canon::aggregates`). use super::*; @@ -31,6 +31,15 @@ enum Move { /// `store [2 x i32] [7, 7]` into a threadgroup array, loaded back whole, element 1 stored to /// `out`. Threadgroup, + /// `store { <2 x float>, i32 } zeroinitializer` into a stack allocation, field 1 overwritten + /// from the constant buffer, the whole loaded back, its field 1 stored to `out`. + Stack, + /// [`Move::Stack`] with a volatile whole load. + StackVolatile, + /// A `{ ptr addrspace(1), i32 }` stack allocation, zero-stored and loaded whole. + StackPointer, + /// A `[300 x float]` stack allocation, zero-stored and loaded whole. + StackWide, } /// `kernel k(constant void *in [[buffer(0)]], device void *out [[buffer(1)]])`, moving an aggregate @@ -81,6 +90,16 @@ fn moved(how: Move) -> Entry { let k = m.constant(i32t, Const::Int(7)); Value::Const(m.constant(tg_ty, Const::Aggregate([k, k].into()))) }; + let m_thread = m.ptr(AddrSpace::Thread); + let stack_ty = m.struct_ty(&[v2, i32t]); + let zero_of = |m: &mut ModuleBuilder, ty| Value::Const(m.constant(ty, Const::Null)); + let (stack_zero, ptr_zero, wide_zero) = ( + zero_of(&mut m, stack_ty), + zero_of(&mut m, with_ptr), + zero_of(&mut m, wide), + ); + let zero64 = m.const_int(i64t, 0); + let one = m.const_int(i32t, 1); let fty = m.fn_ty(void, &[cst, dev]); let mut f = m.define("k", fty); let load_as = |f: &mut FunctionBuilder<'_>, ty: TypeId, ptr: Value, volatile: bool| { @@ -177,6 +196,69 @@ fn moved(how: Move) -> Entry { let x = extract(&mut f, i32t, whole, 1); store_as(&mut f, x, 4); } + Move::Stack | Move::StackVolatile | Move::StackPointer | Move::StackWide => { + let ty = match how { + Move::StackPointer => with_ptr, + Move::StackWide => wide, + _ => stack_ty, + }; + let zero = match how { + Move::StackPointer => ptr_zero, + Move::StackWide => wide_zero, + _ => stack_zero, + }; + let slot = f.push( + m_thread, + InstKind::Alloca { + ty, + count: one, + align: None, + }, + ); + f.push_void(InstKind::Store { + val: zero, + ptr: slot, + align: Align::from_bytes(8), + volatile: false, + atomic: None, + }); + { + let live = load_as(&mut f, i32t, arg(0), false); + let field1 = f.push( + m_thread, + InstKind::Gep { + src_ty: ty, + base: slot, + indices: [zero64, one].into(), + inbounds: true, + }, + ); + f.push_void(InstKind::Store { + val: live, + ptr: field1, + align: Align::from_bytes(4), + volatile: false, + atomic: None, + }); + } + // The slot is 8-aligned (its zero store says so), and so is the whole load. + let whole = f.push( + ty, + InstKind::Load { + ptr: slot, + align: Align::from_bytes(8), + volatile: how == Move::StackVolatile, + atomic: None, + range: None, + speculative: false, + }, + ); + let x = match how { + Move::StackWide => extract(&mut f, f32t, whole, 7), + _ => extract(&mut f, i32t, whole, 1), + }; + store_as(&mut f, x, 4); + } Move::Wide => { let whole = load_as(&mut f, wide, arg(0), false); let x = extract(&mut f, f32t, whole, 7); @@ -251,6 +333,36 @@ fn a_threadgroup_aggregate_is_split() { assert_eq!(text.matches("store ").count(), 3, "{text}"); } +/// A whole struct stored to and loaded from a stack allocation, where the rest of the body +/// writes it in part, is its members' accesses as a buffer's is: the zero store is two member +/// stores (aligned 8 and 8, the second at byte 8), the whole load two member loads (8 and 8) +/// joined by two `insertvalue`s from `undef`, and no access of the whole struct is left. +#[test] +fn a_stack_aggregate_is_split() { + let text = body_text(&moved(Move::Stack)); + let lines = |what: &str| text.lines().filter(|l| l.contains(what)).count(); + assert_eq!(lines("load %t7"), 0, "{text}"); + assert_eq!(lines("store %t7"), 0, "{text}"); + assert_eq!(lines("alloca"), 1, "{text}"); + assert_eq!( + lines("extractvalue <2 x float> %t7 zeroinitializer, 0"), + 1, + "{text}" + ); + assert_eq!( + lines("extractvalue i32 %t7 zeroinitializer, 1"), + 1, + "{text}" + ); + assert_eq!(lines("store %i1, %i0, align 8"), 1, "{text}"); + assert_eq!(lines("store %i3, %i4, align 8"), 1, "{text}"); + assert_eq!(lines("load <2 x float>, %i0, align 8"), 1, "{text}"); + assert_eq!(lines("load i32, %i11, align 8"), 1, "{text}"); + assert_eq!(lines("getelementptr inbounds i8, %i0, i64 8"), 2, "{text}"); + assert_eq!(lines("insertvalue"), 2, "{text}"); + assert_eq!(lines("insertvalue %t7 undef"), 1, "{text}"); +} + /// A volatile access, an aggregate that holds a pointer, and one of more members than the split /// takes stay whole. #[test] @@ -259,6 +371,9 @@ fn what_is_not_split_stays_whole() { (Move::Volatile, "load volatile %t8"), (Move::PointerStruct, "load %t5"), (Move::Wide, "load %t5"), + (Move::StackVolatile, "load volatile %t7"), + (Move::StackPointer, "= load %t"), + (Move::StackWide, "= load %t"), ] { let text = body_text(&moved(how)); assert!(text.contains(whole), "{how:?}: {text}"); diff --git a/crates/metal2vulkan-air/src/canon/tests/private_memory.rs b/crates/metal2vulkan-air/src/canon/tests/private_memory.rs index f65b969bc..4fd6334d0 100644 --- a/crates/metal2vulkan-air/src/canon/tests/private_memory.rs +++ b/crates/metal2vulkan-air/src/canon/tests/private_memory.rs @@ -229,7 +229,8 @@ fn an_absent_value_stored_into_private_bytes_nothing_reads_is_unobserved() { } /// Every read that reaches the gated-off field's bytes observes the store: the field itself, an -/// aggregate load covering it, one byte of it, the field through the pointer loaded back from the +/// aggregate load covering it (split into its members' loads, joined from an `undef` by +/// `insertvalue`), one byte of it, the field through the pointer loaded back from the /// cell, a copy of the struct, and any read once the struct's address escapes. The observed value /// reads zero. #[test] @@ -248,7 +249,15 @@ fn a_read_that_reaches_an_absent_value_in_private_memory_observes_it() { text.contains("store i32 zeroinitializer"), "{reach:?}: {text}" ); - assert!(!text.contains("undef"), "{reach:?}: {text}"); + // The one `undef` left is the start of the `insertvalue` chain that joins a split load's + // members: `insertvalue %tN undef %iM, 0`. + let observed_undef = text.lines().any(|l| { + l.contains("undef") + && !(l.contains("= insertvalue %t") + && l.contains(" undef %i") + && l.ends_with(", 0")) + }); + assert!(!observed_undef, "{reach:?}: {text}"); assert!(!text.contains("%a1"), "{reach:?}: {text}"); } } diff --git a/crates/metal2vulkan-validation/fixtures/public/vertex_stack_return_aggregate.ll b/crates/metal2vulkan-validation/fixtures/public/vertex_stack_return_aggregate.ll new file mode 100644 index 000000000..1c543d494 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/vertex_stack_return_aggregate.ll @@ -0,0 +1,29 @@ +; Owned synthetic fixture: a vertex function that builds its return value in a stack slot, in the +; shape seen in modules Apple's Metal shader converter wrote: a store of zeroinitializer, element +; stores, one load of the whole struct. Written by hand; not derived from a third-party metallib. +source_filename = "vertex_stack_return_aggregate.ll" +target triple = "air64-apple-macosx14.0.0" + +%struct.Out = type { <4 x float>, <2 x float> } + +define %struct.Out @stack_return_vertex(<2 x float> %position, <2 x float> %uv) { + %slot = alloca %struct.Out, align 16 + store %struct.Out zeroinitializer, ptr %slot, align 16 + %extended = shufflevector <2 x float> %position, <2 x float> poison, <4 x i32> + %clip = shufflevector <4 x float> %extended, <4 x float> , <4 x i32> + %p0 = getelementptr inbounds %struct.Out, ptr %slot, i32 0, i32 0 + store <4 x float> %clip, ptr %p0, align 16 + %p1 = getelementptr inbounds %struct.Out, ptr %slot, i32 0, i32 1 + store <2 x float> %uv, ptr %p1, align 8 + %whole = load %struct.Out, ptr %slot, align 16 + ret %struct.Out %whole +} + +!air.vertex = !{!0} +!0 = !{ptr @stack_return_vertex, !1, !4} +!1 = !{!2, !3} +!2 = !{!"air.position", !"air.arg_type_name", !"float4", !"air.arg_name", !"position"} +!3 = !{!"air.vertex_output", !"generated(2uvDv2_f)", !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} +!4 = !{!5, !6} +!5 = !{i32 0, !"air.vertex_input", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"float2", !"air.arg_name", !"position"} +!6 = !{i32 1, !"air.vertex_input", !"air.location_index", i32 1, i32 1, !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index fdba05a25..346de8df3 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -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 = 2389; +pub const SEMANTICS_VERSION: u32 = 2390; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/tests/engine_stack_return_aggregate.rs b/crates/metal2vulkan/src/tests/engine_stack_return_aggregate.rs new file mode 100644 index 000000000..0bbbf2bd9 --- /dev/null +++ b/crates/metal2vulkan/src/tests/engine_stack_return_aggregate.rs @@ -0,0 +1,27 @@ +//! A vertex function that builds its return value in a stack slot compiles. +//! +//! The owned fixture `vertex_stack_return_aggregate` stores a zeroinitializer struct to an +//! alloca, writes its members, and loads the whole struct back, the shape seen in modules Apple's +//! Metal shader converter wrote. `llvm-as` assembles the fixture; a missing tool skips +//! visibly. + +use crate::pipeline::testing::{ + compile, fixtures_dir, link_and_validate, llvm_assemble, profile, AssembleError, +}; + +/// The vertex function compiles, links and validates. +#[test] +fn a_return_value_built_in_a_stack_slot_compiles() { + let path = fixtures_dir().join("vertex_stack_return_aggregate.ll"); + let bc = match llvm_assemble(&path) { + Ok(b) => b, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the fixture: {why}"), + }; + let p = profile("nvidia-rtx50-linux").expect("profile"); + let a = compile(&bc, "stack_return_vertex", &p).unwrap_or_else(|e| panic!("{e}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("L3: {e}")); +} diff --git a/crates/metal2vulkan/src/tests/mod.rs b/crates/metal2vulkan/src/tests/mod.rs index 84c7da5af..a28554bc3 100644 --- a/crates/metal2vulkan/src/tests/mod.rs +++ b/crates/metal2vulkan/src/tests/mod.rs @@ -164,6 +164,7 @@ mod engine_specialized_folds; mod engine_specialized_name; mod engine_spirv_cross_forms; mod engine_srgb_decode; +mod engine_stack_return_aggregate; mod engine_stage_io; mod engine_stage_io_tail; mod engine_stage_root_attributes; diff --git a/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv b/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv index 388c30892..87206a883 100644 --- a/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv +++ b/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv @@ -180,6 +180,7 @@ vertex_escaping_pointer_integers.ll escaping_pointer_integers ok ok ok ok ok ok vertex_fetch_buffer_without_address_space.ll fetch_from_device ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok vertex_fetch_buffer_without_address_space.ll fetch_from_constant ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok vertex_narrow_attributes.ll vertex_narrow_attributes ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok +vertex_stack_return_aggregate.ll stack_return_vertex ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok vertex_varying_triangle.ll vertex_varying_triangle ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok engine/a1_bool_from_float.air.ll a1_bool_from_float ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok engine/a1_bool_from_float.ll a1_bool_from_float ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 6addb6556..549b1ceff 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -72,7 +72,7 @@ rest is `crates/metal2vulkan/src/`. | Module | Role | |---|---| -| `air` (`metal2vulkan-air`) | The container (metallib, bitcode wrapper, raw module), the bitstream and bitcode readers, the typed IR, the `air.*` and `llvm.*` intrinsic families, the metadata ABI (`AirAbi`), and canonicalization (`canon`: inlining (each `byval` argument's copy made at its call first), function constants, SCCP (a value one of a few constants, from a PHI of literal constants or a `select` between two, folded member by member so a branch every member decides alike is decided; reading the integer fields of a constant global whose value is known, folding the integer bit operations `llvm.ctpop`, `llvm.bitreverse` and `llvm.bswap` and the scalar `llvm.umin`, `llvm.umax`, `llvm.smin` and `llvm.smax` on constants, settling the float operations a constant operand decides whatever the others hold (a NaN operand, the first in order: as it is beside a value the run computes, quieted where every operand is a constant; a product by a zero under `nnan` and `nsz`, `+0`), and reading the immediates of an intrinsic call that its function's parameters gave it once inlining makes them its call site's constants), the unrolling of loops whose constant trip count decides a branch where that leaves the entry no larger, the allocation of a private array of nested arrays as far as its indices reach where constants and counted loops bound every one of them (`canon::reach`), a pointer loaded from a constant table at an index only the run knows (a table of constexpr samplers) read as the `select` chain over the table's elements (`canon::const_tables`), each of MSL 4.1's generic pointers made a pointer of the one address space every pointer it may hold comes from, its space queries answered (`canon::generic`), devirtualization, linking, the load of a stack allocation's field that one dominating store wrote read as the stored value (an aggregate holding a pointer excepted), and an allocation no read is left of removed with its stores, so a lambda's closure of captured addresses leaves the captured objects' own (`canon::forward`), pruning, pointee erasure, the speculation of the short branch arms Metal's backend runs in every lane, the split of a whole array's or struct's load or store in buffer or threadgroup memory into its members' accesses, a kernel's plain loads of a `constant T&` parameter's object at an offset fixed at translation time made in its entry block where nothing on any path before them may write device memory and no other root shares the parameter's pointer, each marked speculative, as the speculated arms' loads are, so lowering contains it against a short or absent binding at any offset (`canon::const_hoist`), and a kernel of a pipeline of OpenCL mode linked as Metal links it: the three hidden buffers appended, its buffer indices 3 on, OpenCL C's work-item reads made reads of their built-ins or of the first buffer, each image format query a read of the third, each `air.wg.barrier`'s flags read as OpenCL C's `cl_mem_fence_flags`, and each operation of a 1D image the 2D texture's that Apple's OpenCL binds for it, `canon::opencl` and `canon::opencl_1d`; and OpenCL C's vector data loads and stores made their element accesses, `canon::vector_data`). Canonicalization also writes the formats the binder and runtime read: the visible-function table's words and the rasterization-rate map's parameter data | +| `air` (`metal2vulkan-air`) | The container (metallib, bitcode wrapper, raw module), the bitstream and bitcode readers, the typed IR, the `air.*` and `llvm.*` intrinsic families, the metadata ABI (`AirAbi`), and canonicalization (`canon`: inlining (each `byval` argument's copy made at its call first), function constants, SCCP (a value one of a few constants, from a PHI of literal constants or a `select` between two, folded member by member so a branch every member decides alike is decided; reading the integer fields of a constant global whose value is known, folding the integer bit operations `llvm.ctpop`, `llvm.bitreverse` and `llvm.bswap` and the scalar `llvm.umin`, `llvm.umax`, `llvm.smin` and `llvm.smax` on constants, settling the float operations a constant operand decides whatever the others hold (a NaN operand, the first in order: as it is beside a value the run computes, quieted where every operand is a constant; a product by a zero under `nnan` and `nsz`, `+0`), and reading the immediates of an intrinsic call that its function's parameters gave it once inlining makes them its call site's constants), the unrolling of loops whose constant trip count decides a branch where that leaves the entry no larger, the allocation of a private array of nested arrays as far as its indices reach where constants and counted loops bound every one of them (`canon::reach`), a pointer loaded from a constant table at an index only the run knows (a table of constexpr samplers) read as the `select` chain over the table's elements (`canon::const_tables`), each of MSL 4.1's generic pointers made a pointer of the one address space every pointer it may hold comes from, its space queries answered (`canon::generic`), devirtualization, linking, the load of a stack allocation's field that one dominating store wrote read as the stored value (an aggregate holding a pointer excepted), and an allocation no read is left of removed with its stores, so a lambda's closure of captured addresses leaves the captured objects' own (`canon::forward`), pruning, pointee erasure, the speculation of the short branch arms Metal's backend runs in every lane, the split of a whole array's or struct's load or store in buffer, threadgroup or stack memory into its members' accesses, a kernel's plain loads of a `constant T&` parameter's object at an offset fixed at translation time made in its entry block where nothing on any path before them may write device memory and no other root shares the parameter's pointer, each marked speculative, as the speculated arms' loads are, so lowering contains it against a short or absent binding at any offset (`canon::const_hoist`), and a kernel of a pipeline of OpenCL mode linked as Metal links it: the three hidden buffers appended, its buffer indices 3 on, OpenCL C's work-item reads made reads of their built-ins or of the first buffer, each image format query a read of the third, each `air.wg.barrier`'s flags read as OpenCL C's `cl_mem_fence_flags`, and each operation of a 1D image the 2D texture's that Apple's OpenCL binds for it, `canon::opencl` and `canon::opencl_1d`; and OpenCL C's vector data loads and stores made their element accesses, `canon::vector_data`). Canonicalization also writes the formats the binder and runtime read: the visible-function table's words and the rasterization-rate map's parameter data | | `graph` | The shared RPO, dominator tree, loop forest and SCC | | `budget` | `Budget`, `CancelToken`, and the per-attempt `Meter`: deadline, cancellation, work units, the byte ledger and every structural cap | | `memory/` | Pointer provenance, the `MemoryPlan`, leaf access emitters, arenas, root and handle lookups, word composition, atomics; `memory::views`, a descriptor slot's views: a storage block or a uniform block |