Skip to content

Commit

Permalink
feat: add (x | 1) optimization for booleans (#6795)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Dec 12, 2024
1 parent 919149d commit feec2a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ impl Binary {
if rhs_is_zero {
return SimplifyResult::SimplifiedTo(self.lhs);
}
if operand_type == NumericType::bool() && (lhs_is_one || rhs_is_one) {
let one = dfg.make_constant(FieldElement::one(), operand_type);
return SimplifyResult::SimplifiedTo(one);
}
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) {
return SimplifyResult::SimplifiedTo(self.lhs);
}
Expand Down
19 changes: 8 additions & 11 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,20 +480,14 @@ fn simplify_slice_push_back(
}

fn simplify_slice_pop_back(
element_type: Type,
slice_type: Type,
arguments: &[ValueId],
dfg: &mut DataFlowGraph,
block: BasicBlockId,
call_stack: CallStack,
) -> SimplifyResult {
let element_types = match element_type.clone() {
Type::Slice(element_types) | Type::Array(element_types, _) => element_types,
_ => {
unreachable!("ICE: Expected slice or array, but got {element_type}");
}
};

let element_count = element_type.element_size();
let element_types = slice_type.element_types();
let element_count = element_types.len();
let mut results = VecDeque::with_capacity(element_count + 1);

let new_slice_length = update_slice_length(arguments[0], dfg, BinaryOp::Sub, block);
Expand All @@ -507,14 +501,17 @@ fn simplify_slice_pop_back(
flattened_len = update_slice_length(flattened_len, dfg, BinaryOp::Sub, block);

// We must pop multiple elements in the case of a slice of tuples
for _ in 0..element_count {
// Iterating through element types in reverse here since we're popping from the end
for element_type in element_types.iter().rev() {
let get_last_elem_instr =
Instruction::ArrayGet { array: arguments[1], index: flattened_len };

let element_type = Some(vec![element_type.clone()]);
let get_last_elem = dfg
.insert_instruction_and_results(
get_last_elem_instr,
block,
Some(element_types.to_vec()),
element_type,
call_stack.clone(),
)
.first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ pub(super) fn simplify_msm(
var_scalars.push(zero);
let result_x = dfg.make_constant(result_x, NumericType::NativeField);
let result_y = dfg.make_constant(result_y, NumericType::NativeField);

// Pushing a bool here is intentional, multi_scalar_mul takes two arguments:
// `points: [(Field, Field, bool); N]` and `scalars: [(Field, Field); N]`.
let result_is_infinity = dfg.make_constant(result_is_infinity, NumericType::bool());

var_points.push(result_x);
var_points.push(result_y);
var_points.push(result_is_infinity);
Expand Down

0 comments on commit feec2a1

Please sign in to comment.