From 5fd09eda6384750f4d2f6551aca921da3741f686 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Mon, 2 Dec 2024 19:28:31 +0000 Subject: [PATCH] bring back the rc reload state --- .../noirc_evaluator/src/ssa/opt/mem2reg.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs b/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs index 53a31ae57c1..1750ecb453a 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs @@ -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>, + + /// 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> { @@ -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, } } @@ -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); } @@ -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 {