Skip to content

Commit 3c9549b

Browse files
Explicitly don't inline user-written rust-call fns
1 parent 0391af0 commit 3c9549b

13 files changed

+102
-201
lines changed

compiler/rustc_mir_transform/src/inline.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,14 @@ impl<'tcx> Inliner<'tcx> {
222222
trace!(?output_type, ?destination_ty);
223223
return Err("failed to normalize return type");
224224
}
225-
if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
225+
if callsite.fn_sig.abi() == Abi::RustCall {
226+
// FIXME: Don't inline user-written `extern "rust-call"` functions,
227+
// since this is generally perf-negative on rustc, and we hope that
228+
// LLVM will inline these functions instead.
229+
if callee_body.spread_arg.is_some() {
230+
return Err("do not inline user-written rust-call functions");
231+
}
232+
226233
let (self_arg, arg_tuple) = match &args[..] {
227234
[arg_tuple] => (None, arg_tuple),
228235
[self_arg, arg_tuple] => (Some(self_arg), arg_tuple),

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {main};
8-
+ let mut _5: ();
98
+ scope 1 (inlined f::<fn() {main}>) {
109
+ debug g => _2;
1110
+ let mut _3: &fn() {main};
1211
+ let _4: ();
13-
+ scope 2 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
14-
+ }
1512
+ }
1613

1714
bb0: {
@@ -22,9 +19,7 @@
2219
+ StorageLive(_4);
2320
+ StorageLive(_3);
2421
+ _3 = &_2;
25-
+ StorageLive(_5);
26-
+ _5 = const ();
27-
+ _4 = move (*_3)() -> [return: bb2, unwind unreachable];
22+
+ _4 = <fn() {main} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind unreachable];
2823
}
2924

3025
bb1: {
@@ -36,7 +31,6 @@
3631
+ }
3732
+
3833
+ bb2: {
39-
+ StorageDead(_5);
4034
+ StorageDead(_3);
4135
+ drop(_2) -> [return: bb1, unwind unreachable];
4236
}

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

+7-13
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {main};
8-
+ let mut _5: ();
98
+ scope 1 (inlined f::<fn() {main}>) {
109
+ debug g => _2;
1110
+ let mut _3: &fn() {main};
1211
+ let _4: ();
13-
+ scope 2 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
14-
+ }
1512
+ }
1613

1714
bb0: {
@@ -22,9 +19,7 @@
2219
+ StorageLive(_4);
2320
+ StorageLive(_3);
2421
+ _3 = &_2;
25-
+ StorageLive(_5);
26-
+ _5 = const ();
27-
+ _4 = move (*_3)() -> [return: bb4, unwind: bb2];
22+
+ _4 = <fn() {main} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind: bb3];
2823
}
2924

3025
bb1: {
@@ -35,18 +30,17 @@
3530
return;
3631
+ }
3732
+
38-
+ bb2 (cleanup): {
39-
+ drop(_2) -> [return: bb3, unwind terminate];
33+
+ bb2: {
34+
+ StorageDead(_3);
35+
+ drop(_2) -> [return: bb1, unwind continue];
4036
+ }
4137
+
4238
+ bb3 (cleanup): {
43-
+ resume;
39+
+ drop(_2) -> [return: bb4, unwind terminate];
4440
+ }
4541
+
46-
+ bb4: {
47-
+ StorageDead(_5);
48-
+ StorageDead(_3);
49-
+ drop(_2) -> [return: bb1, unwind continue];
42+
+ bb4 (cleanup): {
43+
+ resume;
5044
}
5145
}
5246

tests/mir-opt/inline/cycle.main.Inline.panic-abort.diff

+1-17
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {g};
8-
+ let mut _5: ();
98
+ scope 1 (inlined f::<fn() {g}>) {
109
+ debug g => _2;
1110
+ let mut _3: &fn() {g};
1211
+ let _4: ();
13-
+ scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) {
14-
+ scope 3 (inlined g) {
15-
+ scope 4 (inlined f::<fn() {main}>) {
16-
+ debug g => main;
17-
+ let _6: ();
18-
+ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
19-
+ }
20-
+ }
21-
+ }
22-
+ }
2312
+ }
2413

2514
bb0: {
@@ -30,10 +19,7 @@
3019
+ StorageLive(_4);
3120
+ StorageLive(_3);
3221
+ _3 = &_2;
33-
+ StorageLive(_5);
34-
+ _5 = const ();
35-
+ StorageLive(_6);
36-
+ _6 = main() -> [return: bb2, unwind unreachable];
22+
+ _4 = <fn() {g} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind unreachable];
3723
}
3824

