Say which view went unrepaired, not which arithmetic computed the offset

Nine of the twelve remaining non-function-pointer corpus rejections are one shape, and its
diagnostic pointed away from it. `index 0 into %22 (TypeStruct %21) is %658 (BitwiseOr %657 %655)`
names the instruction that computed the offset, so the message reads as though the AIR contained an
`or` — it does not; none of these sources contain a single `or`, and the operand is emitter-
synthesized address arithmetic that varies by source (`BitwiseOr` in eight, `SConvert` in the
ninth). A previous session authored three synthetic reductions of that `BitwiseOr` shape and all
three translated cleanly, because the operand was never the trigger.

What the module does say, and did not report: `%22` is a single-member struct over a runtime array
of `TypeInt 32`, and the chain's own result pointee is `TypeInt 8`. That is the whole diagnosis — a
byte view of a word array, arriving as one dynamic index into the raw-word block, which is the shape
the four `rewrite_dynamic_struct_index_*` passes exist to repair and none of them matched. It also
distinguishes the ninth source, whose struct has six members and so is a genuine bad member select,
not a view at all.

So on that shape — and only that shape, since a dynamic index into any other struct really is a bad
member select — the violation now names the element type and the view type. Verified against the
corpus: it reports `TypeInt 8` over `TypeInt 32` on all eight, and stays silent on the ninth.

Behaviour is otherwise unchanged: 2880 corpus sources byte-identical against d862cd1 (a rejected
source is still rejected, with a longer message). `cargo fmt --all --check`, clippy `-D warnings`,
`RUSTDOCFLAGS='-D warnings' cargo doc`, `cargo test -p metal2vulkan` debug and release,
`cargo test -p metal2vulkan-validation`.
This commit is contained in:
Anees Iqbal committed 2026-08-31 20:20:06 +03:00
1 parent d862cd11ee
commit bc23fe636b
1 file changed
+120 -2
+120 -2
View File
@@ -2860,6 +2860,55 @@ fn describe_owned_id(id: Word, definitions: &HashMap<Word, &Instruction>) -> Str
text
}
/// The extra sentence a dynamic struct-member index gets when the struct is a single-member buffer
/// block — the shape a raw-word buffer takes, `{ RuntimeArray<E> }`.
///
/// A dynamic index there is never a member select; it is an element view of the block's array whose
/// width the access chain's own result pointee names. Saying so is the difference between a message
/// a reader can act on and one that describes the address arithmetic: the index is whatever the
/// emitter computed the offset with, so this violation reports things like `BitwiseOr` or `SConvert`
/// that exist nowhere in the AIR and say nothing about which view went unrepaired. The pair that
/// does say it is (element type, view type) — `TypeInt 8` over `TypeInt 32` is a byte view of a word
/// array, and that is the fact that picks out which of the dynamic-struct-index rewrites should have
/// matched and did not.
fn describe_element_view(
struct_ty: Word,
result_type: Option<Word>,
definitions: &HashMap<Word, &Instruction>,
) -> String {
let Some(definition) = definitions.get(&struct_ty) else {
return String::new();
};
if definition.class.opcode != Op::TypeStruct || definition.operands.len() != 1 {
return String::new();
}
let Some(Operand::IdRef(member)) = definition.operands.first() else {
return String::new();
};
let Some(member_definition) = definitions.get(member) else {
return String::new();
};
if !matches!(
member_definition.class.opcode,
Op::TypeArray | Op::TypeRuntimeArray
) {
return String::new();
}
let Some(Operand::IdRef(element)) = member_definition.operands.first() else {
return String::new();
};
let view = result_type
.and_then(|ty| pointer_type_shape(ty, definitions))
.map(|(_, pointee)| describe_owned_id(pointee, definitions))
.unwrap_or_else(|| "an unknown pointee".to_string());
format!(
". The struct is a single-member block over {}, so this is not a member select but a {} \
view of that array which no dynamic-struct-index rewrite repaired",
describe_owned_id(*element, definitions),
view
)
}
fn owned_access_chain_error(
instruction: &Instruction,
definitions: &HashMap<Word, &Instruction>,
@@ -2945,9 +2994,10 @@ fn owned_access_chain_error(
let Some(member) = owned_constant_u32(*index, definitions, value_types) else {
return Err(format!(
"index {position} into {} is {}, and a struct member index must be an \
OpConstant of 32-bit integer type",
OpConstant of 32-bit integer type{}",
describe_owned_id(selected, definitions),
describe_owned_id(*index, definitions)
describe_owned_id(*index, definitions),
describe_element_view(selected, instruction.result_type, definitions)
));
};
let Some(Operand::IdRef(member_type)) =
@@ -7839,6 +7889,74 @@ mod tests {
assert_eq!(describe_owned_id(99, &definitions), "%99 (undefined)");
}
/// A dynamic index into a single-member block is the raw-word-buffer view shape, and the index
/// itself says nothing about it: the emitter computes the offset, so the violation reports
/// whatever arithmetic produced it. The pair that identifies the unrepaired view is (element
/// type, result pointee), so the message carries it — and only for that shape, since on any
/// other struct a dynamic index really is a bad member select.
#[test]
fn a_dynamic_index_into_a_single_member_block_reports_the_view_it_failed_to_repair() {
let word = Instruction::new(
Op::TypeInt,
None,
Some(1),
vec![Operand::LiteralBit32(32), Operand::LiteralBit32(0)],
);
let byte = Instruction::new(
Op::TypeInt,
None,
Some(2),
vec![Operand::LiteralBit32(8), Operand::LiteralBit32(0)],
);
let words = Instruction::new(Op::TypeRuntimeArray, None, Some(3), vec![Operand::IdRef(1)]);
let block = Instruction::new(Op::TypeStruct, None, Some(4), vec![Operand::IdRef(3)]);
let byte_pointer = Instruction::new(
Op::TypePointer,
None,
Some(5),
vec![
Operand::StorageClass(spirv::StorageClass::StorageBuffer),
Operand::IdRef(2),
],
);
// Not a block view: two members, so a dynamic index is a genuine bad member select.
let pair = Instruction::new(
Op::TypeStruct,
None,
Some(6),
vec![Operand::IdRef(1), Operand::IdRef(1)],
);
// One member, but not an array — nothing to be an element view OF.
let wrapper = Instruction::new(Op::TypeStruct, None, Some(7), vec![Operand::IdRef(1)]);
let definitions = [&word, &byte, &words, &block, &byte_pointer, &pair, &wrapper]
.into_iter()
.map(|instruction| (instruction.result_id.expect("result id"), instruction))
.collect::<HashMap<_, _>>();
assert_eq!(
describe_element_view(4, Some(5), &definitions),
". The struct is a single-member block over %1 (TypeInt 32 0), so this is not a member \
select but a %2 (TypeInt 8 0) view of that array which no dynamic-struct-index rewrite \
repaired",
"a byte view of a word array is the fact that picks out the missing rewrite"
);
assert_eq!(
describe_element_view(6, Some(5), &definitions),
"",
"a multi-member struct is not a block, so the index really is a bad member select"
);
assert_eq!(
describe_element_view(7, Some(5), &definitions),
"",
"a single member that is not an array has no elements to view"
);
assert_eq!(
describe_element_view(1, Some(5), &definitions),
"",
"only a struct can have a member index"
);
}
#[test]
fn owned_module_enforces_access_chain_contracts() {
let chain = |opcode, result_type, base, indices: &[Word]| {