Skip to content

Commit

Permalink
chore: execute any Brillig opcode optimistically and return None if…
Browse files Browse the repository at this point in the history
… unable
  • Loading branch information
TomAFrench committed Aug 5, 2023
1 parent d58d84c commit bdcf96b
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -932,13 +923,13 @@ impl AcirContext {
Ok(outputs_var)
}

fn execute_constant_brillig(
fn execute_brillig(
&mut self,
code: Vec<BrilligOpcode>,
inputs: &[BrilligInputs],
outputs: &[AcirType],
) -> Option<Vec<AcirValue>> {
let results = execute_constant_brillig(code, inputs, outputs)?;
let results = execute_brillig(code, inputs, outputs)?;

let mut b_outputs: Vec<AcirValue> = Vec::new();
for (output, result) in outputs.iter().zip(results) {
Expand Down Expand Up @@ -1214,7 +1205,7 @@ enum BrilligOutputValue {
Array(Vec<FieldElement>),
}

fn execute_constant_brillig(
fn execute_brillig(
code: Vec<BrilligOpcode>,
inputs: &[BrilligInputs],
outputs: &[AcirType],
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bdcf96b

Please sign in to comment.