Skip to content

Commit 8cdcf3f

Browse files
Only inline FnPtr and FnDef shims in inliner
1 parent 0931ef1 commit 8cdcf3f

6 files changed

+42
-47
lines changed

compiler/rustc_mir_transform/src/inline.rs

+6
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ fn try_inlining<'tcx, I: Inliner<'tcx>>(
625625
return Err("implementation limitation -- return type mismatch");
626626
}
627627
if callsite.fn_sig.abi() == ExternAbi::RustCall {
628+
match callsite.callee.def {
629+
InstanceKind::Item(def_id) if tcx.is_closure_like(def_id) => {}
630+
InstanceKind::FnPtrShim(..) | InstanceKind::ClosureOnceShim { .. } => {}
631+
_ => return Err("not inlining non-builtin call"),
632+
}
633+
628634
let (self_arg, arg_tuple) = match &args[..] {
629635
[arg_tuple] => (None, arg_tuple),
630636
[self_arg, arg_tuple] => (Some(self_arg), arg_tuple),

tests/mir-opt/inline/inline_cycle.two.Inline.panic-unwind.diff

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {f};
8+
+ let mut _4: ();
89
+ scope 1 (inlined call::<fn() {f}>) {
910
+ debug f => _2;
1011
+ let _3: ();
12+
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
13+
+ scope 3 (inlined f) {
14+
+ let _5: ();
15+
+ }
16+
+ }
1117
+ }
1218

1319
bb0: {
@@ -16,10 +22,15 @@
1622
+ StorageLive(_2);
1723
+ _2 = f;
1824
+ StorageLive(_3);
19-
+ _3 = <fn() {f} as FnOnce<()>>::call_once(move _2, const ()) -> [return: bb1, unwind continue];
25+
+ StorageLive(_4);
26+
+ _4 = const ();
27+
+ StorageLive(_5);
28+
+ _5 = call::<fn() {f}>(f) -> [return: bb1, unwind continue];
2029
}
2130

2231
bb1: {
32+
+ StorageDead(_5);
33+
+ StorageDead(_4);
2334
+ StorageDead(_3);
2435
+ StorageDead(_2);
2536
StorageDead(_1);

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

+9-31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
let mut _0: ();
66
let _1: (!, !);
77
+ let mut _2: fn() -> ! {sleep};
8+
+ let mut _8: ();
89
+ scope 1 (inlined call_twice::<!, fn() -> ! {sleep}>) {
910
+ debug f => _2;
1011
+ let mut _3: &fn() -> ! {sleep};
@@ -18,6 +19,10 @@
1819
+ debug b => _6;
1920
+ }
2021
+ }
22+
+ scope 4 (inlined <fn() -> ! {sleep} as Fn<()>>::call - shim(fn() -> ! {sleep})) {
23+
+ scope 5 (inlined sleep) {
24+
+ }
25+
+ }
2126
+ }
2227

2328
bb0: {
@@ -29,40 +34,13 @@
2934
+ StorageLive(_4);
3035
+ StorageLive(_3);
3136
+ _3 = &_2;
32-
+ _4 = <fn() -> ! {sleep} as Fn<()>>::call(move _3, const ()) -> [return: bb1, unwind: bb5];
37+
+ StorageLive(_8);
38+
+ _8 = const ();
39+
+ goto -> bb1;
3340
+ }
3441
+
3542
+ bb1: {
36-
+ StorageDead(_3);
37-
+ StorageLive(_5);
38-
+ _5 = &_2;
39-
+ _6 = <fn() -> ! {sleep} as Fn<()>>::call(move _5, const ()) -> [return: bb2, unwind: bb4];
40-
+ }
41-
+
42-
+ bb2: {
43-
+ StorageDead(_5);
44-
+ StorageLive(_7);
45-
+ _7 = move _4;
46-
+ _1 = (move _7, copy _6);
47-
+ StorageDead(_7);
48-
+ StorageDead(_4);
49-
+ drop(_2) -> [return: bb3, unwind continue];
50-
+ }
51-
+
52-
+ bb3: {
53-
+ unreachable;
54-
+ }
55-
+
56-
+ bb4 (cleanup): {
57-
+ drop(_4) -> [return: bb5, unwind terminate(cleanup)];
58-
+ }
59-
+
60-
+ bb5 (cleanup): {
61-
+ drop(_2) -> [return: bb6, unwind terminate(cleanup)];
62-
+ }
63-
+
64-
+ bb6 (cleanup): {
65-
+ resume;
43+
+ goto -> bb1;
6644
}
6745
}
6846

tests/mir-opt/inline/inline_diverging.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub fn g(i: i32) -> u32 {
2626
pub fn h() {
2727
// CHECK-LABEL: fn h(
2828
// CHECK: (inlined call_twice::<!, fn() -> ! {sleep}>)
29-
// CHECK-NOT: inlined
29+
// CHECK: (inlined <fn() -> ! {sleep} as Fn<()>>::call - shim(fn() -> ! {sleep}))
30+
// CHECK: (inlined sleep)
3031
call_twice(sleep);
3132
}
3233

tests/mir-opt/inline/issue_78442.bar.Inline.panic-unwind.diff

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
let _4: fn() {foo};
1010
let mut _5: ();
1111
+ scope 1 (inlined hide_foo) {
12+
+ }
13+
+ scope 2 (inlined <fn() {foo} as Fn<()>>::call - shim(fn() {foo})) {
14+
+ scope 3 (inlined foo) {
15+
+ }
1216
+ }
1317

1418
bb0: {
@@ -23,33 +27,29 @@
2327
StorageLive(_5);
2428
_5 = ();
2529
- _2 = <fn() {foo} as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4];
26-
+ _2 = <fn() {foo} as Fn<()>>::call(move _3, move _5) -> [return: bb1, unwind: bb3];
27-
}
28-
30+
- }
31+
-
2932
- bb2: {
30-
+ bb1: {
3133
StorageDead(_5);
3234
StorageDead(_3);
3335
StorageDead(_4);
3436
StorageDead(_2);
3537
_0 = const ();
3638
- drop(_1) -> [return: bb3, unwind: bb5];
37-
+ drop(_1) -> [return: bb2, unwind: bb4];
39+
+ drop(_1) -> [return: bb1, unwind: bb2];
3840
}
3941

4042
- bb3: {
41-
+ bb2: {
43+
+ bb1: {
4244
return;
4345
}
4446

4547
- bb4 (cleanup): {
4648
- drop(_1) -> [return: bb5, unwind terminate(cleanup)];
47-
+ bb3 (cleanup): {
48-
+ drop(_1) -> [return: bb4, unwind terminate(cleanup)];
49-
}
50-
49+
- }
50+
-
5151
- bb5 (cleanup): {
52-
+ bb4 (cleanup): {
52+
+ bb2 (cleanup): {
5353
resume;
5454
}
5555
}

tests/mir-opt/inline/issue_78442.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ pub fn bar<P>(
99
_baz: P,
1010
) {
1111
// CHECK-LABEL: fn bar(
12-
// CHECK: let mut {{.*}}: &fn() {foo};
13-
// CHECK: let {{.*}}: fn() {foo};
1412
// CHECK: (inlined hide_foo)
15-
// CHECK-NOT: inlined
13+
// CHECK: (inlined <fn() {foo} as Fn<()>>::call - shim(fn() {foo}))
14+
// CHECK: (inlined foo)
1615
hide_foo()();
1716
}
1817

0 commit comments

Comments
 (0)