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: remove orphaned blocks from cfg to improve simplify_cfg pass. #6198

Merged
merged 4 commits into from
Oct 2, 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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ControlFlowGraph {

/// Clears out a given block's successors. This also removes the given block from
/// being a predecessor of any of its previous successors.
fn invalidate_block_successors(&mut self, basic_block_id: BasicBlockId) {
pub(crate) fn invalidate_block_successors(&mut self, basic_block_id: BasicBlockId) {
let node = self
.data
.get_mut(&basic_block_id)
Expand Down
14 changes: 11 additions & 3 deletions compiler/noirc_evaluator/src/ssa/opt/simplify_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ impl Function {
check_for_constant_jmpif(self, block, &mut cfg);

let mut predecessors = cfg.predecessors(block);

if predecessors.len() == 1 {
let predecessor =
predecessors.next().expect("Already checked length of predecessors");
Expand Down Expand Up @@ -99,14 +98,23 @@ fn check_for_constant_jmpif(
}) = function.dfg[block].terminator()
{
if let Some(constant) = function.dfg.get_numeric_constant(*condition) {
let destination =
if constant.is_zero() { *else_destination } else { *then_destination };
let (destination, unchosen_destination) = if constant.is_zero() {
(*else_destination, *then_destination)
} else {
(*then_destination, *else_destination)
};

let arguments = Vec::new();
let call_stack = call_stack.clone();
let jmp = TerminatorInstruction::Jmp { destination, arguments, call_stack };
function.dfg[block].set_terminator(jmp);
cfg.recompute_block(function, block);

// If `block` was the only predecessor to `unchosen_destination` then it's no long reachable through the CFG,
// we can then invalidate it successors as it's an invalid predecessor.
if cfg.predecessors(unchosen_destination).len() == 0 {
cfg.invalidate_block_successors(unchosen_destination);
}
}
}
}
Expand Down
Loading