Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/noirc_evaluator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ similar-asserts.workspace = true
tracing-test = "0.2.5"
num-traits.workspace = true
test-case.workspace = true
glob = "0.3.2"

[features]
bn254 = ["noirc_frontend/bn254"]
Expand Down
9 changes: 8 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/array_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
impl Function {
pub(crate) fn array_set_optimization(&mut self) {
if matches!(self.runtime(), RuntimeType::Brillig(_)) {
// Brillig is supposed to use refcounting to decide whether to mutate an array;

Check warning on line 32 in compiler/noirc_evaluator/src/ssa/opt/array_set.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (refcounting)
// array mutation was only meant for ACIR. We could use it with Brillig as well,
// but then some of the optimizations that we can do in ACIR around shared
// references have to be skipped, which makes it more cumbersome.
Expand Down Expand Up @@ -191,7 +191,14 @@

#[cfg(test)]
mod tests {
use crate::ssa::{Ssa, opt::assert_normalized_ssa_equals};
use crate::ssa::Ssa;

use crate::ssa::{opt::assert_normalized_ssa_equals, opt::run_snapshots};

#[test]
fn array_set_optimization_snapshots() {
run_snapshots("array_set_optimizations", Ssa::array_set_optimization);
}

#[test]
fn array_set_in_loop_with_conditional_clone() {
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/as_slice_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ fn replace_known_slice_lengths(

#[cfg(test)]
mod test {
use crate::ssa::opt::assert_normalized_ssa_equals;
use crate::ssa::opt::{assert_normalized_ssa_equals, run_snapshots};

use super::Ssa;

#[test]
fn as_slice_length_snapshots() {
run_snapshots("as_slice_optimization", Ssa::as_slice_optimization);
}

#[test]
fn as_slice_length_optimization() {
// In this code we expect `return v2` to be replaced with `return u32 3` because
Expand Down
12 changes: 12 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/assert_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,15 @@ fn evaluate_static_assert(
}
}
}

#[cfg(test)]
mod tests {
use crate::ssa::opt::run_snapshots;

#[test]
fn assert_constant_snapshots() {
run_snapshots("static_assert_and_assert_constant", |ssa| {
ssa.evaluate_static_assert_and_assert_constant().unwrap()
});
}
}
10 changes: 9 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,15 @@ impl Context<'_> {

#[cfg(test)]
mod test {
use crate::ssa::{Ssa, opt::assert_normalized_ssa_equals};
use crate::ssa::{
Ssa,
opt::{assert_normalized_ssa_equals, run_snapshots},
};

#[test]
fn flatten_basic_conditional_snapshots() {
run_snapshots("simplify_conditionals_for_unconstrained", Ssa::flatten_basic_conditionals);
}

#[test]
fn basic_jmpif() {
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/brillig_array_gets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ impl Function {

#[cfg(test)]
mod tests {
use crate::ssa::opt::assert_normalized_ssa_equals;
use crate::ssa::opt::{assert_normalized_ssa_equals, run_snapshots};

use super::Ssa;

#[test]
fn brillig_array_gets_snapshots() {
run_snapshots("brillig_array_get_optimizations", Ssa::brillig_array_gets);
}

#[test]
fn offset_array_get_constant_index() {
let src = "
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/brillig_entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,15 @@ pub(crate) fn build_inner_call_to_entry_points(

#[cfg(test)]
mod tests {
use crate::ssa::opt::assert_normalized_ssa_equals;
use crate::ssa::opt::{assert_normalized_ssa_equals, run_snapshots};

use super::Ssa;

#[test]
fn brillig_entry_point_analysis_snapshots() {
run_snapshots("brillig_entry_point_analysis", Ssa::brillig_entry_point_analysis);
}

#[test]
fn duplicate_inner_call_with_multiple_entry_points() {
let src = "
Expand Down
10 changes: 9 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/check_u128_mul_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ fn check_u128_mul_overflow(

#[cfg(test)]
mod tests {
use crate::ssa::{opt::assert_normalized_ssa_equals, ssa_gen::Ssa};
use crate::ssa::{
opt::{assert_normalized_ssa_equals, run_snapshots},
ssa_gen::Ssa,
};

#[test]
fn check_u128_mul_overflow_snapshots() {
run_snapshots("check_u128_mul_overflow", Ssa::check_u128_mul_overflow);
}

#[test]
fn does_not_insert_check_if_lhs_is_less_than_two_pow_64() {
Expand Down
12 changes: 11 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@
// These can fail.
Constrain(..) | ConstrainNotEqual(..) | RangeCheck { .. } => true,

// This should never be side-effectful

Check warning on line 832 in compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (effectful)
MakeArray { .. } | Noop => false,

// Some binary math can overflow or underflow
Expand Down Expand Up @@ -943,10 +943,20 @@
map::Id,
types::{NumericType, Type},
},
opt::assert_normalized_ssa_equals,
opt::{assert_normalized_ssa_equals, run_snapshots},
},
};

#[test]
fn fold_constants_snapshots() {
run_snapshots("constant_folding", Ssa::fold_constants);
}

#[test]
fn fold_constraints_snapshots() {
run_snapshots("constraint_folding", Ssa::fold_constants_using_constraints);
}

#[test]
fn simple_constant_fold() {
// After constructing this IR, we set the value of v0 to 2.
Expand Down
8 changes: 7 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,16 @@ fn build_return_block(

#[cfg(test)]
mod tests {
use crate::ssa::opt::assert_normalized_ssa_equals;

use crate::ssa::opt::{assert_normalized_ssa_equals, run_snapshots};

use super::Ssa;

#[test]
fn defunctionalize_snapshots() {
run_snapshots("defunctionalization", Ssa::defunctionalize);
}

#[test]
fn apply_inherits_caller_runtime() {
// Extracted from `execution_success/brillig_fns_as_values` with `--force-brillig`
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,14 @@ mod test {
map::Id,
types::{NumericType, Type},
},
opt::assert_normalized_ssa_equals,
opt::{assert_normalized_ssa_equals, run_snapshots},
};

#[test]
fn die_snapshots() {
run_snapshots("dead_instruction_elimination", Ssa::dead_instruction_elimination);
}

#[test]
fn dead_instruction_elimination() {
let src = "
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,14 @@ mod test {
instruction::{Instruction, TerminatorInstruction},
value::{Value, ValueId},
},
opt::assert_normalized_ssa_equals,
opt::{assert_normalized_ssa_equals, run_snapshots},
};

#[test]
fn flatten_cfg_snapshots() {
run_snapshots("flattening", Ssa::flatten_cfg);
}

#[test]
fn basic_jmpif() {
let src = "
Expand Down
10 changes: 9 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,17 @@ mod test {
map::Id,
types::{NumericType, Type},
},
opt::{assert_normalized_ssa_equals, inlining::inline_info::compute_bottom_up_order},
opt::{
assert_normalized_ssa_equals, inlining::inline_info::compute_bottom_up_order,
run_snapshots,
},
};

#[test]
fn inlining_snapshots() {
run_snapshots("inlining", |ssa| ssa.inline_functions(i64::MAX));
}

#[test]
fn basic_inlining() {
// fn foo {
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,12 @@ fn can_be_hoisted(
#[cfg(test)]
mod test {
use crate::ssa::Ssa;
use crate::ssa::opt::assert_normalized_ssa_equals;
use crate::ssa::opt::{assert_normalized_ssa_equals, run_snapshots};

#[test]
fn loop_invariant_snapshots() {
run_snapshots("loop_invariant_code_motion", Ssa::loop_invariant_code_motion);
}

#[test]
fn simple_loop_invariant_code_motion() {
Expand Down
11 changes: 11 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/make_constrain_not_equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ impl Function {
}
}
}

#[cfg(test)]
mod tests {

use crate::ssa::{opt::run_snapshots, ssa_gen::Ssa};

#[test]
fn constrain_not_equal_snapshots() {
run_snapshots("adding_constrain_not_equal", Ssa::make_constrain_not_equal_instructions);
}
}
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
//!
//! Repeating this algorithm for each block in the function in program order should result in
//! optimizing out most known loads. However, identifying all aliases correctly has been proven
//! undecidable in general (Landi, 1992). So this pass will not always optimize out all loads

Check warning on line 71 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Landi)
//! that could theoretically be optimized out. This pass can be performed at any time in the
//! SSA optimization pipeline, although it will be more successful the simpler the program's CFG is.
//! This pass is currently performed several times to enable other passes - most notably being
Expand Down Expand Up @@ -681,9 +681,14 @@
map::Id,
types::Type,
},
opt::assert_normalized_ssa_equals,
opt::{assert_normalized_ssa_equals, run_snapshots},
};

#[test]
fn mem2reg_snapshots() {
run_snapshots("mem2reg", Ssa::mem2reg);
}

#[test]
fn test_simple() {
// fn func() {
Expand Down
32 changes: 32 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,35 @@ pub(crate) fn assert_normalized_ssa_equals(mut ssa: super::Ssa, expected: &str)
println!("Got:\n~~~\n{ssa}\n~~~");
similar_asserts::assert_eq!(expected_ssa, ssa);
}

#[cfg(test)]
fn run_snapshots(snapshot_title: &str, f: fn(super::ssa_gen::Ssa) -> super::ssa_gen::Ssa) {
use super::ssa_gen::Ssa;

let inputs = glob::glob(&format!("src/ssa/opt/snapshots/**/[0-9][0-9]_{snapshot_title}*"))
.expect("Failed to read glob pattern");

let mut empty_glob = true;
for path in inputs {
empty_glob = false;
let path = path.unwrap();

let index = &path.file_name().unwrap().to_str().unwrap()[0..2];
let next_index = index.parse::<u32>().unwrap() + 1;
let glob_pattern = format!("{}/{next_index:02}_*", path.parent().unwrap().display());
let output_file = glob::glob(&glob_pattern)
.expect("Failed to read glob pattern")
.next()
.unwrap()
.unwrap();

let src = std::fs::read_to_string(&path).unwrap();
let expected = std::fs::read_to_string(&output_file).unwrap();

let ssa = Ssa::from_str(&src).unwrap();

let ssa = f(ssa);
assert_normalized_ssa_equals(ssa, &expected);
}
assert!(!empty_glob, "Glob was empty");
}
11 changes: 11 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/preprocess_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ impl Ssa {
self.remove_unreachable_functions()
}
}

#[cfg(test)]
mod tests {

use crate::ssa::opt::run_snapshots;

#[test]
fn preprocess_fns_snapshots() {
run_snapshots("preprocessing_functions", |ssa| ssa.preprocess_functions(i64::MAX));
}
}
11 changes: 10 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
// Set of functions we call which the purity result depends on.
// `is_pure` is intended to be called on each function, building
// up a call graph of sorts to check afterwards to propagate impurity
// from called functions to their callers. Resultingly, an initial "Pure"

Check warning on line 116 in compiler/noirc_evaluator/src/ssa/opt/pure.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Resultingly)
// result here could be overridden by one of these dependencies being impure.
let mut dependencies = BTreeSet::new();

Expand Down Expand Up @@ -279,7 +279,16 @@

#[cfg(test)]
mod test {
use crate::ssa::{ir::function::FunctionId, opt::pure::Purity, ssa_gen::Ssa};
use crate::ssa::{
ir::function::FunctionId,
opt::{pure::Purity, run_snapshots},
ssa_gen::Ssa,
};

#[test]
fn purity_analysis_snapshots() {
run_snapshots("purity_analysis", Ssa::purity_analysis);
}

#[test]
fn classify_functions() {
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ mod test {

use crate::ssa::{
ir::{basic_block::BasicBlockId, dfg::DataFlowGraph, instruction::Instruction},
opt::run_snapshots,
ssa_gen::Ssa,
};

#[test]
fn rc_snapshots() {
run_snapshots("removing_paired_rc_inc_&_rc_decs", Ssa::remove_paired_rc);
}

fn count_inc_rcs(block: BasicBlockId, dfg: &DataFlowGraph) -> usize {
dfg[block]
.instructions()
Expand Down
10 changes: 10 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,13 @@ impl Context<'_> {
result
}
}

#[cfg(test)]
mod tests {
use crate::ssa::{opt::run_snapshots, ssa_gen::Ssa};

#[test]
fn remove_bit_shifts_snapshots() {
run_snapshots("removing_bit_shifts", Ssa::remove_bit_shifts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,15 @@
map::Id,
types::{NumericType, Type},
},
opt::run_snapshots,
ssa_gen::Ssa,
};

#[test]
fn remove_enable_side_effects_snapshots() {
run_snapshots("enablesideeffectsif_removal", Ssa::remove_enable_side_effects);

Check warning on line 216 in compiler/noirc_evaluator/src/ssa/opt/remove_enable_side_effects.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (enablesideeffectsif)
}

#[test]
fn remove_chains_of_same_condition() {
// acir(inline) fn main f0 {
Expand Down
10 changes: 10 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/remove_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,13 @@ fn slice_capacity_change(
| Intrinsic::FieldLessThan => SizeChange::None,
}
}

#[cfg(test)]
mod tests {
use crate::ssa::{opt::run_snapshots, ssa_gen::Ssa};

#[test]
fn remove_if_else_snapshots() {
run_snapshots("remove_ifelse", Ssa::remove_if_else);
}
}
Loading
Loading