Skip to content

Commit 826c462

Browse files
authored
Rollup merge of #144566 - scottmcm:align-of-slice, r=oli-obk
Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()` I spotted this while working on the inliner (#144561). In particular, if [`Layout::for_value`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value) inlines, then it can be pretty easy to end up with an `align_of_val::<[T]>` today (demo: <https://rust.godbolt.org/z/Tesnscj4a>) where we can save at least a block, if not more, by using the version that's an rvalue and not a call.
2 parents f837028 + 8ef9233 commit 826c462

8 files changed

+516
-0
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
5555

5656
let terminator = block.terminator.as_mut().unwrap();
5757
ctx.simplify_primitive_clone(terminator, &mut block.statements);
58+
ctx.simplify_align_of_slice_val(terminator, &mut block.statements);
5859
ctx.simplify_intrinsic_assert(terminator);
5960
ctx.simplify_nounwind_call(terminator);
6061
simplify_duplicate_switch_targets(terminator);
@@ -252,6 +253,36 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
252253
terminator.kind = TerminatorKind::Goto { target: *destination_block };
253254
}
254255

256+
// Convert `align_of_val::<[T]>(ptr)` to `align_of::<T>()`, since the
257+
// alignment of a slice doesn't actually depend on metadata at all
258+
// and the element type is always `Sized`.
259+
//
260+
// This is here so it can run after inlining, where it's more useful.
261+
// (LowerIntrinsics is done in cleanup, before the optimization passes.)
262+
fn simplify_align_of_slice_val(
263+
&self,
264+
terminator: &mut Terminator<'tcx>,
265+
statements: &mut Vec<Statement<'tcx>>,
266+
) {
267+
if let TerminatorKind::Call {
268+
func, args, destination, target: Some(destination_block), ..
269+
} = &terminator.kind
270+
&& args.len() == 1
271+
&& let Some((fn_def_id, generics)) = func.const_fn_def()
272+
&& self.tcx.is_intrinsic(fn_def_id, sym::align_of_val)
273+
&& let ty::Slice(elem_ty) = *generics.type_at(0).kind()
274+
{
275+
statements.push(Statement::new(
276+
terminator.source_info,
277+
StatementKind::Assign(Box::new((
278+
*destination,
279+
Rvalue::NullaryOp(NullOp::AlignOf, elem_ty),
280+
))),
281+
));
282+
terminator.kind = TerminatorKind::Goto { target: *destination_block };
283+
}
284+
}
285+
255286
fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
256287
let TerminatorKind::Call { ref func, ref mut unwind, .. } = terminator.kind else {
257288
return;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
- // MIR for `of_val_slice` before InstSimplify-after-simplifycfg
2+
+ // MIR for `of_val_slice` after InstSimplify-after-simplifycfg
3+
4+
fn of_val_slice(_1: &[T]) -> usize {
5+
debug slice => _1;
6+
let mut _0: usize;
7+
let mut _2: *const [T];
8+
9+
bb0: {
10+
StorageLive(_2);
11+
_2 = &raw const (*_1);
12+
- _0 = std::intrinsics::align_of_val::<[T]>(move _2) -> [return: bb1, unwind unreachable];
13+
+ _0 = AlignOf(T);
14+
+ goto -> bb1;
15+
}
16+
17+
bb1: {
18+
StorageDead(_2);
19+
return;
20+
}
21+
}
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ test-mir-pass: InstSimplify-after-simplifycfg
2+
//@ needs-unwind
3+
4+
#![crate_type = "lib"]
5+
#![feature(core_intrinsics)]
6+
7+
// EMIT_MIR align_of_slice.of_val_slice.InstSimplify-after-simplifycfg.diff
8+
pub fn of_val_slice<T>(slice: &[T]) -> usize {
9+
// CHECK-LABEL: fn of_val_slice(_1: &[T])
10+
// CHECK: _0 = AlignOf(T);
11+
unsafe { core::intrinsics::align_of_val(slice) }
12+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 _12: ();
12+
scope 3 {
13+
let _8: std::ptr::alignment::AlignmentEnum;
14+
scope 4 {
15+
scope 12 (inlined Layout::size) {
16+
}
17+
scope 13 (inlined Unique::<[T]>::cast::<u8>) {
18+
scope 14 (inlined NonNull::<[T]>::cast::<u8>) {
19+
scope 15 (inlined NonNull::<[T]>::as_ptr) {
20+
}
21+
}
22+
}
23+
scope 16 (inlined <NonNull<u8> as From<Unique<u8>>>::from) {
24+
scope 17 (inlined Unique::<u8>::as_non_null_ptr) {
25+
}
26+
}
27+
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
28+
let mut _9: *mut u8;
29+
scope 19 (inlined Layout::size) {
30+
}
31+
scope 20 (inlined NonNull::<u8>::as_ptr) {
32+
}
33+
scope 21 (inlined std::alloc::dealloc) {
34+
let mut _11: usize;
35+
scope 22 (inlined Layout::size) {
36+
}
37+
scope 23 (inlined Layout::align) {
38+
scope 24 (inlined std::ptr::Alignment::as_usize) {
39+
let mut _10: u32;
40+
}
41+
}
42+
}
43+
}
44+
}
45+
scope 5 (inlined Unique::<[T]>::as_ptr) {
46+
scope 6 (inlined NonNull::<[T]>::as_ptr) {
47+
}
48+
}
49+
scope 7 (inlined Layout::for_value_raw::<[T]>) {
50+
let mut _5: usize;
51+
let mut _6: usize;
52+
scope 8 {
53+
scope 11 (inlined #[track_caller] Layout::from_size_align_unchecked) {
54+
let mut _7: std::ptr::Alignment;
55+
}
56+
}
57+
scope 9 (inlined size_of_val_raw::<[T]>) {
58+
}
59+
scope 10 (inlined align_of_val_raw::<[T]>) {
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
bb0: {
67+
StorageLive(_2);
68+
_2 = copy (((*_1).0: std::ptr::Unique<[T]>).0: std::ptr::NonNull<[T]>);
69+
StorageLive(_4);
70+
_3 = copy _2 as *mut [T] (Transmute);
71+
_4 = copy _2 as *const [T] (Transmute);
72+
StorageLive(_6);
73+
_5 = std::intrinsics::size_of_val::<[T]>(move _4) -> [return: bb1, unwind unreachable];
74+
}
75+
76+
bb1: {
77+
_6 = AlignOf(T);
78+
StorageLive(_7);
79+
_7 = copy _6 as std::ptr::Alignment (Transmute);
80+
_8 = move (_7.0: std::ptr::alignment::AlignmentEnum);
81+
StorageDead(_7);
82+
StorageDead(_6);
83+
StorageDead(_4);
84+
switchInt(copy _5) -> [0: bb4, otherwise: bb2];
85+
}
86+
87+
bb2: {
88+
StorageLive(_9);
89+
_9 = copy _3 as *mut u8 (PtrToPtr);
90+
StorageLive(_11);
91+
StorageLive(_10);
92+
_10 = discriminant(_8);
93+
_11 = move _10 as usize (IntToInt);
94+
StorageDead(_10);
95+
_12 = alloc::alloc::__rust_dealloc(move _9, move _5, move _11) -> [return: bb3, unwind unreachable];
96+
}
97+
98+
bb3: {
99+
StorageDead(_11);
100+
StorageDead(_9);
101+
goto -> bb4;
102+
}
103+
104+
bb4: {
105+
StorageDead(_2);
106+
return;
107+
}
108+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 _12: ();
12+
scope 3 {
13+
let _8: std::ptr::alignment::AlignmentEnum;
14+
scope 4 {
15+
scope 12 (inlined Layout::size) {
16+
}
17+
scope 13 (inlined Unique::<[T]>::cast::<u8>) {
18+
scope 14 (inlined NonNull::<[T]>::cast::<u8>) {
19+
scope 15 (inlined NonNull::<[T]>::as_ptr) {
20+
}
21+
}
22+
}
23+
scope 16 (inlined <NonNull<u8> as From<Unique<u8>>>::from) {
24+
scope 17 (inlined Unique::<u8>::as_non_null_ptr) {
25+
}
26+
}
27+
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
28+
let mut _9: *mut u8;
29+
scope 19 (inlined Layout::size) {
30+
}
31+
scope 20 (inlined NonNull::<u8>::as_ptr) {
32+
}
33+
scope 21 (inlined std::alloc::dealloc) {
34+
let mut _11: usize;
35+
scope 22 (inlined Layout::size) {
36+
}
37+
scope 23 (inlined Layout::align) {
38+
scope 24 (inlined std::ptr::Alignment::as_usize) {
39+
let mut _10: u32;
40+
}
41+
}
42+
}
43+
}
44+
}
45+
scope 5 (inlined Unique::<[T]>::as_ptr) {
46+
scope 6 (inlined NonNull::<[T]>::as_ptr) {
47+
}
48+
}
49+
scope 7 (inlined Layout::for_value_raw::<[T]>) {
50+
let mut _5: usize;
51+
let mut _6: usize;
52+
scope 8 {
53+
scope 11 (inlined #[track_caller] Layout::from_size_align_unchecked) {
54+
let mut _7: std::ptr::Alignment;
55+
}
56+
}
57+
scope 9 (inlined size_of_val_raw::<[T]>) {
58+
}
59+
scope 10 (inlined align_of_val_raw::<[T]>) {
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
bb0: {
67+
StorageLive(_2);
68+
_2 = copy (((*_1).0: std::ptr::Unique<[T]>).0: std::ptr::NonNull<[T]>);
69+
StorageLive(_4);
70+
_3 = copy _2 as *mut [T] (Transmute);
71+
_4 = copy _2 as *const [T] (Transmute);
72+
StorageLive(_6);
73+
_5 = std::intrinsics::size_of_val::<[T]>(move _4) -> [return: bb1, unwind unreachable];
74+
}
75+
76+
bb1: {
77+
_6 = AlignOf(T);
78+
StorageLive(_7);
79+
_7 = copy _6 as std::ptr::Alignment (Transmute);
80+
_8 = move (_7.0: std::ptr::alignment::AlignmentEnum);
81+
StorageDead(_7);
82+
StorageDead(_6);
83+
StorageDead(_4);
84+
switchInt(copy _5) -> [0: bb4, otherwise: bb2];
85+
}
86+
87+
bb2: {
88+
StorageLive(_9);
89+
_9 = copy _3 as *mut u8 (PtrToPtr);
90+
StorageLive(_11);
91+
StorageLive(_10);
92+
_10 = discriminant(_8);
93+
_11 = move _10 as usize (IntToInt);
94+
StorageDead(_10);
95+
_12 = alloc::alloc::__rust_dealloc(move _9, move _5, move _11) -> [return: bb3, unwind unreachable];
96+
}
97+
98+
bb3: {
99+
StorageDead(_11);
100+
StorageDead(_9);
101+
goto -> bb4;
102+
}
103+
104+
bb4: {
105+
StorageDead(_2);
106+
return;
107+
}
108+
}

0 commit comments

Comments
 (0)