Skip to content

Commit 9eee230

Browse files
committed
Auto merge of #112240 - cjgillot:recurse-inline, r=scottmcm
Only check inlining counter after recursing. This PR aims to reduce the strength of #105119 even more. In the current implementation, we check the inline count before recursing. This means that we never actually reach inlining depth 3. This PR checks the counter after recursion, to give a chance to inline at depth >= 3. r? `@scottmcm` cc `@JakobDegen`
2 parents d8095ac + 9e68344 commit 9eee230

11 files changed

+392
-268
lines changed

Diff for: compiler/rustc_mir_transform/src/inline.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,16 @@ impl<'tcx> Inliner<'tcx> {
145145
Ok(new_blocks) => {
146146
debug!("inlined {}", callsite.callee);
147147
self.changed = true;
148+
149+
self.history.push(callsite.callee.def_id());
150+
self.process_blocks(caller_body, new_blocks);
151+
self.history.pop();
152+
148153
inlined_count += 1;
149154
if inlined_count == inline_limit {
155+
debug!("inline count reached");
150156
return;
151157
}
152-
self.history.push(callsite.callee.def_id());
153-
self.process_blocks(caller_body, new_blocks);
154-
self.history.pop();
155158
}
156159
}
157160
}

Diff for: tests/mir-opt/inline/cycle.main.Inline.diff

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
+ let mut _3: &fn() {g}; // in scope 1 at $DIR/cycle.rs:6:5: 6:6
1212
+ let _4: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8
1313
+ scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) { // at $DIR/cycle.rs:6:5: 6:8
14+
+ scope 3 (inlined g) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL
15+
+ scope 4 (inlined f::<fn() {main}>) { // at $DIR/cycle.rs:12:5: 12:12
16+
+ debug g => main; // in scope 4 at $DIR/cycle.rs:5:6: 5:7
17+
+ let _6: (); // in scope 4 at $DIR/cycle.rs:6:5: 6:8
18+
+ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) { // at $DIR/cycle.rs:6:5: 6:8
19+
+ }
20+
+ }
21+
+ }
1422
+ }
1523
+ }
1624

@@ -30,7 +38,11 @@
3038
+ _3 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6
3139
+ StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
3240
+ _5 = const (); // scope 1 at $DIR/cycle.rs:6:5: 6:8
33-
+ _4 = move (*_3)() -> [return: bb4, unwind: bb2]; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
41+
+ StorageLive(_6); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
42+
+ _6 = main() -> [return: bb4, unwind: bb2]; // scope 5 at $SRC_DIR/core/src/ops/function.rs:LL:COL
43+
+ // mir::Constant
44+
+ // + span: no-location
45+
+ // + literal: Const { ty: fn() {main}, val: Value(<ZST>) }
3446
}
3547

3648
bb1: {
@@ -50,6 +62,7 @@
5062
+ }
5163
+
5264
+ bb4: {
65+
+ StorageDead(_6); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
5366
+ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
5467
+ StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:7: 6:8
5568
+ drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2

Diff for: tests/mir-opt/inline/exponential_runtime.main.Inline.diff

+103-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
+ let _5: (); // in scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
1313
+ let _6: (); // in scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25
1414
+ let _7: (); // in scope 2 at $DIR/exponential_runtime.rs:64:9: 64:25
15+
+ scope 3 (inlined <() as E>::call) { // at $DIR/exponential_runtime.rs:62:9: 62:25
16+
+ let _8: (); // in scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
17+
+ let _9: (); // in scope 3 at $DIR/exponential_runtime.rs:51:9: 51:25
18+
+ let _10: (); // in scope 3 at $DIR/exponential_runtime.rs:52:9: 52:25
19+
+ scope 4 (inlined <() as D>::call) { // at $DIR/exponential_runtime.rs:50:9: 50:25
20+
+ let _11: (); // in scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
21+
+ let _12: (); // in scope 4 at $DIR/exponential_runtime.rs:39:9: 39:25
22+
+ let _13: (); // in scope 4 at $DIR/exponential_runtime.rs:40:9: 40:25
23+
+ scope 5 (inlined <() as C>::call) { // at $DIR/exponential_runtime.rs:38:9: 38:25
24+
+ let _14: (); // in scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
25+
+ let _15: (); // in scope 5 at $DIR/exponential_runtime.rs:27:9: 27:25
26+
+ let _16: (); // in scope 5 at $DIR/exponential_runtime.rs:28:9: 28:25
27+
+ scope 6 (inlined <() as B>::call) { // at $DIR/exponential_runtime.rs:26:9: 26:25
28+
+ let _17: (); // in scope 6 at $DIR/exponential_runtime.rs:14:9: 14:25
29+
+ let _18: (); // in scope 6 at $DIR/exponential_runtime.rs:15:9: 15:25
30+
+ let _19: (); // in scope 6 at $DIR/exponential_runtime.rs:16:9: 16:25
31+
+ }
32+
+ }
33+
+ }
34+
+ }
1535
+ }
1636
+ }
1737

