From bdcf96bf965eb82fd7dad51ef3a3d87fece539c0 Mon Sep 17 00:00:00 2001 From: Tom French Date: Sat, 5 Aug 2023 12:39:07 +0100 Subject: [PATCH] chore: execute any Brillig opcode optimistically and return `None` if unable --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 2f3e082123b..df96a856a2f 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -896,21 +896,12 @@ impl AcirContext { } })?; - // Check if we can evaluate the Brillig opcode at compile time. - if b_inputs.iter().all(|input| match input { - BrilligInputs::Single(expr) => expr.is_const(), - BrilligInputs::Array(var_expressions) => { - var_expressions.iter().all(|expr| expr.is_const()) - } - }) { - if let Some(brillig_outputs) = - self.execute_constant_brillig(code.clone(), &b_inputs, &outputs) - { - return Ok(brillig_outputs); - } + // Optimistically try executing the brillig now, if we can complete execution they just return the results. + if let Some(brillig_outputs) = self.execute_brillig(code.clone(), &b_inputs, &outputs) { + return Ok(brillig_outputs); } - // We can't execute the opcode at compile time so we must generate ACIR for it. + // Otherwise we must generate ACIR for it and execute at runtime. let mut b_outputs = Vec::new(); let outputs_var = vecmap(outputs, |output| match output { @@ -932,13 +923,13 @@ impl AcirContext { Ok(outputs_var) } - fn execute_constant_brillig( + fn execute_brillig( &mut self, code: Vec, inputs: &[BrilligInputs], outputs: &[AcirType], ) -> Option> { - let results = execute_constant_brillig(code, inputs, outputs)?; + let results = execute_brillig(code, inputs, outputs)?; let mut b_outputs: Vec = Vec::new(); for (output, result) in outputs.iter().zip(results) { @@ -1214,7 +1205,7 @@ enum BrilligOutputValue { Array(Vec), } -fn execute_constant_brillig( +fn execute_brillig( code: Vec, inputs: &[BrilligInputs], outputs: &[AcirType], @@ -1254,13 +1245,13 @@ fn execute_constant_brillig( for input in inputs { match input { BrilligInputs::Single(expr) => { - input_register_values.push(expr.to_const().unwrap().into()); + input_register_values.push(expr.to_const()?.into()); } BrilligInputs::Array(expr_arr) => { // Attempt to fetch all array input values let memory_pointer = input_memory.len(); for expr in expr_arr.iter() { - input_memory.push(expr.to_const().unwrap().into()); + input_memory.push(expr.to_const()?.into()); } // Push value of the array pointer as a register