Skip to content

Commit 66300b6

Browse files
committed
Just pass Layout directly to box_new_uninit
We have a constant for it already (used in `RawVec` for basically the same polymorphization) so let's use it.
1 parent 3c9faa0 commit 66300b6

7 files changed

Lines changed: 95 additions & 183 deletions

library/alloc/src/boxed.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,10 @@ pub struct Box<
237237
>(Unique<T>, A);
238238

239239
/// Monomorphic function for allocating an uninit `Box`.
240-
///
241-
/// # Safety
242-
///
243-
/// size and align need to be safe for `Layout::from_size_align_unchecked`.
244240
#[inline]
245241
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
246242
#[cfg(not(no_global_oom_handling))]
247-
unsafe fn box_new_uninit(size: usize, align: usize) -> *mut u8 {
248-
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
243+
fn box_new_uninit(layout: Layout) -> *mut u8 {
249244
match Global.allocate(layout) {
250245
Ok(ptr) => ptr.as_mut_ptr(),
251246
Err(_) => handle_alloc_error(layout),
@@ -284,10 +279,7 @@ impl<T> Box<T> {
284279
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
285280
pub fn new(x: T) -> Self {
286281
// This is `Box::new_uninit` but inlined to avoid build time regressions.
287-
// SAFETY: The size and align of a valid type `T` are always valid for `Layout`.
288-
let ptr = unsafe {
289-
box_new_uninit(<T as SizedTypeProperties>::SIZE, <T as SizedTypeProperties>::ALIGN)
290-
} as *mut T;
282+
let ptr = box_new_uninit(<T as SizedTypeProperties>::LAYOUT) as *mut T;
291283
// Nothing below can panic so we do not have to worry about deallocating `ptr`.
292284
// SAFETY: we just allocated the box to store `x`.
293285
unsafe { core::intrinsics::write_via_move(ptr, x) };
@@ -319,12 +311,7 @@ impl<T> Box<T> {
319311
// SAFETY:
320312
// - The size and align of a valid type `T` are always valid for `Layout`.
321313
// - If `allocate` succeeds, the returned pointer exactly matches what `Box` needs.
322-
unsafe {
323-
mem::transmute(box_new_uninit(
324-
<T as SizedTypeProperties>::SIZE,
325-
<T as SizedTypeProperties>::ALIGN,
326-
))
327-
}
314+
unsafe { mem::transmute(box_new_uninit(<T as SizedTypeProperties>::LAYOUT)) }
328315
}
329316

330317
/// Constructs a new `Box` with uninitialized contents, with the memory

tests/codegen-units/item-collection/opaque-return-impls.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ pub fn foo2() -> Box<dyn TestTrait2> {
4545
//~ MONO_ITEM fn foo2
4646
//~ MONO_ITEM fn std::alloc::Global::alloc_impl_runtime
4747
//~ MONO_ITEM fn std::boxed::Box::<TestStruct2>::new
48-
//~ MONO_ITEM fn std::alloc::Layout::from_size_align_unchecked::precondition_check
49-
//~ MONO_ITEM fn std::ptr::Alignment::new_unchecked::precondition_check
5048
//~ MONO_ITEM fn std::ptr::NonNull::<T>::new_unchecked::precondition_check
5149

5250
struct Counter {

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

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
let mut _9: *const [()];
1212
let mut _10: std::boxed::Box<()>;
1313
let mut _11: *const ();
14-
let mut _14: usize;
15-
let mut _15: usize;
16-
let mut _24: usize;
14+
let mut _14: std::alloc::Layout;
15+
let mut _20: usize;
1716
scope 1 {
1817
debug vp_ctx => _1;
1918
let _5: *const ();
@@ -26,12 +25,12 @@
2625
scope 4 {
2726
debug _x => _8;
2827
}
29-
scope 20 (inlined foo) {
30-
let mut _25: *const [()];
28+
scope 18 (inlined foo) {
29+
let mut _21: *const [()];
3130
}
3231
}
33-
scope 18 (inlined slice_from_raw_parts::<()>) {
34-
scope 19 (inlined std::ptr::from_raw_parts::<[()], ()>) {
32+
scope 16 (inlined slice_from_raw_parts::<()>) {
33+
scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) {
3534
}
3635
}
3736
}
@@ -42,33 +41,26 @@
4241
scope 6 {
4342
}
4443
scope 7 (inlined boxed::box_new_uninit) {
45-
let _16: std::alloc::Layout;
46-
let mut _17: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
47-
let mut _18: isize;
48-
let mut _20: !;
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: !;
4948
scope 8 {
50-
let _19: std::ptr::NonNull<[u8]>;
51-
scope 9 {
52-
scope 13 (inlined NonNull::<[u8]>::as_mut_ptr) {
53-
scope 14 (inlined NonNull::<[u8]>::as_non_null_ptr) {
54-
scope 15 (inlined NonNull::<[u8]>::cast::<u8>) {
55-
let mut _23: *mut [u8];
56-
scope 16 (inlined NonNull::<[u8]>::as_ptr) {
57-
}
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) {
5854
}
5955
}
60-
scope 17 (inlined NonNull::<u8>::as_ptr) {
61-
}
6256
}
63-
}
64-
scope 11 (inlined <std::alloc::Global as Allocator>::allocate) {
65-
scope 12 (inlined std::alloc::Global::alloc_impl) {
57+
scope 15 (inlined NonNull::<u8>::as_ptr) {
6658
}
6759
}
6860
}
69-
scope 10 (inlined #[track_caller] Layout::from_size_align_unchecked) {
70-
let _21: ();
71-
let mut _22: std::ptr::Alignment;
61+
scope 9 (inlined <std::alloc::Global as Allocator>::allocate) {
62+
scope 10 (inlined std::alloc::Global::alloc_impl) {
63+
}
7264
}
7365
}
7466
}
@@ -83,17 +75,14 @@
8375
StorageLive(_12);
8476
StorageLive(_13);
8577
StorageLive(_14);
86-
- _14 = const <() as std::mem::SizedTypeProperties>::SIZE;
87-
+ _14 = const 0_usize;
88-
StorageLive(_15);
89-
- _15 = const <() as std::mem::SizedTypeProperties>::ALIGN;
90-
+ _15 = const 1_usize;
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 }} }};
9180
StorageLive(_16);
81+
StorageLive(_17);
9282
StorageLive(_18);
93-
StorageLive(_19);
94-
StorageLive(_20);
95-
StorageLive(_21);
96-
switchInt(UbChecks) -> [0: bb6, otherwise: bb5];
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];
9786
}
9887

9988
bb1: {
@@ -107,29 +96,26 @@
10796
}
10897

10998
bb3: {
110-
- _20 = handle_alloc_error(move _16) -> unwind unreachable;
111-
+ _20 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable;
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;
112101
}
113102

114103
bb4: {
115-
_19 = copy ((_17 as Ok).0: std::ptr::NonNull<[u8]>);
116-
- StorageLive(_23);
104+
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
105+
- StorageLive(_19);
117106
+ nop;
118-
_23 = copy _19 as *mut [u8] (Transmute);
119-
_13 = copy _23 as *mut u8 (PtrToPtr);
120-
- StorageDead(_23);
107+
_19 = copy _17 as *mut [u8] (Transmute);
108+
_13 = copy _19 as *mut u8 (PtrToPtr);
109+
- StorageDead(_19);
121110
+ nop;
122-
StorageDead(_17);
123-
StorageDead(_21);
124-
StorageDead(_20);
125-
StorageDead(_19);
111+
StorageDead(_15);
126112
StorageDead(_18);
113+
StorageDead(_17);
127114
StorageDead(_16);
128-
StorageDead(_15);
129115
StorageDead(_14);
130116
- _12 = copy _13 as *mut () (PtrToPtr);
131117
- (*_12) = move _4;
132-
+ _12 = copy _23 as *mut () (PtrToPtr);
118+
+ _12 = copy _19 as *mut () (PtrToPtr);
133119
+ (*_12) = const ();
134120
_3 = copy _13 as std::boxed::Box<()> (Transmute);
135121
StorageDead(_13);
@@ -147,21 +133,21 @@
147133
+ nop;
148134
StorageLive(_7);
149135
_7 = copy _5;
150-
StorageLive(_24);
151-
_24 = const 1_usize;
152-
- _6 = *const [()] from (copy _7, copy _24);
136+
StorageLive(_20);
137+
_20 = const 1_usize;
138+
- _6 = *const [()] from (copy _7, copy _20);
153139
+ _6 = *const [()] from (copy _5, const 1_usize);
154-
StorageDead(_24);
140+
StorageDead(_20);
155141
StorageDead(_7);
156142
StorageLive(_8);
157143
StorageLive(_9);
158144
_9 = copy _6;
159-
StorageLive(_25);
160-
- _25 = copy _9;
145+
StorageLive(_21);
146+
- _21 = copy _9;
161147
- _8 = copy _9 as *mut () (PtrToPtr);
162-
+ _25 = copy _6;
148+
+ _21 = copy _6;
163149
+ _8 = copy _5 as *mut () (PtrToPtr);
164-
StorageDead(_25);
150+
StorageDead(_21);
165151
StorageDead(_9);
166152
_0 = const ();
167153
StorageDead(_8);
@@ -173,25 +159,8 @@
173159
}
174160

175161
bb5: {
176-
- _21 = Layout::from_size_align_unchecked::precondition_check(copy _14, copy _15) -> [return: bb6, unwind unreachable];
177-
+ _21 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable];
178-
}
179-
180-
bb6: {
181-
StorageLive(_22);
182-
- _22 = copy _15 as std::ptr::Alignment (Transmute);
183-
- _16 = Layout { size: copy _14, align: move _22 };
184-
+ _22 = const std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }};
185-
+ _16 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }};
186-
StorageDead(_22);
187-
StorageLive(_17);
188-
- _17 = std::alloc::Global::alloc_impl_runtime(copy _16, const false) -> [return: bb7, unwind unreachable];
189-
+ _17 = 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: bb7, unwind unreachable];
190-
}
191-
192-
bb7: {
193-
_18 = discriminant(_17);
194-
switchInt(move _18) -> [0: bb4, 1: bb3, otherwise: bb2];
162+
_16 = discriminant(_15);
163+
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
195164
}
196165
+ }
197166
+

0 commit comments

Comments
 (0)