Skip to content

Commit e63e769

Browse files
authored
Rollup merge of #144883 - scottmcm:remove-unneeded-drop_in_place, r=nnethercote
Remove unneeded `drop_in_place` calls Might as well pull this out from #144561 because this is still used in things like `Vec::truncate` where it'd be nice to allow it be removed if inlined enough to see that the type is `Copy`. So long as perf says it's ok, at least 🤞
2 parents 4f80489 + 4e81eca commit e63e769

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

compiler/rustc_mir_transform/src/remove_unneeded_drops.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
//! useful because (unlike MIR building) it runs after type checking, so it can make use of
55
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
66
//! types.
7+
//!
8+
//! When we're optimizing, we also remove calls to `drop_in_place<T>` when `T` isn't `needs_drop`,
9+
//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert
10+
//! them automatically, preferring the built-in instead, they're common in generic code (such as
11+
//! `Vec::truncate`) so removing them from things like inlined `Vec<u8>` is helpful.
712
13+
use rustc_hir::LangItem;
814
use rustc_middle::mir::*;
915
use rustc_middle::ty::TyCtxt;
1016
use tracing::{debug, trace};
@@ -21,15 +27,26 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
2127
let mut should_simplify = false;
2228
for block in body.basic_blocks.as_mut() {
2329
let terminator = block.terminator_mut();
24-
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
25-
let ty = place.ty(&body.local_decls, tcx);
26-
if ty.ty.needs_drop(tcx, typing_env) {
27-
continue;
30+
let (ty, target) = match terminator.kind {
31+
TerminatorKind::Drop { place, target, .. } => {
32+
(place.ty(&body.local_decls, tcx).ty, target)
33+
}
34+
TerminatorKind::Call { ref func, target: Some(target), .. }
35+
if tcx.sess.mir_opt_level() > 0
36+
&& let Some((def_id, generics)) = func.const_fn_def()
37+
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
38+
{
39+
(generics.type_at(0), target)
2840
}
29-
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
30-
terminator.kind = TerminatorKind::Goto { target };
31-
should_simplify = true;
41+
_ => continue,
42+
};
43+
44+
if ty.needs_drop(tcx, typing_env) {
45+
continue;
3246
}
47+
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
48+
terminator.kind = TerminatorKind::Goto { target };
49+
should_simplify = true;
3350
}
3451

3552
// if we applied optimizations, we potentially have some cfg to cleanup to
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ test-mir-pass: RemoveUnneededDrops
2+
//@ needs-unwind
3+
//@ compile-flags: -Z mir-opt-level=1
4+
5+
// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
6+
unsafe fn slice_in_place(ptr: *mut [char]) {
7+
// CHECK-LABEL: fn slice_in_place(_1: *mut [char])
8+
// CHECK: bb0: {
9+
// CHECK-NEXT: return;
10+
// CHECK-NEXT: }
11+
std::ptr::drop_in_place(ptr)
12+
}
13+
14+
fn main() {
15+
let mut a = ['o', 'k'];
16+
unsafe { slice_in_place(&raw mut a) };
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- // MIR for `slice_in_place` before RemoveUnneededDrops
2+
+ // MIR for `slice_in_place` after RemoveUnneededDrops
3+
4+
fn slice_in_place(_1: *mut [char]) -> () {
5+
debug ptr => _1;
6+
let mut _0: ();
7+
let mut _2: *mut [char];
8+
9+
bb0: {
10+
StorageLive(_2);
11+
_2 = copy _1;
12+
- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
13+
- }
14+
-
15+
- bb1: {
16+
StorageDead(_2);
17+
return;
18+
}
19+
}
20+

0 commit comments

Comments
 (0)