-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add unreachable propagation mir optimization pass #66329
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
bors
merged 1 commit into
rust-lang:master
from
ktrianta:mir-opt-unreachable-propagation
Jan 15, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//! A pass that propagates the unreachable terminator of a block to its predecessors | ||
//! when all of their successors are unreachable. This is achieved through a | ||
//! post-order traversal of the blocks. | ||
|
||
use crate::transform::simplify; | ||
use crate::transform::{MirPass, MirSource}; | ||
use rustc::mir::*; | ||
use rustc::ty::TyCtxt; | ||
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | ||
use std::borrow::Cow; | ||
|
||
pub struct UnreachablePropagation; | ||
|
||
impl MirPass<'_> for UnreachablePropagation { | ||
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) { | ||
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 { | ||
// Enable only under -Zmir-opt-level=3 as in some cases (check the deeply-nested-opt | ||
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code. | ||
return; | ||
} | ||
|
||
let mut unreachable_blocks = FxHashSet::default(); | ||
let mut replacements = FxHashMap::default(); | ||
|
||
for (bb, bb_data) in traversal::postorder(body) { | ||
let terminator = bb_data.terminator(); | ||
// HACK: If the block contains any asm statement it is not regarded as unreachable. | ||
// This is a temporary solution that handles possibly diverging asm statements. | ||
// Accompanying testcases: mir-opt/unreachable_asm.rs and mir-opt/unreachable_asm_2.rs | ||
let asm_stmt_in_block = || { | ||
bb_data.statements.iter().any(|stmt: &Statement<'_>| match stmt.kind { | ||
StatementKind::InlineAsm(..) => true, | ||
_ => false, | ||
}) | ||
}; | ||
|
||
if terminator.kind == TerminatorKind::Unreachable && !asm_stmt_in_block() { | ||
unreachable_blocks.insert(bb); | ||
} else { | ||
let is_unreachable = |succ: BasicBlock| unreachable_blocks.contains(&succ); | ||
let terminator_kind_opt = remove_successors(&terminator.kind, is_unreachable); | ||
|
||
ktrianta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let Some(terminator_kind) = terminator_kind_opt { | ||
if terminator_kind == TerminatorKind::Unreachable && !asm_stmt_in_block() { | ||
unreachable_blocks.insert(bb); | ||
} | ||
replacements.insert(bb, terminator_kind); | ||
} | ||
} | ||
} | ||
|
||
let replaced = !replacements.is_empty(); | ||
for (bb, terminator_kind) in replacements { | ||
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind; | ||
} | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if replaced { | ||
simplify::remove_dead_blocks(body); | ||
} | ||
} | ||
} | ||
|
||
fn remove_successors<F>( | ||
terminator_kind: &TerminatorKind<'tcx>, | ||
predicate: F, | ||
) -> Option<TerminatorKind<'tcx>> | ||
where | ||
F: Fn(BasicBlock) -> bool, | ||
{ | ||
match *terminator_kind { | ||
TerminatorKind::Goto { target } if predicate(target) => Some(TerminatorKind::Unreachable), | ||
TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => { | ||
let original_targets_len = targets.len(); | ||
let (otherwise, targets) = targets.split_last().unwrap(); | ||
let retained = values | ||
.iter() | ||
.zip(targets.iter()) | ||
.filter(|(_, &t)| !predicate(t)) | ||
.collect::<Vec<_>>(); | ||
let mut values = retained.iter().map(|&(v, _)| *v).collect::<Vec<_>>(); | ||
let mut targets = retained.iter().map(|&(_, d)| *d).collect::<Vec<_>>(); | ||
|
||
if !predicate(*otherwise) { | ||
targets.push(*otherwise); | ||
} else { | ||
values.pop(); | ||
} | ||
|
||
let retained_targets_len = targets.len(); | ||
|
||
if targets.is_empty() { | ||
Some(TerminatorKind::Unreachable) | ||
} else if targets.len() == 1 { | ||
Some(TerminatorKind::Goto { target: targets[0] }) | ||
} else if original_targets_len != retained_targets_len { | ||
Some(TerminatorKind::SwitchInt { | ||
discr: discr.clone(), | ||
switch_ty, | ||
values: Cow::from(values), | ||
targets, | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.