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: replace boolean ANDs with multiplication #1954

Merged
merged 13 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ impl DataFlowGraph {
SimplifiedToMultiple(simplification)
}
SimplifyResult::Remove => InstructionRemoved,
SimplifyResult::None => {
SimplifyResult::SimplifiedToInstruction(simplified_instruction) => {
let instruction = simplified_instruction.unwrap_or(instruction);
let id = self.make_instruction(instruction, ctrl_typevars);
self.blocks[block].insert_instruction(id);
if let Some(location) = location {
Expand Down
70 changes: 43 additions & 27 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ impl Instruction {
if let Instruction::Not(value) = &dfg[*instruction] {
SimplifiedTo(*value)
} else {
None
SimplifyResult::could_not_simplify()
}
}
_ => None,
_ => SimplifyResult::could_not_simplify(),
}
}
Instruction::Constrain(value) => {
Expand All @@ -292,7 +292,7 @@ impl Instruction {
return Remove;
}
}
None
SimplifyResult::could_not_simplify()
}
Instruction::ArrayGet { array, index } => {
let array = dfg.get_array_constant(*array);
Expand All @@ -304,7 +304,7 @@ impl Instruction {
return SimplifiedTo(array[index]);
}
}
None
SimplifyResult::could_not_simplify()
}
Instruction::ArraySet { array, index, value } => {
let array = dfg.get_array_constant(*array);
Expand All @@ -318,15 +318,15 @@ impl Instruction {
return SimplifiedTo(new_array);
}
}
None
SimplifyResult::could_not_simplify()
}
Instruction::Truncate { value, bit_size, .. } => {
if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) {
let integer_modulus = 2_u128.pow(*bit_size);
let truncated = numeric_constant.to_u128() % integer_modulus;
SimplifiedTo(dfg.make_constant(truncated.into(), typ))
} else {
None
SimplifyResult::could_not_simplify()
}
}
Instruction::Call { func, arguments } => simplify_call(*func, arguments, dfg),
Expand All @@ -338,11 +338,11 @@ impl Instruction {
return Remove;
}
}
None
SimplifyResult::could_not_simplify()
}
Instruction::Allocate { .. } | Instruction::Load { .. } | Instruction::Store { .. } => {
SimplifyResult::could_not_simplify()
}
Instruction::Allocate { .. } => None,
Instruction::Load { .. } => None,
Instruction::Store { .. } => None,
}
}
}
Expand Down Expand Up @@ -376,12 +376,12 @@ fn simplify_cast(value: ValueId, dst_typ: &Type, dfg: &mut DataFlowGraph) -> Sim
let truncated = FieldElement::from_be_bytes_reduce(&truncated.to_bytes_be());
SimplifiedTo(dfg.make_constant(truncated, dst_typ.clone()))
}
_ => None,
_ => SimplifyResult::could_not_simplify(),
}
} else if *dst_typ == dfg.type_of_value(value) {
SimplifiedTo(value)
} else {
None
SimplifyResult::could_not_simplify()
}
}

Expand All @@ -391,7 +391,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
use SimplifyResult::*;
let intrinsic = match &dfg[func] {
Value::Intrinsic(intrinsic) => *intrinsic,
_ => return None,
_ => return SimplifyResult::could_not_simplify(),
};

let constant_args: Option<Vec<_>> =
Expand All @@ -404,7 +404,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let limb_count = constant_args[1].to_u128() as u32;
SimplifiedTo(constant_to_radix(endian, field, 2, limb_count, dfg))
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::ToRadix(endian) => {
Expand All @@ -414,7 +414,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let limb_count = constant_args[2].to_u128() as u32;
SimplifiedTo(constant_to_radix(endian, field, radix, limb_count, dfg))
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::ArrayLen => {
Expand All @@ -430,7 +430,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
);
SimplifiedTo(slice_len)
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SlicePushBack => {
Expand All @@ -440,7 +440,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedTo(new_slice)
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SlicePushFront => {
Expand All @@ -450,7 +450,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedTo(new_slice)
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SlicePopBack => {
Expand All @@ -461,7 +461,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedToMultiple(vec![new_slice, elem])
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SlicePopFront => {
Expand All @@ -472,7 +472,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedToMultiple(vec![elem, new_slice])
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SliceInsert => {
Expand All @@ -485,7 +485,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedTo(new_slice)
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::SliceRemove => {
Expand All @@ -496,10 +496,12 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
let new_slice = dfg.make_array(slice, element_type);
SimplifiedToMultiple(vec![new_slice, removed_elem])
} else {
None
SimplifyResult::could_not_simplify()
}
}
Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => None,
Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => {
SimplifyResult::could_not_simplify()
}
}
}

Expand Down Expand Up @@ -673,7 +675,7 @@ impl Binary {
if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
return match self.eval_constants(dfg, lhs, rhs, operand_type) {
Some(value) => SimplifyResult::SimplifiedTo(value),
None => SimplifyResult::None,
None => SimplifyResult::could_not_simplify(),
};
}

Expand Down Expand Up @@ -737,6 +739,11 @@ impl Binary {
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
if operand_type == Type::bool() {
// Boolean AND is equivalent to multiplication, which is a cheaper operation.
let instruction = Instruction::binary(BinaryOp::Mul, self.lhs, self.rhs);
return SimplifyResult::SimplifiedToInstruction(Some(instruction));
}
}
BinaryOp::Or => {
if lhs_is_zero {
Expand All @@ -763,7 +770,7 @@ impl Binary {
}
}
}
SimplifyResult::None
SimplifyResult::could_not_simplify()
}

/// Evaluate the two constants with the operation specified by self.operator.
Expand Down Expand Up @@ -913,6 +920,15 @@ pub(crate) enum SimplifyResult {
/// Remove the instruction, it is unnecessary
Remove,

/// Instruction could not be simplified
None,
/// Instruction cannot be simplified to a known value.
///
/// The instruction may be able to be replaced with simpler but equivalent instruction.
/// If so it will be included here.
SimplifiedToInstruction(Option<Instruction>),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}

impl SimplifyResult {
fn could_not_simplify() -> Self {
SimplifyResult::SimplifiedToInstruction(None)
}
}