From 66f15caba8466501256a98cee289c49376b27097 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Tue, 5 Nov 2024 12:43:16 +0000 Subject: [PATCH] fix(ssa): Resolve value IDs in terminator before comparing to array (#6448) --- compiler/noirc_evaluator/src/ssa/ir/dfg.rs | 5 +++-- compiler/noirc_evaluator/src/ssa/opt/array_set.rs | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs index d79916a9e11..2be9ffa9afa 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs @@ -74,8 +74,9 @@ pub(crate) struct DataFlowGraph { blocks: DenseMap, /// Debugging information about which `ValueId`s have had their underlying `Value` substituted - /// for that of another. This information is purely used for printing the SSA, and has no - /// material effect on the SSA itself. + /// for that of another. In theory this information is purely used for printing the SSA, + /// and has no material effect on the SSA itself, however in practice the IDs can get out of + /// sync and may need this resolution before they can be compared. #[serde(skip)] replaced_value_ids: HashMap, diff --git a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs index 7035345436e..7d9694d4872 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/array_set.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/array_set.rs @@ -105,13 +105,15 @@ impl<'f> Context<'f> { // If the array comes from a load we may potentially being mutating an array at a reference // that is loaded from by other values. let terminator = self.dfg[block_id].unwrap_terminator(); + // If we are in a return block we are not concerned about the array potentially being mutated again. let is_return_block = matches!(terminator, TerminatorInstruction::Return { .. }); // We also want to check that the array is not part of the terminator arguments, as this means it is used again. let mut array_in_terminator = false; terminator.for_each_value(|value| { - if value == array { + // The terminator can contain original IDs, while the SSA has replaced the array value IDs; we need to resolve to compare. + if !array_in_terminator && self.dfg.resolve(value) == array { array_in_terminator = true; } });