Skip to content

Commit

Permalink
Merge 2003e38 into fb1a8ca
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Nov 5, 2024
2 parents fb1a8ca + 2003e38 commit 9032919
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a> ValueMerger<'a> {
dfg: &mut DataFlowGraph,
block: BasicBlockId,
then_condition: ValueId,
else_condition: ValueId,
_else_condition: ValueId,
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
Expand All @@ -114,31 +114,38 @@ impl<'a> ValueMerger<'a> {
// We must cast the bool conditions to the actual numeric type used by each value.
let then_condition = dfg
.insert_instruction_and_results(
Instruction::Cast(then_condition, then_type),
block,
None,
call_stack.clone(),
)
.first();
let else_condition = dfg
.insert_instruction_and_results(
Instruction::Cast(else_condition, else_type),
Instruction::Cast(then_condition, Type::field()),
block,
None,
call_stack.clone(),
)
.first();

let mul = Instruction::binary(BinaryOp::Mul, then_condition, then_value);
let then_value =
dfg.insert_instruction_and_results(mul, block, None, call_stack.clone()).first();
let then_field = Instruction::Cast(then_value, Type::field());
let then_field_value =
dfg.insert_instruction_and_results(then_field, block, None, call_stack.clone()).first();

let else_field = Instruction::Cast(else_value, Type::field());
let else_field_value =
dfg.insert_instruction_and_results(else_field, block, None, call_stack.clone()).first();

let diff = Instruction::binary(BinaryOp::Sub, then_field_value, else_field_value);
let diff_value =
dfg.insert_instruction_and_results(diff, block, None, call_stack.clone()).first();

let mul = Instruction::binary(BinaryOp::Mul, else_condition, else_value);
let else_value =
dfg.insert_instruction_and_results(mul, block, None, call_stack.clone()).first();
let conditional_diff = Instruction::binary(BinaryOp::Mul, then_condition, diff_value);
let conditional_diff_value = dfg
.insert_instruction_and_results(conditional_diff, block, None, call_stack.clone())
.first();

let merged_field =
Instruction::binary(BinaryOp::Add, else_field_value, conditional_diff_value);
let merged_field_value = dfg
.insert_instruction_and_results(merged_field, block, None, call_stack.clone())
.first();

let add = Instruction::binary(BinaryOp::Add, then_value, else_value);
dfg.insert_instruction_and_results(add, block, None, call_stack).first()
let merged = Instruction::Cast(merged_field_value, then_type);
dfg.insert_instruction_and_results(merged, block, None, call_stack).first()
}

/// Given an if expression that returns an array: `if c { array1 } else { array2 }`,
Expand Down

0 comments on commit 9032919

Please sign in to comment.