InstSimplify from_raw_parts(p, ())p as _

This commit is contained in:
Scott McMurray 2024-04-14 11:12:32 -07:00
parent de64ff76f8
commit 9520cebfc5
6 changed files with 48 additions and 41 deletions

View File

@ -36,6 +36,7 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
ctx.simplify_ref_deref(&statement.source_info, rvalue);
ctx.simplify_len(&statement.source_info, rvalue);
ctx.simplify_ptr_aggregate(&statement.source_info, rvalue);
ctx.simplify_cast(rvalue);
}
_ => {}
@ -58,8 +59,17 @@ struct InstSimplifyContext<'tcx, 'a> {
impl<'tcx> InstSimplifyContext<'tcx, '_> {
fn should_simplify(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
self.should_simplify_custom(source_info, "Rvalue", rvalue)
}
fn should_simplify_custom(
&self,
source_info: &SourceInfo,
label: &str,
value: impl std::fmt::Debug,
) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstSimplify - Rvalue: {rvalue:?} SourceInfo: {source_info:?}")
format!("InstSimplify - {label}: {value:?} SourceInfo: {source_info:?}")
})
}
@ -147,6 +157,30 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
}
}
/// Transform "Aggregate(RawPtr, \[p, ()\])" ==> "Cast(PtrToPtr, p)".
fn simplify_ptr_aggregate(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue
{
let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx);
if meta_ty.is_unit() {
// The mutable borrows we're holding prevent printing `rvalue` here
if !self.should_simplify_custom(
source_info,
"Aggregate::RawPtr",
(&pointee_ty, *mutability, &fields),
) {
return;
}
let mut fields = std::mem::take(fields);
let _meta = fields.pop().unwrap();
let data = fields.pop().unwrap();
let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
*rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
}
}
}
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());

View File

@ -1,6 +1,7 @@
//@ test-mir-pass: InstSimplify
//@ compile-flags: -Zinline-mir
#![crate_type = "lib"]
#![feature(core_intrinsics)]
#[inline(always)]
fn generic_cast<T, U>(x: *const T) -> *const U {
@ -23,3 +24,11 @@ pub fn roundtrip(x: *const u8) -> *const u8 {
// CHECK: _2 = move _3 as *const u8 (PointerCoercion(MutToConstPointer));
x as *mut u8 as *const u8
}
// EMIT_MIR casts.roundtrip.InstSimplify.diff
pub fn cast_thin_via_aggregate(x: *const u8) -> *const () {
// CHECK-LABEL: fn cast_thin_via_aggregate(
// CHECK: _2 = _1;
// CHECK: _0 = move _2 as *const () (PtrToPtr);
std::intrinsics::aggregate_raw_ptr(x, ())
}

View File

@ -28,8 +28,6 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
debug data_pointer => _5;
debug metadata => _7;
let mut _8: std::ptr::metadata::PtrComponents<[u32]>;
let mut _9: std::ptr::metadata::PtrRepr<[u32]>;
}
}
}
@ -47,13 +45,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
_6 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: _1 };
_7 = ((_6.2: std::ptr::metadata::PtrComponents<[u32]>).1: usize);
StorageDead(_6);
StorageLive(_9);
StorageLive(_8);
_8 = std::ptr::metadata::PtrComponents::<[u32]> { data_pointer: _5, metadata: _7 };
_9 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _8 };
StorageDead(_8);
_0 = (_9.0: *const [u32]);
StorageDead(_9);
_0 = *const [u32] from (_5, _7);
StorageDead(_7);
StorageDead(_5);
StorageDead(_4);

View File

@ -28,8 +28,6 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
debug data_pointer => _5;
debug metadata => _7;
let mut _8: std::ptr::metadata::PtrComponents<[u32]>;
let mut _9: std::ptr::metadata::PtrRepr<[u32]>;
}
}
}
@ -47,13 +45,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
_6 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: _1 };
_7 = ((_6.2: std::ptr::metadata::PtrComponents<[u32]>).1: usize);
StorageDead(_6);
StorageLive(_9);
StorageLive(_8);
_8 = std::ptr::metadata::PtrComponents::<[u32]> { data_pointer: _5, metadata: _7 };
_9 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _8 };
StorageDead(_8);
_0 = (_9.0: *const [u32]);
StorageDead(_9);
_0 = *const [u32] from (_5, _7);
StorageDead(_7);
StorageDead(_5);
StorageDead(_4);

View File

@ -26,29 +26,19 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
debug data_pointer => _5;
debug metadata => const ();
let mut _6: std::ptr::metadata::PtrComponents<u32>;
let mut _7: std::ptr::metadata::PtrRepr<u32>;
}
}
}
bb0: {
StorageLive(_4);
StorageLive(_3);
_3 = _1 as *const u8 (PtrToPtr);
_4 = Offset(_3, _2);
StorageDead(_3);
StorageLive(_5);
_5 = _4 as *const () (PtrToPtr);
StorageLive(_7);
StorageLive(_6);
_6 = std::ptr::metadata::PtrComponents::<u32> { data_pointer: _5, metadata: const () };
_7 = std::ptr::metadata::PtrRepr::<u32> { const_ptr: move _6 };
StorageDead(_6);
_0 = (_7.0: *const u32);
StorageDead(_7);
_0 = _4 as *const u32 (PtrToPtr);
StorageDead(_5);
StorageDead(_4);
return;
}
}

View File

@ -26,29 +26,19 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
debug data_pointer => _5;
debug metadata => const ();
let mut _6: std::ptr::metadata::PtrComponents<u32>;
let mut _7: std::ptr::metadata::PtrRepr<u32>;
}
}
}
bb0: {
StorageLive(_4);
StorageLive(_3);
_3 = _1 as *const u8 (PtrToPtr);
_4 = Offset(_3, _2);
StorageDead(_3);
StorageLive(_5);
_5 = _4 as *const () (PtrToPtr);
StorageLive(_7);
StorageLive(_6);
_6 = std::ptr::metadata::PtrComponents::<u32> { data_pointer: _5, metadata: const () };
_7 = std::ptr::metadata::PtrRepr::<u32> { const_ptr: move _6 };
StorageDead(_6);
_0 = (_7.0: *const u32);
StorageDead(_7);
_0 = _4 as *const u32 (PtrToPtr);
StorageDead(_5);
StorageDead(_4);
return;
}
}