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

test(flatten_cfg): unit test and end to end test for #1792 #1806

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.6.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let randomness = dep::std::hash::pedersen_with_separator([0], 0)[0];
let note0_is_real = ((randomness as u32) % 2) as bool;

let mut note0_value = 0;
if note0_is_real {
note0_value = randomness + 1;
} else {
note0_value = 0;
}

assert(note0_is_real == true);
}
135 changes: 135 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ impl<'f> Context<'f> {
#[cfg(test)]
mod test {

use std::rc::Rc;

use crate::ssa_refactor::{
ir::{
dfg::DataFlowGraph,
Expand Down Expand Up @@ -1196,4 +1198,137 @@ mod test {
_ => Vec::new(),
}
}

#[test]
fn should_not_merge_incorrectly_to_false() {
// Regression test for #1792
// Tests that it does not simplify to a true constraint an always-false constraint
// fn main f1 {
// b0():
// v4 = call pedersen([Field 0], u32 0)
// v5 = array_get v4, index Field 0
// v6 = cast v5 as u32
// v8 = mod v6, u32 2
// v9 = cast v8 as u1
// v10 = allocate
// store Field 0 at v10
// jmpif v9 then: b1, else: b2
// b1():
// v14 = add v5, Field 1
// store v14 at v10
// jmp b3()
// b3():
// v12 = eq v9, u1 1
// constrain v12
// return
// b2():
// store Field 0 at v10
// jmp b3()
// }
let main_id = Id::test_new(1);
let mut builder = FunctionBuilder::new("main".into(), main_id, RuntimeType::Acir);

builder.insert_block(); // b0
let b1 = builder.insert_block();
let b2 = builder.insert_block();
let b3 = builder.insert_block();

let element_type = Rc::new(vec![Type::field()]);
let zero = builder.field_constant(0_u128);
let zero_array = builder.array_constant(im::Vector::unit(zero), element_type.clone());
let i_zero = builder.numeric_constant(0_u128, Type::unsigned(32));
let pedersen =
builder.import_intrinsic_id(Intrinsic::BlackBox(acvm::acir::BlackBoxFunc::Pedersen));
let v4 = builder.insert_call(
pedersen,
vec![zero_array, i_zero],
vec![Type::Array(element_type, 2)],
)[0];
let v5 = builder.insert_array_get(v4, zero, Type::field());
let v6 = builder.insert_cast(v5, Type::unsigned(32));
let i_two = builder.numeric_constant(2_u128, Type::unsigned(32));
let v8 = builder.insert_binary(v6, BinaryOp::Mod, i_two);
let v9 = builder.insert_cast(v8, Type::bool());

let v10 = builder.insert_allocate();
builder.insert_store(v10, zero);

builder.terminate_with_jmpif(v9, b1, b2);

builder.switch_to_block(b1);
let one = builder.field_constant(1_u128);
let v14 = builder.insert_binary(v5, BinaryOp::Add, one);
builder.insert_store(v10, v14);
builder.terminate_with_jmp(b3, vec![]);

builder.switch_to_block(b2);
builder.insert_store(v10, zero);
builder.terminate_with_jmp(b3, vec![]);

builder.switch_to_block(b3);
let b_true = builder.numeric_constant(1_u128, Type::unsigned(1));
let v12 = builder.insert_binary(v9, BinaryOp::Eq, b_true);
builder.insert_constrain(v12);
builder.terminate_with_return(vec![]);

let ssa = builder.finish().flatten_cfg();
let main = ssa.main();

// Now assert that there is not an always-false constraint after flattening:
let mut num_constraints = 0;
for instruction in main.dfg[main.entry_block()].instructions() {
if let Instruction::Constrain(value) = main.dfg[*instruction] {
if let Some(constant) = main.dfg.get_numeric_constant(value) {
assert!(constant.is_one());
}
num_constraints += 1;
}
}
assert!(num_constraints == 1);
}
#[test]
fn should_not_merge_away_constraints() {
// Very simplified derived regression test for #1792
// Tests that it does not merge away a constrain
// We work on this SSA:
// fn main f1 {
// ... simplified ...
// v_false = u1 0
// jmpif v_false then: b_useless_branch, else: b_fallthrough
// b_useless_branch():
// jmp b2()
// b_fallthrough():
// constrain v_false // was incorrectly removed
// return
// }
let main_id = Id::test_new(1);
let mut builder = FunctionBuilder::new("main".into(), main_id, RuntimeType::Acir);

builder.insert_block(); // entry

let b_useless_branch = builder.insert_block();
let b_fallthrough = builder.insert_block();
let v_false = builder.numeric_constant(0_u128, Type::unsigned(1));

builder.terminate_with_jmpif(v_false, b_useless_branch, b_fallthrough);

builder.switch_to_block(b_useless_branch);
builder.terminate_with_jmp(b_fallthrough, vec![]);

builder.switch_to_block(b_fallthrough);
builder.insert_constrain(v_false); // should not be removed
builder.terminate_with_return(vec![]);

let ssa = builder.finish().flatten_cfg();
let main = ssa.main();

// Assert we have not incorrectly removed a constraint:
let mut num_constraints = 0;
for instruction in main.dfg[main.entry_block()].instructions() {
if let Instruction::Constrain(_) = main.dfg[*instruction] {
num_constraints += 1;
}
}
assert!(num_constraints == 1);
}
}