3925
bb1: {
@@ -45,8 +31,6 @@
4531
+ }
4632
+
4733
+ bb2: {
48-
+ StorageDead(_6);
49-
+ StorageDead(_5);
5034
+ StorageDead(_3);
5135
+ drop(_2) -> [return: bb1, unwind unreachable];
5236
}

tests/mir-opt/inline/cycle.main.Inline.panic-unwind.diff

+7-23
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {g};
8-
+ let mut _5: ();
98
+ scope 1 (inlined f::<fn() {g}>) {
109
+ debug g => _2;
1110
+ let mut _3: &fn() {g};
1211
+ let _4: ();
13-
+ scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) {
14-
+ scope 3 (inlined g) {
15-
+ scope 4 (inlined f::<fn() {main}>) {
16-
+ debug g => main;
17-
+ let _6: ();
18-
+ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
19-
+ }
20-
+ }
21-
+ }
22-
+ }
2312
+ }
2413

2514
bb0: {
@@ -30,10 +19,7 @@
3019
+ StorageLive(_4);
3120
+ StorageLive(_3);
3221
+ _3 = &_2;
33-
+ StorageLive(_5);
34-
+ _5 = const ();
35-
+ StorageLive(_6);
36-
+ _6 = main() -> [return: bb4, unwind: bb2];
22+
+ _4 = <fn() {g} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind: bb3];
3723
}
3824

3925
bb1: {
@@ -44,19 +30,17 @@
4430
return;
4531
+ }
4632
+
47-
+ bb2 (cleanup): {
48-
+ drop(_2) -> [return: bb3, unwind terminate];
33+
+ bb2: {
34+
+ StorageDead(_3);
35+
+ drop(_2) -> [return: bb1, unwind continue];
4936
+ }
5037
+
5138
+ bb3 (cleanup): {
52-
+ resume;
39+
+ drop(_2) -> [return: bb4, unwind terminate];
5340
+ }
5441
+
55-
+ bb4: {
56-
+ StorageDead(_6);
57-
+ StorageDead(_5);
58-
+ StorageDead(_3);
59-
+ drop(_2) -> [return: bb1, unwind continue];
42+
+ bb4 (cleanup): {
43+
+ resume;
6044
}
6145
}
6246

tests/mir-opt/inline/dont_ice_on_generic_rust_call.call.Inline.diff

+5-22
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,23 @@
77
let mut _0: ();
88
let mut _3: &mut std::boxed::Box<dyn std::ops::FnMut<I, Output = ()>>;
99
let mut _4: I;
10-
+ scope 1 (inlined <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut) {
11-
+ debug self => _3;
12-
+ debug args => _4;
13-
+ let mut _5: &mut dyn std::ops::FnMut<I, Output = ()>;
14-
+ let mut _6: std::boxed::Box<dyn std::ops::FnMut<I, Output = ()>>;
15-
+ let mut _7: *const dyn std::ops::FnMut<I, Output = ()>;
16-
+ }
1710

1811
bb0: {
1912
StorageLive(_3);
2013
_3 = &mut _1;
2114
StorageLive(_4);
2215
_4 = move _2;
23-
- _0 = <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut(move _3, move _4) -> [return: bb1, unwind unreachable];
24-
+ StorageLive(_5);
25-
+ _6 = deref_copy (*_3);
26-
+ _7 = (((_6.0: std::ptr::Unique<dyn std::ops::FnMut<I, Output = ()>>).0: std::ptr::NonNull<dyn std::ops::FnMut<I, Output = ()>>).0: *const dyn std::ops::FnMut<I, Output = ()>);
27-
+ _5 = &mut (*_7);
28-
+ _0 = <dyn FnMut<I, Output = ()> as FnMut<I>>::call_mut(move _5, move _4) -> [return: bb2, unwind unreachable];
16+
_0 = <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut(move _3, move _4) -> [return: bb1, unwind unreachable];
2917
}
3018

3119
bb1: {
32-
- StorageDead(_4);
33-
- StorageDead(_3);
34-
- drop(_1) -> [return: bb2, unwind unreachable];
35-
+ return;
20+
StorageDead(_4);
21+
StorageDead(_3);
22+
drop(_1) -> [return: bb2, unwind unreachable];
3623
}
3724

3825
bb2: {
39-
- return;
40-
+ StorageDead(_5);
41-
+ StorageDead(_4);
42-
+ StorageDead(_3);
43-
+ drop(_1) -> [return: bb1, unwind unreachable];
26+
return;
4427
}
4528
}
4629

tests/mir-opt/inline/inline_box_fn.call.Inline.diff

