Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip emission of brillig calls which will never be executed #5314

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,26 @@ impl<F: AcirField> AcirContext<F> {
brillig_function_index: u32,
brillig_stdlib_func: Option<BrilligStdlibFunc>,
) -> Result<Vec<AcirValue>, RuntimeError> {
let predicate = self.var_to_expression(predicate)?;
if predicate.is_zero() {
// If the predicate has a constant value of zero, the brillig call will never be executed.
// We can then immediately zero out all of its outputs as this is the value which would be written
// if we waited until runtime to resolve this call.
let outputs_var = vecmap(outputs, |output| match output {
AcirType::NumericType(_) => {
let var = self.add_constant(F::zero());
AcirValue::Var(var, output.clone())
}
AcirType::Array(element_types, size) => {
self.zeroed_array_output(&element_types, size)
}
});

return Ok(outputs_var);
}
// Remove "always true" predicates.
let predicate = if predicate == Expression::one() { None } else { Some(predicate) };

let brillig_inputs: Vec<BrilligInputs<F>> =
try_vecmap(inputs, |i| -> Result<_, InternalError> {
match i {
Expand Down Expand Up @@ -1569,10 +1589,9 @@ impl<F: AcirField> AcirContext<F> {
acir_value
}
});
let predicate = self.var_to_expression(predicate)?;

self.acir_ir.brillig_call(
Some(predicate),
predicate,
generated_brillig,
brillig_inputs,
brillig_outputs,
Expand Down Expand Up @@ -1643,6 +1662,27 @@ impl<F: AcirField> AcirContext<F> {
Ok(())
}

/// Recursively create zeroed-out acir values for returned arrays. This is necessary because a brillig returned array can have nested arrays as elements.
fn zeroed_array_output(&mut self, element_types: &[AcirType], size: usize) -> AcirValue {
let mut array_values = im::Vector::new();
for _ in 0..size {
for element_type in element_types {
match element_type {
AcirType::Array(nested_element_types, nested_size) => {
let nested_acir_value =
self.zeroed_array_output(nested_element_types, *nested_size);
array_values.push_back(nested_acir_value);
}
AcirType::NumericType(_) => {
let var = self.add_constant(F::zero());
array_values.push_back(AcirValue::Var(var, element_type.clone()));
}
}
}
}
AcirValue::Array(array_values)
}

/// Recursively create acir values for returned arrays. This is necessary because a brillig returned array can have nested arrays as elements.
/// A singular array of witnesses is collected for a top level array, by deflattening the assigned witnesses at each level.
fn brillig_array_output(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl<F: AcirField> GeneratedAcir<F> {
let inputs = vec![BrilligInputs::Single(expr)];
let outputs = vec![BrilligOutputs::Simple(inverted_witness)];
self.brillig_call(
Some(Expression::one()),
None,
&inverse_code,
inputs,
outputs,
Expand Down
Loading