diff --git a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs index ea422fdff09..3b86ded4a87 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs @@ -843,4 +843,57 @@ mod test { let instructions = main.dfg[main.entry_block()].instructions(); assert_eq!(instructions.len(), 10); } + + // This test currently fails. It being fixed will address the issue 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, 2); + + 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); + } }