From 9a642e930cebc141e9ec9c7e39de84fa63fc6b61 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 31 Aug 2026 15:03:17 +0300 Subject: [PATCH] Hold constructed shapes to validity and to a binary round trip Executing a construction says it computes the right thing. It does not say a driver would accept it: SPIR-V's structured control flow rules -- a block before every block it dominates, a merge dominated by its header, a construct left only at its own merge or an enclosing loop's continue -- are all things a semantically correct rewrite can still break. spirv-val is what knows them, and until now it only ever saw authored fixtures. Point it at the generated family. The authored shape is invalid on purpose, since that is what selects construction, so only the output is validated. Two fixture corrections were needed first, both of them the fixture's fault rather than the product's: the header claimed SPIR-V 1.6, which is rejected before any content is examined under the Vulkan 1.2 target, and the entry point did not list its Private globals, which SPIR-V 1.4 onward requires whatever the storage class. Also round-trip each constructed module through binary and compare the disassembly. An assembler and a parser that disagree about an instruction's encoding are invisible to both validation and execution of the in-memory module, and a consumer takes the bytes. Verified: 3600 constructed modules over depths 2-7 and 0-3 crossings, every one accepted by spirv-val. The committed guards hold 64 of those plus 48 round trips, which keeps the suite fast -- spirv-val is a subprocess per module. cargo test -p metal2vulkan: 1704 passed. Workspace clippy -D warnings and all-features Rustdoc: clean. --- src/native/cfg_testkit/build.rs | 12 +++++- src/native/cfg_testkit/tests.rs | 75 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/native/cfg_testkit/build.rs b/src/native/cfg_testkit/build.rs index b5ad0cb0f..66c42a514 100644 --- a/src/native/cfg_testkit/build.rs +++ b/src/native/cfg_testkit/build.rs @@ -177,6 +177,12 @@ impl CfgBuilder { Operand::IdRef(initializer), ], )); + // From SPIR-V 1.4 on, an entry point's interface lists every module-scope variable it can + // reach, whatever the storage class. Leaving one out is rejected before anything about the + // module's control flow is looked at. + for entry_point in &mut self.module.entry_points { + entry_point.operands.push(Operand::IdRef(id)); + } id } @@ -460,7 +466,11 @@ impl CfgBuilder { }); } module.functions[0].blocks = emitted; - module.header = Some(ModuleHeader::new(next_id)); + let mut header = ModuleHeader::new(next_id); + // The version the product emits, and the one `spirv-val` is asked about: a fixture at a + // later version is rejected before anything about its content is examined. + header.set_version(1, 5); + module.header = Some(header); module } } diff --git a/src/native/cfg_testkit/tests.rs b/src/native/cfg_testkit/tests.rs index ae614abe9..8cf26d214 100644 --- a/src/native/cfg_testkit/tests.rs +++ b/src/native/cfg_testkit/tests.rs @@ -460,3 +460,78 @@ fn constant_folding_actually_removes_blocks_from_a_constant_seeded_shape() { it decided the whole function away" ); } + +/// Executing a construction says it computes the right thing; it does not say a driver would +/// accept it. SPIR-V's structured control flow rules -- a block appears before every block it +/// dominates, a merge block is dominated by its header, a construct is left only at its own merge +/// or an enclosing loop's continue -- are all things a semantically correct rewrite can still get +/// wrong, and `spirv-val` is what knows them. +/// +/// The authored shape is invalid on purpose (that is what selects construction), so only the +/// output is validated. +#[test] +fn constructed_shapes_are_valid_spirv() { + let tmp = std::env::temp_dir().join("m2v_cfg_testkit_val"); + for depth in [3u32, 5] { + for crossings in [0u32, 2] { + for seed in 0..16u64 { + let shape = shapes::irreducible_shape(seed, depth, crossings); + let (mut module, _) = shapes::author_constant_seeded(&shape, 3); + let _ = crate::native::rewrites::construct_cfg_functions_module( + &mut module, + &std::collections::HashSet::new(), + ); + let bytes = assemble(&module); + crate::tools::spirv_val_bytes(&bytes, &tmp).unwrap_or_else(|error| { + panic!( + "construction produced invalid SPIR-V: {error}\n{}", + shapes::describe(&shape) + ) + }); + } + } + } +} + +/// A constructed module has to survive the trip through binary that a consumer puts it through. +/// Round-tripping it and comparing the disassembly catches an assembler and a parser that disagree +/// about an instruction's encoding, which neither validation nor execution of the in-memory module +/// would notice. +#[test] +fn constructed_shapes_survive_a_binary_round_trip_unchanged() { + for depth in [3u32, 5] { + for seed in 0..24u64 { + let shape = shapes::irreducible_shape(seed, depth, 2); + let (mut module, slot) = shapes::author_constant_seeded(&shape, 3); + let _ = crate::native::rewrites::construct_cfg_functions_module( + &mut module, + &std::collections::HashSet::new(), + ); + let expected = interp::run_module_to_global(&module, slot, STEP_LIMIT) + .expect("constructed module runs"); + + let reloaded = crate::spirv_module::load_bytes(assemble(&module)) + .unwrap_or_else(|error| panic!("reloading: {error:?}")); + assert_eq!( + reloaded.disassemble(), + module.disassemble(), + "a binary round trip changed this module:\n{}", + shapes::describe(&shape) + ); + assert_eq!( + interp::run_module_to_global(&reloaded, slot, STEP_LIMIT).expect("reloaded runs"), + expected, + "a binary round trip changed what this module computes:\n{}", + shapes::describe(&shape) + ); + } + } +} + +fn assemble(module: &crate::spirv_module::Module) -> Vec { + module + .assemble() + .iter() + .flat_map(|word| word.to_le_bytes()) + .collect() +}