Skip to content

Commit 2e4bdd0

Browse files
committed
Lower inling cost for a statement.
1 parent 5aa23be commit 2e4bdd0

20 files changed

+5145
-475
lines changed

compiler/rustc_mir_transform/src/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::ops::{Range, RangeFrom};
2222

2323
pub(crate) mod cycle;
2424

25-
const INSTR_COST: usize = 5;
25+
const INSTR_COST: usize = 1;
2626
const CALL_PENALTY: usize = 25;
2727
const LANDINGPAD_PENALTY: usize = 50;
2828
const RESUME_PENALTY: usize = 45;

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

+693-3
Large diffs are not rendered by default.

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

+11-185
Original file line numberDiff line numberDiff line change
@@ -10,108 +10,6 @@
1010
}
1111
+ scope 2 (inlined Vec::<u32>::new) {
1212
+ let mut _3: alloc::raw_vec::RawVec<u32>;
13-
+ }
14-
+ scope 3 (inlined Box::<Vec<u32>>::new) {
15-
+ debug x => _2;
16-
+ let mut _4: usize;
17-
+ let mut _5: usize;
18-
+ let mut _6: *mut u8;
19-
+ let mut _7: *const std::vec::Vec<u32>;
20-
+ scope 4 {
21-
+ scope 5 (inlined alloc::alloc::exchange_malloc) {
22-
+ debug size => _4;
23-
+ debug align => _5;
24-
+ let _8: std::alloc::Layout;
25-
+ let mut _9: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
26-
+ let mut _10: isize;
27-
+ let mut _12: !;
28-
+ scope 6 {
29-
+ debug layout => _8;
30-
+ let _11: std::ptr::NonNull<[u8]>;
31-
+ let mut _13: &std::alloc::Global;
32-
+ scope 8 {
33-
+ debug ptr => _11;
34-
+ scope 18 (inlined NonNull::<[u8]>::as_mut_ptr) {
35-
+ debug self => _11;
36-
+ let mut _15: std::ptr::NonNull<u8>;
37-
+ scope 19 (inlined NonNull::<[u8]>::as_non_null_ptr) {
38-
+ debug self => _11;
39-
+ let mut _16: *mut u8;
40-
+ let mut _17: *mut [u8];
41-
+ scope 20 {
42-
+ scope 21 (inlined NonNull::<[u8]>::as_ptr) {
43-
+ debug self => _11;
44-
+ let mut _18: *const [u8];
45-
+ }
46-
+ scope 22 (inlined ptr::mut_ptr::<impl *mut [u8]>::as_mut_ptr) {
47-
+ debug self => _17;
48-
+ }
49-
+ scope 23 (inlined NonNull::<u8>::new_unchecked) {
50-
+ debug ptr => _16;
51-
+ let mut _19: *const u8;
52-
+ scope 24 {
53-
+ scope 25 (inlined NonNull::<T>::new_unchecked::runtime::<u8>) {
54-
+ debug ptr => _16;
55-
+ scope 26 (inlined ptr::mut_ptr::<impl *mut u8>::is_null) {
56-
+ debug self => _16;
57-
+ let mut _20: *mut u8;
58-
+ scope 27 {
59-
+ scope 28 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) {
60-
+ debug ptr => _20;
61-
+ scope 29 (inlined ptr::mut_ptr::<impl *mut u8>::addr) {
62-
+ debug self => _20;
63-
+ scope 30 {
64-
+ scope 31 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) {
65-
+ debug self => _20;
66-
+ }
67-
+ }
68-
+ }
69-
+ }
70-
+ }
71-
+ }
72-
+ }
73-
+ }
74-
+ }
75-
+ }
76-
+ }
77-
+ scope 32 (inlined NonNull::<u8>::as_ptr) {
78-
+ debug self => _15;
79-
+ let mut _21: *const u8;
80-
+ }
81-
+ }
82-
+ }
83-
+ scope 17 (inlined <std::alloc::Global as Allocator>::allocate) {
84-
+ debug self => const _;
85-
+ debug layout => _8;
86-
+ }
87-
+ }
88-
+ scope 7 {
89-
+ scope 9 (inlined Layout::from_size_align_unchecked) {
90-
+ debug size => _4;
91-
+ debug align => _5;
92-
+ let mut _14: std::ptr::Alignment;
93-
+ scope 10 {
94-
+ scope 11 (inlined std::ptr::Alignment::new_unchecked) {
95-
+ debug align => _5;
96-
+ scope 12 {
97-
+ scope 14 (inlined std::ptr::Alignment::new_unchecked::runtime) {
98-
+ debug align => _5;
99-
+ scope 15 (inlined core::num::<impl usize>::is_power_of_two) {
100-
+ debug self => _5;
101-
+ scope 16 (inlined core::num::<impl usize>::count_ones) {
102-
+ debug self => _5;
103-
+ }
104-
+ }
105-
+ }
106-
+ }
107-
+ scope 13 {
108-
+ }
109-
+ }
110-
+ }
111-
+ }
112-
+ }
113-
+ }
114-
+ }
11513
+ }
11614

