Skip to content

Commit b8f401a

Browse files
committed
Auto merge of #130691 - scottmcm:elaborate-drops-to-calls, r=<try>
[WIP] Elaborate drops to calls For now just borrowing CI to check for annoying no_core tests that I broke r? ghost
2 parents 80aa6fa + b154623 commit b8f401a

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

‎compiler/rustc_mir_transform/src/inline/cycle.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
22
use rustc_data_structures::stack::ensure_sufficient_stack;
33
use rustc_hir::def_id::{DefId, LocalDefId};
4+
use rustc_hir::LangItem;
45
use rustc_middle::mir::TerminatorKind;
5-
use rustc_middle::ty::{self, GenericArgsRef, InstanceKind, TyCtxt, TypeVisitableExt};
6+
use rustc_middle::ty::{self, GenericArg, GenericArgsRef, InstanceKind, TyCtxt, TypeVisitableExt};
67
use rustc_session::Limit;
78
use rustc_span::sym;
89
use tracing::{instrument, trace};
@@ -191,6 +192,18 @@ pub(crate) fn mir_inliner_callees<'tcx>(
191192
};
192193
calls.insert(call);
193194
}
195+
// This query is called on *analysis* MIR, which doesn't have drops elaborated,
196+
// let alone any of the later runtime MIR optimizations.
197+
if let TerminatorKind::Drop { place, .. } = &terminator.kind {
198+
let dropped_ty = place.ty(&body.local_decls, tcx).ty;
199+
let param_env = tcx.param_env_reveal_all_normalized(instance.def_id());
200+
if dropped_ty.needs_drop(tcx, param_env) {
201+
let drop_in_place_fn =
202+
tcx.require_lang_item(LangItem::DropInPlace, Some(terminator.source_info.span));
203+
let args = tcx.mk_args(&[GenericArg::from(dropped_ty)]);
204+
calls.insert((drop_in_place_fn, args));
205+
}
206+
}
194207
}
195208
tcx.arena.alloc_from_iter(calls.iter().copied())
196209
}

‎tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
let mut _4: i32;
1010
let mut _5: !;
1111
let _6: !;
12-
+ scope 1 (inlined panic) {
13-
+ let mut _7: !;
12+
+ scope 1 (inlined calls_panic) {
13+
+ let _7: !;
1414
+ }
1515

1616
bb0: {
@@ -34,9 +34,9 @@
3434
bb2: {
3535
StorageDead(_3);
3636
StorageLive(_6);
37-
- _6 = panic() -> unwind unreachable;
37+
- _6 = calls_panic() -> unwind unreachable;
3838
+ StorageLive(_7);
39-
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable;
39+
+ _7 = panic_cold_explicit() -> unwind unreachable;
4040
}
4141
}
4242

‎tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
let mut _4: i32;
1010
let mut _5: !;
1111
let _6: !;
12-
+ scope 1 (inlined panic) {
13-
+ let mut _7: !;
12+
+ scope 1 (inlined calls_panic) {
13+
+ let _7: !;
1414
+ }
1515

1616
bb0: {
@@ -34,9 +34,9 @@
3434
bb2: {
3535
StorageDead(_3);
3636
StorageLive(_6);
37-
- _6 = panic() -> unwind continue;
37+
- _6 = calls_panic() -> unwind continue;
3838
+ StorageLive(_7);
39-
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind continue;
39+
+ _7 = panic_cold_explicit() -> unwind continue;
4040
}
4141
}
4242

‎tests/mir-opt/inline/inline_diverging.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Tests inlining of diverging calls.
22
//
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4-
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full
4+
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full --edition=2021
55
#![crate_type = "lib"]
66

77
// EMIT_MIR inline_diverging.f.Inline.diff
@@ -17,8 +17,8 @@ pub fn g(i: i32) -> u32 {
1717
i as u32
1818
} else {
1919
// CHECK-LABEL: fn g(
20-
// CHECK: (inlined panic)
21-
panic();
20+
// CHECK: (inlined calls_panic)
21+
calls_panic();
2222
}
2323
}
2424

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

4040
#[inline(always)]
41-
fn panic() -> ! {
41+
fn calls_panic() -> ! {
4242
panic!();
4343
}
4444

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- // MIR for `drop_both_arguments` before Inline
2+
+ // MIR for `drop_both_arguments` after Inline
3+
4+
fn drop_both_arguments(_1: CallThingOnDrop, _2: CallThingOnDrop) -> () {
5+
debug _a => _1;
6+
debug _b => _2;
7+
let mut _0: ();
8+
9+
bb0: {
10+
_0 = const ();
11+
drop(_2) -> [return: bb1, unwind: bb3];
12+
}
13+
14+
bb1: {
15+
drop(_1) -> [return: bb2, unwind: bb4];
16+
}
17+
18+
bb2: {
19+
return;
20+
}
21+
22+
bb3 (cleanup): {
23+
drop(_1) -> [return: bb4, unwind terminate(cleanup)];
24+
}
25+
26+
bb4 (cleanup): {
27+
resume;
28+
}
29+
}
30+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Test for inlining with an indirect destination place.
2+
//
3+
//@ test-mir-pass: Inline
4+
//@ edition: 2021
5+
//@ needs-unwind
6+
#![crate_type = "lib"]
7+
8+
#[inline(never)]
9+
fn thing() {}
10+
11+
struct CallThingOnDrop;
12+
13+
impl Drop for CallThingOnDrop {
14+
#[inline]
15+
fn drop(&mut self) {
16+
thing();
17+
}
18+
}
19+
20+
// EMIT_MIR inline_drop.drop_both_arguments.Inline.diff
21+
pub fn drop_both_arguments(_a: CallThingOnDrop, _b: CallThingOnDrop) {
22+
// CHECK: drop(_2)
23+
// CHECK: drop(_1)
24+
}

0 commit comments

Comments
 (0)