Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion acvm-repo/acir_field/src/generic_ark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub trait AcirField:

fn is_zero(&self) -> bool;
fn is_one(&self) -> bool;

fn pow(&self, exponent: &Self) -> Self;

/// Maximum number of bits needed to represent a field element
Expand Down
15 changes: 15 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ pub(super) fn simplify_binary(binary: &Binary, dfg: &mut DataFlowGraph) -> Simpl

let lhs_is_one = lhs_value.is_some_and(|lhs| lhs.is_one());
let rhs_is_one = rhs_value.is_some_and(|rhs| rhs.is_one());
let lhs_is_max = if lhs_type.is_unsigned() {
lhs_value.is_some_and(|lhs| lhs == lhs_type.max_value().unwrap())
} else {
false
};
let rhs_is_max = if rhs_type.is_unsigned() {
rhs_value.is_some_and(|rhs| rhs == rhs_type.max_value().unwrap())
} else {
false
};
Comment thread
kashbrti marked this conversation as resolved.
Outdated

match binary.operator {
BinaryOp::Add { .. } => {
Expand Down Expand Up @@ -254,6 +264,11 @@ pub(super) fn simplify_binary(binary: &Binary, dfg: &mut DataFlowGraph) -> Simpl
if lhs == rhs {
return SimplifyResult::SimplifiedTo(lhs);
}

if lhs_type.is_unsigned() && (lhs_is_max || rhs_is_max) {
let max = dfg.make_constant(lhs_type.max_value().unwrap(), lhs_type);
return SimplifyResult::SimplifiedTo(max);
}
}
BinaryOp::Xor => {
if lhs_is_zero {
Expand Down
16 changes: 16 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ impl NumericType {
pub(crate) fn is_unsigned(&self) -> bool {
matches!(self, NumericType::Unsigned { .. })
}

pub(crate) fn max_value(&self) -> Result<FieldElement, String> {
match self {
NumericType::Unsigned { bit_size } => {
if *bit_size == 128 {
Ok(FieldElement::from(u128::MAX))
} else {
Ok(FieldElement::from(2u128.pow(*bit_size) - 1))
}
Comment thread
TomAFrench marked this conversation as resolved.
}
NumericType::Signed { bit_size } => Ok(FieldElement::from(2u128.pow(*bit_size) - 1)),
Comment thread
kashbrti marked this conversation as resolved.
Outdated
Comment thread
kashbrti marked this conversation as resolved.
Outdated
NumericType::NativeField => {
Err("Cannot get max value for native field type".to_string())
}
}
}
Comment thread
TomAFrench marked this conversation as resolved.
}

/// All types representable in the IR.
Expand Down