Skip to content

Commit

Permalink
chore: add testcase for array deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 8, 2024
1 parent a4fcd00 commit ffe7017
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,57 @@ mod test {
let instructions = main.dfg[main.entry_block()].instructions();
assert_eq!(instructions.len(), 10);
}

// Regression for https://github.com/noir-lang/noir/issues/5756
#[test]
#[should_panic]
fn constant_array_deduplication() {
// fn main f0 {
// b0(v0: u64):
// v5 = call keccakf1600([v0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0])
// v6 = call keccakf1600([v0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0])
// }
//
// Here we're checking a situation where two identical arrays are being initialized twice and being assigned separate `ValueId`s.
// This would result in otherwise identical instructions not being deduplicated.
let main_id = Id::test_new(0);

// Compiling main
let mut builder = FunctionBuilder::new("main".into(), main_id);
let v0 = builder.add_parameter(Type::unsigned(64));
let zero = builder.numeric_constant(0u128, Type::unsigned(64));
let typ = Type::Array(Arc::new(vec![Type::unsigned(64)]), 25);

let array_contents = vec![
v0, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero,
zero, zero, zero, zero, zero, zero, zero, zero, zero, zero,
];
let array1 = builder.array_constant(array_contents.clone().into(), typ.clone());
let array2 = builder.array_constant(array_contents.into(), typ.clone());

assert_eq!(array1, array2, "arrays were assigned different value ids");

let keccakf1600 =
builder.import_intrinsic("keccakf1600").expect("keccakf1600 intrinsic should exist");
let _v10 = builder.insert_call(keccakf1600, vec![array1], vec![typ.clone()]);
let _v11 = builder.insert_call(keccakf1600, vec![array2], vec![typ.clone()]);

let ssa = builder.finish();

println!("{ssa}");

let main = ssa.main();
let instructions = main.dfg[main.entry_block()].instructions();
let starting_instruction_count = instructions.len();
assert_eq!(starting_instruction_count, 3);

let ssa = ssa.fold_constants();

println!("{ssa}");

let main = ssa.main();
let instructions = main.dfg[main.entry_block()].instructions();
let ending_instruction_count = instructions.len();
assert_eq!(ending_instruction_count, 1);
}
}

0 comments on commit ffe7017

Please sign in to comment.