diff --git a/src/native/owned_cfg.rs b/src/native/owned_cfg.rs index 526544130..9d58a5dd8 100644 --- a/src/native/owned_cfg.rs +++ b/src/native/owned_cfg.rs @@ -187,6 +187,14 @@ impl OwnedCfg { value_types: &HashMap, ) -> Result<(), String> { self.check_block_layout(function, definitions, value_types)?; + self.check_structure(function) + } + + /// The half of [`Self::check`] that reads only control flow: construct containment, structured + /// exits, switch merges, back edges, and continue-target ownership. It needs no module-wide + /// type tables, so a pass that rewrites a function's blocks can hold its own output to the + /// same rules before adopting it. + fn check_structure(&self, function: &Function) -> Result<(), String> { if !dominators_precede_blocks(&self.flow_reachable, &self.flow_idom) { return Err( "native emitter: owned block is serialized before one of its dominators" @@ -1025,15 +1033,16 @@ fn definition_sites(function: &Function) -> Result 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. +/// Report the first way `function` breaks the part of the owned contract that a control-flow +/// rewrite is answerable for: an unowned or badly nested construct, an unstructured exit, 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( +/// A pass that reshapes a function's blocks uses this to hold its own output to those rules before +/// adopting it, rather than assume the reshape preserved them. Everything left out needs the +/// module-wide type tables and is checked once, later, over the whole module. `value_types` maps +/// result ids to their result types; ids missing from it are not type-checked. +pub(super) fn owned_function_construction_error( function: &Function, value_types: &HashMap, ) -> Option { @@ -1057,7 +1066,7 @@ pub(super) fn owned_function_value_flow_error( } } } - None + cfg.check_structure(function).err() } fn build_predecessors(successors: &[Vec]) -> Vec> { diff --git a/src/native/reloop_nest/mod.rs b/src/native/reloop_nest/mod.rs index fbbc77653..581286470 100644 --- a/src/native/reloop_nest/mod.rs +++ b/src/native/reloop_nest/mod.rs @@ -70,17 +70,18 @@ pub(crate) fn structure_selected_functions( 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. + // A nesting is adopted only if it really is a well-formed construction. That is the + // same question the caller asked to select construction in the first place, so it + // is asked the same way, then extended to everything else a control-flow rewrite + // owns: construct nesting, structured exits, and value flow. // - // 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. + // Value flow has to be checked because nesting keeps ordinary values in registers. + // That is only sound where it preserved the paths that made their definitions + // dominate their uses, and it does not always: 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()) @@ -89,7 +90,7 @@ pub(crate) fn structure_selected_functions( .then(|| "nesting left an unowned back edge".to_string()) }) .or_else(|| { - super::owned_cfg::owned_function_value_flow_error( + super::owned_cfg::owned_function_construction_error( function, &value_types, )