-
Notifications
You must be signed in to change notification settings - Fork 13.4k
MIR pass to remove unneeded drops on types not needing drop #76673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
290dca1
b4bdaa1
d3338dc
f472303
b6f51d6
ff24163
05f84c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! This pass replaces a drop of a type that does not need dropping, with a goto | ||
|
||
use crate::transform::{MirPass, MirSource}; | ||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_middle::mir::visit::Visitor; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
|
||
use super::simplify::simplify_cfg; | ||
|
||
pub struct RemoveUnneededDrops; | ||
|
||
impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) { | ||
trace!("Running RemoveUnneededDrops on {:?}", source); | ||
let mut opt_finder = RemoveUnneededDropsOptimizationFinder { | ||
tcx, | ||
body, | ||
optimizations: vec![], | ||
def_id: source.def_id().expect_local(), | ||
}; | ||
opt_finder.visit_body(body); | ||
let should_simplify = !opt_finder.optimizations.is_empty(); | ||
for (loc, target) in opt_finder.optimizations { | ||
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut(); | ||
debug!("SUCCESS: replacing `drop` with goto({:?})", target); | ||
terminator.kind = TerminatorKind::Goto { target }; | ||
} | ||
|
||
// if we applied optimizations, we potentially have some cfg to cleanup to | ||
// make it easier for further passes | ||
if should_simplify { | ||
simplify_cfg(body); | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for RemoveUnneededDropsOptimizationFinder<'a, 'tcx> { | ||
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { | ||
match terminator.kind { | ||
TerminatorKind::Drop { place, target, .. } | ||
| TerminatorKind::DropAndReplace { place, target, .. } => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DropAndReplace terminator should never occur in an input to this pass, since it should be removed during drop elaboration. Additionally, its semantics is to drop and assign a new value, so the transformation wouldn't be correct without additional assignment. Either way, I would just remove it from here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
let ty = place.ty(self.body, self.tcx); | ||
let needs_drop = ty.ty.needs_drop(self.tcx, self.tcx.param_env(self.def_id)); | ||
if !needs_drop { | ||
self.optimizations.push((location, target)); | ||
} | ||
} | ||
_ => {} | ||
} | ||
self.super_terminator(terminator, location); | ||
} | ||
} | ||
pub struct RemoveUnneededDropsOptimizationFinder<'a, 'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
body: &'a Body<'tcx>, | ||
optimizations: Vec<(Location, BasicBlock)>, | ||
def_id: LocalDefId, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
- // MIR for `cannot_opt_generic` before RemoveUnneededDrops | ||
+ // MIR for `cannot_opt_generic` after RemoveUnneededDrops | ||
|
||
fn cannot_opt_generic(_1: T) -> () { | ||
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:20:26: 20:27 | ||
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:20:32: 20:32 | ||
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:21:5: 21:12 | ||
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11 | ||
scope 1 { | ||
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:21:5: 21:12 | ||
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11 | ||
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11 | ||
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb1 (cleanup): { | ||
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:20:1: 22:2 | ||
} | ||
|
||
bb2: { | ||
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:21:11: 21:12 | ||
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:21:12: 21:13 | ||
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:20:32: 22:2 | ||
return; // scope 0 at $DIR/remove_unneeded_drops.rs:22:2: 22:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
- // MIR for `dont_opt` before RemoveUnneededDrops | ||
+ // MIR for `dont_opt` after RemoveUnneededDrops | ||
|
||
fn dont_opt(_1: Vec<bool>) -> () { | ||
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:8:13: 8:14 | ||
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:8:27: 8:27 | ||
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:9:5: 9:12 | ||
let mut _3: std::vec::Vec<bool>; // in scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11 | ||
scope 1 { | ||
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:9:5: 9:12 | ||
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11 | ||
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11 | ||
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb1 (cleanup): { | ||
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:8:1: 10:2 | ||
} | ||
|
||
bb2: { | ||
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:9:11: 9:12 | ||
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:9:12: 9:13 | ||
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:8:27: 10:2 | ||
return; // scope 0 at $DIR/remove_unneeded_drops.rs:10:2: 10:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
- // MIR for `opt` before RemoveUnneededDrops | ||
+ // MIR for `opt` after RemoveUnneededDrops | ||
|
||
fn opt(_1: bool) -> () { | ||
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:3:8: 3:9 | ||
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:3:17: 3:17 | ||
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:4:5: 4:12 | ||
let mut _3: bool; // in scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11 | ||
scope 1 { | ||
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:4:5: 4:12 | ||
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11 | ||
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11 | ||
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
- } | ||
- | ||
- bb1: { | ||
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:4:11: 4:12 | ||
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:4:12: 4:13 | ||
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:3:17: 5:2 | ||
return; // scope 0 at $DIR/remove_unneeded_drops.rs:5:2: 5:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
- // MIR for `opt_generic_copy` before RemoveUnneededDrops | ||
+ // MIR for `opt_generic_copy` after RemoveUnneededDrops | ||
|
||
fn opt_generic_copy(_1: T) -> () { | ||
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:13:30: 13:31 | ||
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:13:36: 13:36 | ||
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:14:5: 14:12 | ||
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11 | ||
scope 1 { | ||
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:14:5: 14:12 | ||
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11 | ||
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11 | ||
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
- } | ||
- | ||
- bb1: { | ||
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:14:11: 14:12 | ||
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:14:12: 14:13 | ||
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:13:36: 15:2 | ||
return; // scope 0 at $DIR/remove_unneeded_drops.rs:15:2: 15:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// ignore-wasm32-bare compiled with panic=abort by default | ||
// EMIT_MIR remove_unneeded_drops.opt.RemoveUnneededDrops.diff | ||
fn opt(x: bool) { | ||
drop(x); | ||
} | ||
|
||
// EMIT_MIR remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff | ||
fn dont_opt(x: Vec<bool>) { | ||
drop(x); | ||
} | ||
|
||
// EMIT_MIR remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff | ||
fn opt_generic_copy<T: Copy>(x: T) { | ||
drop(x); | ||
} | ||
|
||
// EMIT_MIR remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff | ||
// since the pass is not running on monomorphisized code, | ||
// we can't (but probably should) optimize this | ||
fn cannot_opt_generic<T>(x: T) { | ||
drop(x); | ||
} | ||
|
||
fn main() { | ||
opt(true); | ||
opt_generic_copy(42); | ||
cannot_opt_generic(42); | ||
dont_opt(vec![true]); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.