From 5a58eb807a1a63eb5301f1b9bca2292b208528d9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 6 Apr 2026 14:11:52 +0100 Subject: Seal blocks as soon as possible --- patio-to-cranelift/src/codegen/context.rs | 5 +++-- patio-to-cranelift/src/codegen/mod.rs | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/patio-to-cranelift/src/codegen/context.rs b/patio-to-cranelift/src/codegen/context.rs index ddd50492..f7e5d4d0 100644 --- a/patio-to-cranelift/src/codegen/context.rs +++ b/patio-to-cranelift/src/codegen/context.rs @@ -92,6 +92,7 @@ impl CompilerContext { let entry_block = function_builder.create_block(); function_builder.append_block_params_for_function_params(entry_block); function_builder.switch_to_block(entry_block); + function_builder.seal_block(entry_block); let mut context = CompilerFunctionContext { func_name_to_func_id: &self.func_name_to_func_id, @@ -216,8 +217,8 @@ impl<'a> CompilerFunctionContext<'a> { self.function_builder.switch_to_block(block); } - pub(crate) fn seal_current_block(&mut self) { - self.function_builder.seal_block(self.current_block()); + pub(crate) fn seal_block(&mut self, block: cranelift_codegen::ir::Block) { + self.function_builder.seal_block(block); } pub(crate) fn current_block(&self) -> cranelift_codegen::ir::Block { diff --git a/patio-to-cranelift/src/codegen/mod.rs b/patio-to-cranelift/src/codegen/mod.rs index 7fad42f5..798c0381 100644 --- a/patio-to-cranelift/src/codegen/mod.rs +++ b/patio-to-cranelift/src/codegen/mod.rs @@ -456,7 +456,8 @@ fn compile_if(if_: &PatioIf, context: &mut CompilerFunctionContext<'_>) { cranelift_else_block, &[], ); - context.seal_current_block(); + context.seal_block(cranelift_then_block); + context.seal_block(cranelift_else_block); context.switch_to_block(cranelift_then_block); let cranelift_then_results = compile_block(&if_.true_body, context); @@ -464,7 +465,6 @@ fn compile_if(if_: &PatioIf, context: &mut CompilerFunctionContext<'_>) { cranelift_end_block, cranelift_block_args(cranelift_then_results).iter(), ); - context.function_builder.seal_block(cranelift_then_block); context.switch_to_block(cranelift_else_block); let cranelift_else_results = compile_block(&if_.false_body, context); @@ -472,8 +472,8 @@ fn compile_if(if_: &PatioIf, context: &mut CompilerFunctionContext<'_>) { cranelift_end_block, cranelift_block_args(cranelift_else_results).iter(), ); - context.function_builder.seal_block(cranelift_else_block); + context.seal_block(cranelift_end_block); context.switch_to_block(cranelift_end_block); for (destination, cranelift_end_param) in if_.destinations.iter().zip(cranelift_end_params) { @@ -645,12 +645,15 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_ for (case, case_block) in switch.cases.iter().zip(&case_blocks) { cranelift_switch.set_entry(case.value.into(), *case_block); } - context.seal_current_block(); cranelift_switch.emit( &mut context.function_builder, cranelift_value, default_block, ); + for case_block in &case_blocks { + context.seal_block(*case_block); + } + context.seal_block(default_block); // Compile the case and default blocks. for (case, case_block) in switch.cases.iter().zip(&case_blocks) { @@ -660,7 +663,6 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_ .function_builder .ins() .jump(end_block, &cranelift_block_args(end_args)); - context.seal_current_block(); } { @@ -670,9 +672,9 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_ .function_builder .ins() .jump(end_block, &cranelift_block_args(end_args)); - context.seal_current_block(); } + context.seal_block(end_block); context.switch_to_block(end_block); for (destination, cranelift_end_param) in switch.destinations.iter().zip(cranelift_end_params) { def_destination(destination, cranelift_end_param, context); -- cgit v1.2.3