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

fix: Respect order in bubble up for redundant asserts #4109

Merged
merged 11 commits into from
Jan 22, 2024
9 changes: 8 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/bubble_up_constrains.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use crate::ssa::{ir::instruction::Instruction, ssa_gen::Ssa};

impl Ssa {
Expand All @@ -9,6 +11,8 @@ impl Ssa {
for block in function.reachable_blocks() {
let instructions = function.dfg[block].take_instructions();
let mut filtered_instructions = Vec::with_capacity(instructions.len());
let mut inserted_at_index: HashMap<usize, usize> =
HashMap::with_capacity(instructions.len());

let dfg = &function.dfg;
for instruction in instructions {
Expand All @@ -32,7 +36,10 @@ impl Ssa {
.map(|reversed_index| filtered_instructions.len() - reversed_index)
.unwrap_or(0);

filtered_instructions.insert(index, instruction);
let already_inserted_at_index = inserted_at_index.entry(index).or_default();

filtered_instructions.insert(index + *already_inserted_at_index, instruction);
*already_inserted_at_index += 1;
sirasistant marked this conversation as resolved.
Show resolved Hide resolved
}

*function.dfg[block].instructions_mut() = filtered_instructions;
Expand Down
sirasistant marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "bubble_up_constrains"
type = "bin"
authors = [""]
[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use dep::std::field::bn254::{TWO_POW_128, assert_gt};

#[test(should_fail_with="Balance too low")]
fn test_assert_gt_should_fail_eq() {
let a: u64 = 4;
let b: u64 = 5;
assert(a >= b, "Balance too low");
assert(a >= b, "Redundant constrain");
}