Skip to content

Commit 9c5d0c1

Browse files
committed
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
1 parent dd33766 commit 9c5d0c1

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;
@@ -455,6 +456,7 @@ fn run_optimization_passes<'tcx>(
455456

456457
// The main optimizations that we do on MIR.
457458
let optimizations: &[&dyn MirPass<'tcx>] = &[
459+
&remove_unneeded_drops::RemoveUnneededDrops::new(def_id),
458460
&match_branches::MatchBranchSimplification,
459461
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
460462
&instcombine::InstCombine,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
pub struct RemoveUnneededDrops {
10+
def_id: LocalDefId,
11+
}
12+
13+
impl RemoveUnneededDrops {
14+
pub fn new(def_id: LocalDefId) -> Self {
15+
Self { def_id }
16+
}
17+
}
18+
19+
impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
20+
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
21+
trace!("Running SimplifyComparisonIntegral on {:?}", source);
22+
let mut opt_finder = RemoveUnneededDropsOptimizationFinder {
23+
tcx,
24+
body,
25+
optimizations: vec![],
26+
def_id: self.def_id,
27+
};
28+
opt_finder.visit_body(body);
29+
for (loc, target) in opt_finder.optimizations {
30+
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
31+
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
32+
terminator.kind = TerminatorKind::Goto { target };
33+
}
34+
}
35+
}
36+
37+
impl<'a, 'tcx> Visitor<'tcx> for RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
38+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
39+
match terminator.kind {
40+
TerminatorKind::Drop { place, target, .. } => {
41+
let ty = place.ty(self.body, self.tcx);
42+
let needs_drop = ty.ty.needs_drop(self.tcx, self.tcx.param_env(self.def_id));
43+
if !needs_drop {
44+
self.optimizations.push((location, target));
45+
}
46+
}
47+
_ => {}
48+
}
49+
self.super_terminator(terminator, location);
50+
}
51+
}
52+
pub struct RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
53+
tcx: TyCtxt<'tcx>,
54+
body: &'a Body<'tcx>,
55+
optimizations: Vec<(Location, BasicBlock)>,
56+
def_id: LocalDefId,
57+
}
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,29 @@
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+
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
20+
}
21+
22+
bb1: {
23+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:11: 3:12
24+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:12: 3:13
25+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 4:2
26+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:4:2: 4:2
27+
}
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
20+
}
21+
22+
bb1: {
23+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:11: 13:12
24+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:12: 13:13
25+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 14:2
26+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:14:2: 14:2
27+
}
28+
}
29+
+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)