Skip to content

Commit e58844d

Browse files
authored
feat: simplify multiplications by 0 or 1 in ACIR gen (#3924)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR avoids churn on `AcirVar`s when we perform trivial multiplications. This avoids some situations where we were codegenning expressions with zero-coefficient terms. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent 64dd78b commit e58844d

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,19 @@ impl AcirContext {
533533
let lhs_data = self.vars[&lhs].clone();
534534
let rhs_data = self.vars[&rhs].clone();
535535
let result = match (lhs_data, rhs_data) {
536+
// (x * 1) == (1 * x) == x
537+
(AcirVarData::Const(constant), _) if constant.is_one() => rhs,
538+
(_, AcirVarData::Const(constant)) if constant.is_one() => lhs,
539+
540+
// (x * 0) == (0 * x) == 0
541+
(AcirVarData::Const(constant), _) | (_, AcirVarData::Const(constant))
542+
if constant.is_zero() =>
543+
{
544+
self.add_constant(FieldElement::zero())
545+
}
546+
536547
(AcirVarData::Const(lhs_constant), AcirVarData::Const(rhs_constant)) => {
537-
self.add_data(AcirVarData::Const(lhs_constant * rhs_constant))
548+
self.add_constant(lhs_constant * rhs_constant)
538549
}
539550
(AcirVarData::Witness(witness), AcirVarData::Const(constant))
540551
| (AcirVarData::Const(constant), AcirVarData::Witness(witness)) => {

0 commit comments

Comments
 (0)