Skip to content

Commit

Permalink
fix: avoid duplicating constant arrays (#5287)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #5286 

## Summary\*
Keep track of constant arrays and the array they resolve to in mem2reg,
so that another constant array is not created for the same array.


## 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
guipublic authored Jun 19, 2024
1 parent e100017 commit 3ef3645
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions compiler/noirc_evaluator/src/ssa/ir/function_inserter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ pub(crate) struct FunctionInserter<'f> {
pub(crate) function: &'f mut Function,

values: HashMap<ValueId, ValueId>,
const_arrays: HashMap<im::Vector<ValueId>, ValueId>,
}

impl<'f> FunctionInserter<'f> {
pub(crate) fn new(function: &'f mut Function) -> FunctionInserter<'f> {
Self { function, values: HashMap::default() }
Self { function, values: HashMap::default(), const_arrays: HashMap::default() }
}

/// Resolves a ValueId to its new, updated value.
Expand All @@ -34,10 +35,17 @@ impl<'f> FunctionInserter<'f> {
super::value::Value::Array { array, typ } => {
let array = array.clone();
let typ = typ.clone();
let new_array = array.iter().map(|id| self.resolve(*id)).collect();
let new_id = self.function.dfg.make_array(new_array, typ);
self.values.insert(value, new_id);
new_id
let new_array: im::Vector<ValueId> =
array.iter().map(|id| self.resolve(*id)).collect();
if self.const_arrays.get(&new_array) == Some(&value) {
value
} else {
let new_array_clone = new_array.clone();
let new_id = self.function.dfg.make_array(new_array, typ);
self.values.insert(value, new_id);
self.const_arrays.insert(new_array_clone, new_id);
new_id
}
}
_ => value,
},
Expand Down

0 comments on commit 3ef3645

Please sign in to comment.