Skip to content

Commit 8b03cfb

Browse files
committed
Add a workaround for the TailDuplicator compile time overhead
1 parent 239dfe9 commit 8b03cfb

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::MirPass;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_middle::mir::patch::MirPatch;
66
use rustc_middle::mir::{
7-
BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, TerminatorKind,
7+
BasicBlock, BasicBlockData, BasicBlocks, Body, Local, Operand, Rvalue, StatementKind,
8+
TerminatorKind,
89
};
910
use rustc_middle::ty::layout::TyAndLayout;
1011
use rustc_middle::ty::{Ty, TyCtxt};
@@ -118,9 +119,23 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
118119
unreachable_targets.push(index);
119120
}
120121
}
121-
122-
let replace_otherwise_to_unreachable = allowed_variants.len() <= 1
123-
&& !body.basic_blocks[targets.otherwise()].is_empty_unreachable();
122+
let otherwise_is_empty_unreachable =
123+
body.basic_blocks[targets.otherwise()].is_empty_unreachable();
124+
// After resolving https://github.com/llvm/llvm-project/issues/78578,
125+
// we can remove the limit on the number of successors.
126+
fn check_successors(basic_blocks: &BasicBlocks<'_>, bb: BasicBlock) -> bool {
127+
let mut successors = basic_blocks[bb].terminator().successors();
128+
let Some(first_successor) = successors.next() else { return true };
129+
if successors.next().is_some() {
130+
return true;
131+
}
132+
basic_blocks[first_successor].terminator().successors().count() <= 2
133+
}
134+
let otherwise_is_last_variant = !otherwise_is_empty_unreachable
135+
&& allowed_variants.len() == 1
136+
&& check_successors(&body.basic_blocks, targets.otherwise());
137+
let replace_otherwise_to_unreachable = otherwise_is_last_variant
138+
|| !otherwise_is_empty_unreachable && allowed_variants.is_empty();
124139

125140
if unreachable_targets.is_empty() && !replace_otherwise_to_unreachable {
126141
continue;
@@ -129,7 +144,6 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
129144
let unreachable_block = patch.unreachable_no_cleanup_block();
130145
let mut targets = targets.clone();
131146
if replace_otherwise_to_unreachable {
132-
let otherwise_is_last_variant = !allowed_variants.is_empty();
133147
if otherwise_is_last_variant {
134148
#[allow(rustc::potential_query_instability)]
135149
let last_variant = *allowed_variants.iter().next().unwrap();

0 commit comments

Comments
 (0)