Skip to content

Commit 7ce8cce

Browse files
authored
fix(ssa refactor): Fix flatten_cfg for ifs with no else (#1671)
* Add check for if with no else * Reorder if
1 parent 301e244 commit 7ce8cce

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -441,20 +441,26 @@ impl<'f> Context<'f> {
441441
new_condition: ValueId,
442442
condition_value: FieldElement,
443443
) -> Branch {
444-
self.push_condition(jmpif_block, new_condition);
445-
self.insert_current_side_effects_enabled();
446-
let old_stores = std::mem::take(&mut self.store_values);
444+
if destination == self.branch_ends[&jmpif_block] {
445+
// If the branch destination is the same as the end of the branch, this must be the
446+
// 'else' case of an if with no else - so there is no else branch.
447+
Branch { condition: new_condition, last_block: jmpif_block, store_values: HashMap::new() }
448+
} else {
449+
self.push_condition(jmpif_block, new_condition);
450+
self.insert_current_side_effects_enabled();
451+
let old_stores = std::mem::take(&mut self.store_values);
447452

448-
// Remember the old condition value is now known to be true/false within this branch
449-
let known_value = self.function.dfg.make_constant(condition_value, Type::bool());
450-
self.values.insert(old_condition, known_value);
453+
// Remember the old condition value is now known to be true/false within this branch
454+
let known_value = self.function.dfg.make_constant(condition_value, Type::bool());
455+
self.values.insert(old_condition, known_value);
451456

452-
let final_block = self.inline_block(destination, &[]);
457+
let final_block = self.inline_block(destination, &[]);
453458

454-
self.conditions.pop();
455-
let stores_in_branch = std::mem::replace(&mut self.store_values, old_stores);
459+
self.conditions.pop();
460+
let stores_in_branch = std::mem::replace(&mut self.store_values, old_stores);
456461

457-
Branch { condition: new_condition, last_block: final_block, store_values: stores_in_branch }
462+
Branch { condition: new_condition, last_block: final_block, store_values: stores_in_branch }
463+
}
458464
}
459465

460466
/// Inline the ending block of a branch, the point where all blocks from a jmpif instruction

0 commit comments

Comments
 (0)