Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip codegen of zero iteration loops #7030

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@
/// br loop_entry(v0)
/// loop_entry(i: Field):
/// v2 = lt i v1
/// brif v2, then: loop_body, else: loop_end

Check warning on line 522 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// loop_body():
/// v3 = ... codegen body ...
/// v4 = add 1, i
Expand All @@ -528,6 +528,22 @@
/// ... 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 there's no need to codegen the loop.
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,12 +556,6 @@
// 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.
self.builder.set_location(for_expr.start_range_location);
Expand Down Expand Up @@ -580,7 +590,7 @@
///
/// ```text
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: else_block

Check warning on line 593 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// then_block():
/// v1 = ... codegen a ...
/// br end_if(v1)
Expand All @@ -595,7 +605,7 @@
///
/// ```text
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: end_if

Check warning on line 608 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
/// then_block:
/// v1 = ... codegen a ...
/// br end_if()
Expand Down
Loading