Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Somewhat reduce mem2reg memory usage #3572

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading