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: add (x | 1) optimization for booleans #6795

Merged
merged 8 commits into from
Dec 12, 2024
Merged
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
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 @@ -39,9 +39,9 @@
Or,
/// Bitwise xor (^)
Xor,
/// Bitshift left (<<)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shl,
/// Bitshift right (>>)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shr,
}

Expand Down Expand Up @@ -256,6 +256,10 @@
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
Loading