Skip to content

Commit 9ed30a0

Browse files
committed
What if we discourage mir-inlining of box_new_uninit?
1 parent 66300b6 commit 9ed30a0

3 files changed

Lines changed: 34 additions & 156 deletions

File tree

library/alloc/src/boxed.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ pub struct Box<
238238

239239
/// Monomorphic function for allocating an uninit `Box`.
240240
#[inline]
241+
// The is a separate function to avoid doing it in every generic version, but it
242+
// looks small to the mir inliner (particularly in panic=abort) so leave it to
243+
// the backend to decide whether pulling it in everywhere is worth doing.
244+
#[rustc_no_mir_inline]
241245
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
242246
#[cfg(not(no_global_oom_handling))]
243247
fn box_new_uninit(layout: Layout) -> *mut u8 {

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
let mut _9: *const [()];
1212
let mut _10: std::boxed::Box<()>;
1313
let mut _11: *const ();
14-
let mut _14: std::alloc::Layout;
15-
let mut _20: usize;
14+
let mut _14: usize;
1615
scope 1 {
1716
debug vp_ctx => _1;
1817
let _5: *const ();
@@ -25,12 +24,12 @@
2524
scope 4 {
2625
debug _x => _8;
2726
}
28-
scope 18 (inlined foo) {
29-
let mut _21: *const [()];
27+
scope 9 (inlined foo) {
28+
let mut _15: *const [()];
3029
}
3130
}
32-
scope 16 (inlined slice_from_raw_parts::<()>) {
33-
scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) {
31+
scope 7 (inlined slice_from_raw_parts::<()>) {
32+
scope 8 (inlined std::ptr::from_raw_parts::<[()], ()>) {
3433
}
3534
}
3635
}
@@ -40,29 +39,6 @@
4039
let mut _13: *mut u8;
4140
scope 6 {
4241
}
43-
scope 7 (inlined boxed::box_new_uninit) {
44-
let mut _15: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
45-
let mut _16: isize;
46-
let _17: std::ptr::NonNull<[u8]>;
47-
let mut _18: !;
48-
scope 8 {
49-
scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) {
50-
scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) {
51-
scope 13 (inlined NonNull::<[u8]>::cast::<u8>) {
52-
let mut _19: *mut [u8];
53-
scope 14 (inlined NonNull::<[u8]>::as_ptr) {
54-
}
55-
}
56-
}
57-
scope 15 (inlined NonNull::<u8>::as_ptr) {
58-
}
59-
}
60-
}
61-
scope 9 (inlined <std::alloc::Global as Allocator>::allocate) {
62-
scope 10 (inlined std::alloc::Global::alloc_impl) {
63-
}
64-
}
65-
}
6642
}
6743

6844
bb0: {
@@ -74,15 +50,7 @@
7450
+ _4 = const ();
7551
StorageLive(_12);
7652
StorageLive(_13);
77-
StorageLive(_14);
78-
- _14 = const <() as std::mem::SizedTypeProperties>::LAYOUT;
79-
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }};
80-
StorageLive(_16);
81-
StorageLive(_17);
82-
StorageLive(_18);
83-
StorageLive(_15);
84-
- _15 = std::alloc::Global::alloc_impl_runtime(copy _14, const false) -> [return: bb5, unwind unreachable];
85-
+ _15 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}, const false) -> [return: bb5, unwind unreachable];
53+
_13 = boxed::box_new_uninit(const <() as std::mem::SizedTypeProperties>::LAYOUT) -> [return: bb2, unwind unreachable];
8654
}
8755

8856
bb1: {
@@ -92,30 +60,8 @@
9260
}
9361

