SEMANTICS 2134 / RUNTIME 2427: A uniform view's storage twin is decorated NonWritable

What: memory::views::declare_twin states the twin's binding unwritten
(ModuleBuilder::unwritten_buffer). The serializer then decorates the twin's variables NonWritable,
its word view and its uvec4 view, where its own walk finds no store reaching them. No store goes
through a twin: TwinMem's write, write_bits, write_unit and write_vec all refuse.

Why: on the Iris profile (DriverWorkaround::PushReadsLengthenThreadPayload, ANV, Mesa 26.2.6)
RUNTIME 2413 reads a kernel's constant slot through its storage twin where the slot's
RangeAtBindOffset holds. The twin was not decorated NonWritable, because serialize::non_writable
only takes the bindings the lowering states unwritten. ANV runs nir_opt_access
(anv_shader_compile.c), which marks a storage buffer's loads reorderable only where the variable
is NonWritable or the shader writes no buffer. A kernel that writes its output therefore kept
every twin load apart, wherever it stood: no CSE, no hoisting, no merging. The uniform view's
loads, a uniform buffer's, are always reorderable, and the driver folded them.

Metal's compiler leaves a constant buffer's repeated loads for its own backend to fold. One guest
depthwise convolution kernel loads its params.srcSize (<4 x i32>) four times in every iteration
of its tap loop and its dstSize about ten times, and every inlined sample reads its ndArrayParams
again. Read through the twin, each of those loads ran per thread. A benchmark's Iris walk rose
from 754 to 1120 us a dispatch on that workload between pins 825997e49c and 44505b3807, and
825997e49c rebuilt matched the old walk (771). dispatch_chain's bump_wide reads its constant
word once, so the 2413 A/B could not show this. --binds params now can.

Aliasing (why NonWritable states nothing the uniform view did not):
- storage_twins gives a twin only to a slot whose form is a uniform view by its own window. That
  is view_kind's uniform view: a Metal constant address-space slot that no store or atomic of the
  function reaches, with a Bounded window, never the per-stage storage-buffer fallback.
- bind::instantiate::bind_buffers writes the twin's descriptor with the uniform binding's own
  buffer and DescriptorRange, pushed once for both.
- So the twin's bytes are exactly the uniform view's, under the same rule that chose a uniform
  view before the twin existed (the base form, 825997e49c).
- A uniform buffer is read-only to the module, and nothing orders it with a write through another
  descriptor of the same dispatch. Metal's constant address space is read-only for the dispatch.
  A guest that binds one MTLBuffer range as both a constant and a written device slot got no
  ordering from the uniform view either; the decoration claims no more than that.
- The runtime binds a twin nowhere the uniform view is not bound. A slot bound to nothing binds
  both from the device's zero buffer (RUNTIME 2420), which no command writes.

Docs: views.rs (the module docs and declare_twin), ModuleBuilder::unwritten_buffer,
serialize/non_writable.rs, the workaround's profile doc, docs/API.md section 5.

Test: engine_access's
a_kernel_uniform_view_reads_its_storage_twin_at_its_bind_offset asserts that every variable bound
at the twin's binding is decorated NonWritable. It fails without the change, where no twin
variable is.

SEMANTICS_VERSION 2134: translation output changes on the Iris profile alone (the twin's
decoration). The golden hashes and the authored compile census re-bless on development. New
Degradation kinds: none.

Also: dispatch_chain gains --binds params and params-off (bench only), a kernel that reads its
constant block again in a loop: params reads it through the twin at its bind offset
(RangeAtBindOffset 1), params-off through the uniform view at its residual (the base form).

