Skip to content

Commit 12f11b8

Browse files
Auto merge of #144572 - scottmcm:alloc-using-alignment, r=<try>
Need less bumf to pass `Layout` to `__rustc_alloc`
2 parents 733dab5 + b954406 commit 12f11b8

File tree

6 files changed

+223
-11
lines changed

6 files changed

+223
-11
lines changed

library/alloc/src/alloc.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#[doc(inline)]
77
pub use core::alloc::*;
88
use core::hint;
9-
use core::ptr::{self, NonNull};
9+
use core::ptr::{self, Alignment, NonNull};
1010

1111
unsafe extern "Rust" {
1212
// These are the magic symbols to call the global allocator. rustc generates
@@ -17,19 +17,19 @@ unsafe extern "Rust" {
1717
#[rustc_allocator]
1818
#[rustc_nounwind]
1919
#[rustc_std_internal_symbol]
20-
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
20+
fn __rust_alloc(size: usize, align: Alignment) -> *mut u8;
2121
#[rustc_deallocator]
2222
#[rustc_nounwind]
2323
#[rustc_std_internal_symbol]
24-
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
24+
fn __rust_dealloc(ptr: *mut u8, size: usize, align: Alignment);
2525
#[rustc_reallocator]
2626
#[rustc_nounwind]
2727
#[rustc_std_internal_symbol]
28-
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
28+
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: Alignment, new_size: usize) -> *mut u8;
2929
#[rustc_allocator_zeroed]
3030
#[rustc_nounwind]
3131
#[rustc_std_internal_symbol]
32-
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
32+
fn __rust_alloc_zeroed(size: usize, align: Alignment) -> *mut u8;
3333

3434
#[rustc_nounwind]
3535
#[rustc_std_internal_symbol]
@@ -91,7 +91,7 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
9191
// stable code until it is actually stabilized.
9292
__rust_no_alloc_shim_is_unstable_v2();
9393

94-
__rust_alloc(layout.size(), layout.align())
94+
__rust_alloc(layout.size(), layout.alignment())
9595
}
9696
}
9797

@@ -111,7 +111,7 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
111111
#[inline]
112112
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
113113
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
114-
unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
114+
unsafe { __rust_dealloc(ptr, layout.size(), layout.alignment()) }
115115
}
116116

117117
/// Reallocates memory with the global allocator.
@@ -131,7 +131,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
131131
#[inline]
132132
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
133133
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
134-
unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
134+
unsafe { __rust_realloc(ptr, layout.size(), layout.alignment(), new_size) }
135135
}
136136

137137
/// Allocates zero-initialized memory with the global allocator.
@@ -174,7 +174,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
174174
// stable code until it is actually stabilized.
175175
__rust_no_alloc_shim_is_unstable_v2();
176176

177-
__rust_alloc_zeroed(layout.size(), layout.align())
177+
__rust_alloc_zeroed(layout.size(), layout.alignment())
178178
}
179179
}
180180