@@ -24,12 +44,24 @@
2444
+ StorageLive(_5); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
2545
+ StorageLive(_6); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
2646
+ StorageLive(_7); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
27-
+ _5 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
47+
+ StorageLive(_8); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
48+
+ StorageLive(_9); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
49+
+ StorageLive(_10); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
50+
+ StorageLive(_11); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
51+
+ StorageLive(_12); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
52+
+ StorageLive(_13); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
53+
+ StorageLive(_14); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
54+
+ StorageLive(_15); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
55+
+ StorageLive(_16); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
56+
+ StorageLive(_17); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
57+
+ StorageLive(_18); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
58+
+ StorageLive(_19); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
59+
+ _17 = <() as A>::call() -> bb12; // scope 6 at $DIR/exponential_runtime.rs:14:9: 14:25
2860
// mir::Constant
2961
- // + span: $DIR/exponential_runtime.rs:87:5: 87:20
3062
- // + literal: Const { ty: fn() {<() as G>::call}, val: Value(<ZST>) }
31-
+ // + span: $DIR/exponential_runtime.rs:62:9: 62:23
32-
+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) }
63+
+ // + span: $DIR/exponential_runtime.rs:14:9: 14:23
64+
+ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) }
3365
}
3466

3567
bb1: {
@@ -59,6 +91,9 @@
5991
+ }
6092
+
6193
+ bb4: {
94+
+ StorageDead(_10); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
95+
+ StorageDead(_9); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
96+
+ StorageDead(_8); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
6297
+ _6 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25
6398
+ // mir::Constant
6499
+ // + span: $DIR/exponential_runtime.rs:63:9: 63:23
@@ -70,6 +105,71 @@
70105
+ // mir::Constant
71106
+ // + span: $DIR/exponential_runtime.rs:64:9: 64:23
72107
+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) }
108+
+ }
109+
+
110+
+ bb6: {
111+
+ StorageDead(_13); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
112+
+ StorageDead(_12); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
113+
+ StorageDead(_11); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25
114+
+ _9 = <() as D>::call() -> bb7; // scope 3 at $DIR/exponential_runtime.rs:51:9: 51:25
115+
+ // mir::Constant
116+
+ // + span: $DIR/exponential_runtime.rs:51:9: 51:23
117+
+ // + literal: Const { ty: fn() {<() as D>::call}, val: Value(<ZST>) }
118+
+ }
119+
+
120+
+ bb7: {
121+
+ _10 = <() as D>::call() -> bb4; // scope 3 at $DIR/exponential_runtime.rs:52:9: 52:25
122+
+ // mir::Constant
123+
+ // + span: $DIR/exponential_runtime.rs:52:9: 52:23
124+
+ // + literal: Const { ty: fn() {<() as D>::call}, val: Value(<ZST>) }
125+
+ }
126+
+
127+
+ bb8: {
128+
+ StorageDead(_16); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
129+
+ StorageDead(_15); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
130+
+ StorageDead(_14); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25
131+
+ _12 = <() as C>::call() -> bb9; // scope 4 at $DIR/exponential_runtime.rs:39:9: 39:25
132+
+ // mir::Constant
133+
+ // + span: $DIR/exponential_runtime.rs:39:9: 39:23
134+
+ // + literal: Const { ty: fn() {<() as C>::call}, val: Value(<ZST>) }
135+
+ }
136+
+
137+
+ bb9: {
138+
+ _13 = <() as C>::call() -> bb6; // scope 4 at $DIR/exponential_runtime.rs:40:9: 40:25
139+
+ // mir::Constant
140+
+ // + span: $DIR/exponential_runtime.rs:40:9: 40:23
141+
+ // + literal: Const { ty: fn() {<() as C>::call}, val: Value(<ZST>) }
142+
+ }
143+
+
144+
+ bb10: {
145+
+ StorageDead(_19); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
146+
+ StorageDead(_18); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
147+
+ StorageDead(_17); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25
148+
+ _15 = <() as B>::call() -> bb11; // scope 5 at $DIR/exponential_runtime.rs:27:9: 27:25
149+
+ // mir::Constant
150+
+ // + span: $DIR/exponential_runtime.rs:27:9: 27:23
151+
+ // + literal: Const { ty: fn() {<() as B>::call}, val: Value(<ZST>) }
152+
+ }
153+
+
154+
+ bb11: {
155+
+ _16 = <() as B>::call() -> bb8; // scope 5 at $DIR/exponential_runtime.rs:28:9: 28:25
156+
+ // mir::Constant
157+
+ // + span: $DIR/exponential_runtime.rs:28:9: 28:23
158+
+ // + literal: Const { ty: fn() {<() as B>::call}, val: Value(<ZST>) }
159+
+ }
160+
+
161+
+ bb12: {
162+
+ _18 = <() as A>::call() -> bb13; // scope 6 at $DIR/exponential_runtime.rs:15:9: 15:25
163+
+ // mir::Constant
164+
+ // + span: $DIR/exponential_runtime.rs:15:9: 15:23
165+
+ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) }
166+
+ }
167+
+
168+
+ bb13: {
169+
+ _19 = <() as A>::call() -> bb10; // scope 6 at $DIR/exponential_runtime.rs:16:9: 16:25
170+
+ // mir::Constant
171+
+ // + span: $DIR/exponential_runtime.rs:16:9: 16:23
172+
+ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) }
73173
}
74174
}
75175

