Skip to content

Commit

Permalink
fix(ssa): synchronisation for functions (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic authored Feb 9, 2023
1 parent a0c0c2c commit 615357a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 12 additions & 0 deletions crates/nargo/tests/test_data/9_conditional/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ fn main(a: u32, mut c: [u32; 4], x: [u8; 5], result: pub [u8; 32]){
}
constrain c_661[0] < 20000;

// Test case for function synchronisation
let mut c_sync = 0;
if a == 42 {
c_sync = foo2();
} else {
c_sync = foo2() + foo2();
}
constrain c_sync == 6;

// Regression for predicate simplification
safe_inverse(0);
}
Expand Down Expand Up @@ -254,6 +263,9 @@ fn issue_661_bar(a : [u32;4]) ->[u32;4] {
b
}

fn foo2() -> Field {
3
}

fn safe_inverse(n: Field) -> Field
{
Expand Down
11 changes: 8 additions & 3 deletions crates/noirc_evaluator/src/ssa/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ impl DecisionTree {
}
let mut modified = false;
super::optimizations::cse_block(ctx, left, &mut merged_ins, &mut modified)?;

if modified {
// a second round is necessary when the synchronisation optimise function calls between the two branches
// in that case, the first cse update the result instructions to the same call
// the second cse can (and must) then simplify identical result instructions.
super::optimizations::cse_block(ctx, left, &mut merged_ins, &mut modified)?;
}
//housekeeping...
let if_block = &mut ctx[if_block_id];
if_block.dominated = vec![left];
Expand Down Expand Up @@ -977,8 +982,8 @@ impl Segment {
Segment { left: (left_node.0, *left_node.1), right: (right_node.0, *right_node.1) }
}
pub fn intersect(&self, other: &Segment) -> bool {
(self.right.0 < other.right.0 && self.left.0 < other.left.0)
|| (self.right.0 > other.right.0 && self.left.0 > other.left.0)
!((self.right.0 < other.right.0 && self.left.0 < other.left.0)
|| (self.right.0 > other.right.0 && self.left.0 > other.left.0))
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/noirc_evaluator/src/ssa/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,9 @@ impl SsaContext {
//Optimization
block::compute_dom(self);
optimizations::full_cse(self, self.first_block, false)?;

// the second cse is recommended because of opportunities occuring from the first one
// we could use an optimisation level that will run more cse pass
optimizations::full_cse(self, self.first_block, false)?;
//flattening
self.log(enable_logging, "\nCSE:", "\nunrolling:");
//Unrolling
Expand Down
12 changes: 11 additions & 1 deletion crates/noirc_evaluator/src/ssa/optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn cse_block_with_anchor(

new_list.push(*ins_id);
} else if let Some(similar) = anchor.find_similar_instruction(&operator) {
debug_assert!(similar != ins.id);
assert_ne!(similar, ins.id);
*modified = true;
new_mark = Mark::ReplaceWith(similar);
} else if binary.operator == BinaryOp::Assign {
Expand All @@ -244,6 +244,16 @@ fn cse_block_with_anchor(
anchor.push_front(&ins.operation, *ins_id);
}
}
Operation::Result { .. } => {
if let Some(similar) = anchor.find_similar_instruction(&operator) {
assert_ne!(similar, ins.id);
*modified = true;
new_mark = Mark::ReplaceWith(similar);
} else {
new_list.push(*ins_id);
anchor.push_front(&ins.operation, *ins_id);
}
}
Operation::Load { array_id: x, .. } | Operation::Store { array_id: x, .. } => {
if !is_join && ins.operation.is_dummy_store() {
continue;
Expand Down

0 comments on commit 615357a

Please sign in to comment.