tests/codegen-llvm/box-uninit-bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ pub fn box_lotsa_padding() -> Box<LotsaPadding> {
4141

4242
// Hide the `allocalign` attribute in the declaration of __rust_alloc
4343
// from the CHECK-NOT above, and also verify the attributes got set reasonably.
44-
// CHECK: declare {{(dso_local )?}}noalias noundef ptr @{{.*}}__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]]
44+
// CHECK: declare {{(dso_local )?}}noalias noundef ptr @{{.*}}__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef range({{i64 1, -9223372036854775807|i32 1, -2147483647}})) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]]
4545

4646
// CHECK-DAG: attributes [[RUST_ALLOC_ATTRS]] = { {{.*}} allockind("alloc,uninitialized,aligned") allocsize(0) {{(uwtable )?}}"alloc-family"="__rust_alloc" {{.*}} }

tests/codegen-llvm/vec-calloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ pub fn vec_option_i32(n: usize) -> Vec<Option<i32>> {
177177
}
178178

179179
// Ensure that __rust_alloc_zeroed gets the right attributes for LLVM to optimize it away.
180-
// CHECK: declare noalias noundef ptr @{{.*}}__rust_alloc_zeroed(i64 noundef, i64 allocalign noundef) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]
180+
// CHECK: declare noalias noundef ptr @{{.*}}__rust_alloc_zeroed(i64 noundef, i64 allocalign noundef range(i64 1, -9223372036854775807)) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]
181181

182182
// CHECK-DAG: attributes [[RUST_ALLOC_ZEROED_ATTRS]] = { {{.*}} allockind("alloc,zeroed,aligned") allocsize(0) uwtable "alloc-family"="__rust_alloc" {{.*}} }
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// MIR for `generic_in_place` after PreCodegen
2+
3+
fn generic_in_place(_1: *mut Box<[T]>) -> () {
4+
debug ptr => _1;
5+
let mut _0: ();
6+
scope 1 (inlined drop_in_place::<Box<[T]>> - shim(Some(Box<[T]>))) {
7+
scope 2 (inlined <Box<[T]> as Drop>::drop) {
8+
let _2: std::ptr::NonNull<[T]>;
9+
let mut _3: *mut [T];
10+
let mut _4: *const [T];
11+
let _9: ();
12+
scope 3 {
13+
scope 4 {
14+
scope 12 (inlined Layout::size) {
15+
}
16+
scope 13 (inlined Unique::<[T]>::cast::<u8>) {
17+
scope 14 (inlined NonNull::<[T]>::cast::<u8>) {
18+
scope 15 (inlined NonNull::<[T]>::as_ptr) {
19+
}
20+
}
21+
}
22+
scope 16 (inlined <NonNull<u8> as From<Unique<u8>>>::from) {
23+
scope 17 (inlined Unique::<u8>::as_non_null_ptr) {
24+
}
25+
}
26+
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
27+
let mut _8: *mut u8;
28+
scope 19 (inlined Layout::size) {
29+
}
30+
scope 20 (inlined NonNull::<u8>::as_ptr) {
31+
}
32+
scope 21 (inlined std::alloc::dealloc) {
33+
scope 22 (inlined Layout::size) {
34+
}
35+
scope 23 (inlined Layout::alignment) {
36+
}
37+
}
38+
}
39+
}
40+
scope 5 (inlined Unique::<[T]>::as_ptr) {
41+
scope 6 (inlined NonNull::<[T]>::as_ptr) {
42+
}
43+
}
44+
scope 7 (inlined Layout::for_value_raw::<[T]>) {
45+
let mut _5: usize;
46+
let mut _6: usize;
47+
scope 8 {
48+
scope 11 (inlined #[track_caller] Layout::from_size_align_unchecked) {
49+
let mut _7: std::ptr::Alignment;
50+
}
51+
}
52+
scope 9 (inlined size_of_val_raw::<[T]>) {
53+
}
54+
scope 10 (inlined align_of_val_raw::<[T]>) {
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
bb0: {
62+
StorageLive(_2);
63+
_2 = copy (((*_1).0: std::ptr::Unique<[T]>).0: std::ptr::NonNull<[T]>);
64+
StorageLive(_4);
65+
_3 = copy _2 as *mut [T] (Transmute);
66+
_4 = copy _2 as *const [T] (Transmute);
67+
StorageLive(_6);
68+
_5 = std::intrinsics::size_of_val::<[T]>(copy _4) -> [return: bb1, unwind unreachable];
69+
}
70+
71+
bb1: {
72+
_6 = std::intrinsics::align_of_val::<[T]>(move _4) -> [return: bb2, unwind unreachable];
73+
}
74+
75+
bb2: {
76+
_7 = copy _6 as std::ptr::Alignment (Transmute);
77+
StorageDead(_6);
78+
StorageDead(_4);
79+
switchInt(copy _5) -> [0: bb5, otherwise: bb3];
80+
}
81+
82+
bb3: {
83+
StorageLive(_8);
84+
_8 = copy _3 as *mut u8 (PtrToPtr);
85+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb4, unwind unreachable];
86+
}
87+
88+
bb4: {
89+
StorageDead(_8);
90+
goto -> bb5;
91+
}
92+
93+
bb5: {
94+
StorageDead(_2);
95+
return;
96+
}
97+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// MIR for `generic_in_place` after PreCodegen
2+
3+
fn generic_in_place(_1: *mut Box<[T]>) -> () {
4+
debug ptr => _1;
5+
let mut _0: ();
6+
scope 1 (inlined drop_in_place::<Box<[T]>> - shim(Some(Box<[T]>))) {
7+
scope 2 (inlined <Box<[T]> as Drop>::drop) {
8+
let _2: std::ptr::NonNull<[T]>;
9+
let mut _3: *mut [T];
10+
let mut _4: *const [T];
11+
let _9: ();
12+
scope 3 {
13+
scope 4 {
14+
scope 12 (inlined Layout::size) {
15+
}
16+
scope 13 (inlined Unique::<[T]>::cast::<u8>) {
17+
scope 14 (inlined NonNull::<[T]>::cast::<u8>) {
18+
scope 15 (inlined NonNull::<[T]>::as_ptr) {
19+
}
20+
}
21+
}
22+
scope 16 (inlined <NonNull<u8> as From<Unique<u8>>>::from) {
23+
scope 17 (inlined Unique::<u8>::as_non_null_ptr) {
24+
}
25+
}
26+
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
27+
let mut _8: *mut u8;
28+
scope 19 (inlined Layout::size) {
29+
}
30+
scope 20 (inlined NonNull::<u8>::as_ptr) {
31+
}
32+
scope 21 (inlined std::alloc::dealloc) {
33+
scope 22 (inlined Layout::size) {
34+
}
35+
scope 23 (inlined Layout::alignment) {
36+
}
37+
}
38+
}
39+
}
40+
scope 5 (inlined Unique::<[T]>::as_ptr) {
41+
scope 6 (inlined NonNull::<[T]>::as_ptr) {
42+
}
43+
}
44+
scope 7 (inlined Layout::for_value_raw::<[T]>) {
45+
let mut _5: usize;
46+
let mut _6: usize;
47+
scope 8 {
48+
scope 11 (inlined #[track_caller] Layout::from_size_align_unchecked) {
49+
let mut _7: std::ptr::Alignment;
50+
}
51+
}
52+
scope 9 (inlined size_of_val_raw::<[T]>) {
53+
}
54+
scope 10 (inlined align_of_val_raw::<[T]>) {
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
bb0: {
62+
StorageLive(_2);
63+
_2 = copy (((*_1).0: std::ptr::Unique<[T]>).0: std::ptr::NonNull<[T]>);
64+
StorageLive(_4);
65+
_3 = copy _2 as *mut [T] (Transmute);
66+
_4 = copy _2 as *const [T] (Transmute);
67+
StorageLive(_6);
68+
_5 = std::intrinsics::size_of_val::<[T]>(copy _4) -> [return: bb1, unwind unreachable];
69+
}
70+
71+
bb1: {
72+
_6 = std::intrinsics::align_of_val::<[T]>(move _4) -> [return: bb2, unwind unreachable];
73+
}
74+
75+
bb2: {
76+
_7 = copy _6 as std::ptr::Alignment (Transmute);
77+
StorageDead(_6);
78+
StorageDead(_4);
79+
switchInt(copy _5) -> [0: bb5, otherwise: bb3];
80+
}
81+
82+
bb3: {
83+
StorageLive(_8);
84+
_8 = copy _3 as *mut u8 (PtrToPtr);
85+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb4, unwind unreachable];
86+
}
87+
88+
bb4: {
89+
StorageDead(_8);
90+
goto -> bb5;
91+
}
92+
93+
bb5: {
94+
StorageDead(_2);
95+
return;
96+
}
97+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ compile-flags: -O -Zmir-opt-level=2
2+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3+
4+
#![crate_type = "lib"]
5+
#![feature(step_trait)]
6+
7+
// EMIT_MIR drop_boxed_slice.generic_in_place.PreCodegen.after.mir
8+
pub unsafe fn generic_in_place<T: Copy>(ptr: *mut Box<[T]>) {
9+
// CHECK-LABEL: fn generic_in_place(_1: *mut Box<[T]>)
10+
// CHECK: (inlined <Box<[T]> as Drop>::drop)
11+
// CHECK: [[SIZE:_.+]] = std::intrinsics::size_of_val::<[T]>
12+
// CHECK: [[ALIGN:_.+]] = std::intrinsics::align_of_val::<[T]>
13+
// CHECK: [[ALIGNMENT:_.+]] = copy [[ALIGN]] as std::ptr::Alignment (Transmute);
14+
// CHECK-NOT: discriminant
15+
// CHECK-NOT: IntToInt
16+
// CHECK: = alloc::alloc::__rust_dealloc({{.+}}, move [[SIZE]], move [[ALIGNMENT]]) ->
17+
std::ptr::drop_in_place(ptr)
18+
}

0 commit comments

Comments
 (0)