From acc37fdcc569042de5561929dbbda413ad6b841a Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 31 Aug 2026 12:05:52 +0300 Subject: [PATCH] Check the nesting structurizer's value flow before adopting it Nesting keeps ordinary values in registers on the premise that it preserves the original CFG's dominance. It does not always: an edge that has to leave more than one construct is staged through a merge dispatch, so its destination is reached from that merge rather than from the block that left it, and a definition inside the construct can stop reaching a use beyond it. The state machine this pass replaced repaired such functions incidentally, by demoting every crossing value. Two private corpus cases regressed to a translation failure because of that. Verify the emitted function against the same value-flow contract the module is held to -- definitions dominating their uses, and phi incoming pairs matching the block's predecessors and result type -- and put the function back on the state machine when it fails, alongside the existing structural gates. owned_cfg gains the function-scoped entry point for that check, reusing its existing phi and ordinary-use rules rather than restating them. --- src/native/owned_cfg.rs | 80 ++++++++++++++++++-------- src/native/reloop_nest/emit.rs | 9 ++- src/native/reloop_nest/mod.rs | 102 +++++++++++++++++++++++++++++---- 3 files changed, 154 insertions(+), 37 deletions(-) diff --git a/src/native/owned_cfg.rs b/src/native/owned_cfg.rs index 7c3557300..526544130 100644 --- a/src/native/owned_cfg.rs +++ b/src/native/owned_cfg.rs @@ -384,29 +384,7 @@ impl OwnedCfg { ); } - let mut definition_sites = HashMap::new(); - for (block_index, block) in function.blocks.iter().enumerate() { - for (instruction_index, instruction) in block.instructions.iter().enumerate() { - let Some(id) = instruction.result_id else { - continue; - }; - if definition_sites - .insert( - id, - DefinitionSite { - block: block_index, - instruction: instruction_index, - }, - ) - .is_some() - { - return Err( - "native emitter: owned function defines a result id more than once" - .to_string(), - ); - } - } - } + let definition_sites = definition_sites(function)?; let mut entry_value_seen = false; for (block_index, block) in function.blocks.iter().enumerate() { let mut non_phi_seen = false; @@ -1026,6 +1004,62 @@ fn switch_info(operands: &[Operand], labels: &HashMap) -> Result Result, String> { + let mut sites = HashMap::new(); + for (block, instructions) in function.blocks.iter().enumerate() { + for (instruction, definition) in instructions.instructions.iter().enumerate() { + let Some(id) = definition.result_id else { + continue; + }; + if sites + .insert(id, DefinitionSite { block, instruction }) + .is_some() + { + return Err( + "native emitter: owned function defines a result id more than once".to_string(), + ); + } + } + } + Ok(sites) +} + +/// Report the first way `function` breaks the owned contract that binds values to control flow: +/// a use its definition does not dominate, or an `OpPhi` whose incoming pairs do not match the +/// block's predecessors and result type. +/// +/// This is the part of the module contract a control-flow rewrite is answerable for, so a pass +/// that reshapes blocks can check its own output before adopting it rather than assume the reshape +/// preserved dominance. `value_types` maps result ids to their result types; ids missing from it +/// are not type-checked. +pub(super) fn owned_function_value_flow_error( + function: &Function, + value_types: &HashMap, +) -> Option { + let cfg = match OwnedCfg::new(function) { + Ok(cfg) => cfg, + Err(error) => return Some(error), + }; + let sites = match definition_sites(function) { + Ok(sites) => sites, + Err(error) => return Some(error), + }; + for (block, instructions) in function.blocks.iter().enumerate() { + for (index, instruction) in instructions.instructions.iter().enumerate() { + let result = if instruction.class.opcode == Op::Phi { + cfg.check_phi(block, instruction, value_types, &sites) + } else { + cfg.check_ordinary_uses(block, index, instruction, &sites) + }; + if let Err(error) = result { + return Some(error); + } + } + } + None +} + fn build_predecessors(successors: &[Vec]) -> Vec> { let mut predecessors = vec![Vec::new(); successors.len()]; for (source, targets) in successors.iter().enumerate() { diff --git a/src/native/reloop_nest/emit.rs b/src/native/reloop_nest/emit.rs index 11a0f502d..cfa7e4b89 100644 --- a/src/native/reloop_nest/emit.rs +++ b/src/native/reloop_nest/emit.rs @@ -2,9 +2,12 @@ //! //! Walks the [`Shape`](super::shape::Shape) tree and writes the nested loop and selection //! constructs it describes. The point of the nesting is what it does NOT do: it keeps every value -//! in SSA, because the shape tree preserves the original CFG's paths and therefore its dominance. -//! Only `OpPhi` results are register-demoted, because a phi's predecessor identity is exactly what -//! nesting rewrites. +//! in SSA, because the shape tree preserves the original CFG's paths. Only `OpPhi` results are +//! register-demoted, because a phi's predecessor identity is exactly what nesting rewrites. +//! +//! Preserved paths do not guarantee preserved dominance — a staged edge (below) reaches its +//! destination from a merge dispatch rather than from the block it left. The caller checks the +//! emitted function's value flow and discards a nesting that broke it; see [`super`]. //! //! # Leaving more than one construct //! diff --git a/src/native/reloop_nest/mod.rs b/src/native/reloop_nest/mod.rs index 5bc05cf34..31b0445f5 100644 --- a/src/native/reloop_nest/mod.rs +++ b/src/native/reloop_nest/mod.rs @@ -11,12 +11,19 @@ //! //! Reducible control flow does not need that. This module derives the relooper shape tree (see //! [`shape`]) and emits it as genuinely nested SPIR-V loop and selection constructs. Nesting -//! preserves the original CFG's paths, and therefore its dominance, so ordinary SSA values are -//! emitted untouched; only `OpPhi` — whose meaning is its predecessor list, which is exactly what -//! nesting rewrites — is demoted. +//! keeps the original CFG's paths, so ordinary SSA values are emitted untouched; only `OpPhi` — +//! whose meaning is its predecessor list, which is exactly what nesting rewrites — is demoted. //! -//! It is a strict addition, not a replacement. Irreducible graphs, and any shape the emitter cannot -//! express, are declined with an `Err` and stay on the state-machine constructor. +//! Keeping paths is not the same as keeping dominance. An edge that has to leave more than one +//! construct is staged through a merge dispatch, and its destination is then reached from that +//! merge rather than from the block that left, so a definition inside the construct can stop +//! dominating a use beyond it. The state machine repaired that incidentally, by demoting every +//! crossing value. This pass does not, so [`structure_selected_functions`] checks the function it +//! emitted against the module's value-flow contract and discards it when the check fails. +//! +//! It is a strict addition, not a replacement. Irreducible graphs, any shape the emitter cannot +//! express, and any nesting that does not survive that check stay on the state-machine +//! constructor. mod emit; mod shape; @@ -25,7 +32,7 @@ use super::relooper::{block_label, decode_term, Term, TypeCtx}; use crate::spirv_module::{Function, Module}; use shape::Graph; use spirv::Word; -use std::collections::{BTreeMap, HashSet}; +use std::collections::{BTreeMap, HashMap, HashSet}; /// Restructure every selected function whose control flow this module can nest. Returns the result /// ids it rewrote, so the caller can leave the rest to the state-machine constructor. @@ -38,6 +45,13 @@ pub(crate) fn structure_selected_functions( .as_ref() .map(|header| header.bound) .unwrap_or(1); + // Verifying the emitted nesting needs the result type of every value it can name. Snapshot the + // module's before its functions are borrowed mutably; result ids are unique module-wide, so the + // ids each emitted function adds can simply be folded into the same map. + let mut value_types = module + .all_inst_iter() + .filter_map(|instruction| Some((instruction.result_id?, instruction.result_type?))) + .collect::>(); let mut tc = TypeCtx::new(module, &mut next_id); let mut structured = HashSet::new(); for function in &mut module.functions { @@ -50,16 +64,40 @@ pub(crate) fn structure_selected_functions( let before = function.blocks.len(); match structure_function(function, &mut tc) { Ok(blocks) => { + let candidate = std::mem::replace(&mut function.blocks, blocks); + value_types.extend(function.blocks.iter().flat_map(|block| { + block.instructions.iter().filter_map(|instruction| { + Some((instruction.result_id?, instruction.result_type?)) + }) + })); // The same structural checks the caller uses to select construction in the first // place. A nesting that leaves an unowned selection header or back edge has not // structured the function, so it is discarded rather than shipped. - let candidate = std::mem::replace(&mut function.blocks, blocks); - if super::rewrites::blocks_have_unowned_selection_header(&function.blocks) - || super::rewrites::function_has_unowned_backedge(function) - { + // + // Value flow is checked for the same reason. Keeping ordinary values in registers + // is only sound where the nesting really did preserve the paths that made their + // definitions dominate their uses, and that does not always hold: an edge staged + // through a construct's merge dispatch reaches its destination from that merge + // rather than from the block it left. The state machine this pass replaces repaired + // such a function incidentally, by demoting every crossing value; nesting keeps + // them, so it has to prove the definitions still reach. + let broken = + super::rewrites::blocks_have_unowned_selection_header(&function.blocks) + .then(|| "nesting left an unowned construct".to_string()) + .or_else(|| { + super::rewrites::function_has_unowned_backedge(function) + .then(|| "nesting left an unowned back edge".to_string()) + }) + .or_else(|| { + super::owned_cfg::owned_function_value_flow_error( + function, + &value_types, + ) + }); + if let Some(reason) = broken { function.blocks = candidate; if crate::env_vars::reloop_why() { - eprintln!("NEST-DECLINE blocks={before} nesting left an unowned construct"); + eprintln!("NEST-DECLINE blocks={before} {reason}"); } } else { structured.insert(id); @@ -375,6 +413,48 @@ mod tests { assert!(asm.contains("OpIAdd"), "{asm}"); } + /// A value defined in one arm of a selection and used after the merge. The state-machine + /// constructor repairs this by demoting every crossing value; the nesting keeps values in + /// registers, so it cannot, and must decline rather than ship a use its definition never + /// reaches. Earlier stages hand construction such functions, so this is a real input, not a + /// hypothetical malformed one. + const USE_WITHOUT_DOMINATING_DEFINITION: &str = r#" + OpCapability Shader + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" + OpExecutionMode %main LocalSize 1 1 1 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %bool = OpTypeBool + %uint_0 = OpConstant %uint 0 + %uint_1 = OpConstant %uint 1 + %fnvoid = OpTypeFunction %void + %main = OpFunction %void None %fnvoid + %entry = OpLabel + %cond = OpIEqual %bool %uint_0 %uint_1 + OpBranchConditional %cond %arm %other + %arm = OpLabel + %value = OpIAdd %uint %uint_0 %uint_1 + OpBranch %join + %other = OpLabel + OpBranch %join + %join = OpLabel + %used = OpIAdd %uint %value %uint_1 + OpReturn + OpFunctionEnd +"#; + + #[test] + fn nesting_declines_a_use_its_definition_does_not_dominate() { + let Some(spv) = assemble(USE_WITHOUT_DOMINATING_DEFINITION) else { + return; + }; + assert!( + nested_bytes(&spv).is_none(), + "a function whose value flow the nesting cannot honour must stay on the state machine" + ); + } + /// `while (c) { if (d) { p(); if (e) break; } q(); }` — the break leaves a selection AND the /// loop on one edge, which SPIR-V cannot express as a single branch. const NESTED_BREAK: &str = r#"