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 value merge optimization #6409

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix optimization
jfecher committed Oct 30, 2024
commit ead6d9e4e540b74a96df345473ef76e3483dad94
34 changes: 19 additions & 15 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
@@ -41,9 +41,9 @@
Or,
/// Bitwise xor (^)
Xor,
/// Bitshift left (<<)

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

GitHub Actions / Code

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

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

GitHub Actions / Code

Unknown word (Bitshift)
Shr,
}

@@ -116,6 +116,12 @@
if rhs_is_zero {
return SimplifyResult::SimplifiedTo(self.lhs);
}

let lhs = dfg.resolve(self.lhs);
let rhs = dfg.resolve(self.rhs);
if let Some(value) = check_for_noop_value_merge(dfg, lhs, rhs) {
return SimplifyResult::SimplifiedTo(value);
}
}
BinaryOp::Sub => {
if rhs_is_zero {
@@ -141,12 +147,6 @@
// Squaring a boolean value is a noop.
return SimplifyResult::SimplifiedTo(lhs);
}

if let Some(value) = check_for_noop_value_merge(dfg, lhs, rhs) {
return SimplifyResult::SimplifiedTo(value);
} else {
return SimplifyResult::None;
}
}
BinaryOp::Div => {
if rhs_is_one {
@@ -310,10 +310,10 @@
/// Checks for `(c as _) * a + ((not c) as _) * a`
/// which is equivalent to `if c then a else a` = `a`
fn check_for_noop_value_merge(dfg: &DataFlowGraph, lhs: ValueId, rhs: ValueId) -> Option<ValueId> {
let (rhs_cond, rhs_value, rhs_is_not) = match source_instruction(dfg, rhs)? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Add }) => {
match source_instruction(dfg, *lhs)? {
Instruction::Cast(cond, _) => match source_instruction(dfg, *rhs) {
let (rhs_cond, rhs_value, rhs_is_not) = match dbg!(source_instruction(dfg, rhs))? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Mul }) => {
match dbg!(source_instruction(dfg, *lhs))? {
Instruction::Cast(cond, _) => match dbg!(source_instruction(dfg, *cond)) {
Some(Instruction::Not(cond)) => (*cond, *rhs, true),
_ => (*cond, *rhs, false),
},
@@ -323,14 +323,14 @@
_ => return None,
};

if !dfg.type_of_value(rhs_cond).is_bool() {
if !dbg!(dfg.type_of_value(rhs_cond)).is_bool() {
return None;
}

let (lhs_cond, lhs_value, lhs_is_not) = match source_instruction(dfg, lhs)? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Add }) => {
match source_instruction(dfg, *lhs)? {
Instruction::Cast(cond, _) => match source_instruction(dfg, *rhs) {
let (lhs_cond, lhs_value, lhs_is_not) = match dbg!(source_instruction(dfg, lhs))? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Mul }) => {
match dbg!(source_instruction(dfg, *lhs))? {
Instruction::Cast(cond, _) => match dbg!(source_instruction(dfg, *cond)) {
Some(Instruction::Not(cond)) if !rhs_is_not => (*cond, *rhs, true),
_ => (*cond, *rhs, false),
},
@@ -340,6 +340,10 @@
_ => return None,
};

eprintln!("{lhs_cond} == {rhs_cond}");
eprintln!(" && {lhs_value} == {rhs_value}");
eprintln!(" && {lhs_is_not} ^ {rhs_is_not}");

if lhs_cond == rhs_cond && lhs_value == rhs_value && (lhs_is_not ^ rhs_is_not) {
Some(lhs_value)
} else {