+6-24
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,26 @@
77
let _2: ();
88
let mut _3: &std::boxed::Box<dyn std::ops::Fn(i32)>;
99
let mut _4: (i32,);
10-
+ scope 1 (inlined <Box<dyn Fn(i32)> as Fn<(i32,)>>::call) {
11-
+ debug self => _3;
12-
+ debug args => _4;
13-
+ let mut _5: &dyn std::ops::Fn(i32);
14-
+ let mut _6: std::boxed::Box<dyn std::ops::Fn(i32)>;
15-
+ let mut _7: *const dyn std::ops::Fn(i32);
16-
+ }
1710

1811
bb0: {
1912
StorageLive(_2);
2013
StorageLive(_3);
2114
_3 = &_1;
2215
StorageLive(_4);
2316
_4 = (const 1_i32,);
24-
- _2 = <Box<dyn Fn(i32)> as Fn<(i32,)>>::call(move _3, move _4) -> [return: bb1, unwind unreachable];
25-
+ StorageLive(_5);
26-
+ _6 = deref_copy (*_3);
27-
+ _7 = (((_6.0: std::ptr::Unique<dyn std::ops::Fn(i32)>).0: std::ptr::NonNull<dyn std::ops::Fn(i32)>).0: *const dyn std::ops::Fn(i32));
28-
+ _5 = &(*_7);
29-
+ _2 = <dyn Fn(i32) as Fn<(i32,)>>::call(move _5, move _4) -> [return: bb2, unwind unreachable];
17+
_2 = <Box<dyn Fn(i32)> as Fn<(i32,)>>::call(move _3, move _4) -> [return: bb1, unwind unreachable];
3018
}
3119

3220
bb1: {
33-
+ return;
34-
+ }
35-
+
36-
+ bb2: {
37-
+ StorageDead(_5);
3821
StorageDead(_4);
3922
StorageDead(_3);
4023
StorageDead(_2);
4124
_0 = const ();
42-
- drop(_1) -> [return: bb2, unwind unreachable];
43-
- }
44-
-
45-
- bb2: {
46-
- return;
47-
+ drop(_1) -> [return: bb1, unwind unreachable];
25+
drop(_1) -> [return: bb2, unwind unreachable];
26+
}
27+
28+
bb2: {
29+
return;
4830
}
4931
}
5032

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

+1-17
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,9 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {f};
8-
+ let mut _4: ();
98
+ scope 1 (inlined call::<fn() {f}>) {
109
+ debug f => _2;
1110
+ let _3: ();
12-
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
13-
+ scope 3 (inlined f) {
14-
+ scope 4 (inlined call::<fn() {f}>) {
15-
+ debug f => f;
16-
+ let _5: ();
17-
+ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
18-
+ }
19-
+ }
20-
+ }
21-
+ }
2211
+ }
2312

2413
bb0: {
@@ -27,15 +16,10 @@
2716
+ StorageLive(_2);
2817
+ _2 = f;
2918
+ StorageLive(_3);
30-
+ StorageLive(_4);
31-
+ _4 = const ();
32-
+ StorageLive(_5);
33-
+ _5 = f() -> [return: bb1, unwind unreachable];
19+
+ _3 = <fn() {f} as FnOnce<()>>::call_once(move _2, const ()) -> [return: bb1, unwind unreachable];
3420
}
3521

3622
bb1: {
37-
+ StorageDead(_5);
38-
+ StorageDead(_4);
3923
+ StorageDead(_3);
4024
+ StorageDead(_2);
4125
StorageDead(_1);

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

+1-17
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,9 @@
55
let mut _0: ();
66
let _1: ();
77
+ let mut _2: fn() {f};
8-
+ let mut _4: ();
98
+ scope 1 (inlined call::<fn() {f}>) {
109
+ debug f => _2;
1110
+ let _3: ();
12-
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
13-
+ scope 3 (inlined f) {
14-
+ scope 4 (inlined call::<fn() {f}>) {
15-
+ debug f => f;
16-
+ let _5: ();
17-
+ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
18-
+ }
19-
+ }
20-
+ }
21-
+ }
2211
+ }
2312

2413
bb0: {
@@ -27,15 +16,10 @@
2716
+ StorageLive(_2);
2817
+ _2 = f;
2918
+ StorageLive(_3);
30-
+ StorageLive(_4);
31-
+ _4 = const ();
32-
+ StorageLive(_5);
33-
+ _5 = f() -> [return: bb1, unwind continue];
19+
+ _3 = <fn() {f} as FnOnce<()>>::call_once(move _2, const ()) -> [return: bb1, unwind continue];
3420
}
3521

3622
bb1: {
37-
+ StorageDead(_5);
38-
+ StorageDead(_4);
3923
+ StorageDead(_3);
4024
+ StorageDead(_2);
4125
StorageDead(_1);

0 commit comments

Comments
 (0)