Diff for: tests/mir-opt/inline/inline_cycle.two.Inline.diff

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
+ debug f => _2; // in scope 1 at $DIR/inline_cycle.rs:54:22: 54:23
1111
+ let _3: (); // in scope 1 at $DIR/inline_cycle.rs:55:5: 55:8
1212
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) { // at $DIR/inline_cycle.rs:55:5: 55:8
13+
+ scope 3 (inlined f) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL
14+
+ scope 4 (inlined call::<fn() {f}>) { // at $DIR/inline_cycle.rs:60:5: 60:12
15+
+ debug f => f; // in scope 4 at $DIR/inline_cycle.rs:54:22: 54:23
16+
+ let _5: (); // in scope 4 at $DIR/inline_cycle.rs:55:5: 55:8
17+
+ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) { // at $DIR/inline_cycle.rs:55:5: 55:8
18+
+ }
19+
+ }
20+
+ }
1321
+ }
1422
+ }
1523

@@ -27,10 +35,15 @@
2735
+ StorageLive(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12
2836
+ StorageLive(_4); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8
2937
+ _4 = const (); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8
30-
+ _3 = move _2() -> bb1; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
38+
+ StorageLive(_5); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
39+
+ _5 = f() -> bb1; // scope 5 at $SRC_DIR/core/src/ops/function.rs:LL:COL
40+
+ // mir::Constant
41+
+ // + span: no-location
42+
+ // + literal: Const { ty: fn() {f}, val: Value(<ZST>) }
3143
}
3244

