Skip to content

Commit

Permalink
Merge 4d294bb into 31640e9
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm authored Dec 5, 2024
2 parents 31640e9 + 4d294bb commit 2c1bce9
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ struct PerFunctionContext<'f> {
/// instruction that aliased that reference.
/// If that store has been set for removal, we can also remove this instruction.
aliased_references: HashMap<ValueId, HashSet<InstructionId>>,

/// Track whether the last instruction is an inc_rc/dec_rc instruction.
/// If it is we should not remove any repeat last loads.
inside_rc_reload: bool,
}

impl<'f> PerFunctionContext<'f> {
Expand All @@ -158,6 +162,7 @@ impl<'f> PerFunctionContext<'f> {
last_loads: HashMap::default(),
calls_reference_input: HashSet::default(),
aliased_references: HashMap::default(),
inside_rc_reload: false,
}
}

Expand Down Expand Up @@ -435,7 +440,7 @@ impl<'f> PerFunctionContext<'f> {
let result = self.inserter.function.dfg.instruction_results(instruction)[0];
let previous_result =
self.inserter.function.dfg.instruction_results(*last_load)[0];
if *previous_address == address {
if *previous_address == address && !self.inside_rc_reload {
self.inserter.map_value(result, previous_result);
self.instructions_to_remove.insert(instruction);
}
Expand Down Expand Up @@ -553,6 +558,18 @@ impl<'f> PerFunctionContext<'f> {
}
_ => (),
}

self.track_rc_reload_state(instruction);
}

fn track_rc_reload_state(&mut self, instruction: InstructionId) {
match &self.inserter.function.dfg[instruction] {
// We just had an increment or decrement to an array's reference counter
Instruction::IncrementRc { .. } | Instruction::DecrementRc { .. } => {
self.inside_rc_reload = true;
}
_ => self.inside_rc_reload = false,
}
}

fn contains_references(typ: &Type) -> bool {
Expand Down

0 comments on commit 2c1bce9

Please sign in to comment.