From af3a097cdce75cf3772d62a4c9547ec8a8d252e9 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 31 Aug 2026 16:38:28 +0300 Subject: [PATCH] Bound the contract renderer, and correct two claims I made earlier describe_owned_id printed every operand of the defining instruction, so a wide OpTypeStruct or a phi with many arms would have buried the sentence the message exists to say. It now shows eight and counts the rest, and has a test that pins all four renderings including the undefined-id case. Two corrections to the record, since the commits that made these claims cannot be amended: 1. c2ba4ab said the non-constant-struct-index class is the "base has type TypeInt 64" class seen from the other side. It is not. The repro I checked (entry particle_compact_data) has zero device-address loads -- no `load ptr addrspace(1), ptr addrspace(2)` anywhere -- and its 64-bit value comes from lower_scalar_i64_arithmetic_to_u32_halves recombining u32 halves. The two classes share a symptom, not a cause, and want two fixes. 2. I then read METAL2VULKAN_PASS_CONTRACT's "cleanup start" verdict as proof the class originates in the emitter. Also wrong: that verdict is a different violation which a later phase repairs. Scanning for the bad *shape* after every pass instead puts the origin in passes::stage_input::build_stage_input, which emits 24 chains of the form `OpInBoundsAccessChain %blockvar %runtimeIndex` on synthesized StorageBuffer block variables -- one index where a {RuntimeArray} block struct needs two. word_access.rs gets the same rebase right: it pushes const_uint(0) ahead of the word index for every storage class except Workgroup. No fix here. The exact site inside build_stage_input is a base/result-type substitution on an existing chain rather than one of the two AccessChain constructions in stage_input/, and I would be guessing. The localization is recorded so the next pass at it does not start from the symptom. Also verified while here: the cross-stage corpus sweep is clean at 706 translating source/stage pairs, 0 varied. --- src/native/owned_cfg.rs | 57 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/native/owned_cfg.rs b/src/native/owned_cfg.rs index 51cb464c5..81a8f815b 100644 --- a/src/native/owned_cfg.rs +++ b/src/native/owned_cfg.rs @@ -2947,13 +2947,17 @@ struct OwnedAccessChainFailure { /// naming the definition makes the diagnosis fit in the message. `definitions` covers every result /// id in the module, not just types, so this works for an operand as well as for a type. Operand /// kinds that do not survive as a short literal are dropped rather than debug-printed, so the -/// rendering stays one line. +/// rendering stays one line, and a long operand list is elided: a wide `OpTypeStruct` or a phi with +/// many arms would otherwise bury the sentence the message is trying to say. fn describe_owned_id(id: Word, definitions: &HashMap) -> String { + /// Enough operands to recognize a type or an instruction by, and few enough to read. + const SHOWN_OPERANDS: usize = 8; + let Some(definition) = definitions.get(&id) else { return format!("%{id} (undefined)"); }; let mut text = format!("%{id} ({:?}", definition.class.opcode); - for operand in &definition.operands { + for operand in definition.operands.iter().take(SHOWN_OPERANDS) { match operand { Operand::IdRef(id) => text.push_str(&format!(" %{id}")), Operand::LiteralBit32(value) => text.push_str(&format!(" {value}")), @@ -2962,6 +2966,11 @@ fn describe_owned_id(id: Word, definitions: &HashMap) -> Str _ => {} } } + if let Some(elided) = definition.operands.len().checked_sub(SHOWN_OPERANDS) { + if elided > 0 { + text.push_str(&format!(" +{elided} more")); + } + } text.push(')'); text } @@ -7859,6 +7868,50 @@ mod tests { ); } + #[test] + fn an_id_renders_as_its_definition_and_a_wide_one_is_elided() { + let int = Instruction::new( + Op::TypeInt, + None, + Some(7), + vec![Operand::LiteralBit32(64), Operand::LiteralBit32(0)], + ); + let pointer = Instruction::new( + Op::TypePointer, + None, + Some(8), + vec![ + Operand::StorageClass(spirv::StorageClass::StorageBuffer), + Operand::IdRef(7), + ], + ); + // Twelve members: four more than the renderer shows. + let wide = Instruction::new( + Op::TypeStruct, + None, + Some(9), + (0..12).map(|_| Operand::IdRef(7)).collect(), + ); + let definitions = [&int, &pointer, &wide] + .into_iter() + .map(|instruction| (instruction.result_id.expect("result id"), instruction)) + .collect::>(); + + assert_eq!(describe_owned_id(7, &definitions), "%7 (TypeInt 64 0)"); + assert_eq!( + describe_owned_id(8, &definitions), + "%8 (TypePointer StorageBuffer %7)" + ); + assert_eq!( + describe_owned_id(9, &definitions), + "%9 (TypeStruct %7 %7 %7 %7 %7 %7 %7 %7 +4 more)", + "a wide aggregate must not bury the sentence the message is trying to say" + ); + // An id the module never defines is the interesting case for a contract message, so it says + // so rather than rendering as nothing. + assert_eq!(describe_owned_id(99, &definitions), "%99 (undefined)"); + } + #[test] fn owned_module_enforces_access_chain_contracts() { let chain = |opcode, result_type, base, indices: &[Word]| {