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: flatten nested if-else statements with equivalent conditions #6875

Merged
merged 4 commits into from
Dec 19, 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
51 changes: 47 additions & 4 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
// These can fail.
Constrain(..) | RangeCheck { .. } => true,

// This should never be side-effectful

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

View workflow job for this annotation

GitHub Actions / Code

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

// Some binary math can overflow or underflow
Expand Down Expand Up @@ -958,9 +958,11 @@
}
}
Instruction::IfElse { then_condition, then_value, else_condition, else_value } => {
let then_condition = dfg.resolve(*then_condition);
let else_condition = dfg.resolve(*else_condition);
let typ = dfg.type_of_value(*then_value);

if let Some(constant) = dfg.get_numeric_constant(*then_condition) {
if let Some(constant) = dfg.get_numeric_constant(then_condition) {
if constant.is_one() {
return SimplifiedTo(*then_value);
} else if constant.is_zero() {
Expand All @@ -974,10 +976,51 @@
return SimplifiedTo(then_value);
}

if matches!(&typ, Type::Numeric(_)) {
let then_condition = *then_condition;
let else_condition = *else_condition;
if let Value::Instruction { instruction, .. } = &dfg[then_value] {
if let Instruction::IfElse {
then_condition: inner_then_condition,
then_value: inner_then_value,
else_condition: inner_else_condition,
..
} = dfg[*instruction]
{
if then_condition == inner_then_condition {
let instruction = Instruction::IfElse {
then_condition,
then_value: inner_then_value,
else_condition: inner_else_condition,
else_value,
};
return SimplifiedToInstruction(instruction);
}
// TODO: We could check to see if `then_condition == inner_else_condition`
// but we run into issues with duplicate NOT instructions having distinct ValueIds.
}
};

if let Value::Instruction { instruction, .. } = &dfg[else_value] {
if let Instruction::IfElse {
then_condition: inner_then_condition,
else_condition: inner_else_condition,
else_value: inner_else_value,
..
} = dfg[*instruction]
{
if then_condition == inner_then_condition {
let instruction = Instruction::IfElse {
then_condition,
then_value,
else_condition: inner_else_condition,
else_value: inner_else_value,
};
return SimplifiedToInstruction(instruction);
}
// TODO: We could check to see if `then_condition == inner_else_condition`
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
// but we run into issues with duplicate NOT instructions having distinct ValueIds.
}
};

if matches!(&typ, Type::Numeric(_)) {
let result = ValueMerger::merge_numeric_values(
dfg,
block,
Expand Down
84 changes: 84 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,4 +1459,88 @@ mod test {
let merged_ssa = Ssa::from_str(src).unwrap();
let _ = merged_ssa.flatten_cfg();
}

#[test]
fn eliminates_unnecessary_if_else_instructions_on_numeric_types() {
let src = "
acir(inline) fn main f0 {
b0(v0: bool):
v1 = allocate -> &mut [Field; 1]
store Field 0 at v1
jmpif v0 then: b1, else: b2
b1():
store Field 1 at v1
store Field 2 at v1
jmp b2()
b2():
v3 = load v1 -> Field
return v3
}";

let ssa = Ssa::from_str(src).unwrap();

let ssa = ssa.flatten_cfg().mem2reg().fold_constants();

let expected = "
acir(inline) fn main f0 {
b0(v0: u1):
v1 = allocate -> &mut [Field; 1]
enable_side_effects v0
v2 = not v0
v3 = cast v0 as Field
v4 = cast v2 as Field
v6 = mul v3, Field 2
v7 = mul v4, v3
v8 = add v6, v7
enable_side_effects u1 1
return v8
}
";

assert_normalized_ssa_equals(ssa, expected);
}

#[test]
fn eliminates_unnecessary_if_else_instructions_on_array_types() {
let src = "
acir(inline) fn main f0 {
b0(v0: bool, v1: bool):
v2 = make_array [Field 0] : [Field; 1]
v3 = allocate -> &mut [Field; 1]
store v2 at v3
jmpif v0 then: b1, else: b2
b1():
v4 = make_array [Field 1] : [Field; 1]
store v4 at v3
v5 = make_array [Field 2] : [Field; 1]
store v5 at v3
jmp b2()
b2():
v24 = load v3 -> Field
return v24
}";

let ssa = Ssa::from_str(src).unwrap();

let ssa = ssa
.flatten_cfg()
.mem2reg()
.remove_if_else()
.fold_constants()
.dead_instruction_elimination();

let expected = "
acir(inline) fn main f0 {
b0(v0: u1, v1: u1):
enable_side_effects v0
v2 = cast v0 as Field
v4 = mul v2, Field 2
v5 = make_array [v4] : [Field; 1]
enable_side_effects u1 1
return v5
}
";

assert_normalized_ssa_equals(ssa, expected);
}
}
Loading