3345
bb1: {
46+
+ StorageDead(_5); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
3447
+ StorageDead(_4); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8
3548
+ StorageDead(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12
3649
+ StorageDead(_2); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12

Diff for: tests/mir-opt/inline/inline_cycle_generic.main.Inline.diff

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
let _1: (); // in scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24
77
+ scope 1 (inlined <C as Call>::call) { // at $DIR/inline_cycle_generic.rs:10:5: 10:24
88
+ scope 2 (inlined <B<A> as Call>::call) { // at $DIR/inline_cycle_generic.rs:39:9: 39:31
9+
+ scope 3 (inlined <A as Call>::call) { // at $DIR/inline_cycle_generic.rs:32:9: 32:28
10+
+ }
911
+ }
1012
+ }
1113

1214
bb0: {
1315
StorageLive(_1); // scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24
1416
- _1 = <C as Call>::call() -> bb1; // scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24
15-
+ _1 = <A as Call>::call() -> bb1; // scope 2 at $DIR/inline_cycle_generic.rs:32:9: 32:28
17+
+ _1 = <B<C> as Call>::call() -> bb1; // scope 3 at $DIR/inline_cycle_generic.rs:24:9: 24:31
1618
// mir::Constant
1719
- // + span: $DIR/inline_cycle_generic.rs:10:5: 10:22
1820
- // + literal: Const { ty: fn() {<C as Call>::call}, val: Value(<ZST>) }
19-
+ // + span: $DIR/inline_cycle_generic.rs:32:9: 32:26
20-
+ // + literal: Const { ty: fn() {<A as Call>::call}, val: Value(<ZST>) }
21+
+ // + span: $DIR/inline_cycle_generic.rs:24:9: 24:29
22+
+ // + literal: Const { ty: fn() {<B<C> as Call>::call}, val: Value(<ZST>) }
2123
}
2224

2325
bb1: {

Diff for: tests/mir-opt/inline/inline_diverging.h.Inline.diff

+4-35
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
+ }
2121
+ }
2222
+ scope 4 (inlined <fn() -> ! {sleep} as Fn<()>>::call - shim(fn() -> ! {sleep})) { // at $DIR/inline_diverging.rs:27:13: 27:16
23+
+ scope 5 (inlined sleep) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL
24+
+ }
2325
+ }
2426
+ }
2527

@@ -40,44 +42,11 @@
4042
+ _3 = &_2; // scope 1 at $DIR/inline_diverging.rs:27:13: 27:14
4143
+ StorageLive(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16
4244
+ _8 = const (); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16
43-
+ _4 = move (*_3)() -> [return: bb6, unwind: bb4]; // scope 4 at $SRC_DIR/core/src/ops/function.rs:LL:COL
45+
+ goto -> bb1; // scope 5 at $DIR/inline_diverging.rs:39:5: 39:12
4446
+ }
4547
+
4648
+ bb1: {
47-
+ StorageDead(_5); // scope 2 at $DIR/inline_diverging.rs:28:15: 28:16
48-
+ StorageLive(_7); // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7
49-
+ _7 = move _4; // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7
50-
+ _1 = (move _7, move _6); // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11
51-
+ StorageDead(_7); // scope 3 at $DIR/inline_diverging.rs:29:10: 29:11
52-
+ StorageDead(_4); // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2
53-
+ drop(_2) -> bb2; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2
54-
+ }
55-
+
56-
+ bb2: {
57-
+ unreachable; // scope 0 at $DIR/inline_diverging.rs:30:2: 30:2
58-
+ }
59-
+
60-
+ bb3 (cleanup): {
61-
+ drop(_4) -> [return: bb4, unwind terminate]; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2
62-
+ }
63-
+
64-
+ bb4 (cleanup): {
65-
+ drop(_2) -> [return: bb5, unwind terminate]; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2
66-
+ }
67-
+
68-
+ bb5 (cleanup): {
69-
+ resume; // scope 1 at $DIR/inline_diverging.rs:26:1: 30:2
70-
+ }
71-
+
72-
+ bb6: {
73-
+ StorageDead(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16
74-
+ StorageDead(_3); // scope 1 at $DIR/inline_diverging.rs:27:15: 27:16
75-
+ StorageLive(_5); // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14
76-
+ _5 = &_2; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14
77-
+ _6 = <fn() -> ! {sleep} as Fn<()>>::call(move _5, const ()) -> [return: bb1, unwind: bb3]; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:16
78-
+ // mir::Constant
79-
+ // + span: $DIR/inline_diverging.rs:28:13: 28:14
80-
+ // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a fn() -> ! {sleep}, ()) -> <fn() -> ! {sleep} as FnOnce<()>>::Output {<fn() -> ! {sleep} as Fn<()>>::call}, val: Value(<ZST>) }
49+
+ goto -> bb1; // scope 5 at $DIR/inline_diverging.rs:39:5: 39:12
8150
}
8251
}
8352

0 commit comments

Comments
 (0)