Read a buffer's one element view, not the first of several

A buffer parameter whose entry body indexes it with a single index and
reads a different type at each index is navigating a record, not a flat
array. body_buf_elem_type returned the first of those views, so the
interface builder wrapped the buffer as { RuntimeArray<that type> } and
every access through another view kept its own result type while the
chain now addressed the wrong element: a float2 load off a chain into a
float4 array.

Return the element type only when the body has exactly one view. With
several, the AIR struct layout is reconstructed instead, which is what
the metadata already describes.

Re-auditing the 107 known translation failures: 88 fail, 19 translate
(was 90/17). Both cross-library cases that reported 'owned Load violates
its pointer-pointee and value-type contract' now translate.
This commit is contained in:
Anees Iqbal committed 2026-08-31 13:10:54 +03:00
1 parent b7393a94d6
commit 28580a51f6
2 files changed
+96 -1

No files matched your search

+5
View File
@@ -371,6 +371,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- A buffer parameter whose body reads several distinct types through a one-index access chain is no
longer wrapped as a flat `{ RuntimeArray<T> }` of whichever type happened to be seen first. A
genuine `device T*` array has exactly one element view; several views mean the index selects
members of a record, so the buffer's AIR struct layout is reconstructed instead. Wrapping such a
buffer flatly kept the first view's stride and left every other access loading the wrong type.
- `air.get_num_samples_texture*` now lowers from the image type instead of an unconditional literal
`1`: a multisampled 2D image queries `OpImageQuerySamples`, a single-sample image keeps the exact
constant, and a multisampled non-2D dimensionality is refused. `ImageQuery` capability inference
+91 -1
View File
@@ -426,8 +426,14 @@ pub(in crate::passes) fn body_buf_elem_types(ctx: &Ctx, func: &Function, pid: Wo
elements
}
/// The body's one unambiguous flat element view of a buffer param, or `None` when it has more than
/// one. A genuine `device T*` array has exactly one element type. Several distinct views through
/// the same pointer mean the single index navigates an aggregate instead — reading them as array
/// elements would keep the first view's stride and mistype every access that used another one.
pub(in crate::passes) fn body_buf_elem_type(ctx: &Ctx, func: &Function, pid: Word) -> Option<Word> {
body_buf_elem_types(ctx, func, pid).into_iter().next()
let mut elements = body_buf_elem_types(ctx, func, pid).into_iter();
let element = elements.next()?;
elements.next().is_none().then_some(element)
}
/// Detect the canonical flat-word view emitted for a homogeneous aggregate buffer. Every rooted
@@ -988,6 +994,90 @@ mod tests {
);
}
#[test]
fn buffer_flat_element_view_is_none_when_the_body_reads_several_types() {
let mut module = Module::new();
module.types_global_values = vec![
Instruction::new(
Op::TypeFloat,
None,
Some(1),
vec![Operand::LiteralBit32(32)],
),
Instruction::new(
Op::TypeVector,
None,
Some(2),
vec![Operand::IdRef(1), Operand::LiteralBit32(4)],
),
Instruction::new(
Op::TypeVector,
None,
Some(3),
vec![Operand::IdRef(1), Operand::LiteralBit32(2)],
),
Instruction::new(
Op::TypePointer,
None,
Some(4),
vec![
Operand::StorageClass(StorageClass::UniformConstant),
Operand::IdRef(2),
],
),
Instruction::new(
Op::TypePointer,
None,
Some(5),
vec![
Operand::StorageClass(StorageClass::UniformConstant),
Operand::IdRef(3),
],
),
];
// `buf %uint_0` reads a float4 and `buf %uint_1` reads a float2: the index selects members
// of a `{ float4, float2 }` record, so there is no single flat element type to wrap.
let member_chains = vec![
Instruction::new(
Op::InBoundsAccessChain,
Some(4),
Some(11),
vec![Operand::IdRef(10), Operand::IdRef(30)],
),
Instruction::new(
Op::InBoundsAccessChain,
Some(5),
Some(12),
vec![Operand::IdRef(10), Operand::IdRef(31)],
),
];
module.functions.push(Function {
def: None,
end: None,
parameters: vec![],
blocks: vec![Block {
label: Some(Instruction::new(Op::Label, None, Some(20), vec![])),
instructions: member_chains.clone(),
}],
});
module.functions.push(Function {
def: None,
end: None,
parameters: vec![],
blocks: vec![Block {
label: Some(Instruction::new(Op::Label, None, Some(21), vec![])),
instructions: member_chains[..1].to_vec(),
}],
});
let ctx = Ctx::new(module);
assert_eq!(body_buf_elem_type(&ctx, &ctx.module.functions[0], 10), None);
assert_eq!(
body_buf_elem_type(&ctx, &ctx.module.functions[1], 10),
Some(2)
);
}
#[test]
fn texture_arg_comp_reads_nested_texture_array_scalar() {
assert_eq!(