Skip to content

Commit

Permalink
feat: short-circuit empty loop codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 11, 2025
1 parent f823e47 commit 9edf47d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,19 @@ impl<'a> FunctionContext<'a> {
/// ... This is the current insert point after codegen_for finishes ...
/// ```
fn codegen_for(&mut self, for_expr: &ast::For) -> Result<Values, RuntimeError> {
self.builder.set_location(for_expr.start_range_location);
let start_index = self.codegen_non_tuple_expression(&for_expr.start_range)?;

self.builder.set_location(for_expr.end_range_location);
let end_index = self.codegen_non_tuple_expression(&for_expr.end_range)?;

if let (Some(start_constant), Some(end_constant)) = (self.builder.current_function.dfg.get_numeric_constant(start_index), self.builder.current_function.dfg.get_numeric_constant(end_index)) {
// If we can determine that the loop contains zero iterations then we can short-circuit codegen.
if start_constant == end_constant {
return Ok(Self::unit_value())
}
}

let loop_entry = self.builder.insert_block();
let loop_body = self.builder.insert_block();
let loop_end = self.builder.insert_block();
Expand All @@ -540,11 +553,6 @@ impl<'a> FunctionContext<'a> {
// within the loop which need to jump to them.
self.enter_loop(loop_entry, loop_index, loop_end);

self.builder.set_location(for_expr.start_range_location);
let start_index = self.codegen_non_tuple_expression(&for_expr.start_range)?;

self.builder.set_location(for_expr.end_range_location);
let end_index = self.codegen_non_tuple_expression(&for_expr.end_range)?;

// Set the location of the initial jmp instruction to the start range. This is the location
// used to issue an error if the start range cannot be determined at compile-time.
Expand Down

0 comments on commit 9edf47d

Please sign in to comment.