Skip to content

Commit df5acf1

Browse files
committed
InstSimplify from_raw_parts(p, ())p as _
1 parent 467c53a commit df5acf1

6 files changed

+48
-41
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
3636
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
3737
ctx.simplify_ref_deref(&statement.source_info, rvalue);
3838
ctx.simplify_len(&statement.source_info, rvalue);
39+
ctx.simplify_ptr_aggregate(&statement.source_info, rvalue);
3940
ctx.simplify_cast(rvalue);
4041
}
4142
_ => {}
@@ -58,8 +59,17 @@ struct InstSimplifyContext<'tcx, 'a> {
5859

5960
impl<'tcx> InstSimplifyContext<'tcx, '_> {
6061
fn should_simplify(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
62+
self.should_simplify_custom(source_info, "Rvalue", rvalue)
63+
}
64+
65+
fn should_simplify_custom(
66+
&self,
67+
source_info: &SourceInfo,
68+
label: &str,
69+
value: impl std::fmt::Debug,
70+
) -> bool {
6171
self.tcx.consider_optimizing(|| {
62-
format!("InstSimplify - Rvalue: {rvalue:?} SourceInfo: {source_info:?}")
72+
format!("InstSimplify - {label}: {value:?} SourceInfo: {source_info:?}")
6373
})
6474
}
6575

@@ -147,6 +157,30 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
147157
}
148158
}
149159

160+
/// Transform "Aggregate(RawPtr, \[p, ()\])" ==> "Cast(PtrToPtr, p)".
161+
fn simplify_ptr_aggregate(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
162+
if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue
163+
{
164+
let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx);
165+
if meta_ty.is_unit() {
166+
// The mutable borrows we're holding prevent printing `rvalue` here
167+
if !self.should_simplify_custom(
168+
source_info,
169+
"Aggregate::RawPtr",
170+
(&pointee_ty, *mutability, &fields),
171+
) {
172+
return;
173+
}
174+
175+
let mut fields = std::mem::take(fields);
176+
let _meta = fields.pop().unwrap();
177+
let data = fields.pop().unwrap();
178+
let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
179+
*rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
180+
}
181+
}
182+
}
183+
150184
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
151185
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
152186
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());

tests/mir-opt/instsimplify/casts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ unit-test: InstSimplify
22
//@ compile-flags: -Zinline-mir
33
#![crate_type = "lib"]
4+
#![feature(core_intrinsics)]
45