9462
bb2: {
95-
unreachable;
96-
}
97-
98-
bb3: {
99-
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
100-
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable;
101-
}
102-
103-
bb4: {
104-
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
105-
- StorageLive(_19);
106-
+ nop;
107-
_19 = copy _17 as *mut [u8] (Transmute);
108-
_13 = copy _19 as *mut u8 (PtrToPtr);
109-
- StorageDead(_19);
110-
+ nop;
111-
StorageDead(_15);
112-
StorageDead(_18);
113-
StorageDead(_17);
114-
StorageDead(_16);
115-
StorageDead(_14);
116-
- _12 = copy _13 as *mut () (PtrToPtr);
63+
_12 = copy _13 as *mut () (PtrToPtr);
11764
- (*_12) = move _4;
118-
+ _12 = copy _19 as *mut () (PtrToPtr);
11965
+ (*_12) = const ();
12066
_3 = copy _13 as std::boxed::Box<()> (Transmute);
12167
StorageDead(_13);
@@ -133,21 +79,21 @@
13379
+ nop;
13480
StorageLive(_7);
13581
_7 = copy _5;
136-
StorageLive(_20);
137-
_20 = const 1_usize;
138-
- _6 = *const [()] from (copy _7, copy _20);
82+
StorageLive(_14);
83+
_14 = const 1_usize;
84+
- _6 = *const [()] from (copy _7, copy _14);
13985
+ _6 = *const [()] from (copy _5, const 1_usize);
140-
StorageDead(_20);
86+
StorageDead(_14);
14187
StorageDead(_7);
14288
StorageLive(_8);
14389
StorageLive(_9);
14490
_9 = copy _6;
145-
StorageLive(_21);
146-
- _21 = copy _9;
91+
StorageLive(_15);
92+
- _15 = copy _9;
14793
- _8 = copy _9 as *mut () (PtrToPtr);
148-
+ _21 = copy _6;
94+
+ _15 = copy _6;
14995
+ _8 = copy _5 as *mut () (PtrToPtr);
150-
StorageDead(_21);
96+
StorageDead(_15);
15197
StorageDead(_9);
15298
_0 = const ();
15399
StorageDead(_8);
@@ -157,14 +103,5 @@
157103
+ nop;
158104
drop(_3) -> [return: bb1, unwind unreachable];
159105
}
160-
161-
bb5: {
162-
_16 = discriminant(_15);
163-
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
164-
}
165-
+ }
166-
+
167-
+ ALLOC0 (size: 8, align: 4) {
168-
+ 01 00 00 00 00 00 00 00 │ ........
169106
}
170107

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
let mut _9: *const [()];
1212
let mut _10: std::boxed::Box<()>;
1313
let mut _11: *const ();
14-
let mut _14: std::alloc::Layout;
15-
let mut _20: usize;
14+
let mut _14: usize;
1615
scope 1 {
1716
debug vp_ctx => _1;
1817
let _5: *const ();
@@ -25,12 +24,12 @@
2524
scope 4 {
2625
debug _x => _8;
2726
}
28-
scope 18 (inlined foo) {
29-
let mut _21: *const [()];
27+
scope 9 (inlined foo) {
28+
let mut _15: *const [()];
3029
}
3130
}
32-
scope 16 (inlined slice_from_raw_parts::<()>) {
33-
scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) {
31+
scope 7 (inlined slice_from_raw_parts::<()>) {
32+
scope 8 (inlined std::ptr::from_raw_parts::<[()], ()>) {
3433
}
3534
}
3635
}
@@ -40,29 +39,6 @@
4039
let mut _13: *mut u8;
4140
scope 6 {
4241
}
43-
scope 7 (inlined boxed::box_new_uninit) {
44-
let mut _15: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
45-
let mut _16: isize;
46-
let _17: std::ptr::NonNull<[u8]>;
47-
let mut _18: !;
48-
scope 8 {
49-
scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) {
50-
scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) {
51-
scope 13 (inlined NonNull::<[u8]>::cast::<u8>) {
52-
let mut _19: *mut [u8];
53-
scope 14 (inlined NonNull::<[u8]>::as_ptr) {
54-
}
55-
}
56-
}
57-
scope 15 (inlined NonNull::<u8>::as_ptr) {
58-
}
59-
}
60-
}
61-
scope 9 (inlined <std::alloc::Global as Allocator>::allocate) {
62-
scope 10 (inlined std::alloc::Global::alloc_impl) {
63-
}
64-
}
65-
}
6642
}
6743

