Skip to content

Commit

Permalink
fix: Somewhat reduce mem2reg memory usage (#3572)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Nov 27, 2023
1 parent 127b6aa commit 9b9ed89
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions compiler/noirc_evaluator/src/ssa/opt/mem2reg/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::BTreeMap};
use std::borrow::Cow;

use crate::ssa::ir::{
function::Function,
Expand All @@ -19,27 +19,27 @@ pub(super) struct Block {
/// Maps a ValueId to the Expression it represents.
/// Multiple ValueIds can map to the same Expression, e.g.
/// dereferences to the same allocation.
pub(super) expressions: BTreeMap<ValueId, Expression>,
pub(super) expressions: im::OrdMap<ValueId, Expression>,

/// Each expression is tracked as to how many aliases it
/// may have. If there is only 1, we can attempt to optimize
/// out any known loads to that alias. Note that "alias" here
/// includes the original reference as well.
pub(super) aliases: BTreeMap<Expression, AliasSet>,
pub(super) aliases: im::OrdMap<Expression, AliasSet>,

/// Each allocate instruction result (and some reference block parameters)
/// will map to a Reference value which tracks whether the last value stored
/// to the reference is known.
pub(super) references: BTreeMap<ValueId, ReferenceValue>,
pub(super) references: im::OrdMap<ValueId, ReferenceValue>,

/// The last instance of a `Store` instruction to each address in this block
pub(super) last_stores: BTreeMap<ValueId, InstructionId>,
pub(super) last_stores: im::OrdMap<ValueId, InstructionId>,
}

/// An `Expression` here is used to represent a canonical key
/// into the aliases map since otherwise two dereferences of the
/// same address will be given different ValueIds.
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub(super) enum Expression {
Dereference(Box<Expression>),
ArrayElement(Box<Expression>),
Expand Down Expand Up @@ -111,10 +111,7 @@ impl Block {
}

fn invalidate_all_references(&mut self) {
for reference_value in self.references.values_mut() {
*reference_value = ReferenceValue::Unknown;
}

self.references.clear();
self.last_stores.clear();
}

Expand All @@ -137,7 +134,7 @@ impl Block {
}

// Keep only the references present in both maps.
let mut intersection = BTreeMap::new();
let mut intersection = im::OrdMap::new();
for (value_id, reference) in &other.references {
if let Some(existing) = self.references.get(value_id) {
intersection.insert(*value_id, existing.unify(*reference));
Expand Down

0 comments on commit 9b9ed89

Please sign in to comment.