Skip to content

Commit 2b56019

Browse files
authored
Rollup merge of rust-lang#76673 - simonvandel:remove-unneeded-drops, r=oli-obk
MIR pass to remove unneeded drops on types not needing drop This is heavily dependent on MIR inlining running to actually see the drop statement. Do we want to special case replacing a call to std::mem::drop with a goto aswell?
2 parents 8549f2e + 804f673 commit 2b56019

7 files changed

+209
-0
lines changed

compiler/rustc_mir/src/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod nrvo;
3636
pub mod promote_consts;
3737
pub mod qualify_min_const_fn;
3838
pub mod remove_noop_landing_pads;
39+
pub mod remove_unneeded_drops;
3940
pub mod required_consts;
4041
pub mod rustc_peek;
4142
pub mod simplify;
@@ -459,6 +460,7 @@ fn run_optimization_passes<'tcx>(
459460

460461
// The main optimizations that we do on MIR.
461462
let optimizations: &[&dyn MirPass<'tcx>] = &[
463+
&remove_unneeded_drops::RemoveUnneededDrops,
462464
&match_branches::MatchBranchSimplification,
463465
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
464466
&instcombine::InstCombine,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! This pass replaces a drop of a type that does not need dropping, with a goto
2+
3+
use crate::transform::{MirPass, MirSource};
4+
use rustc_hir::def_id::LocalDefId;
5+
use rustc_middle::mir::visit::Visitor;
6+
use rustc_middle::mir::*;
7+
use rustc_middle::ty::TyCtxt;
8+
9+
use super::simplify::simplify_cfg;
10+
11+
pub struct RemoveUnneededDrops;
12+
13+
impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
14+
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
15+
trace!("Running RemoveUnneededDrops on {:?}", source);
16+
let mut opt_finder = RemoveUnneededDropsOptimizationFinder {
17+
tcx,
18+
body,
19+
optimizations: vec![],
20+
def_id: source.def_id().expect_local(),
21+
};
22+
opt_finder.visit_body(body);
23+
let should_simplify = !opt_finder.optimizations.is_empty();
24+
for (loc, target) in opt_finder.optimizations {
25+
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
26+
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
27+
terminator.kind = TerminatorKind::Goto { target };
28+
}
29+
30+
// if we applied optimizations, we potentially have some cfg to cleanup to
31+
// make it easier for further passes
32+
if should_simplify {
33+
simplify_cfg(body);
34+
}
35+
}
36+
}
37+
38+
impl<'a, 'tcx> Visitor<'tcx> for RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
39+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
40+
match terminator.kind {
41+
TerminatorKind::Drop { place, target, .. }
42+
| TerminatorKind::DropAndReplace { place, target, .. } => {
43+
let ty = place.ty(self.body, self.tcx);
44+
let needs_drop = ty.ty.needs_drop(self.tcx, self.tcx.param_env(self.def_id));
45+
if !needs_drop {
46+
self.optimizations.push((location, target));
47+
}
48+
}
49+
_ => {}
50+
}
51+
self.super_terminator(terminator, location);
52+
}
53+
}
54+
pub struct RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
55+
tcx: TyCtxt<'tcx>,
56+
body: &'a Body<'tcx>,
57+
optimizations: Vec<(Location, BasicBlock)>,
58+
def_id: LocalDefId,
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
- // MIR for `cannot_opt_generic` before RemoveUnneededDrops
2+
+ // MIR for `cannot_opt_generic` after RemoveUnneededDrops
3+
4+
fn cannot_opt_generic(_1: T) -> () {
5+
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:19:26: 19:27
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:19:32: 19:32
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:20:5: 20:12
8+
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
9+
scope 1 {
10+
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
}
12+
13+
bb0: {
14+
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:20:5: 20:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
16+
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
17+
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18+
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19+
}
20+
21+
bb1 (cleanup): {
22+
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:19:1: 21:2
23+
}
24+
25+
bb2: {
26+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:20:11: 20:12
27+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:20:12: 20:13
28+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:19:32: 21:2
29+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:21:2: 21:2
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
- // MIR for `dont_opt` before RemoveUnneededDrops
2+
+ // MIR for `dont_opt` after RemoveUnneededDrops
3+
4+
fn dont_opt(_1: Vec<bool>) -> () {
5+
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:7:13: 7:14
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:7:27: 7:27
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:8:5: 8:12
8+
let mut _3: std::vec::Vec<bool>; // in scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
9+
scope 1 {
10+
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
}
12+
13+
bb0: {
14+
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:8:5: 8:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
16+
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
17+
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18+
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19+
}
20+
21+
bb1 (cleanup): {
22+
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:7:1: 9:2
23+
}
24+
25+
bb2: {
26+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:8:11: 8:12
27+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:8:12: 8:13
28+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:7:27: 9:2
29+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:9:2: 9:2
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- // MIR for `opt` before RemoveUnneededDrops
2+
+ // MIR for `opt` after RemoveUnneededDrops
3+
4+
fn opt(_1: bool) -> () {
5+
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:2:8: 2:9
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 2:17
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:3:5: 3:12
8+
let mut _3: bool; // in scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
9+
scope 1 {
10+
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
}
12+
13+
bb0: {
14+
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:5: 3:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
16+
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
17+
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18+
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19+
- }
20+
-
21+
- bb1: {
22+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:11: 3:12
23+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:12: 3:13
24+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 4:2
25+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:4:2: 4:2
26+
}
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- // MIR for `opt_generic_copy` before RemoveUnneededDrops
2+
+ // MIR for `opt_generic_copy` after RemoveUnneededDrops
3+
4+
fn opt_generic_copy(_1: T) -> () {
5+
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:12:30: 12:31
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 12:36
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:13:5: 13:12
8+
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
9+
scope 1 {
10+
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
}
12+
13+
bb0: {
14+
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:5: 13:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
16+
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
17+
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18+
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19+
- }
20+
-
21+
- bb1: {
22+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:11: 13:12
23+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:12: 13:13
24+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 14:2
25+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:14:2: 14:2
26+
}
27+
}
28+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// EMIT_MIR remove_unneeded_drops.opt.RemoveUnneededDrops.diff
2+
fn opt(x: bool) {
3+
drop(x);
4+
}
5+
6+
// EMIT_MIR remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff
7+
fn dont_opt(x: Vec<bool>) {
8+
drop(x);
9+
}
10+
11+
// EMIT_MIR remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff
12+
fn opt_generic_copy<T: Copy>(x: T) {
13+
drop(x);
14+
}
15+
16+
// EMIT_MIR remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff
17+
// since the pass is not running on monomorphisized code,
18+
// we can't (but probably should) optimize this
19+
fn cannot_opt_generic<T>(x: T) {
20+
drop(x);
21+
}
22+
23+
fn main() {
24+
opt(true);
25+
opt_generic_copy(42);
26+
cannot_opt_generic(42);
27+
dont_opt(vec![true]);
28+
}

0 commit comments

Comments
 (0)