Skip to content
Closed
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: 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