Skip to content

Commit b954406

Browse files
committed
Pass Alignment to the __rust_*alloc* shims
1 parent 670ce23 commit b954406

File tree

6 files changed

+23
-50
lines changed

6 files changed

+23
-50
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" {{.*}} }

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.panic-abort.mir

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
88
let _2: std::ptr::NonNull<[T]>;
99
let mut _3: *mut [T];
1010
let mut _4: *const [T];
11-
let _12: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 12 (inlined Layout::size) {
@@ -30,14 +30,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3030
scope 20 (inlined NonNull::<u8>::as_ptr) {
3131
}
3232
scope 21 (inlined std::alloc::dealloc) {
33-
let mut _11: usize;
3433
scope 22 (inlined Layout::size) {
3534
}
36-
scope 23 (inlined Layout::align) {
37-
scope 24 (inlined std::ptr::Alignment::as_usize) {
38-
let _9: std::ptr::alignment::AlignmentEnum;
39-
let mut _10: u32;
40-
}
35+
scope 23 (inlined Layout::alignment) {
4136
}
4237
}
4338
}
@@ -87,19 +82,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
8782
bb3: {
8883
StorageLive(_8);
8984
_8 = copy _3 as *mut u8 (PtrToPtr);
90-
StorageLive(_11);
91-
StorageLive(_10);
92-
StorageLive(_9);
93-
_9 = copy (_7.0: std::ptr::alignment::AlignmentEnum);
94-
_10 = discriminant(_9);
95-
_11 = move _10 as usize (IntToInt);
96-
StorageDead(_9);
97-
StorageDead(_10);
98-
_12 = alloc::alloc::__rust_dealloc(move _8, move _5, move _11) -> [return: bb4, unwind unreachable];
85+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb4, unwind unreachable];
9986
}
10087

10188
bb4: {
102-
StorageDead(_11);
10389
StorageDead(_8);
10490
goto -> bb5;
10591
}

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.panic-unwind.mir

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
88
let _2: std::ptr::NonNull<[T]>;
99
let mut _3: *mut [T];
1010
let mut _4: *const [T];
11-
let _12: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 12 (inlined Layout::size) {
@@ -30,14 +30,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3030
scope 20 (inlined NonNull::<u8>::as_ptr) {
3131
}
3232
scope 21 (inlined std::alloc::dealloc) {
33-
let mut _11: usize;
3433
scope 22 (inlined Layout::size) {
3534
}
36-
scope 23 (inlined Layout::align) {
37-
scope 24 (inlined std::ptr::Alignment::as_usize) {
38-
let _9: std::ptr::alignment::AlignmentEnum;
39-
let mut _10: u32;
40-
}
35+
scope 23 (inlined Layout::alignment) {
4136
}
4237
}
4338
}
@@ -87,19 +82,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
8782
bb3: {
8883
StorageLive(_8);
8984
_8 = copy _3 as *mut u8 (PtrToPtr);
90-
StorageLive(_11);
91-
StorageLive(_10);
92-
StorageLive(_9);
93-
_9 = copy (_7.0: std::ptr::alignment::AlignmentEnum);
94-
_10 = discriminant(_9);
95-
_11 = move _10 as usize (IntToInt);
96-
StorageDead(_9);
97-
StorageDead(_10);
98-
_12 = alloc::alloc::__rust_dealloc(move _8, move _5, move _11) -> [return: bb4, unwind unreachable];
85+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb4, unwind unreachable];
9986
}
10087

10188
bb4: {
102-
StorageDead(_11);
10389
StorageDead(_8);
10490
goto -> bb5;
10591
}

tests/mir-opt/pre-codegen/drop_boxed_slice.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
pub unsafe fn generic_in_place<T: Copy>(ptr: *mut Box<[T]>) {
99
// CHECK-LABEL: fn generic_in_place(_1: *mut Box<[T]>)
1010
// CHECK: (inlined <Box<[T]> as Drop>::drop)
11-
// CHECK: _7 = copy _6 as std::ptr::Alignment (Transmute);
12-
// CHECK: _9 = copy (_7.0: std::ptr::alignment::AlignmentEnum);
13-
// CHECK: _10 = discriminant(_9);
14-
// CHECK: _11 = move _10 as usize (IntToInt);
15-
// CHECK: = alloc::alloc::__rust_dealloc(move _8, move _5, move _11) ->
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]]) ->
1617
std::ptr::drop_in_place(ptr)
1718
}

0 commit comments

Comments
 (0)