Skip to content

Commit e5ebdd8

Browse files
committed
Experiment: Try letting DeduplicateBlocks delete redundant return blocks
1 parent 98ec48d commit e5ebdd8

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ pub struct DeduplicateBlocks;
1616

1717
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
1818
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
19-
sess.mir_opt_level() >= 4
19+
sess.mir_opt_level() >= 1
2020
}
2121

2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2323
debug!("Running DeduplicateBlocks on `{:?}`", body.source);
24-
let duplicates = find_duplicates(body);
24+
25+
let limit = if tcx.sess.mir_opt_level() < 3 {
26+
0
27+
} else {
28+
// Basic blocks can get really big, so to avoid checking for duplicates in basic blocks
29+
// that are unlikely to have duplicates, we stop early. The early bail number has been
30+
// found experimentally by eprintln while compiling the crates in the rustc-perf suite.
31+
10
32+
};
33+
34+
let duplicates = find_duplicates(body, limit);
2535
let has_opts_to_apply = !duplicates.is_empty();
2636

2737
if has_opts_to_apply {
@@ -54,7 +64,7 @@ impl<'tcx> MutVisitor<'tcx> for OptApplier<'tcx> {
5464
}
5565
}
5666

57-
fn find_duplicates(body: &Body<'_>) -> FxHashMap<BasicBlock, BasicBlock> {
67+
fn find_duplicates(body: &Body<'_>, limit: usize) -> FxHashMap<BasicBlock, BasicBlock> {
5868
let mut duplicates = FxHashMap::default();
5969

6070
let bbs_to_go_through =
@@ -72,10 +82,7 @@ fn find_duplicates(body: &Body<'_>) -> FxHashMap<BasicBlock, BasicBlock> {
7282
// with replacement bb3.
7383
// When the duplicates are removed, we will end up with only bb3.
7484
for (bb, bbd) in body.basic_blocks.iter_enumerated().rev().filter(|(_, bbd)| !bbd.is_cleanup) {
75-
// Basic blocks can get really big, so to avoid checking for duplicates in basic blocks
76-
// that are unlikely to have duplicates, we stop early. The early bail number has been
77-
// found experimentally by eprintln while compiling the crates in the rustc-perf suite.
78-
if bbd.statements.len() > 10 {
85+
if bbd.statements.len() > limit {
7986
continue;
8087
}
8188

tests/mir-opt/deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
}
4545

4646
bb5: {
47-
- switchInt((*_2)[3 of 4]) -> [47: bb11, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
48-
+ switchInt((*_2)[3 of 4]) -> [47: bb10, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
47+
switchInt((*_2)[3 of 4]) -> [47: bb11, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
4948
}
5049

5150
bb6: {
@@ -64,35 +63,30 @@
6463
}
6564

6665
bb9: {
67-
- switchInt((*_2)[2 of 3]) -> [47: bb12, 33: bb13, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
68-
+ switchInt((*_2)[2 of 3]) -> [47: bb11, 33: bb11, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
66+
switchInt((*_2)[2 of 3]) -> [47: bb12, 33: bb13, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23
6967
}
7068

7169
bb10: {
72-
- _0 = const false; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19
73-
- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19
74-
- }
75-
-
76-
- bb11: {
70+
_0 = const false; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19
71+
goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19
72+
}
73+
74+
bb11: {
7775
_0 = const false; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46
78-
- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46
79-
+ goto -> bb12; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46
76+
goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46
77+
}
78+
79+
bb12: {
80+
_0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39
81+
goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39
8082
}
8183

82-
- bb12: {
83-
- _0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39
84-
- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39
85-
- }
86-
-
87-
- bb13: {
88-
+ bb11: {
84+
bb13: {
8985
_0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39
90-
- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39
91-
+ goto -> bb12; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39
86+
goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39
9287
}
9388

94-
- bb14: {
95-
+ bb12: {
89+
bb14: {
9690
StorageDead(_2); // scope 0 at $DIR/deduplicate_blocks.rs:+7:1: +7:2
9791
return; // scope 0 at $DIR/deduplicate_blocks.rs:+7:2: +7:2
9892
}

0 commit comments

Comments
 (0)