Let RemoveUnneededDrops also remove drop_in_place

This commit is contained in:
Scott McMurray 2025-07-27 21:39:56 -07:00
parent 9b107bed9f
commit 4e81ecaf3a
3 changed files with 33 additions and 12 deletions

View file

@ -4,7 +4,13 @@
//! useful because (unlike MIR building) it runs after type checking, so it can make use of //! useful because (unlike MIR building) it runs after type checking, so it can make use of
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque //! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
//! types. //! types.
//!
//! When we're optimizing, we also remove calls to `drop_in_place<T>` when `T` isn't `needs_drop`,
//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert
//! them automatically, preferring the built-in instead, they're common in generic code (such as
//! `Vec::truncate`) so removing them from things like inlined `Vec<u8>` is helpful.
use rustc_hir::LangItem;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use tracing::{debug, trace}; use tracing::{debug, trace};
@ -21,15 +27,26 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
let mut should_simplify = false; let mut should_simplify = false;
for block in body.basic_blocks.as_mut() { for block in body.basic_blocks.as_mut() {
let terminator = block.terminator_mut(); let terminator = block.terminator_mut();
if let TerminatorKind::Drop { place, target, .. } = terminator.kind { let (ty, target) = match terminator.kind {
let ty = place.ty(&body.local_decls, tcx); TerminatorKind::Drop { place, target, .. } => {
if ty.ty.needs_drop(tcx, typing_env) { (place.ty(&body.local_decls, tcx).ty, target)
continue;
} }
debug!("SUCCESS: replacing `drop` with goto({:?})", target); TerminatorKind::Call { ref func, target: Some(target), .. }
terminator.kind = TerminatorKind::Goto { target }; if tcx.sess.mir_opt_level() > 0
should_simplify = true; && let Some((def_id, generics)) = func.const_fn_def()
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
{
(generics.type_at(0), target)
}
_ => continue,
};
if ty.needs_drop(tcx, typing_env) {
continue;
} }
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
should_simplify = true;
} }
// if we applied optimizations, we potentially have some cfg to cleanup to // if we applied optimizations, we potentially have some cfg to cleanup to

View file

@ -1,13 +1,17 @@
//@ test-mir-pass: RemoveUnneededDrops //@ test-mir-pass: RemoveUnneededDrops
//@ needs-unwind //@ needs-unwind
//@ compile-flags: -Z mir-opt-level=1
// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff // EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
unsafe fn slice_in_place(ptr: *mut [char]) { unsafe fn slice_in_place(ptr: *mut [char]) {
// CHECK-LABEL: fn slice_in_place(_1: *mut [char])
// CHECK: bb0: {
// CHECK-NEXT: return;
// CHECK-NEXT: }
std::ptr::drop_in_place(ptr) std::ptr::drop_in_place(ptr)
} }
fn main() { fn main() {
// CHECK-LABEL: fn main(
let mut a = ['o', 'k']; let mut a = ['o', 'k'];
unsafe { slice_in_place(&raw mut a) }; unsafe { slice_in_place(&raw mut a) };
} }

View file

@ -9,10 +9,10 @@
bb0: { bb0: {
StorageLive(_2); StorageLive(_2);
_2 = copy _1; _2 = copy _1;
_0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue]; - _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
} - }
-
bb1: { - bb1: {
StorageDead(_2); StorageDead(_2);
return; return;
} }