Skip to content

Commit 8b40853

Browse files
committed
Auto 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 a6008fa + 05f84c6 commit 8b40853

9 files changed

+210
-8
lines changed

compiler/rustc_mir/src/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub mod nrvo;
3838
pub mod promote_consts;
3939
pub mod qualify_min_const_fn;
4040
pub mod remove_noop_landing_pads;
41+
pub mod remove_unneeded_drops;
4142
pub mod required_consts;
4243
pub mod rustc_peek;
4344
pub mod simplify;
@@ -461,6 +462,7 @@ fn run_optimization_passes<'tcx>(
461462

462463
// The main optimizations that we do on MIR.
463464
let optimizations: &[&dyn MirPass<'tcx>] = &[
465+
&remove_unneeded_drops::RemoveUnneededDrops,
464466
&match_branches::MatchBranchSimplification,
465467
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
466468
&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+
}

src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff

-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@
5757
- _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11
5858
+ _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11
5959
_5 = const (); // scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
60-
drop(_6) -> bb2; // scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
61-
}
62-
63-
bb2: {
6460
StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12
6561
StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13
6662
_0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2

src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff

-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
3333
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
3434
_3 = const (); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
35-
drop(_4) -> bb2; // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
36-
}
37-
38-
bb2: {
3935
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
4036
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
4137
_0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2
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:20:26: 20:27
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:20:32: 20:32
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:21:5: 21:12
8+
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21: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:21:5: 21:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11
16+
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21: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:20:1: 22:2
23+
}
24+
25+
bb2: {
26+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:21:11: 21:12
27+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:21:12: 21:13
28+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:20:32: 22:2
29+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:22:2: 22: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:8:13: 8:14
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:8:27: 8:27
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:9:5: 9:12
8+
let mut _3: std::vec::Vec<bool>; // in scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9: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:9:5: 9:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11
16+
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9: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:8:1: 10:2
23+
}
24+
25+
bb2: {
26+
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:9:11: 9:12
27+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:9:12: 9:13
28+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:8:27: 10:2
29+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:10:2: 10: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:3:8: 3:9
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:3:17: 3:17
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:4:5: 4:12
8+
let mut _3: bool; // in scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4: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:4:5: 4:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11
16+
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4: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:4:11: 4:12
23+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:4:12: 4:13
24+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:3:17: 5:2
25+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:5:2: 5: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:13:30: 13:31
6+
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:13:36: 13:36
7+
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:14:5: 14:12
8+
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14: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:14:5: 14:12
15+
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11
16+
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14: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:14:11: 14:12
23+
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:14:12: 14:13
24+
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:13:36: 15:2
25+
return; // scope 0 at $DIR/remove_unneeded_drops.rs:15:2: 15:2
26+
}
27+
}
28+
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ignore-wasm32-bare compiled with panic=abort by default
2+
// EMIT_MIR remove_unneeded_drops.opt.RemoveUnneededDrops.diff
3+
fn opt(x: bool) {
4+
drop(x);
5+
}
6+
7+
// EMIT_MIR remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff
8+
fn dont_opt(x: Vec<bool>) {
9+
drop(x);
10+
}
11+
12+
// EMIT_MIR remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff
13+
fn opt_generic_copy<T: Copy>(x: T) {
14+
drop(x);
15+
}
16+
17+
// EMIT_MIR remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff
18+
// since the pass is not running on monomorphisized code,
19+
// we can't (but probably should) optimize this
20+
fn cannot_opt_generic<T>(x: T) {
21+
drop(x);
22+
}
23+
24+
fn main() {
25+
opt(true);
26+
opt_generic_copy(42);
27+
cannot_opt_generic(42);
28+
dont_opt(vec![true]);
29+
}

0 commit comments

Comments
 (0)