Skip to content
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

[WIP] Elaborate drops to calls #130691

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Include drops in mir_inliner_callees
  • Loading branch information
scottmcm committed Sep 22, 2024
commit b1546232e58af3afe5877fc27d7813da7de2f50e
15 changes: 14 additions & 1 deletion compiler/rustc_mir_transform/src/inline/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::LangItem;
use rustc_middle::mir::TerminatorKind;
use rustc_middle::ty::{self, GenericArgsRef, InstanceKind, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{self, GenericArg, GenericArgsRef, InstanceKind, TyCtxt, TypeVisitableExt};
use rustc_session::Limit;
use rustc_span::sym;
use tracing::{instrument, trace};
Expand Down Expand Up @@ -191,6 +192,18 @@ pub(crate) fn mir_inliner_callees<'tcx>(
};
calls.insert(call);
}
// This query is called on *analysis* MIR, which doesn't have drops elaborated,
// let alone any of the later runtime MIR optimizations.
if let TerminatorKind::Drop { place, .. } = &terminator.kind {
let dropped_ty = place.ty(&body.local_decls, tcx).ty;
let param_env = tcx.param_env_reveal_all_normalized(instance.def_id());
if dropped_ty.needs_drop(tcx, param_env) {
let drop_in_place_fn =
tcx.require_lang_item(LangItem::DropInPlace, Some(terminator.source_info.span));
let args = tcx.mk_args(&[GenericArg::from(dropped_ty)]);
calls.insert((drop_in_place_fn, args));
}
}
}
tcx.arena.alloc_from_iter(calls.iter().copied())
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
let mut _4: i32;
let mut _5: !;
let _6: !;
+ scope 1 (inlined panic) {
+ let mut _7: !;
+ scope 1 (inlined calls_panic) {
+ let _7: !;
+ }

bb0: {
Expand All @@ -34,9 +34,9 @@
bb2: {
StorageDead(_3);
StorageLive(_6);
- _6 = panic() -> unwind unreachable;
- _6 = calls_panic() -> unwind unreachable;
+ StorageLive(_7);
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable;
+ _7 = panic_cold_explicit() -> unwind unreachable;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
let mut _4: i32;
let mut _5: !;
let _6: !;
+ scope 1 (inlined panic) {
+ let mut _7: !;
+ scope 1 (inlined calls_panic) {
+ let _7: !;
+ }

bb0: {
Expand All @@ -34,9 +34,9 @@
bb2: {
StorageDead(_3);
StorageLive(_6);
- _6 = panic() -> unwind continue;
- _6 = calls_panic() -> unwind continue;
+ StorageLive(_7);
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind continue;
+ _7 = panic_cold_explicit() -> unwind continue;
}
}

8 changes: 4 additions & 4 deletions tests/mir-opt/inline/inline_diverging.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Tests inlining of diverging calls.
//
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full --edition=2021
#![crate_type = "lib"]

// EMIT_MIR inline_diverging.f.Inline.diff
Expand All @@ -17,8 +17,8 @@ pub fn g(i: i32) -> u32 {
i as u32
} else {
// CHECK-LABEL: fn g(
// CHECK: (inlined panic)
panic();
// CHECK: (inlined calls_panic)
calls_panic();
}
}

Expand All @@ -38,7 +38,7 @@ pub fn call_twice<R, F: Fn() -> R>(f: F) -> (R, R) {
}

#[inline(always)]
fn panic() -> ! {
fn calls_panic() -> ! {
panic!();
}

Expand Down
Loading