Skip to content

Commit 99332f6

Browse files
authored
chore: add regression test for #5756 (#5770)
# Description ## Problem\* Adds regression tests for #5756 ## Summary\* This PR is some investigation for #5756 as I'm not sure on the underlying cause as the keccakf1600 instructions _should_ be deduplicated. I'm getting different behaviour in the constant folding pass test compared to `test_programs` which I'm a little confused by. Posting this so other people can investigate as well. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent f6dfbcf commit 99332f6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,4 +843,57 @@ mod test {
843843
let instructions = main.dfg[main.entry_block()].instructions();
844844
assert_eq!(instructions.len(), 10);
845845
}
846+
847+
// This test currently fails. It being fixed will address the issue https://github.com/noir-lang/noir/issues/5756
848+
#[test]
849+
#[should_panic]
850+
fn constant_array_deduplication() {
851+
// fn main f0 {
852+
// b0(v0: u64):
853+
// 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])
854+
// 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])
855+
// }
856+
//
857+
// Here we're checking a situation where two identical arrays are being initialized twice and being assigned separate `ValueId`s.
858+
// This would result in otherwise identical instructions not being deduplicated.
859+
let main_id = Id::test_new(0);
860+
861+
// Compiling main
862+
let mut builder = FunctionBuilder::new("main".into(), main_id);
863+
let v0 = builder.add_parameter(Type::unsigned(64));
864+
let zero = builder.numeric_constant(0u128, Type::unsigned(64));
865+
let typ = Type::Array(Arc::new(vec![Type::unsigned(64)]), 25);
866+
867+
let array_contents = vec![
868+
v0, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero,
869+
zero, zero, zero, zero, zero, zero, zero, zero, zero, zero,
870+
];
871+
let array1 = builder.array_constant(array_contents.clone().into(), typ.clone());
872+
let array2 = builder.array_constant(array_contents.into(), typ.clone());
873+
874+
assert_eq!(array1, array2, "arrays were assigned different value ids");
875+
876+
let keccakf1600 =
877+
builder.import_intrinsic("keccakf1600").expect("keccakf1600 intrinsic should exist");
878+
let _v10 = builder.insert_call(keccakf1600, vec![array1], vec![typ.clone()]);
879+
let _v11 = builder.insert_call(keccakf1600, vec![array2], vec![typ.clone()]);
880+
881+
let ssa = builder.finish();
882+
883+
println!("{ssa}");
884+
885+
let main = ssa.main();
886+
let instructions = main.dfg[main.entry_block()].instructions();
887+
let starting_instruction_count = instructions.len();
888+
assert_eq!(starting_instruction_count, 2);
889+
890+
let ssa = ssa.fold_constants();
891+
892+
println!("{ssa}");
893+
894+
let main = ssa.main();
895+
let instructions = main.dfg[main.entry_block()].instructions();
896+
let ending_instruction_count = instructions.len();
897+
assert_eq!(ending_instruction_count, 1);
898+
}
846899
}

0 commit comments

Comments
 (0)