Verified (ftreg, msi m3-max-35578 at 00a41f052f): clippy -p metal2vulkan -p metal2vulkan-runtime
--all-targets --all-features -D warnings 0; twin/NonWritable tests 44/44; negative control
(views.rs before the change) fails engine_access's twin test with [(12, false)]. Release A/B
arms built (35573/35574/35576); the Iris A/B follows. RUNTIME 2427: the translation changed.
This commit is contained in:
Anees Iqbal committed 2026-11-16 00:20:20 -08:00
1 parent be19769dcd
commit a585a74686
8 files changed
+189 -25

No files matched your search

@@ -5,7 +5,7 @@
//! ```sh
//! cargo run --release -p metal2vulkan-runtime --example dispatch_chain -- \
//! [--dispatches N] [--groups G] [--threads T] [--runs R] [--only SHAPE,...] \
//! [--binds 1|4|tex|ptr] [--host C] [--pages] [--heap] [--heap-churn K] [--heap-kept H] \
//! [--binds 1|4|tex|ptr|params|params-off] [--host C] [--pages] [--heap] [--heap-churn K] [--heap-kept H] \
//! [--live L] [--idle MS] [--vary] [--ragged] [--samplers K] [--classes K] [--zero-classes Z]
//! [--stay Q] [--states K]
//! ```
@@ -114,6 +114,15 @@
//! (`a[gid] += k`, plus 1000 where a texel's red is below -1e30, which none is): each dispatch
//! binds the pipeline state, the two textures (the other way round from the dispatch before it),
//! the sampler, the words and the bytes, the shape of Geekbench's compute workloads' encoders.
//! With `--binds params` they run `bump_params`, which reads a `constant` block of two `uint4`
//! set as bytes at index 1: its second `uint4`'s first word bounds a loop of [`PARAMS_STEPS`]
//! steps, and each step reads the first `uint4` again four times, between two reads of a
//! `Private` buffer of zeros at index 2 at indices the block's words steer, as a convolution
//! kernel's tap loop reads its sizes again around each sample (Metal's compiler leaves such
//! repeated `constant` loads for its own backend to fold): `a[gid] += 1` once the loop ends.
//! `--binds params-off` binds the block as a buffer at offset [`PARAMS_OFFSET`], which no view
//! base alignment of 32 or more divides, so where a kernel's uniform view would read the block at
//! its bind offset alone it reads it at its residual.
//! With `--binds ptr` they run `bump_ptr`, `bump_wide` with its first zero buffer read through an
//! address the dispatch sets as bytes at index 1 (a `Holder` of one pointer, as an argument buffer
//! holds it): a command that loads a pointer, which the device's resident set of its live buffers
@@ -257,6 +266,75 @@ entry:\n\
!8 = !{i32 2, i32 8, i32 0}\n!9 = !{!\"Metal\", i32 4, i32 0, i32 0}\n\
!20 = !{ptr @bump_wide, !3, !4}\n";
/// The steps of `bump_params`'s loop, its block's word 4.
const PARAMS_STEPS: u32 = 16;
/// `bump_params`'s block: its first `uint4` (the words the loop's steps read, which leave each
/// word as it was), then the loop's steps and the 1 the kernel adds once it ends.
const PARAMS: [u32; 8] = [0, 1, 0, 0, PARAMS_STEPS, 1, 0, 0];
/// The offset `--binds params-off` binds `bump_params`'s block at.
const PARAMS_OFFSET: u64 = 16;
/// `bump_params`: `a[gid] += 1` after [`PARAMS_STEPS`] steps (`p`'s word 4) that each read `p`'s
/// first `uint4` four times, between reads of `s`; `a` a buffer of words at index 0, `p` a
/// `constant` block of two `uint4` ([`PARAMS`]) at 1, `s` a read-only buffer of zeros at 2.
const KERNEL_PARAMS: &str = "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32\"\n\
target triple = \"air64_v28-apple-macosx26.0.0\"\n\n\
define void @bump_params(ptr addrspace(1) noundef %a, ptr addrspace(2) noundef %p, ptr addrspace(1) noundef readonly %s, i32 noundef %gid) {\n\
entry:\n\
%gw = zext i32 %gid to i64\n\
%pa = getelementptr inbounds i32, ptr addrspace(1) %a, i64 %gw\n\
%x0 = load i32, ptr addrspace(1) %pa, align 4\n\
%pq = getelementptr inbounds <4 x i32>, ptr addrspace(2) %p, i64 1\n\
%q = load <4 x i32>, ptr addrspace(2) %pq, align 16\n\
%n = extractelement <4 x i32> %q, i64 0\n\
%one = extractelement <4 x i32> %q, i64 1\n\
br label %loop\n\n\
loop:\n\
%i = phi i32 [ 0, %entry ], [ %i1, %loop ]\n\
%x = phi i32 [ %x0, %entry ], [ %x4, %loop ]\n\
%v0 = load <4 x i32>, ptr addrspace(2) %p, align 16\n\
%e0 = extractelement <4 x i32> %v0, i64 0\n\
%j0 = and i32 %i, %e0\n\
%k0 = xor i32 %gid, %j0\n\
%kw0 = zext i32 %k0 to i64\n\
%ps0 = getelementptr inbounds i32, ptr addrspace(1) %s, i64 %kw0\n\
%y0 = load i32, ptr addrspace(1) %ps0, align 4\n\
%x1 = add i32 %x, %y0\n\
%v1 = load <4 x i32>, ptr addrspace(2) %p, align 16\n\
%e1 = extractelement <4 x i32> %v1, i64 1\n\
%x2 = mul i32 %x1, %e1\n\
%v2 = load <4 x i32>, ptr addrspace(2) %p, align 16\n\
%e2 = extractelement <4 x i32> %v2, i64 2\n\
%j2 = and i32 %i, %e2\n\
%k2 = xor i32 %gid, %j2\n\
%kw2 = zext i32 %k2 to i64\n\
%ps2 = getelementptr inbounds i32, ptr addrspace(1) %s, i64 %kw2\n\
%y2 = load i32, ptr addrspace(1) %ps2, align 4\n\
%x3 = add i32 %x2, %y2\n\
%v3 = load <4 x i32>, ptr addrspace(2) %p, align 16\n\
%e3 = extractelement <4 x i32> %v3, i64 3\n\
%x4 = xor i32 %x3, %e3\n\
%i1 = add i32 %i, 1\n\
%c = icmp ult i32 %i1, %n\n\
br i1 %c, label %loop, label %exit\n\n\
exit:\n\
%v = add i32 %x4, %one\n\
store i32 %v, ptr addrspace(1) %pa, align 4\n\
ret void\n\
}\n\n\
!llvm.module.flags = !{!0, !1}\n!air.kernel = !{!20}\n!air.version = !{!8}\n!air.language_version = !{!9}\n\
!0 = !{i32 7, !\"air.max_device_buffers\", i32 31}\n\
!1 = !{i32 7, !\"air.max_threadgroup_buffers\", i32 31}\n\
!3 = !{}\n!4 = !{!5, !10, !11, !7}\n\
!5 = !{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 4, !\"air.arg_type_align_size\", i32 4, !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"a\"}\n\
!10 = !{i32 1, !\"air.buffer\", !\"air.buffer_size\", i32 32, !\"air.location_index\", i32 1, i32 1, !\"air.read\", !\"air.address_space\", i32 2, !\"air.arg_type_size\", i32 16, !\"air.arg_type_align_size\", i32 16, !\"air.arg_type_name\", !\"uint4\", !\"air.arg_name\", !\"p\"}\n\
!11 = !{i32 2, !\"air.buffer\", !\"air.location_index\", i32 2, i32 1, !\"air.read\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 4, !\"air.arg_type_align_size\", i32 4, !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"s\"}\n\
!7 = !{i32 3, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"gid\"}\n\
!8 = !{i32 2, i32 8, i32 0}\n!9 = !{!\"Metal\", i32 4, i32 0, i32 0}\n\
!20 = !{ptr @bump_params, !3, !4}\n";
/// `bump_ptr`: `bump_wide` with `b` read through the pointer `h` holds, `h` a `Holder` of one
/// pointer at index 1.
const KERNEL_PTR: &str = "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32\"\n\
@@ -385,6 +463,10 @@ enum Binds {
Moved,
/// [`Binds::Moved`]'s, every offset 0.
Fixed,
/// `bump_params`'s words, block set as bytes and zeros.
Params,
/// [`Binds::Params`]'s, the block bound as a buffer at [`PARAMS_OFFSET`].
ParamsOff,
}
/// The places a moved binding's offset takes in turn (`--binds moved`), and the bytes between
@@ -401,6 +483,8 @@ impl Binds {
Binds::Pointer => "ptr",
Binds::Moved => "moved",
Binds::Fixed => "fixed",
Binds::Params => "params",
Binds::ParamsOff => "params-off",
}
}
}
@@ -501,15 +585,21 @@ impl Shape {
"--runs" => shape.runs = value.parse()?,
"--only" => shape.only = Some(value.split(',').map(str::to_owned).collect()),
"--binds" => {
shape.binds = match value.as_str() {
"1" => Binds::One,
"4" => Binds::Wide,
"tex" => Binds::Textures,
"ptr" => Binds::Pointer,
"moved" => Binds::Moved,
"fixed" => Binds::Fixed,
_ => return Err("--binds takes 1, 4, tex, ptr, moved or fixed".into()),
}
shape.binds =
match value.as_str() {
"1" => Binds::One,
"4" => Binds::Wide,
"tex" => Binds::Textures,
"ptr" => Binds::Pointer,
"moved" => Binds::Moved,
"fixed" => Binds::Fixed,
"params" => Binds::Params,
"params-off" => Binds::ParamsOff,
_ => return Err(
"--binds takes 1, 4, tex, ptr, moved, fixed, params or params-off"
.into(),
),
}
}
"--host" => shape.host = Some(value.parse()?),
"--heap-churn" => shape.churn = Some(value.parse()?),
@@ -675,6 +765,7 @@ fn states(device: &Device, binds: Binds, count: u32) -> Result<Vec<ComputePipeli
Binds::Wide | Binds::Moved | Binds::Fixed => (KERNEL_WIDE, "bump_wide"),
Binds::Textures => (KERNEL_TEX, "bump_tex"),
Binds::Pointer => (KERNEL_PTR, "bump_ptr"),
Binds::Params | Binds::ParamsOff => (KERNEL_PARAMS, "bump_params"),
};
let bitcode = tool("llvm-as", &["-o", "-", "-"], text)?;
let library = metal2vulkan::library::Library::from_air(&bitcode)?;
@@ -1028,6 +1119,23 @@ impl Placed {
}
}
/// `bump_params`'s block ([`PARAMS`]) as bytes.
fn params_bytes() -> Vec<u8> {
PARAMS.iter().flat_map(|word| word.to_le_bytes()).collect()
}
/// A `Shared` buffer of 256 bytes holding `bump_params`'s block at [`PARAMS_OFFSET`].
fn params_buffer(device: &Device) -> Result<Buffer> {
let bytes = params_bytes();
let buffer = device.new_buffer(256, ResourceOptions::shared())?;
let contents = buffer.contents().ok_or("no contents")?;
for (at, byte) in bytes.iter().enumerate() {
// SAFETY: inside the buffer's mapped bytes, which no GPU work uses yet.
unsafe { contents.add(PARAMS_OFFSET as usize + at).write(*byte) };
}
Ok(buffer)
}
/// A `Shared` buffer of words of 1 over [`MOVES`] places of [`MOVE_STRIDE`] bytes, of the length
/// class `class` past the least such buffer's.
fn ones(device: &Device, class: u32) -> Result<Buffer> {
@@ -1137,8 +1245,8 @@ fn cpu_by_group() -> Vec<(String, u64)> {
groups
}
/// The runtime's shapes over one buffer, with two of zeros for `bump_wide`, and two textures and
/// a sampler for `bump_tex`.
/// The runtime's shapes over one buffer, with two of zeros for `bump_wide`, two textures and a
/// sampler for `bump_tex`, and a block for `bump_params`.
struct Ours<'a> {
device: &'a Device,
/// The kernel's states, dispatch `n` of a command buffer of the host's time setting state
@@ -1154,6 +1262,8 @@ struct Ours<'a> {
picks: Vec<(usize, usize)>,
textures: [Texture; 2],
sampler: SamplerState,
/// `bump_params`'s block at [`PARAMS_OFFSET`], for `--binds params-off`.
params: Buffer,
readback: Buffer,
/// What the `blits` shape's copies write.
copies: Buffer,
@@ -1262,6 +1372,14 @@ impl Ours<'_> {
encoder.set_buffers(&[Some(c)], &[at], 2)?;
encoder.set_buffers(&[Some(&self.ones[one])], &[at], 3)?;
}
Binds::Params | Binds::ParamsOff => {
encoder.set_buffer(Some(&self.words), 0, 0)?;
match binds {
Binds::Params => encoder.set_bytes(&params_bytes(), 1)?,
_ => encoder.set_buffer(Some(&self.params), PARAMS_OFFSET, 1)?,
}
encoder.set_buffer(Some(&self.zeros[0][n % 2]), 0, 2)?;
}
Binds::Textures => {
encoder.set_texture(Some(&self.textures[n % 2]), 0)?;
encoder.set_texture(Some(&self.textures[1 - n % 2]), 1)?;
@@ -2186,6 +2304,7 @@ fn main() -> Result<()> {
picks: shape.picks(),
textures: [texture()?, texture()?],
sampler: device.new_sampler_state(&SamplerDescriptor::new())?,
params: params_buffer(&device)?,
readback: device.new_buffer(bytes, ResourceOptions::shared())?,
copies: device.new_buffer(bytes, ResourceOptions::private())?,
churn,
+2 -2
View File
@@ -61,7 +61,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 = 2133;
pub const SEMANTICS_VERSION: u32 = 2134;
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
@@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523;
/// changes no behaviour, and then updates that hash alone with the commit saying why, or changes
/// behaviour and also bumps this version. The commit that bumps it says what changed; this comment
/// keeps no history.
pub const RUNTIME_VERSION: u32 = 2426;
pub const RUNTIME_VERSION: u32 = 2427;
/// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex,
/// of the manifest, the build script, and exactly the files the crate roots' module tree compiles
+3 -1
View File
@@ -1591,7 +1591,9 @@ pub enum DriverWorkaround {
/// its residual, and in 102.1 us with the bytes read through a storage view at their bind
/// offset (job m3-max-34275). So a kernel's uniform view there also has a storage twin
/// (`DescriptorView::UniformWithStorageTwin`, `lower::access`): a storage
/// buffer of the uniform view's own range, which the kernel reads instead where the slot's
/// buffer of the uniform view's own range, decorated `NonWritable` as the uniform buffer is
/// read-only (without it the driver keeps each of its loads apart where the kernel writes any
/// buffer, and folds none), which the kernel reads instead where the slot's
/// key holds (a bind offset the view base alignment divides, inline bytes, nothing bound), and
/// elsewhere the uniform view at its residual, as on every device without
/// `DriverFact::ConstantOffsetUniformReadsPreloaded`. The runtime keeps a twin's key only
+15 -2
View File
@@ -34,7 +34,8 @@
//! at a binding of its own (`ResourceKind::StorageTwin`) that the binder writes with the uniform
//! binding's buffer and range, so one offset reads the same byte through either. Each read loads
//! both and selects the twin's where the slot's `SpecKey::RangeAtBindOffset` holds
//! (`lower::access`): specialization fixes the constant, and the driver drops the other load.
//! (`lower::access`): specialization fixes the constant, and the driver drops the other load. The
//! twin's views are decorated `NonWritable`, as the uniform buffer is read-only.
//!
//! A storage or uniform slot may also have a **`uvec4` view** ([`VecView`]): `StorageBuffer {
//! uvec4[] }` or `Uniform { uvec4[N] }` at the slot's binding, declared where an access of the slot
@@ -136,13 +137,25 @@ pub fn twin_symbol(slot: SlotRef) -> ResourceSlot {
/// `units` (`DescriptorView::UniformWithStorageTwin`): a storage word view at the slot's
/// `StorageTwin` binding ([`twin_symbol`]), and beside it a `StorageBuffer { uvec4[] }` view where
/// the uniform view has its `uvec4` view, so every read of the uniform view's has the twin's
/// counterpart of the same units. No store goes through the twin.
/// counterpart of the same units. No store goes through the twin, and the module states its
/// binding unwritten (`ModuleBuilder::unwritten_buffer`), so the serializer decorates its views
/// `NonWritable`.
///
/// The twin binds the uniform view's own range of a Metal `constant` buffer, which the dispatch
/// only reads, as it reads the uniform buffer. A driver reads a storage buffer's loads as ones it
/// may merge, reuse and move only where the variable is `NonWritable` or the module writes no
/// buffer (Mesa's `nir_opt_access`, which ANV runs): without the decoration each load of a kernel
/// that writes any buffer stays a load of its own wherever it stands, where the uniform view's
/// loads, a uniform buffer's, are folded. Metal's compiler leaves a `constant` buffer's repeated
/// loads to its own backend to fold (a convolution's sizes read again at each sample of its tap
/// loop), so the twin without it read more than the uniform view.
pub fn declare_twin(
mb: &mut ModuleBuilder,
slot: SlotRef,
units: crate::memory::leaf::Units,
) -> BResult<(View, UnitViews)> {
let sym = twin_symbol(slot);
mb.unwritten_buffer(sym);
let view = declare_at(mb, sym, ViewKind::Storage)?;
let vec4 = match units.vec4 {
true => Some(declare_vec_at(mb, sym, view, 4)?),
@@ -943,10 +943,13 @@ impl ModuleBuilder {
}
}
/// State that nothing in the module writes the memory bound at buffer binding `sym`: no other
/// binding or address reaches it (its Metal argument is unaliased), and no store or atomic of
/// the module reaches it through its own views. The serializer decorates its storage views
/// `NonWritable` where its own walk of the module agrees (`spirv::serialize`).
/// State that nothing in the module writes the memory bound at buffer binding `sym`: a Metal
/// buffer argument no other binding or address reaches (it is unaliased) and no store or
/// atomic of the module reaches through its own views; or a uniform view's storage twin
/// (`memory::views::declare_twin`), bound to the uniform view's own range of a `constant`
/// buffer, which the dispatch only reads, as it reads the uniform buffer, and through which
/// nothing stores. The serializer decorates its storage views `NonWritable` where its own walk
/// of the module agrees (`spirv::serialize`).
pub fn unwritten_buffer(&mut self, sym: ResourceSlot) {
self.unwritten_buffers.insert(sym);
}
@@ -10,7 +10,7 @@
//!
//! A compute module's candidates are only the buffer bindings the lowering states unwritten
//! (`ModuleBuilder::unwritten_buffer`: an unaliased Metal buffer argument no write of the function
//! reaches), and the walk below must agree. The decoration says the module does not write the
//! reaches, or a uniform view's storage twin), and the walk below must agree. The decoration says the module does not write the
//! memory, which a device may read as read-only memory: RADV reads a uniform address of it with a
//! scalar load, which an aliasing write would not be ordered with.
//!
+26 -1
View File
@@ -538,6 +538,22 @@ fn twin_slot(index: u32) -> ResourceSlot {
}
}
/// The variables of compiled module `a` bound at `sym`, each with whether it is decorated
/// `NonWritable`.
fn views_at(a: &crate::library::FunctionArtifact, sym: ResourceSlot) -> Vec<(u32, bool)> {
let (words, relocs) = (&a.module.words, &a.module.relocs);
let (_, non_writable) = buffer_views(words, relocs);
(relocs.iter())
.filter_map(|(s, at)| match s {
Symbol::Binding(slot) if *slot == sym => {
words.get((*at as usize).checked_sub(2)?).copied()
}
_ => None,
})
.map(|id| (id, non_writable.contains(&id)))
.collect()
}
/// The form of buffer slot `index` of `a`.
fn form_of(a: &crate::library::FunctionArtifact, index: u32) -> BufferForm {
match a.interface.slots.get(&sref(index)) {
@@ -553,7 +569,11 @@ fn form_of(a: &crate::library::FunctionArtifact, index: u32) -> BufferForm {
/// `RangeAtBindOffset` 1 a load reads the twin at the Metal offset alone, and no value the slot's
/// `SlotBindOffset` word reaches is read once the key is folded ([`reads_push_word`]); at 0, the
/// negative control, it reads the uniform view at the residual plus the offset. The interpreter
/// binds the two to different bytes, so the value read names the view read.
/// binds the two to different bytes, so the value read names the view read. Every variable of the
/// twin's binding is decorated `NonWritable`: the twin binds the uniform view's own range of a
/// `constant` buffer, read-only as the uniform buffer is, and a driver merges, reuses and moves a
/// storage buffer's loads only where its variable says so (Mesa's `nir_opt_access`, which ANV
/// runs, where the kernel writes any buffer, as this one writes slot 1).
#[test]
fn a_kernel_uniform_view_reads_its_storage_twin_at_its_bind_offset() {
let entry = "buffer_end_byte_load_constant";
@@ -585,6 +605,11 @@ fn a_kernel_uniform_view_reads_its_storage_twin_at_its_bind_offset() {
([Some((0, 0)), Some((0, 1))], Some((0, 2))),
"{what}"
);
let views = views_at(&a, twin_slot(0));
assert!(
!views.is_empty() && views.iter().all(|&(_, non_writable)| non_writable),
"{what}: the twin's variables and whether each is NonWritable: {views:?}"
);
let (plan, m) = typed_module(&bc, entry, &p).expect("module");
assert!(!reads_push_word(&m, field, (key, true)), "{what} at 1");
assert!(reads_push_word(&m, field, (key, false)), "{what} at 0");
+4 -2
View File
@@ -1519,8 +1519,10 @@ for dependency in &pipeline.dependencies {
buffer, offset and range, from the same bind offset: a plan writes both from the slot's own
source (`WriteSource::Buffer`, `InlineBytes` or `Neutral`), an instance's `DescriptorRange`s
give both bindings one range, and the plan's `uses` hold a `Storage` read of the slot beside the
`Uniform` one. No function stores through the twin: `BindingPlan::read_only_buffer` holds for
both of the slot's writes, as for every uniform buffer's, so a slot bound to nothing may bind
`Uniform` one. No function stores through the twin, and the module decorates the twin's
variables `NonWritable`, so a driver may merge, reuse and move its loads as it does a uniform
buffer's; it binds the uniform view's own range of a `constant` buffer, which the dispatch only
reads. `BindingPlan::read_only_buffer` holds for both of the slot's writes, as for every uniform buffer's, so a slot bound to nothing may bind
one zero buffer that every command shares at both (the runtime's, made as a uniform and a
storage buffer); a write it does not hold for may be stored through. Where the
slot's `RangeAtBindOffset` holds, the kernel reads the twin at its bind offset alone; elsewhere