Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6331a91
Add some unchecked math operations
asterite Jan 9, 2025
3bb465a
Use unchecked add in `make_offset`
asterite Jan 9, 2025
41ee955
Use correct names when printing
asterite Jan 9, 2025
63af351
Parse unchecked ops
asterite Jan 9, 2025
a308767
Induction variable now uses unchecked add
asterite Jan 9, 2025
199a980
A few more cases where we need to check for unchecked ops
asterite Jan 9, 2025
f279e3d
A few more
asterite Jan 9, 2025
a2beb78
Use a boolean instead of another variant
asterite Jan 9, 2025
84de854
increment and decrement slice length
asterite Jan 9, 2025
20b323e
Merge branch 'master' into ab/unchecked-math
TomAFrench Jan 10, 2025
d230098
Merge branch 'master' into ab/unchecked-math
TomAFrench Jan 10, 2025
7dff466
Handle some TODOs
asterite Jan 10, 2025
626f0a2
Merge branch 'ab/unchecked-math' of github.com:noir-lang/noir into ab…
asterite Jan 10, 2025
dfad378
chore: address some todos
TomAFrench Jan 10, 2025
7840ced
Handle more TODOs
asterite Jan 10, 2025
c10520c
Merge branch 'ab/unchecked-math' of github.com:noir-lang/noir into ab…
asterite Jan 10, 2025
0df89c0
Handle one more TODO
asterite Jan 10, 2025
6f9d1c7
Fix tests
asterite Jan 10, 2025
f34fb51
Handle the final TODO
asterite Jan 10, 2025
e0ff78f
Don't emit unchecked operations for Fields, or when multiplying bools
asterite Jan 10, 2025
bacccc2
Add missing cast in remove_bit_shifts
asterite Jan 10, 2025
83f1fd8
Extract `self.check_binary_instruction`
asterite Jan 10, 2025
820c16c
Check binary instruction before simplification
asterite Jan 10, 2025
4683b0d
clippy
asterite Jan 10, 2025
cf1a9e7
Do cast in both cases
asterite Jan 10, 2025
ef30d7c
Only check and simplify binary instructions in `Binary::simplify`
asterite Jan 10, 2025
63cfd12
Fix comptime to_le_bits returning `[u8; N]` instead of `[u1; N]`
asterite Jan 10, 2025
9b42fd8
Allow casting u1 in comptime
asterite Jan 10, 2025
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
14 changes: 9 additions & 5 deletions compiler/noirc_evaluator/src/acir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1949,9 +1949,9 @@ impl<'a> Context<'a> {
let bit_count = binary_type.bit_size::<FieldElement>();
let num_type = binary_type.to_numeric_type();
let result = match binary.operator {
BinaryOp::Add => self.acir_context.add_var(lhs, rhs),
BinaryOp::Sub => self.acir_context.sub_var(lhs, rhs),
BinaryOp::Mul => self.acir_context.mul_var(lhs, rhs),
BinaryOp::Add { .. } => self.acir_context.add_var(lhs, rhs),
BinaryOp::Sub { .. } => self.acir_context.sub_var(lhs, rhs),
BinaryOp::Mul { .. } => self.acir_context.mul_var(lhs, rhs),
BinaryOp::Div => self.acir_context.div_var(
lhs,
rhs,
Expand Down Expand Up @@ -2070,7 +2070,7 @@ impl<'a> Context<'a> {
Value::Instruction { instruction, .. } => {
if matches!(
&dfg[*instruction],
Instruction::Binary(Binary { operator: BinaryOp::Sub, .. })
Instruction::Binary(Binary { operator: BinaryOp::Sub { .. }, .. })
) {
// Subtractions must first have the integer modulus added before truncation can be
// applied. This is done in order to prevent underflow.
Expand Down Expand Up @@ -3159,7 +3159,11 @@ mod test {
let func_with_nested_call_v1 = builder.add_parameter(Type::field());

let two = builder.field_constant(2u128);
let v0_plus_two = builder.insert_binary(func_with_nested_call_v0, BinaryOp::Add, two);
let v0_plus_two = builder.insert_binary(
func_with_nested_call_v0,
BinaryOp::Add { unchecked: false },
two,
);

let foo_id = Id::test_new(2);
let foo_call = builder.import_function(foo_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1318,9 +1318,9 @@ impl<'block> BrilligBlock<'block> {
BrilligBinaryOp::Modulo
}
}
BinaryOp::Add => BrilligBinaryOp::Add,
BinaryOp::Sub => BrilligBinaryOp::Sub,
BinaryOp::Mul => BrilligBinaryOp::Mul,
BinaryOp::Add { .. } => BrilligBinaryOp::Add,
BinaryOp::Sub { .. } => BrilligBinaryOp::Sub,
BinaryOp::Mul { .. } => BrilligBinaryOp::Mul,
BinaryOp::Eq => BrilligBinaryOp::Equals,
BinaryOp::Lt => {
if is_signed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,14 @@ mod test {
builder.switch_to_block(b2);

let twenty_seven = builder.field_constant(27u128);
let v7 = builder.insert_binary(v0, BinaryOp::Add, twenty_seven);
let v7 = builder.insert_binary(v0, BinaryOp::Add { unchecked: false }, twenty_seven);
builder.insert_store(v3, v7);

builder.terminate_with_jmp(b3, vec![]);

builder.switch_to_block(b1);

let v6 = builder.insert_binary(v1, BinaryOp::Add, twenty_seven);
let v6 = builder.insert_binary(v1, BinaryOp::Add { unchecked: false }, twenty_seven);
builder.insert_store(v3, v6);

builder.terminate_with_jmp(b3, vec![]);
Expand Down Expand Up @@ -501,7 +501,7 @@ mod test {

builder.switch_to_block(b2);

let v6 = builder.insert_binary(v4, BinaryOp::Mul, v4);
let v6 = builder.insert_binary(v4, BinaryOp::Mul { unchecked: false }, v4);

builder.terminate_with_jmp(b4, vec![v0]);

Expand All @@ -526,7 +526,7 @@ mod test {

let v12 = builder.insert_load(v3, Type::field());

let v13 = builder.insert_binary(v12, BinaryOp::Add, v6);
let v13 = builder.insert_binary(v12, BinaryOp::Add { unchecked: false }, v6);

builder.insert_store(v3, v13);

Expand All @@ -535,13 +535,13 @@ mod test {
builder.switch_to_block(b8);

let one = builder.field_constant(1u128);
let v15 = builder.insert_binary(v7, BinaryOp::Add, one);
let v15 = builder.insert_binary(v7, BinaryOp::Add { unchecked: false }, one);

builder.terminate_with_jmp(b4, vec![v15]);

builder.switch_to_block(b6);

let v16 = builder.insert_binary(v4, BinaryOp::Add, one);
let v16 = builder.insert_binary(v4, BinaryOp::Add { unchecked: false }, one);

builder.terminate_with_jmp(b1, vec![v16]);

Expand Down
8 changes: 0 additions & 8 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,6 @@ impl FunctionBuilder {
operator: BinaryOp,
rhs: ValueId,
) -> ValueId {
let lhs_type = self.type_of_value(lhs);
let rhs_type = self.type_of_value(rhs);
if operator != BinaryOp::Shl && operator != BinaryOp::Shr {
assert_eq!(
lhs_type, rhs_type,
"ICE - Binary instruction operands must have the same type"
);
}
let instruction = Instruction::Binary(Binary { lhs, rhs, operator });
self.insert_instruction(instruction, None).first()
}
Expand Down
41 changes: 40 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use super::{
call_stack::{CallStack, CallStackHelper, CallStackId},
function::{FunctionId, RuntimeType},
instruction::{
Instruction, InstructionId, InstructionResultType, Intrinsic, TerminatorInstruction,
Binary, BinaryOp, Instruction, InstructionId, InstructionResultType, Intrinsic,
TerminatorInstruction,
},
map::DenseMap,
types::{NumericType, Type},
Expand Down Expand Up @@ -174,6 +175,44 @@ impl DataFlowGraph {
instruction_data: Instruction,
ctrl_typevars: Option<Vec<Type>>,
) -> InstructionId {
let instruction_data = if let Instruction::Binary(binary) = instruction_data {
let lhs = binary.lhs;
let rhs = binary.rhs;
let operator = binary.operator;
let lhs_type = self.type_of_value(lhs);
let rhs_type = self.type_of_value(rhs);
if operator != BinaryOp::Shl && operator != BinaryOp::Shr {
assert_eq!(
lhs_type, rhs_type,
"ICE - Binary instruction operands must have the same type"
);
}

let operator = if lhs_type.is_field() {
// Unchecked operations between fields or bools don't make sense, so we convert those to non-unchecked
// to reduce noise and confusion in the generated SSA.
match operator {
BinaryOp::Add { unchecked: true } => BinaryOp::Add { unchecked: false },
BinaryOp::Sub { unchecked: true } => BinaryOp::Sub { unchecked: false },
BinaryOp::Mul { unchecked: true } => BinaryOp::Mul { unchecked: false },
_ => operator,
}
} else if lhs_type.is_bool() {
// Unchecked mul between bools doesn't make sense, so we convert that to non-unchecked
if let BinaryOp::Mul { unchecked: true } = operator {
BinaryOp::Mul { unchecked: false }
} else {
operator
}
} else {
operator
};

Instruction::Binary(Binary { operator, ..binary })
} else {
instruction_data
};

let id = self.instructions.insert(instruction_data);
self.make_instruction_results(id, ctrl_typevars);
id
Expand Down
24 changes: 16 additions & 8 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,15 @@ impl Instruction {

// Some binary math can overflow or underflow
Binary(binary) => match binary.operator {
BinaryOp::Add | BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
true
}
BinaryOp::Eq
BinaryOp::Add { unchecked: false }
| BinaryOp::Sub { unchecked: false }
| BinaryOp::Mul { unchecked: false }
| BinaryOp::Div
| BinaryOp::Mod => true,
BinaryOp::Add { unchecked: true }
| BinaryOp::Sub { unchecked: true }
| BinaryOp::Mul { unchecked: true }
| BinaryOp::Eq
| BinaryOp::Lt
| BinaryOp::And
| BinaryOp::Or
Expand Down Expand Up @@ -566,16 +571,19 @@ impl Instruction {
match self {
Instruction::Binary(binary) => {
match binary.operator {
BinaryOp::Add
| BinaryOp::Sub
| BinaryOp::Mul
BinaryOp::Add { unchecked: false }
| BinaryOp::Sub { unchecked: false }
| BinaryOp::Mul { unchecked: false }
| BinaryOp::Div
| BinaryOp::Mod => {
// 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()
}
BinaryOp::Eq
BinaryOp::Add { unchecked: true }
| BinaryOp::Sub { unchecked: true }
| BinaryOp::Mul { unchecked: true }
| BinaryOp::Eq
| BinaryOp::Lt
| BinaryOp::And
| BinaryOp::Or
Expand Down
53 changes: 29 additions & 24 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use super::{
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
pub(crate) enum BinaryOp {
/// Addition of lhs + rhs.
Add,
Add { unchecked: bool },
/// Subtraction of lhs - rhs.
Sub,
Sub { unchecked: bool },
/// Multiplication of lhs * rhs.
Mul,
Mul { unchecked: bool },
/// Division of lhs / rhs.
Div,
/// Modulus of lhs % rhs.
Expand Down Expand Up @@ -48,9 +48,12 @@ pub(crate) enum BinaryOp {
impl std::fmt::Display for BinaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BinaryOp::Add => write!(f, "add"),
BinaryOp::Sub => write!(f, "sub"),
BinaryOp::Mul => write!(f, "mul"),
BinaryOp::Add { unchecked: false } => write!(f, "add"),
BinaryOp::Add { unchecked: true } => write!(f, "unchecked_add"),
BinaryOp::Sub { unchecked: false } => write!(f, "sub"),
BinaryOp::Sub { unchecked: true } => write!(f, "unchecked_sub"),
BinaryOp::Mul { unchecked: false } => write!(f, "mul"),
BinaryOp::Mul { unchecked: true } => write!(f, "unchecked_mul"),
BinaryOp::Div => write!(f, "div"),
BinaryOp::Eq => write!(f, "eq"),
BinaryOp::Mod => write!(f, "mod"),
Expand Down Expand Up @@ -107,20 +110,20 @@ impl Binary {
let rhs_is_one = rhs.map_or(false, |rhs| rhs.is_one());

match self.operator {
BinaryOp::Add => {
BinaryOp::Add { .. } => {
if lhs_is_zero {
return SimplifyResult::SimplifiedTo(self.rhs);
}
if rhs_is_zero {
return SimplifyResult::SimplifiedTo(self.lhs);
}
}
BinaryOp::Sub => {
BinaryOp::Sub { .. } => {
if rhs_is_zero {
return SimplifyResult::SimplifiedTo(self.lhs);
}
}
BinaryOp::Mul => {
BinaryOp::Mul { .. } => {
if lhs_is_one {
return SimplifyResult::SimplifiedTo(self.rhs);
}
Expand All @@ -141,7 +144,7 @@ impl Binary {
if let Instruction::Binary(Binary { lhs, rhs, operator }) =
dfg[*instruction]
{
if operator == BinaryOp::Mul
if matches!(operator, BinaryOp::Mul { .. })
&& (dfg.resolve(self.lhs) == dfg.resolve(lhs)
|| dfg.resolve(self.lhs) == dfg.resolve(rhs))
{
Expand All @@ -156,7 +159,7 @@ impl Binary {
if let Instruction::Binary(Binary { lhs, rhs, operator }) =
dfg[*instruction]
{
if operator == BinaryOp::Mul
if matches!(operator, BinaryOp::Mul { .. })
&& (dfg.resolve(self.rhs) == dfg.resolve(lhs)
|| dfg.resolve(self.rhs) == dfg.resolve(rhs))
{
Expand Down Expand Up @@ -247,7 +250,9 @@ impl Binary {
}
if operand_type == NumericType::bool() {
// Boolean AND is equivalent to multiplication, which is a cheaper operation.
let instruction = Instruction::binary(BinaryOp::Mul, self.lhs, self.rhs);
// (mul unchecked because these are bools so it doesn't matter really)
let instruction =
Instruction::binary(BinaryOp::Mul { unchecked: true }, self.lhs, self.rhs);
return SimplifyResult::SimplifiedToInstruction(instruction);
}
if operand_type.is_unsigned() {
Expand Down Expand Up @@ -331,22 +336,22 @@ impl Binary {
let max_rhs_bits = dfg.get_value_max_num_bits(self.rhs);

let msg = match self.operator {
BinaryOp::Add => {
BinaryOp::Add { unchecked: false } => {
if std::cmp::max(max_lhs_bits, max_rhs_bits) < bit_size {
// `lhs` and `rhs` have both been casted up from smaller types and so cannot overflow.
return None;
}
"attempt to add with overflow"
}
BinaryOp::Sub => {
BinaryOp::Sub { unchecked: false } => {
if dfg.is_constant(self.lhs) && max_lhs_bits > max_rhs_bits {
// `lhs` is a fixed constant and `rhs` is restricted such that `lhs - rhs > 0`
// Note strict inequality as `rhs > lhs` while `max_lhs_bits == max_rhs_bits` is possible.
return None;
}
"attempt to subtract with overflow"
}
BinaryOp::Mul => {
BinaryOp::Mul { unchecked: false } => {
if bit_size == 1
|| max_lhs_bits + max_rhs_bits <= bit_size
|| max_lhs_bits == 1
Expand Down Expand Up @@ -470,9 +475,9 @@ fn truncate(int: u128, bit_size: u32) -> u128 {
impl BinaryOp {
fn get_field_function(self) -> Option<fn(FieldElement, FieldElement) -> FieldElement> {
match self {
BinaryOp::Add => Some(std::ops::Add::add),
BinaryOp::Sub => Some(std::ops::Sub::sub),
BinaryOp::Mul => Some(std::ops::Mul::mul),
BinaryOp::Add { .. } => Some(std::ops::Add::add),
BinaryOp::Sub { .. } => Some(std::ops::Sub::sub),
BinaryOp::Mul { .. } => Some(std::ops::Mul::mul),
BinaryOp::Div => Some(std::ops::Div::div),
BinaryOp::Eq => Some(|x, y| (x == y).into()),
BinaryOp::Lt => Some(|x, y| (x < y).into()),
Expand All @@ -488,9 +493,9 @@ impl BinaryOp {

fn get_u128_function(self) -> fn(u128, u128) -> Option<u128> {
match self {
BinaryOp::Add => u128::checked_add,
BinaryOp::Sub => u128::checked_sub,
BinaryOp::Mul => u128::checked_mul,
BinaryOp::Add { .. } => u128::checked_add,
BinaryOp::Sub { .. } => u128::checked_sub,
BinaryOp::Mul { .. } => u128::checked_mul,
BinaryOp::Div => u128::checked_div,
BinaryOp::Mod => u128::checked_rem,
BinaryOp::And => |x, y| Some(x & y),
Expand All @@ -505,9 +510,9 @@ impl BinaryOp {

fn get_i128_function(self) -> fn(i128, i128) -> Option<i128> {
match self {
BinaryOp::Add => i128::checked_add,
BinaryOp::Sub => i128::checked_sub,
BinaryOp::Mul => i128::checked_mul,
BinaryOp::Add { .. } => i128::checked_add,
BinaryOp::Sub { .. } => i128::checked_sub,
BinaryOp::Mul { .. } => i128::checked_mul,
BinaryOp::Div => i128::checked_div,
BinaryOp::Mod => i128::checked_rem,
BinaryOp::And => |x, y| Some(x & y),
Expand Down
Loading