Skip to content

Commit

Permalink
chore: add regression test for #5756 (#5770)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
TomAFrench authored Oct 8, 2024
1 parent f6dfbcf commit 99332f6
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);
}

// 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);
}
}

0 comments on commit 99332f6

Please sign in to comment.