Skip to content

Commit b76e9cb

Browse files
committed
Add fast path for trivial drops.
1 parent fce390d commit b76e9cb

8 files changed

+18
-19
lines changed

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,23 @@ fn try_inlining<'tcx, I: Inliner<'tcx>>(
611611
caller_body: &mut Body<'tcx>,
612612
callsite: &CallSite<'tcx>,
613613
) -> Result<std::ops::Range<BasicBlock>, &'static str> {
614+
// Fast path to inline trivial drops.
615+
if let InstanceKind::DropGlue(_, None) = callsite.callee.def {
616+
let terminator = caller_body[callsite.block].terminator_mut();
617+
let target = match terminator.kind {
618+
TerminatorKind::Call { target, .. } => target,
619+
TerminatorKind::Drop { target, .. } => Some(target),
620+
_ => bug!("unexpected terminator kind {:?}", terminator.kind),
621+
};
622+
if let Some(target) = target {
623+
terminator.kind = TerminatorKind::Goto { target };
624+
} else {
625+
terminator.kind = TerminatorKind::Unreachable;
626+
}
627+
let next_block = caller_body.basic_blocks.next_index();
628+
return Ok(next_block..next_block);
629+
}
630+
614631
let tcx = inliner.tcx();
615632
check_mir_is_available(inliner, caller_body, callsite.callee)?;
616633

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
debug un => _1;
1010
scope 3 (inlined std::mem::drop::<u32>) {
1111
debug _x => _2;
12-
scope 4 (inlined drop_in_place::<u32> - shim(None)) {
13-
}
1412
}
1513
}
1614
scope 2 (inlined val) {

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
debug un => _1;
1010
scope 3 (inlined std::mem::drop::<u32>) {
1111
debug _x => _2;
12-
scope 4 (inlined drop_in_place::<u32> - shim(None)) {
13-
}
1412
}
1513
}
1614
scope 2 (inlined val) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
+ let mut _2: fn() -> ! {sleep};
88
+ let mut _7: ();
99
+ let mut _8: ();
10-
+ let mut _9: *mut fn() -> ! {sleep};
11-
+ let mut _10: ();
1210
+ scope 1 (inlined call_twice::<!, fn() -> ! {sleep}>) {
1311
+ debug f => _2;
1412
+ let mut _3: &fn() -> ! {sleep};
@@ -29,8 +27,6 @@
2927
+ scope 5 (inlined sleep) {
3028
+ }
3129
+ }
32-
+ scope 8 (inlined drop_in_place::<fn() -> ! {sleep}> - shim(None)) {
33-
+ }
3430
+ }
3531

3632
bb0: {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
+ let mut _2: fn() -> ! {sleep};
88
+ let mut _8: ();
99
+ let mut _9: ();
10-
+ let mut _10: *mut fn() -> ! {sleep};
11-
+ let mut _11: ();
1210
+ scope 1 (inlined call_twice::<!, fn() -> ! {sleep}>) {
1311
+ debug f => _2;
1412
+ let mut _3: &fn() -> ! {sleep};
@@ -30,8 +28,6 @@
3028
+ scope 5 (inlined sleep) {
3129
+ }
3230
+ }
33-
+ scope 8 (inlined drop_in_place::<fn() -> ! {sleep}> - shim(None)) {
34-
+ }
3531
+ }
3632

3733
bb0: {

tests/mir-opt/pre-codegen/checked_ops.saturating_sub_at_home.PreCodegen.after.panic-abort.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ fn saturating_sub_at_home(_1: u32, _2: u32) -> u32 {
1313
let _6: u32;
1414
scope 3 {
1515
}
16-
scope 4 (inlined drop_in_place::<u32> - shim(None)) {
17-
}
1816
}
1917

2018
bb0: {

tests/mir-opt/pre-codegen/checked_ops.saturating_sub_at_home.PreCodegen.after.panic-unwind.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ fn saturating_sub_at_home(_1: u32, _2: u32) -> u32 {
1313
let _6: u32;
1414
scope 3 {
1515
}
16-
scope 4 (inlined drop_in_place::<u32> - shim(None)) {
17-
}
1816
}
1917

2018
bb0: {

tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ fn ezmap(_1: Option<i32>) -> Option<i32> {
88
let _3: i32;
99
let mut _4: i32;
1010
scope 2 {
11-
scope 4 (inlined ezmap::{closure#0}) {
11+
scope 3 (inlined ezmap::{closure#0}) {
1212
}
1313
}
14-
scope 3 (inlined drop_in_place::<{closure@$DIR/simple_option_map.rs:23:12: 23:15}> - shim(None)) {
15-
}
1614
}
1715

1816
bb0: {

0 commit comments

Comments
 (0)