Skip to content

Commit

Permalink
feat: skip reading values immediately after it being written into an …
Browse files Browse the repository at this point in the history
…array (#5449)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

I noticed a couple of cases where we were writing into an array and then
immediately reading the same index from it. This seems like something
that we should be able to unwind.

Draft as I want to do more thinking on how `EnableSideEffects` can
interact with this.

## 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 Jul 12, 2024
1 parent 299703c commit 141ecdd
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,18 @@ impl Instruction {
}
}
Instruction::ArrayGet { array, index } => {
if let Value::Instruction { instruction, .. } = &dfg[*array] {
if let Instruction::ArraySet { index: write_index, value, .. } =
dfg[*instruction]
{
// If we're reading from an index of the array which we just wrote to, we can return
// the value which we wrote without performing the read.
if dfg.resolve(write_index) == dfg.resolve(*index) {
return SimplifiedTo(value);
}
}
}

let array = dfg.get_array_constant(*array);
let index = dfg.get_numeric_constant(*index);
if let (Some((array, _)), Some(index)) = (array, index) {
Expand Down

0 comments on commit 141ecdd

Please sign in to comment.