11715
bb0: {
@@ -122,101 +20,29 @@
12220
+ _3 = const _;
12321
+ _2 = Vec::<u32> { buf: move _3, len: const 0_usize };
12422
+ StorageDead(_3);
125-
+ StorageLive(_4);
126-
+ StorageLive(_5);
127-
+ StorageLive(_6);
128-
+ StorageLive(_7);
129-
+ _4 = SizeOf(std::vec::Vec<u32>);
130-
+ _5 = AlignOf(std::vec::Vec<u32>);
131-
+ StorageLive(_8);
132-
+ StorageLive(_10);
133-
+ StorageLive(_11);
134-
+ StorageLive(_12);
135-
+ StorageLive(_13);
136-
+ StorageLive(_14);
137-
+ _14 = _5 as std::ptr::Alignment (Transmute);
138-
+ _8 = Layout { size: _4, align: move _14 };
139-
+ StorageDead(_14);
140-
+ StorageLive(_9);
141-
+ _13 = const _;
142-
+ _9 = std::alloc::Global::alloc_impl(move _13, _8, const false) -> [return: bb7, unwind: bb3];
23+
+ _1 = Box::<Vec<u32>>::new(move _2) -> [return: bb1, unwind: bb3];
14324
}
14425

14526
bb1: {
14627
- _1 = Box::<Vec<u32>>::new(move _2) -> [return: bb2, unwind: bb4];
147-
+ StorageDead(_1);
148-
+ return;
149-
}
150-
28+
- }
29+
-
15130
- bb2: {
152-
- StorageDead(_2);
153-
- _0 = const ();
31+
StorageDead(_2);
32+
_0 = const ();
15433
- drop(_1) -> [return: bb3, unwind: bb4];
155-
+ bb2 (cleanup): {
156-
+ resume;
34+
+ drop(_1) -> [return: bb2, unwind: bb3];
15735
}
15836

15937
- bb3: {
160-
- StorageDead(_1);
161-
- return;
162-
+ bb3 (cleanup): {
163-
+ drop(_2) -> [return: bb2, unwind terminate(cleanup)];
38+
+ bb2: {
39+
StorageDead(_1);
40+
return;
16441
}
16542

16643
- bb4 (cleanup): {
167-
- resume;
168-
+ bb4: {
169-
+ _12 = handle_alloc_error(move _8) -> bb3;
170-
+ }
171-
+
172-
+ bb5: {
173-
+ unreachable;
174-
+ }
175-
+
176-
+ bb6: {
177-
+ _11 = ((_9 as Ok).0: std::ptr::NonNull<[u8]>);
178-
+ StorageLive(_15);
179-
+ StorageLive(_16);
180-
+ StorageLive(_17);
181-
+ StorageLive(_18);
182-
+ _18 = (_11.0: *const [u8]);
183-
+ _17 = move _18 as *mut [u8] (PtrToPtr);
184-
+ StorageDead(_18);
185-
+ _16 = _17 as *mut u8 (PtrToPtr);
186-
+ StorageDead(_17);
187-
+ StorageLive(_19);
188-
+ StorageLive(_20);
189-
+ _19 = _16 as *const u8 (PointerCoercion(MutToConstPointer));
190-
+ _15 = NonNull::<u8> { pointer: _19 };
191-
+ StorageDead(_20);
192-
+ StorageDead(_19);
193-
+ StorageDead(_16);
194-
+ StorageLive(_21);
195-
+ _21 = (_15.0: *const u8);
196-
+ _6 = move _21 as *mut u8 (PtrToPtr);
197-
+ StorageDead(_21);
198-
+ StorageDead(_15);
199-
+ StorageDead(_9);
200-
+ StorageDead(_13);
201-
+ StorageDead(_12);
202-
+ StorageDead(_11);
203-
+ StorageDead(_10);
204-
+ StorageDead(_8);
205-
+ _1 = ShallowInitBox(move _6, std::vec::Vec<u32>);
206-
+ _7 = (((_1.0: std::ptr::Unique<std::vec::Vec<u32>>).0: std::ptr::NonNull<std::vec::Vec<u32>>).0: *const std::vec::Vec<u32>);
207-
+ (*_7) = move _2;
208-
+ StorageDead(_7);
209-
+ StorageDead(_6);
210-
+ StorageDead(_5);
211-
+ StorageDead(_4);
212-
+ StorageDead(_2);
213-
+ _0 = const ();
214-
+ drop(_1) -> [return: bb1, unwind: bb2];
215-
+ }
216-
+
217-
+ bb7: {
218-
+ _10 = discriminant(_9);
219-
+ switchInt(move _10) -> [0: bb6, 1: bb4, otherwise: bb5];
44+
+ bb3 (cleanup): {
45+
resume;
22046
}
22147
}
22248

tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff

+110-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,59 @@
1212
+ scope 3 (inlined std::ptr::drop_in_place::<Vec<A>> - shim(Some(Vec<A>))) {
1313
+ let mut _6: &mut std::vec::Vec<A>;
1414
+ let mut _7: ();
15+
+ scope 4 (inlined <Vec<A> as Drop>::drop) {
16+
+ debug self => _6;
17+
+ let mut _8: *mut [A];
18+
+ let mut _9: *mut A;
19+
+ let mut _10: usize;
20+
+ scope 5 {
21+
+ scope 6 (inlined Vec::<A>::as_mut_ptr) {
22+
+ debug self => _6;
23+
+ let mut _11: &alloc::raw_vec::RawVec<A>;
24+
+ scope 7 (inlined alloc::raw_vec::RawVec::<A>::ptr) {
25+
+ debug self => _11;
26+
+ let mut _13: std::ptr::NonNull<A>;
27+
+ scope 8 (inlined Unique::<A>::as_ptr) {
28+
+ debug ((self: Unique<A>).0: std::ptr::NonNull<A>) => _13;
29+
+ debug ((self: Unique<A>).1: std::marker::PhantomData<A>) => const ZeroSized: PhantomData<A>;
30+
+ scope 9 (inlined NonNull::<A>::as_ptr) {
31+
+ debug self => _13;
32+
+ let mut _12: *const A;
33+
+ }
34+
+ }
35+
+ }
36+
+ }
37+
+ scope 10 (inlined slice_from_raw_parts_mut::<A>) {
38+
+ debug data => _9;
39+
+ debug len => _10;
40+
+ let mut _14: *mut ();
41+
+ scope 11 (inlined ptr::mut_ptr::<impl *mut A>::cast::<()>) {
42+
+ debug self => _9;
43+
+ }
44+
+ scope 12 (inlined std::ptr::from_raw_parts_mut::<[A]>) {
45+
+ debug data_address => _14;
46+
+ debug metadata => _10;
47+
+ let mut _15: std::ptr::metadata::PtrRepr<[A]>;
48+
+ let mut _16: std::ptr::metadata::PtrComponents<[A]>;
49+
+ let mut _17: *const ();
50+
+ scope 13 {
51+
+ }
52+
+ }
53+
+ }
54+
+ scope 14 (inlined std::ptr::drop_in_place::<[A]> - shim(Some([A]))) {
55+
+ let mut _18: usize;
56+
+ let mut _19: usize;
57+
+ let mut _20: *mut A;
58+
+ let mut _21: bool;
59+
+ }
60+
+ }
61+
+ }
1562
+ }
1663
}
1764
scope 2 {
18-
+ scope 4 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
19-
+ let mut _8: isize;
20-
+ let mut _9: isize;
65+
+ scope 15 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
66+
+ let mut _22: isize;
67+
+ let mut _23: isize;
2168
+ }
2269
}
2370

@@ -29,7 +76,42 @@
2976
+ StorageLive(_6);
3077
+ StorageLive(_7);
3178
+ _6 = &mut (*_4);
32-
+ _7 = <Vec<A> as Drop>::drop(move _6) -> [return: bb2, unwind unreachable];
79+
+ StorageLive(_8);
80+
+ StorageLive(_9);
81+
+ StorageLive(_11);
82+
+ _11 = &((*_6).0: alloc::raw_vec::RawVec<A>);
83+
+ StorageLive(_13);
84+
+ _13 = ((((*_6).0: alloc::raw_vec::RawVec<A>).0: std::ptr::Unique<A>).0: std::ptr::NonNull<A>);
85+
+ StorageLive(_12);
86+
+ _12 = (_13.0: *const A);
87+
+ _9 = move _12 as *mut A (PtrToPtr);
88+
+ StorageDead(_12);
89+
+ StorageDead(_13);
90+
+ StorageDead(_11);
91+
+ StorageLive(_10);
92+
+ _10 = ((*_6).1: usize);
93+
+ StorageLive(_14);
94+
+ _14 = _9 as *mut () (PtrToPtr);
95+
+ StorageLive(_15);
96+
+ StorageLive(_16);
97+
+ StorageLive(_17);
98+
+ _17 = _14 as *const () (PointerCoercion(MutToConstPointer));
99+
+ _16 = ptr::metadata::PtrComponents::<[A]> { data_address: move _17, metadata: _10 };
100+
+ StorageDead(_17);
101+
+ _15 = ptr::metadata::PtrRepr::<[A]> { const_ptr: move _16 };
102+
+ StorageDead(_16);
103+
+ _8 = (_15.1: *mut [A]);
104+
+ StorageDead(_15);
105+
+ StorageDead(_14);
106+
+ StorageDead(_10);
107+
+ StorageDead(_9);
108+
+ StorageLive(_18);
109+
+ StorageLive(_19);
110+
+ StorageLive(_20);
111+
+ StorageLive(_21);
112+
+ _18 = Len((*_8));
113+
+ _19 = const 0_usize;
114+
+ goto -> bb4;
33115
}
34116

35117
bb1: {
@@ -40,25 +122,41 @@
40122
StorageLive(_5);
41123
_5 = _2;
42124
- _0 = std::ptr::drop_in_place::<Option<B>>(move _5) -> [return: bb2, unwind unreachable];
43-
+ StorageLive(_8);
44-
+ StorageLive(_9);
45-
+ _8 = discriminant((*_5));
46-
+ switchInt(move _8) -> [0: bb3, otherwise: bb4];
125+
+ StorageLive(_22);
126+
+ StorageLive(_23);
127+
+ _22 = discriminant((*_5));
128+
+ switchInt(move _22) -> [0: bb5, otherwise: bb6];
47129
}
48130

49131
bb2: {
132+
+ StorageDead(_21);
133+
+ StorageDead(_20);
134+
+ StorageDead(_19);
135+
+ StorageDead(_18);
136+
+ StorageDead(_8);
50137
+ drop(((*_4).0: alloc::raw_vec::RawVec<A>)) -> [return: bb1, unwind unreachable];
51138
+ }
52139
+
53140
+ bb3: {
54-
+ StorageDead(_9);
55-
+ StorageDead(_8);
141+
+ _20 = &raw mut (*_8)[_19];
142+
+ _19 = Add(move _19, const 1_usize);
143+
+ drop((*_20)) -> [return: bb4, unwind unreachable];
144+
+ }
145+
+
146+
+ bb4: {
147+
+ _21 = Eq(_19, _18);
148+
+ switchInt(move _21) -> [0: bb3, otherwise: bb2];
149+
+ }
150+
+
151+
+ bb5: {
152+
+ StorageDead(_23);
153+
+ StorageDead(_22);
56154
StorageDead(_5);
57155
return;
58156
+ }
59157
+
60-
+ bb4: {
61-
+ drop((((*_5) as Some).0: B)) -> [return: bb3, unwind unreachable];
158+
+ bb6: {
159+
+ drop((((*_5) as Some).0: B)) -> [return: bb5, unwind unreachable];
62160
}
63161
}
64162

0 commit comments

Comments
 (0)