-
Notifications
You must be signed in to change notification settings - Fork 13.3k
UnreachableProp: Preserve unreachable branches for multiple targets #99762
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,8 @@ pub struct UnreachablePropagation; | |
|
||
impl MirPass<'_> for UnreachablePropagation { | ||
fn is_enabled(&self, sess: &rustc_session::Session) -> bool { | ||
// Enable only under -Zmir-opt-level=4 as in some cases (check the deeply-nested-opt | ||
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code. | ||
sess.mir_opt_level() >= 4 | ||
// Enable only under -Zmir-opt-level=2 as this can make programs less debuggable. | ||
sess.mir_opt_level() >= 2 | ||
} | ||
|
||
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
|
@@ -38,7 +37,19 @@ impl MirPass<'_> for UnreachablePropagation { | |
} | ||
} | ||
|
||
// We do want do keep some unreachable blocks, but make them empty. | ||
for bb in unreachable_blocks { | ||
if !tcx.consider_optimizing(|| { | ||
format!("UnreachablePropagation {:?} ", body.source.def_id()) | ||
}) { | ||
break; | ||
} | ||
|
||
body.basic_blocks_mut()[bb].statements.clear(); | ||
} | ||
|
||
let replaced = !replacements.is_empty(); | ||
|
||
for (bb, terminator_kind) in replacements { | ||
if !tcx.consider_optimizing(|| { | ||
format!("UnreachablePropagation {:?} ", body.source.def_id()) | ||
|
@@ -57,42 +68,55 @@ impl MirPass<'_> for UnreachablePropagation { | |
|
||
fn remove_successors<'tcx, F>( | ||
terminator_kind: &TerminatorKind<'tcx>, | ||
predicate: F, | ||
is_unreachable: F, | ||
) -> Option<TerminatorKind<'tcx>> | ||
where | ||
F: Fn(BasicBlock) -> bool, | ||
{ | ||
let terminator = match *terminator_kind { | ||
TerminatorKind::Goto { target } if predicate(target) => TerminatorKind::Unreachable, | ||
TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => { | ||
let terminator = match terminator_kind { | ||
// This will unconditionally run into an unreachable and is therefore unreachable as well. | ||
TerminatorKind::Goto { target } if is_unreachable(*target) => TerminatorKind::Unreachable, | ||
TerminatorKind::SwitchInt { targets, discr, switch_ty } => { | ||
let otherwise = targets.otherwise(); | ||
|
||
let original_targets_len = targets.iter().len() + 1; | ||
let (mut values, mut targets): (Vec<_>, Vec<_>) = | ||
targets.iter().filter(|(_, bb)| !predicate(*bb)).unzip(); | ||
// If all targets are unreachable, we can be unreachable as well. | ||
if targets.all_targets().iter().all(|bb| is_unreachable(*bb)) { | ||
TerminatorKind::Unreachable | ||
} else if is_unreachable(otherwise) { | ||
// If there are multiple targets, don't delete unreachable branches (like an unreachable otherwise) | ||
// unless otherwise is unrachable, in which case deleting a normal branch causes it to be merged with | ||
// the otherwise, keeping its unreachable. | ||
// This looses information about reachability causing worse codegen. | ||
// For example (see src/test/codegen/match-optimizes-away.rs) | ||
// | ||
// pub enum Two { A, B } | ||
// pub fn identity(x: Two) -> Two { | ||
// match x { | ||
// Two::A => Two::A, | ||
// Two::B => Two::B, | ||
// } | ||
// } | ||
// | ||
// This generates a `switchInt() -> [0: 0, 1: 1, otherwise: unreachable]`, which allows us or LLVM to | ||
// turn it into just `x` later. Without the unreachable, such a transformation would be illegal. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This statement is only true because we don't have enough range information available, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Also: please add a codegen test showing this opt) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Potentially, but IMO it's a bad idea to turn this: match x {
Enum::A => 1,
Enum::B => 2,
Enum::C => 3,
// Not in the user's code, but added for switchInt's "otherwise" target:
// _ => unreachable_unchecked(),
} into: match x {
Enum::A => 1,
Enum::B => 2,
_ => 3,
} It's erasing the refinement that the third case is only reachable when See also #99762 (comment) where I suggest a solution (make
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// If the otherwise branch is unreachable, we can delete all other unreacahble targets, as they will | ||
// still point to the unreachable and therefore not lose reachability information. | ||
let reachable_iter = targets.iter().filter(|(_, bb)| !is_unreachable(*bb)); | ||
|
||
if !predicate(otherwise) { | ||
targets.push(otherwise); | ||
} else { | ||
values.pop(); | ||
} | ||
let new_targets = SwitchTargets::new(reachable_iter, otherwise); | ||
|
||
let retained_targets_len = targets.len(); | ||
// No unreachable branches were removed. | ||
if new_targets.all_targets().len() == targets.all_targets().len() { | ||
return None; | ||
} | ||
|
||
if targets.is_empty() { | ||
TerminatorKind::Unreachable | ||
} else if targets.len() == 1 { | ||
TerminatorKind::Goto { target: targets[0] } | ||
} else if original_targets_len != retained_targets_len { | ||
TerminatorKind::SwitchInt { | ||
discr: discr.clone(), | ||
switch_ty, | ||
targets: SwitchTargets::new( | ||
values.iter().copied().zip(targets.iter().copied()), | ||
*targets.last().unwrap(), | ||
), | ||
switch_ty: *switch_ty, | ||
targets: new_targets, | ||
} | ||
} else { | ||
// If the otherwise branch is reachable, we don't want to delete any unreachable branches. | ||
return None; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*unreachable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#100907