Skip to content

Commit

Permalink
feat(optimization): Avoid merging identical (by ID) arrays (noir-lang…
Browse files Browse the repository at this point in the history
…#5853)

# Description

## Problem\*

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

## Summary\*

Found while I was working on Jan's bug. There were a number of arrays we
merged where we'd get `vN = if ... then vM else vM` which we had to go
through the long merge for without 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
jfecher authored Aug 28, 2024
1 parent 39b30ba commit 062103e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,15 @@ impl Instruction {
}
}

let then_value = dfg.resolve(*then_value);
let else_value = dfg.resolve(*else_value);
if then_value == else_value {
return SimplifiedTo(then_value);
}

if matches!(&typ, Type::Numeric(_)) {
let then_condition = *then_condition;
let then_value = *then_value;
let else_condition = *else_condition;
let else_value = *else_value;

let result = ValueMerger::merge_numeric_values(
dfg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl<'a> ValueMerger<'a> {
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
let then_value = self.dfg.resolve(then_value);
let else_value = self.dfg.resolve(else_value);

if then_value == else_value {
return then_value;
}

match self.dfg.type_of_value(then_value) {
Type::Numeric(_) => Self::merge_numeric_values(
self.dfg,
Expand Down

0 comments on commit 062103e

Please sign in to comment.