From 68c4cb45a9e87b738800401c8ec969159fe0889e Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 1 Dec 2023 12:50:10 +0000 Subject: [PATCH 1/2] feat: reuse witnesses more when interacting with memory --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 17f9b742625..07e550e1af2 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1425,7 +1425,8 @@ impl AcirContext { index: &AcirVar, ) -> Result { // Fetch the witness corresponding to the index - let index_witness = self.var_to_witness(*index)?; + let index_var = self.get_or_create_witness_var(*index)?; + let index_witness = self.var_to_witness(index_var)?; // Create a Variable to hold the result of the read and extract the corresponding Witness let value_read_var = self.add_variable(); @@ -1446,11 +1447,12 @@ impl AcirContext { value: &AcirVar, ) -> Result<(), InternalError> { // Fetch the witness corresponding to the index - // - let index_witness = self.var_to_witness(*index)?; + let index_var = self.get_or_create_witness_var(*index)?; + let index_witness = self.var_to_witness(index_var)?; // Fetch the witness corresponding to the value to be written - let value_write_witness = self.var_to_witness(*value)?; + let value_write_var = self.get_or_create_witness_var(*value)?; + let value_write_witness = self.var_to_witness(value_write_var)?; // Add the memory write operation to the list of opcodes let op = MemOp::write_to_mem_index(index_witness.into(), value_write_witness.into()); @@ -1492,6 +1494,7 @@ impl AcirContext { ) -> Result<(), InternalError> { match input { AcirValue::Var(var, _) => { + let var = self.get_or_create_witness_var(var)?; witnesses.push(self.var_to_witness(var)?); } AcirValue::Array(values) => { From 20ea191ba29c4d742c4853e495b23ee9ebf5c122 Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 1 Dec 2023 13:14:46 +0000 Subject: [PATCH 2/2] chore: add similar reuse to return witnesses --- .../noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 07e550e1af2..712913841f3 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -877,7 +877,8 @@ impl AcirContext { /// Converts the `AcirVar` to a `Witness` if it hasn't been already, and appends it to the /// `GeneratedAcir`'s return witnesses. pub(crate) fn return_var(&mut self, acir_var: AcirVar) -> Result<(), InternalError> { - let witness = self.var_to_witness(acir_var)?; + let return_var = self.get_or_create_witness_var(acir_var)?; + let witness = self.var_to_witness(return_var)?; self.acir_ir.push_return_witness(witness); Ok(()) }