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

chore: Add short circuit in ssa-gen for known if conditions #7007

Merged
merged 2 commits into from
Jan 9, 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
23 changes: 23 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod program;
mod value;

use acvm::AcirField;
vezenovm marked this conversation as resolved.
Show resolved Hide resolved
use noirc_frontend::token::FmtStrFragment;
pub(crate) use program::Ssa;

Expand Down Expand Up @@ -510,7 +511,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 514 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 Down Expand Up @@ -571,7 +572,7 @@
///
/// ```text
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: else_block

Check warning on line 575 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 @@ -586,7 +587,7 @@
///
/// ```text
/// v0 = ... codegen cond ...
/// brif v0, then: then_block, else: end_if

Check warning on line 590 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 All @@ -595,6 +596,9 @@
/// ```
fn codegen_if(&mut self, if_expr: &ast::If) -> Result<Values, RuntimeError> {
let condition = self.codegen_non_tuple_expression(&if_expr.condition)?;
if let Some(result) = self.try_codegen_constant_if(condition, if_expr) {
return result;
}

let then_block = self.builder.insert_block();
let else_block = self.builder.insert_block();
Expand Down Expand Up @@ -633,6 +637,25 @@
Ok(result)
}

/// If the condition is known, skip codegen for the then/else branch and only compile the
/// relevant branch.
fn try_codegen_constant_if(
&mut self,
condition: ValueId,
if_expr: &ast::If,
) -> Option<Result<Values, RuntimeError>> {
let condition = self.builder.current_function.dfg.get_numeric_constant(condition)?;
vezenovm marked this conversation as resolved.
Show resolved Hide resolved

Some(if condition.is_zero() {
match if_expr.alternative.as_ref() {
Some(alternative) => self.codegen_expression(alternative),
None => Ok(Self::unit_value()),
}
} else {
self.codegen_expression(&if_expr.consequence)
})
}

fn codegen_tuple(&mut self, tuple: &[Expression]) -> Result<Values, RuntimeError> {
Ok(Tree::Branch(try_vecmap(tuple, |expr| self.codegen_expression(expr))?))
}
Expand Down
Loading