56
#[inline(always)]
67
fn generic_cast<T, U>(x: *const T) -> *const U {
@@ -23,3 +24,11 @@ pub fn roundtrip(x: *const u8) -> *const u8 {
2324
// CHECK: _2 = move _3 as *const u8 (PointerCoercion(MutToConstPointer));
2425
x as *mut u8 as *const u8
2526
}
27+
28+
// EMIT_MIR casts.roundtrip.InstSimplify.diff
29+
pub fn cast_thin_via_aggregate(x: *const u8) -> *const () {
30+
// CHECK-LABEL: fn cast_thin_via_aggregate(
31+
// CHECK: _2 = _1;
32+
// CHECK: _0 = move _2 as *const () (PtrToPtr);
33+
std::intrinsics::aggregate_raw_ptr(x, ())
34+
}

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_fat.PreCodegen.after.panic-abort.mir

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
2828
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
2929
debug data_pointer => _5;
3030
debug metadata => _7;
31-
let mut _8: std::ptr::metadata::PtrComponents<[u32]>;
32-
let mut _9: std::ptr::metadata::PtrRepr<[u32]>;
3331
}
3432
}
3533
}
@@ -47,13 +45,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
4745
_6 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: _1 };
4846
_7 = ((_6.2: std::ptr::metadata::PtrComponents<[u32]>).1: usize);
4947
StorageDead(_6);
50-
StorageLive(_9);
51-
StorageLive(_8);
52-
_8 = std::ptr::metadata::PtrComponents::<[u32]> { data_pointer: _5, metadata: _7 };
53-
_9 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _8 };
54-
StorageDead(_8);
55-
_0 = (_9.0: *const [u32]);
56-
StorageDead(_9);
48+
_0 = *const [u32] from (_5, _7);
5749
StorageDead(_7);
5850
StorageDead(_5);
5951
StorageDead(_4);

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_fat.PreCodegen.after.panic-unwind.mir

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
2828
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
2929
debug data_pointer => _5;
3030
debug metadata => _7;
31-
let mut _8: std::ptr::metadata::PtrComponents<[u32]>;
32-
let mut _9: std::ptr::metadata::PtrRepr<[u32]>;
3331
}
3432
}
3533
}
@@ -47,13 +45,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
4745
_6 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: _1 };
4846
_7 = ((_6.2: std::ptr::metadata::PtrComponents<[u32]>).1: usize);
4947
StorageDead(_6);
50-
StorageLive(_9);
51-
StorageLive(_8);
52-
_8 = std::ptr::metadata::PtrComponents::<[u32]> { data_pointer: _5, metadata: _7 };
53-
_9 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _8 };
54-
StorageDead(_8);
55-
_0 = (_9.0: *const [u32]);
56-
StorageDead(_9);
48+
_0 = *const [u32] from (_5, _7);
5749
StorageDead(_7);
5850
StorageDead(_5);
5951
StorageDead(_4);

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_thin.PreCodegen.after.panic-abort.mir

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,19 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
2626
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
2727
debug data_pointer => _5;
2828
debug metadata => const ();
29-
let mut _6: std::ptr::metadata::PtrComponents<u32>;
30-
let mut _7: std::ptr::metadata::PtrRepr<u32>;
3129
}
3230
}
3331
}
3432

3533
bb0: {
36-
StorageLive(_4);
3734
StorageLive(_3);
3835
_3 = _1 as *const u8 (PtrToPtr);
3936
_4 = Offset(_3, _2);
4037
StorageDead(_3);
4138
StorageLive(_5);
4239
_5 = _4 as *const () (PtrToPtr);
43-
StorageLive(_7);
44-
StorageLive(_6);
45-
_6 = std::ptr::metadata::PtrComponents::<u32> { data_pointer: _5, metadata: const () };
46-
_7 = std::ptr::metadata::PtrRepr::<u32> { const_ptr: move _6 };
47-
StorageDead(_6);
48-
_0 = (_7.0: *const u32);
49-
StorageDead(_7);
40+
_0 = _4 as *const u32 (PtrToPtr);
5041
StorageDead(_5);
51-
StorageDead(_4);
5242
return;
5343
}
5444
}

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_thin.PreCodegen.after.panic-unwind.mir

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,19 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
2626
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
2727
debug data_pointer => _5;
2828
debug metadata => const ();
29-
let mut _6: std::ptr::metadata::PtrComponents<u32>;
30-
let mut _7: std::ptr::metadata::PtrRepr<u32>;
3129
}
3230
}
3331
}
3432

3533
bb0: {
36-
StorageLive(_4);
3734
StorageLive(_3);
3835
_3 = _1 as *const u8 (PtrToPtr);
3936
_4 = Offset(_3, _2);
4037
StorageDead(_3);
4138
StorageLive(_5);
4239
_5 = _4 as *const () (PtrToPtr);
43-
StorageLive(_7);
44-
StorageLive(_6);
45-
_6 = std::ptr::metadata::PtrComponents::<u32> { data_pointer: _5, metadata: const () };
46-
_7 = std::ptr::metadata::PtrRepr::<u32> { const_ptr: move _6 };
47-
StorageDead(_6);
48-
_0 = (_7.0: *const u32);
49-
StorageDead(_7);
40+
_0 = _4 as *const u32 (PtrToPtr);
5041
StorageDead(_5);
51-
StorageDead(_4);
5242
return;
5343
}
5444
}

0 commit comments

Comments
 (0)