Skip to content

Commit

Permalink
Apply fix from #2466
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Aug 28, 2023
1 parent 410bc26 commit 1fc27a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
1 change: 1 addition & 0 deletions crates/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) fn optimize_into_acir(
// and this pass is missed, slice merging will fail inside of flattening.
.mem2reg()
.print(print_ssa_passes, "After Mem2Reg:")
.fold_constants()
.flatten_cfg()
.print(print_ssa_passes, "After Flattening:")
// Run mem2reg once more with the flattened CFG to catch any remaining loads/stores
Expand Down
4 changes: 3 additions & 1 deletion crates/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ impl Context {

// If the instruction doesn't have side-effects, cache the results so we can reuse them if
// the same instruction appears again later in the block.
if !instruction.has_side_effects(&function.dfg) {
if !instruction.has_side_effects(&function.dfg)
&& !matches!(instruction, Instruction::Allocate)
{
instruction_result_cache.insert(instruction, new_results.clone());
}
for (old_result, new_result) in old_results.iter().zip(new_results) {
Expand Down
48 changes: 15 additions & 33 deletions crates/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,7 @@ impl PerFunctionContext {
}
}

fn mark_all_unknown(
&self,
values: &[ValueId],
function: &Function,
references: &mut Block,
) {
fn mark_all_unknown(&self, values: &[ValueId], function: &Function, references: &mut Block) {
for value in values {
if function.dfg.value_is_reference(*value) {
let value = function.dfg.resolve(*value);
Expand Down Expand Up @@ -455,26 +450,15 @@ impl Block {
}

/// If the given address is known, set its value to `ReferenceValue::Known(value)`.
fn set_known_value(
&mut self,
address: ValueId,
value: ValueId,
) {
fn set_known_value(&mut self, address: ValueId, value: ValueId) {
self.set_value(address, ReferenceValue::Known(value));
}

fn set_unknown(
&mut self,
address: ValueId,
) {
fn set_unknown(&mut self, address: ValueId) {
self.set_value(address, ReferenceValue::Unknown);
}

fn set_value(
&mut self,
address: ValueId,
value: ReferenceValue,
) {
fn set_value(&mut self, address: ValueId, value: ReferenceValue) {
let expression = self.expressions.entry(address).or_insert(Expression::Other(address));
let aliases = self.aliases.entry(expression.clone()).or_default();

Expand Down Expand Up @@ -555,7 +539,11 @@ impl Block {
}

/// Iterate through each known alias of the given address and apply the function `f` to each.
fn for_each_alias_of<T>(&mut self, address: ValueId, mut f: impl FnMut(&mut Self, ValueId) -> T) {
fn for_each_alias_of<T>(
&mut self,
address: ValueId,
mut f: impl FnMut(&mut Self, ValueId) -> T,
) {
if let Some(expr) = self.expressions.get(&address) {
if let Some(aliases) = self.aliases.get(expr).cloned() {
for alias in aliases {
Expand All @@ -565,11 +553,7 @@ impl Block {
}
}

fn keep_last_stores_for(
&mut self,
address: ValueId,
function: &Function,
) {
fn keep_last_stores_for(&mut self, address: ValueId, function: &Function) {
let address = function.dfg.resolve(address);
self.keep_last_store(address, function);
self.for_each_alias_of(address, |t, alias| t.keep_last_store(alias, function));
Expand All @@ -584,17 +568,15 @@ impl Block {
match &function.dfg[instruction] {
Instruction::Store { value, .. } => {
self.mark_value_used(*value, function);
},
other => unreachable!("last_store held an id of a non-store instruction: {other:?}"),
}
other => {
unreachable!("last_store held an id of a non-store instruction: {other:?}")
}
}
}
}

fn mark_value_used(
&mut self,
value: ValueId,
function: &Function,
) {
fn mark_value_used(&mut self, value: ValueId, function: &Function) {
self.keep_last_stores_for(value, function);

// We must do a recursive check for arrays since they're the only Values which may contain
Expand Down

0 comments on commit 1fc27a3

Please sign in to comment.