From faa2c570126b3c75173c319cf9ff77977465c734 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 25 Oct 2024 13:29:57 +0000 Subject: [PATCH 1/2] do not mark an array set from a paramter mutable --- .../noirc_evaluator/src/ssa/opt/array_set.rs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs index 6ae13bc085a..8f9e0b30099 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs @@ -53,7 +53,9 @@ struct Context<'f> { is_brillig_runtime: bool, array_to_last_use: HashMap, instructions_that_can_be_made_mutable: HashSet, - arrays_from_load: HashSet, + // Mapping of an array that comes from a load and whether the address + // it was loaded from is a reference parameter. + arrays_from_load: HashMap, inner_nested_arrays: HashMap, } @@ -64,7 +66,7 @@ impl<'f> Context<'f> { is_brillig_runtime, array_to_last_use: HashMap::default(), instructions_that_can_be_made_mutable: HashSet::default(), - arrays_from_load: HashSet::default(), + arrays_from_load: HashMap::default(), inner_nested_arrays: HashMap::default(), } } @@ -113,9 +115,13 @@ impl<'f> Context<'f> { array_in_terminator = true; } }); - if (!self.arrays_from_load.contains(&array) || is_return_block) - && !array_in_terminator - { + if let Some(is_from_param) = self.arrays_from_load.get(&array) { + // If the array was loaded from a reference parameter, we cannot + // safely mark that array mutable as it may be shared by another value. + if !is_from_param && is_return_block { + self.instructions_that_can_be_made_mutable.insert(*instruction_id); + } + } else if !array_in_terminator { self.instructions_that_can_be_made_mutable.insert(*instruction_id); } } @@ -133,10 +139,11 @@ impl<'f> Context<'f> { } } } - Instruction::Load { .. } => { + Instruction::Load { address } => { let result = self.dfg.instruction_results(*instruction_id)[0]; if matches!(self.dfg.type_of_value(result), Array { .. } | Slice { .. }) { - self.arrays_from_load.insert(result); + let is_reference_param = self.dfg.block_parameters(block_id).contains(address); + self.arrays_from_load.insert(result, is_reference_param); } } _ => (), From 55278b2cc109e9950a6a7f3919d380fcd1192f2b Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 25 Oct 2024 13:37:47 +0000 Subject: [PATCH 2/2] fmt --- compiler/noirc_evaluator/src/ssa/opt/array_set.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs index 8f9e0b30099..7035345436e 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs @@ -53,7 +53,7 @@ struct Context<'f> { is_brillig_runtime: bool, array_to_last_use: HashMap, instructions_that_can_be_made_mutable: HashSet, - // Mapping of an array that comes from a load and whether the address + // Mapping of an array that comes from a load and whether the address // it was loaded from is a reference parameter. arrays_from_load: HashMap, inner_nested_arrays: HashMap, @@ -116,7 +116,7 @@ impl<'f> Context<'f> { } }); if let Some(is_from_param) = self.arrays_from_load.get(&array) { - // If the array was loaded from a reference parameter, we cannot + // If the array was loaded from a reference parameter, we cannot // safely mark that array mutable as it may be shared by another value. if !is_from_param && is_return_block { self.instructions_that_can_be_made_mutable.insert(*instruction_id); @@ -142,7 +142,8 @@ impl<'f> Context<'f> { Instruction::Load { address } => { let result = self.dfg.instruction_results(*instruction_id)[0]; if matches!(self.dfg.type_of_value(result), Array { .. } | Slice { .. }) { - let is_reference_param = self.dfg.block_parameters(block_id).contains(address); + let is_reference_param = + self.dfg.block_parameters(block_id).contains(address); self.arrays_from_load.insert(result, is_reference_param); } }