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(ssa): Hoist add and mul binary ops using known induction variables #6910

Merged
merged 3 commits into from
Jan 2, 2025
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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
value::{Value, ValueId},
};

mod binary;
pub(crate) mod binary;
mod call;
mod cast;
mod constrain;
Expand Down Expand Up @@ -407,7 +407,7 @@
// These can fail.
Constrain(..) | RangeCheck { .. } => true,

// This should never be side-effectful

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (effectful)
MakeArray { .. } | Noop => false,

// Some binary math can overflow or underflow
Expand Down
2 changes: 1 addition & 1 deletion 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 @@ -294,7 +294,7 @@
}

/// Evaluate a binary operation with constant arguments.
fn eval_constant_binary_op(
pub(crate) fn eval_constant_binary_op(
lhs: FieldElement,
rhs: FieldElement,
operator: BinaryOp,
Expand Down
28 changes: 27 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::ssa::{
basic_block::BasicBlockId,
function::Function,
function_inserter::FunctionInserter,
instruction::{Instruction, InstructionId},
instruction::{binary::eval_constant_binary_op, BinaryOp, Instruction, InstructionId},
types::Type,
value::ValueId,
},
Expand Down Expand Up @@ -207,6 +207,7 @@ impl<'f> LoopInvariantContext<'f> {

let can_be_deduplicated = instruction.can_be_deduplicated(self.inserter.function, false)
|| matches!(instruction, Instruction::MakeArray { .. })
|| matches!(instruction, Instruction::Binary(_))
|| self.can_be_deduplicated_from_upper_bound(&instruction);

is_loop_invariant && can_be_deduplicated
Expand All @@ -231,6 +232,31 @@ impl<'f> LoopInvariantContext<'f> {
false
}
}
Instruction::Binary(binary) => {
if !matches!(binary.operator, BinaryOp::Add | BinaryOp::Mul) {
return false;
}

let operand_type =
self.inserter.function.dfg.type_of_value(binary.lhs).unwrap_numeric();

let lhs_const =
self.inserter.function.dfg.get_numeric_constant_with_type(binary.lhs);
let rhs_const =
self.inserter.function.dfg.get_numeric_constant_with_type(binary.rhs);
let (lhs, rhs) = match (
lhs_const,
rhs_const,
self.outer_induction_variables.get(&binary.lhs),
self.outer_induction_variables.get(&binary.rhs),
) {
(Some((lhs, _)), None, None, Some(upper_bound)) => (lhs, *upper_bound),
(None, Some((rhs, _)), Some(upper_bound), None) => (*upper_bound, rhs),
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
_ => return false,
};

eval_constant_binary_op(lhs, rhs, binary.operator, operand_type).is_some()
}
_ => false,
}
}
Expand Down
Loading