Skip to content

Commit

Permalink
feat: use knowledge of assert_eq(lhs, rhs) enforcing lhs == rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Aug 3, 2023
1 parent d32ac85 commit 0a1d6a3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 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 @@ -331,10 +331,44 @@ impl AcirContext {
self.acir_ir.assert_is_zero(
lhs_data.to_expression().as_ref() - rhs_data.to_expression().as_ref(),
);

// `lhs` and `rhs` are now constrained to be equal so they are equivalent,
// otherwise the constraint above will be broken.
self.mark_vars_equivalent(lhs, rhs);

Ok(())
}
}

/// Informs [`AcirContext`] that the two [`AcirVars`][`AcirVar`] `lhs` and `rhs` are known to be equivalent.
///
/// [`AcirContext`] may then use these two variables interchangeably in future choosing the one which is simpler.
fn mark_vars_equivalent(&mut self, lhs: AcirVar, rhs: AcirVar) {
let lhs_data = &self.vars[&lhs];
let rhs_data = &self.vars[&rhs];

let (new_lhs, new_rhs) = match (lhs_data, rhs_data) {
// If we've constrained the variable to be equal to a constant, inline the constant in
// future appearances.
(constant @ AcirVarData::Const(_), _) | (_, constant @ AcirVarData::Const(_)) => {
(constant.clone(), constant.clone())
}

// If we can replace an expression with a simple witness then do so.
(witness @ AcirVarData::Witness(_), AcirVarData::Expr(_))
| (AcirVarData::Expr(_), witness @ AcirVarData::Witness(_)) => {
(witness.clone(), witness.clone())
}

// Otherwise leave things as they are.
// TODO: We can also inspect this more closely to use the simpler expression of the two in future.
(lhs, rhs) => (lhs.clone(), rhs.clone()),
};

self.vars.insert(lhs, new_lhs);
self.vars.insert(rhs, new_rhs);
}

/// Adds a new Variable to context whose value will
/// be constrained to be the division of `lhs` and `rhs`
pub(crate) fn div_var(
Expand Down

0 comments on commit 0a1d6a3

Please sign in to comment.