From ffe7017fca99285f2d971ced5c53f22bab60c272 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Tue, 8 Oct 2024 17:37:11 +0000 Subject: [PATCH 1/2] chore: add testcase for array deduplication --- .../src/ssa/opt/constant_folding.rs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs index ea422fdff09..2dabbf8b942 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); } + + // 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); + } } From 2ab363949159a351c31c128ed7b2d6a499df7347 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Tue, 8 Oct 2024 17:42:08 +0000 Subject: [PATCH 2/2] chore: improve test --- compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs index 2dabbf8b942..3b86ded4a87 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs @@ -844,7 +844,7 @@ mod test { assert_eq!(instructions.len(), 10); } - // Regression for https://github.com/noir-lang/noir/issues/5756 + // 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() { @@ -885,7 +885,7 @@ mod test { let main = ssa.main(); let instructions = main.dfg[main.entry_block()].instructions(); let starting_instruction_count = instructions.len(); - assert_eq!(starting_instruction_count, 3); + assert_eq!(starting_instruction_count, 2); let ssa = ssa.fold_constants();