Skip to content

Commit 2219766

Browse files
committed
Auto merge of #152605 - scottmcm:box-drop-alignment, r=Mark-Simulacrum
Pass alignments through the shim as `Alignment` (not `usize`) We're using `Layout` on both sides, so might as well skip the transmutes back and forth to `usize`. The mir-opt test shows that doing so allows simplifying the boxed-slice drop slightly, for example.
2 parents 4c37f6d + 774268a commit 2219766

11 files changed

Lines changed: 42 additions & 71 deletions

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ impl AllocFnFactory<'_, '_> {
128128

129129
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
130130
let ty_usize = self.cx.ty_path(usize);
131-
args.push(self.cx.param(self.span, size, ty_usize.clone()));
132-
args.push(self.cx.param(self.span, align, ty_usize));
133-
134-
let layout_new =
135-
self.cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
131+
args.push(self.cx.param(self.span, size, ty_usize));
132+
let ty_align = self.ptr_alignment();
133+
args.push(self.cx.param(self.span, align, ty_align));
134+
135+
let layout_new = self.cx.std_path(&[
136+
sym::alloc,
137+
sym::Layout,
138+
sym::from_size_alignment_unchecked,
139+
]);
136140
let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
137141
let size = self.cx.expr_ident(self.span, size);
138142
let align = self.cx.expr_ident(self.span, align);
@@ -175,6 +179,12 @@ impl AllocFnFactory<'_, '_> {
175179
self.cx.ty_path(usize)
176180
}
177181

182+
fn ptr_alignment(&self) -> Box<Ty> {
183+
let path = self.cx.std_path(&[sym::ptr, sym::Alignment]);
184+
let path = self.cx.path(self.span, path);
185+
self.cx.ty_path(path)
186+
}
187+
178188
fn ptr_u8(&self) -> Box<Ty> {
179189
let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
180190
let ty_u8 = self.cx.ty_path(u8);

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ symbols! {
157157
Abi,
158158
AcqRel,
159159
Acquire,
160+
Alignment,
160161
Any,
161162
Arc,
162163
ArcWeak,
@@ -1148,6 +1149,7 @@ symbols! {
11481149
from_output,
11491150
from_residual,
11501151
from_size_align_unchecked,
1152+
from_size_alignment_unchecked,
11511153
from_str,
11521154
from_str_method,
11531155
from_str_nonconst,

library/alloc/src/alloc.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#[stable(feature = "alloc_module", since = "1.28.0")]
66
#[doc(inline)]
77
pub use core::alloc::*;
8-
use core::ptr::{self, NonNull};
8+
use core::ptr::{self, Alignment, NonNull};
99
use core::{cmp, hint};
1010

1111
unsafe extern "Rust" {
@@ -18,19 +18,19 @@ unsafe extern "Rust" {
1818
#[rustc_nounwind]
1919
#[rustc_std_internal_symbol]
2020
#[rustc_allocator_zeroed_variant = "__rust_alloc_zeroed"]
21-
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
21+
fn __rust_alloc(size: usize, align: Alignment) -> *mut u8;
2222
#[rustc_deallocator]
2323
#[rustc_nounwind]
2424
#[rustc_std_internal_symbol]
25-
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
25+
fn __rust_dealloc(ptr: *mut u8, size: usize, align: Alignment);
2626
#[rustc_reallocator]
2727
#[rustc_nounwind]
2828
#[rustc_std_internal_symbol]
29-
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
29+
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: Alignment, new_size: usize) -> *mut u8;
3030
#[rustc_allocator_zeroed]
3131
#[rustc_nounwind]
3232
#[rustc_std_internal_symbol]
33-
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
33+
fn __rust_alloc_zeroed(size: usize, align: Alignment) -> *mut u8;
3434

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

95-
__rust_alloc(layout.size(), layout.align())
95+
__rust_alloc(layout.size(), layout.alignment())
9696
}
9797
}
9898

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

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

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

178-
__rust_alloc_zeroed(layout.size(), layout.align())
178+
__rust_alloc_zeroed(layout.size(), layout.alignment())
179179
}
180180
}
181181

library/core/src/macros/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ pub(crate) mod builtin {
17831783
///
17841784
/// See also [`std::alloc::GlobalAlloc`](../../../std/alloc/trait.GlobalAlloc.html).
17851785
#[stable(feature = "global_allocator", since = "1.28.0")]
1786-
#[allow_internal_unstable(rustc_attrs)]
1786+
#[allow_internal_unstable(rustc_attrs, ptr_alignment_type)]
17871787
#[rustc_builtin_macro]
17881788
pub macro global_allocator($item:item) {
17891789
/* compiler built-in */

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(i{{[0-9]+}} 1, {{-2147483647|-9223372036854775807}})) 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
@@ -197,6 +197,6 @@ pub fn vec_array(n: usize) -> Vec<[u32; 1_000_000]> {
197197
}
198198

199199
// Ensure that __rust_alloc_zeroed gets the right attributes for LLVM to optimize it away.
200-
// CHECK: declare noalias noundef ptr @{{.*}}__rust_alloc_zeroed(i64 noundef, i64 allocalign noundef) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]
200+
// 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]+]]
201201

202202
// 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.32bit.panic-abort.mir

Lines changed: 3 additions & 13 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 _10: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 17 (inlined Layout::size) {
@@ -32,16 +32,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3232
scope 27 (inlined NonNull::<u8>::as_ptr) {
3333
}
3434
scope 28 (inlined std::alloc::dealloc) {
35-
let mut _9: usize;
3635
scope 29 (inlined Layout::size) {
3736
}
38-
scope 30 (inlined Layout::align) {
39-
scope 31 (inlined std::ptr::Alignment::as_usize) {
40-
scope 32 (inlined std::ptr::Alignment::as_nonzero) {
41-
}
42-
scope 33 (inlined NonZero::<usize>::get) {
43-
}
44-
}
37+
scope 30 (inlined Layout::alignment) {
4538
}
4639
}
4740
}
@@ -100,13 +93,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
10093
bb2: {
10194
StorageLive(_8);
10295
_8 = copy _3 as *mut u8 (PtrToPtr);
103-
StorageLive(_9);
104-
_9 = copy _7 as usize (Transmute);
105-
_10 = alloc::alloc::__rust_dealloc(move _8, move _5, move _9) -> [return: bb3, unwind unreachable];
96+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb3, unwind unreachable];
10697
}
10798

10899
bb3: {
109-
StorageDead(_9);
110100
StorageDead(_8);
111101
goto -> bb4;
112102
}

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

Lines changed: 3 additions & 13 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 _10: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 17 (inlined Layout::size) {
@@ -32,16 +32,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3232
scope 27 (inlined NonNull::<u8>::as_ptr) {
3333
}
3434
scope 28 (inlined std::alloc::dealloc) {
35-
let mut _9: usize;
3635
scope 29 (inlined Layout::size) {
3736
}
38-
scope 30 (inlined Layout::align) {
39-
scope 31 (inlined std::ptr::Alignment::as_usize) {
40-
scope 32 (inlined std::ptr::Alignment::as_nonzero) {
41-
}
42-
scope 33 (inlined NonZero::<usize>::get) {
43-
}
44-
}
37+
scope 30 (inlined Layout::alignment) {
4538
}
4639
}
4740
}
@@ -100,13 +93,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
10093
bb2: {
10194
StorageLive(_8);
10295
_8 = copy _3 as *mut u8 (PtrToPtr);
103-
StorageLive(_9);
104-
_9 = copy _7 as usize (Transmute);
105-
_10 = alloc::alloc::__rust_dealloc(move _8, move _5, move _9) -> [return: bb3, unwind unreachable];
96+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb3, unwind unreachable];
10697
}
10798

10899
bb3: {
109-
StorageDead(_9);
110100
StorageDead(_8);
111101
goto -> bb4;
112102
}

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

Lines changed: 3 additions & 13 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 _10: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 17 (inlined Layout::size) {
@@ -32,16 +32,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3232
scope 27 (inlined NonNull::<u8>::as_ptr) {
3333
}
3434
scope 28 (inlined std::alloc::dealloc) {
35-
let mut _9: usize;
3635
scope 29 (inlined Layout::size) {
3736
}
38-
scope 30 (inlined Layout::align) {
39-
scope 31 (inlined std::ptr::Alignment::as_usize) {
40-
scope 32 (inlined std::ptr::Alignment::as_nonzero) {
41-
}
42-
scope 33 (inlined NonZero::<usize>::get) {
43-
}
44-
}
37+
scope 30 (inlined Layout::alignment) {
4538
}
4639
}
4740
}
@@ -100,13 +93,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
10093
bb2: {
10194
StorageLive(_8);
10295
_8 = copy _3 as *mut u8 (PtrToPtr);
103-
StorageLive(_9);
104-
_9 = copy _7 as usize (Transmute);
105-
_10 = alloc::alloc::__rust_dealloc(move _8, move _5, move _9) -> [return: bb3, unwind unreachable];
96+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb3, unwind unreachable];
10697
}
10798

10899
bb3: {
109-
StorageDead(_9);
110100
StorageDead(_8);
111101
goto -> bb4;
112102
}

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

Lines changed: 3 additions & 13 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 _10: ();
11+
let _9: ();
1212
scope 3 {
1313
scope 4 {
1414
scope 17 (inlined Layout::size) {
@@ -32,16 +32,9 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
3232
scope 27 (inlined NonNull::<u8>::as_ptr) {
3333
}
3434
scope 28 (inlined std::alloc::dealloc) {
35-
let mut _9: usize;
3635
scope 29 (inlined Layout::size) {
3736
}
38-
scope 30 (inlined Layout::align) {
39-
scope 31 (inlined std::ptr::Alignment::as_usize) {
40-
scope 32 (inlined std::ptr::Alignment::as_nonzero) {
41-
}
42-
scope 33 (inlined NonZero::<usize>::get) {
43-
}
44-
}
37+
scope 30 (inlined Layout::alignment) {
4538
}
4639
}
4740
}
@@ -100,13 +93,10 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
10093
bb2: {
10194
StorageLive(_8);
10295
_8 = copy _3 as *mut u8 (PtrToPtr);
103-
StorageLive(_9);
104-
_9 = copy _7 as usize (Transmute);
105-
_10 = alloc::alloc::__rust_dealloc(move _8, move _5, move _9) -> [return: bb3, unwind unreachable];
96+
_9 = alloc::alloc::__rust_dealloc(move _8, move _5, move _7) -> [return: bb3, unwind unreachable];
10697
}
10798

10899
bb3: {
109-
StorageDead(_9);
110100
StorageDead(_8);
111101
goto -> bb4;
112102
}

0 commit comments

Comments
 (0)