6844
bb0: {
@@ -74,15 +50,7 @@
7450
+ _4 = const ();
7551
StorageLive(_12);
7652
StorageLive(_13);
77-
StorageLive(_14);
78-
- _14 = const <() as std::mem::SizedTypeProperties>::LAYOUT;
79-
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }};
80-
StorageLive(_16);
81-
StorageLive(_17);
82-
StorageLive(_18);
83-
StorageLive(_15);
84-
- _15 = std::alloc::Global::alloc_impl_runtime(copy _14, const false) -> [return: bb5, unwind unreachable];
85-
+ _15 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}, const false) -> [return: bb5, unwind unreachable];
53+
_13 = boxed::box_new_uninit(const <() as std::mem::SizedTypeProperties>::LAYOUT) -> [return: bb2, unwind unreachable];
8654
}
8755

8856
bb1: {
@@ -92,30 +60,8 @@
9260
}
9361

9462
bb2: {
95-
unreachable;
96-
}
97-
98-
bb3: {
99-
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
100-
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable;
101-
}
102-
103-
bb4: {
104-
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
105-
- StorageLive(_19);
106-
+ nop;
107-
_19 = copy _17 as *mut [u8] (Transmute);
108-
_13 = copy _19 as *mut u8 (PtrToPtr);
109-
- StorageDead(_19);
110-
+ nop;
111-
StorageDead(_15);
112-
StorageDead(_18);
113-
StorageDead(_17);
114-
StorageDead(_16);
115-
StorageDead(_14);
116-
- _12 = copy _13 as *mut () (PtrToPtr);
63+
_12 = copy _13 as *mut () (PtrToPtr);
11764
- (*_12) = move _4;
118-
+ _12 = copy _19 as *mut () (PtrToPtr);
11965
+ (*_12) = const ();
12066
_3 = copy _13 as std::boxed::Box<()> (Transmute);
12167
StorageDead(_13);
@@ -133,21 +79,21 @@
13379
+ nop;
13480
StorageLive(_7);
13581
_7 = copy _5;
136-
StorageLive(_20);
137-
_20 = const 1_usize;
138-
- _6 = *const [()] from (copy _7, copy _20);
82+
StorageLive(_14);
83+
_14 = const 1_usize;
84+
- _6 = *const [()] from (copy _7, copy _14);
13985
+ _6 = *const [()] from (copy _5, const 1_usize);
140-
StorageDead(_20);
86+
StorageDead(_14);
14187
StorageDead(_7);
14288
StorageLive(_8);
14389
StorageLive(_9);
14490
_9 = copy _6;
145-
StorageLive(_21);
146-
- _21 = copy _9;
91+
StorageLive(_15);
92+
- _15 = copy _9;
14793
- _8 = copy _9 as *mut () (PtrToPtr);
148-
+ _21 = copy _6;
94+
+ _15 = copy _6;
14995
+ _8 = copy _5 as *mut () (PtrToPtr);
150-
StorageDead(_21);
96+
StorageDead(_15);
15197
StorageDead(_9);
15298
_0 = const ();
15399
StorageDead(_8);
@@ -157,14 +103,5 @@
157103
+ nop;
158104
drop(_3) -> [return: bb1, unwind unreachable];
159105
}
160-
161-
bb5: {
162-
_16 = discriminant(_15);
163-
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
164-
}
165-
+ }
166-
+
167-
+ ALLOC0 (size: 16, align: 8) {
168-
+ 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
169106
}
170107

0 commit comments

Comments
 (0)