Skip to content

Commit

Permalink
chore(ssa refactoring): Add CFG flattening pass to flatten cfg into a…
Browse files Browse the repository at this point in the history
… single block (#1397)

* Implement CFG flattening pass

* Remove printlns

* Add more comments

* Fix clippy lints

* Fix typo

* Fix issue found with visiting blocks out of order

* Add doc comment

* Remove typos

* Fix one last typo
  • Loading branch information
jfecher authored May 26, 2023
1 parent e97f649 commit e9d22c9
Show file tree
Hide file tree
Showing 6 changed files with 676 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub(crate) fn optimize_into_acir(program: Program) -> GeneratedAcir {
.print("After Inlining:")
.unroll_loops()
.print("After Unrolling:")
.simplify_cfg()
.print("After Simplifying:")
.flatten_cfg()
.print("After Flattening:")
.mem2reg()
.print("After Mem2Reg:")
.into_acir(func_signature)
}

Expand Down
10 changes: 10 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/ir/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ impl BasicBlock {
std::mem::replace(terminator, TerminatorInstruction::Return { return_values: Vec::new() })
}

/// Return the jmp arguments, if any, of this block's TerminatorInstruction.
///
/// If this block has no terminator, or a Return terminator this will be empty.
pub(crate) fn terminator_arguments(&self) -> &[ValueId] {
match &self.terminator {
Some(TerminatorInstruction::Jmp { arguments, .. }) => arguments,
_ => &[],
}
}

/// Iterate over all the successors of the currently block, as determined by
/// the blocks jumped to in the terminator instruction. If there is no terminator
/// instruction yet, this will iterate 0 times.
Expand Down
9 changes: 9 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ pub(crate) enum Instruction {
}

impl Instruction {
/// Returns a binary instruction with the given operator, lhs, and rhs
pub(crate) fn binary(operator: BinaryOp, lhs: ValueId, rhs: ValueId) -> Instruction {
Instruction::Binary(Binary { lhs, operator, rhs })
}

/// Returns the type that this instruction will return.
pub(crate) fn result_type(&self) -> InstructionResultType {
match self {
Expand Down Expand Up @@ -351,6 +356,10 @@ impl Binary {
if rhs_is_one {
return SimplifyResult::SimplifiedTo(self.lhs);
}
if lhs_is_zero || rhs_is_zero {
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
}
BinaryOp::Div => {
if rhs_is_one {
Expand Down
Loading

0 comments on commit e9d22c9

Please sign in to comment.