Skip to content

Commit

Permalink
Merge 6865065 into 03b58fa
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm authored Dec 21, 2024
2 parents 03b58fa + 6865065 commit 6929ac3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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 @@ use super::{
value::{Value, ValueId},
};

mod binary;
pub(crate) mod binary;
mod call;
mod cast;
mod constrain;
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 @@ -294,7 +294,7 @@ impl Binary {
}

/// 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),
_ => return false,
};

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

0 comments on commit 6929ac3

Please sign in to comment.