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

feat: mark division/modulo by constants as not requiring a predicate #7028

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions compiler/noirc_evaluator/src/acir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,19 @@ impl<F: AcirField, B: BlackBoxFunctionSolver<F>> AcirContext<F, B> {
let var_data = &self.vars[&var];
if let AcirVarData::Const(constant) = var_data {
// Note that this will return a 0 if the inverse is not available
let inverted_var = self.add_data(AcirVarData::Const(constant.inverse()));
let inverted_constant = constant.inverse();
let inverted_var = self.add_constant(inverted_constant);

let should_be_one = self.mul_var(inverted_var, var)?;

// Check that the inverted var is valid.
// This check prevents invalid divisions by zero.
let should_be_one = self.mul_var(inverted_var, var)?;
self.maybe_eq_predicate(should_be_one, predicate)?;
if inverted_constant.is_zero() {
self.maybe_eq_predicate(should_be_one, predicate)?;
} else {
let one = self.add_constant(F::one());
self.assert_eq_var(should_be_one, one, None)?;
}

return Ok(inverted_var);
}
Expand Down
17 changes: 9 additions & 8 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
// These can fail.
Constrain(..) | ConstrainNotEqual(..) | RangeCheck { .. } => true,

// This should never be side-effectful

Check warning on line 414 in compiler/noirc_evaluator/src/ssa/ir/instruction.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (effectful)
MakeArray { .. } | Noop => false,

// Some binary math can overflow or underflow
Expand Down Expand Up @@ -511,11 +511,10 @@
match self {
Binary(binary) => {
if matches!(binary.operator, BinaryOp::Div | BinaryOp::Mod) {
if let Some(rhs) = function.dfg.get_numeric_constant(binary.rhs) {
rhs != FieldElement::zero()
} else {
false
}
function
.dfg
.get_numeric_constant(binary.rhs)
.map_or(false, |rhs| !rhs.is_zero())
} else {
true
}
Expand Down Expand Up @@ -575,11 +574,13 @@
match self {
Instruction::Binary(binary) => {
match binary.operator {
BinaryOp::Div | BinaryOp::Mod => {
// Division and modulo operations can fail if the RHS is zero but is otherwise safe.
dfg.get_numeric_constant(binary.rhs).map_or(false, |rhs| !rhs.is_zero())
}
BinaryOp::Add { unchecked: false }
| BinaryOp::Sub { unchecked: false }
| BinaryOp::Mul { unchecked: false }
| BinaryOp::Div
| BinaryOp::Mod => {
| BinaryOp::Mul { unchecked: false } => {
// Some binary math can overflow or underflow, but this is only the case
// for unsigned types (here we assume the type of binary.lhs is the same)
dfg.type_of_value(binary.rhs).is_unsigned()
Expand Down
Loading