From 61517dbbe6f1f41c935007fcdca5eba8be7477e2 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Mon, 6 May 2024 01:44:27 -0700 Subject: [PATCH] Avoid a cast in `ptr::slice_from_raw_parts(_mut)` Casting to `*const ()` or `*mut ()` just bloats the MIR, so let's not. If ACP#362 goes through we can keep calling `ptr::from_raw_parts(_mut)` in these also without the cast, but that hasn't had any libs-api attention yet, so I'm not waiting on it. --- library/core/src/ptr/mod.rs | 4 +- .../inline_shims.drop.Inline.panic-abort.diff | 56 +++++++++++++++---- ...mut_range.PreCodegen.after.panic-abort.mir | 47 ++++++++++++---- ...ut_range.PreCodegen.after.panic-unwind.mir | 47 ++++++++++++---- ...ked_range.PreCodegen.after.panic-abort.mir | 10 +--- ...ed_range.PreCodegen.after.panic-unwind.mir | 10 +--- ..._to_slice.PreCodegen.after.panic-abort.mir | 17 +----- ...to_slice.PreCodegen.after.panic-unwind.mir | 17 +----- 8 files changed, 130 insertions(+), 78 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 5f3815859c8fa..8d7192c1b0fd1 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -812,7 +812,7 @@ pub const fn from_mut(r: &mut T) -> *mut T { #[rustc_allow_const_fn_unstable(ptr_metadata)] #[rustc_diagnostic_item = "ptr_slice_from_raw_parts"] pub const fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { - from_raw_parts(data.cast(), len) + intrinsics::aggregate_raw_ptr(data, len) } /// Forms a raw mutable slice from a pointer and a length. @@ -858,7 +858,7 @@ pub const fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { #[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")] #[rustc_diagnostic_item = "ptr_slice_from_raw_parts_mut"] pub const fn slice_from_raw_parts_mut(data: *mut T, len: usize) -> *mut [T] { - from_raw_parts_mut(data.cast(), len) + intrinsics::aggregate_raw_ptr(data, len) } /// Swaps the values at two mutable locations of the same type, without diff --git a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff index 2a36ccaab110a..df873600577a3 100644 --- a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff @@ -11,10 +11,28 @@ + scope 1 (inlined std::ptr::drop_in_place::> - shim(Some(Vec))) { + let mut _6: &mut std::vec::Vec; + let mut _7: (); ++ scope 2 (inlined as Drop>::drop) { ++ let mut _8: *mut [A]; ++ let mut _9: *mut A; ++ let mut _10: usize; ++ scope 3 (inlined Vec::::as_mut_ptr) { ++ let mut _11: &alloc::raw_vec::RawVec; ++ scope 4 (inlined alloc::raw_vec::RawVec::::ptr) { ++ let mut _13: std::ptr::NonNull; ++ scope 5 (inlined Unique::::as_ptr) { ++ scope 6 (inlined NonNull::::as_ptr) { ++ let mut _12: *const A; ++ } ++ } ++ } ++ } ++ scope 7 (inlined slice_from_raw_parts_mut::) { ++ } ++ } + } -+ scope 2 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { -+ let mut _8: isize; -+ let mut _9: isize; ++ scope 8 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { ++ let mut _14: isize; ++ let mut _15: isize; + } bb0: { @@ -25,7 +43,24 @@ + StorageLive(_6); + StorageLive(_7); + _6 = &mut (*_4); -+ _7 = as Drop>::drop(move _6) -> [return: bb2, unwind unreachable]; ++ StorageLive(_8); ++ StorageLive(_9); ++ StorageLive(_11); ++ _11 = &((*_6).0: alloc::raw_vec::RawVec); ++ StorageLive(_13); ++ _13 = ((((*_6).0: alloc::raw_vec::RawVec).0: std::ptr::Unique).0: std::ptr::NonNull); ++ StorageLive(_12); ++ _12 = (_13.0: *const A); ++ _9 = move _12 as *mut A (PtrToPtr); ++ StorageDead(_12); ++ StorageDead(_13); ++ StorageDead(_11); ++ StorageLive(_10); ++ _10 = ((*_6).1: usize); ++ _8 = *mut [A] from (_9, _10); ++ StorageDead(_10); ++ StorageDead(_9); ++ _7 = std::ptr::drop_in_place::<[A]>(move _8) -> [return: bb2, unwind unreachable]; } bb1: { @@ -36,19 +71,20 @@ StorageLive(_5); _5 = _2; - _0 = std::ptr::drop_in_place::>(move _5) -> [return: bb2, unwind unreachable]; -+ StorageLive(_8); -+ StorageLive(_9); -+ _8 = discriminant((*_5)); -+ switchInt(move _8) -> [0: bb3, otherwise: bb4]; ++ StorageLive(_14); ++ StorageLive(_15); ++ _14 = discriminant((*_5)); ++ switchInt(move _14) -> [0: bb3, otherwise: bb4]; } bb2: { ++ StorageDead(_8); + drop(((*_4).0: alloc::raw_vec::RawVec)) -> [return: bb1, unwind unreachable]; + } + + bb3: { -+ StorageDead(_9); -+ StorageDead(_8); ++ StorageDead(_15); ++ StorageDead(_14); StorageDead(_5); return; + } diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir index ef3f4a2172005..62fe1a8685780 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir @@ -4,20 +4,47 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range) -> debug slice => _1; debug index => _2; let mut _0: &mut [u32]; + let mut _3: usize; + let mut _4: usize; scope 1 (inlined core::slice::::get_unchecked_mut::>) { - let mut _3: *mut [u32]; - let mut _4: *mut [u32]; + let mut _5: *mut [u32]; + let mut _9: *mut [u32]; + scope 2 (inlined as SliceIndex<[u32]>>::get_unchecked_mut) { + let _6: usize; + let mut _7: *mut u32; + let mut _8: *mut u32; + scope 3 { + scope 6 (inlined std::ptr::mut_ptr::::as_mut_ptr) { + } + scope 7 (inlined std::ptr::mut_ptr::::add) { + } + scope 8 (inlined slice_from_raw_parts_mut::) { + } + } + scope 4 (inlined std::ptr::mut_ptr::::len) { + scope 5 (inlined std::ptr::metadata::<[u32]>) { + } + } + } } bb0: { - StorageLive(_3); - _3 = &raw mut (*_1); - _4 = as SliceIndex<[u32]>>::get_unchecked_mut(move _2, move _3) -> [return: bb1, unwind unreachable]; - } - - bb1: { - StorageDead(_3); - _0 = &mut (*_4); + _3 = move (_2.0: usize); + _4 = move (_2.1: usize); + StorageLive(_5); + _5 = &raw mut (*_1); + StorageLive(_6); + _6 = SubUnchecked(_4, _3); + StorageLive(_8); + StorageLive(_7); + _7 = _5 as *mut u32 (PtrToPtr); + _8 = Offset(_7, _3); + StorageDead(_7); + _9 = *mut [u32] from (_8, _6); + StorageDead(_8); + StorageDead(_6); + StorageDead(_5); + _0 = &mut (*_9); return; } } diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir index 9e93a43ac725b..62fe1a8685780 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir @@ -4,20 +4,47 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range) -> debug slice => _1; debug index => _2; let mut _0: &mut [u32]; + let mut _3: usize; + let mut _4: usize; scope 1 (inlined core::slice::::get_unchecked_mut::>) { - let mut _3: *mut [u32]; - let mut _4: *mut [u32]; + let mut _5: *mut [u32]; + let mut _9: *mut [u32]; + scope 2 (inlined as SliceIndex<[u32]>>::get_unchecked_mut) { + let _6: usize; + let mut _7: *mut u32; + let mut _8: *mut u32; + scope 3 { + scope 6 (inlined std::ptr::mut_ptr::::as_mut_ptr) { + } + scope 7 (inlined std::ptr::mut_ptr::::add) { + } + scope 8 (inlined slice_from_raw_parts_mut::) { + } + } + scope 4 (inlined std::ptr::mut_ptr::::len) { + scope 5 (inlined std::ptr::metadata::<[u32]>) { + } + } + } } bb0: { - StorageLive(_3); - _3 = &raw mut (*_1); - _4 = as SliceIndex<[u32]>>::get_unchecked_mut(move _2, move _3) -> [return: bb1, unwind continue]; - } - - bb1: { - StorageDead(_3); - _0 = &mut (*_4); + _3 = move (_2.0: usize); + _4 = move (_2.1: usize); + StorageLive(_5); + _5 = &raw mut (*_1); + StorageLive(_6); + _6 = SubUnchecked(_4, _3); + StorageLive(_8); + StorageLive(_7); + _7 = _5 as *mut u32 (PtrToPtr); + _8 = Offset(_7, _3); + StorageDead(_7); + _9 = *mut [u32] from (_8, _6); + StorageDead(_8); + StorageDead(_6); + StorageDead(_5); + _0 = &mut (*_9); return; } } diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-abort.mir index 018ff6c357d17..000432ccca7de 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-abort.mir @@ -17,11 +17,6 @@ fn slice_ptr_get_unchecked_range(_1: *const [u32], _2: std::ops::Range) - scope 7 (inlined std::ptr::const_ptr::::add) { } scope 8 (inlined slice_from_raw_parts::) { - let mut _8: *const (); - scope 9 (inlined std::ptr::const_ptr::::cast::<()>) { - } - scope 10 (inlined std::ptr::from_raw_parts::<[u32]>) { - } } } scope 4 (inlined std::ptr::const_ptr::::len) { @@ -41,10 +36,7 @@ fn slice_ptr_get_unchecked_range(_1: *const [u32], _2: std::ops::Range) - _6 = _1 as *const u32 (PtrToPtr); _7 = Offset(_6, _3); StorageDead(_6); - StorageLive(_8); - _8 = _7 as *const () (PtrToPtr); - _0 = *const [u32] from (_8, _5); - StorageDead(_8); + _0 = *const [u32] from (_7, _5); StorageDead(_7); StorageDead(_5); return; diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-unwind.mir index 018ff6c357d17..000432ccca7de 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-unwind.mir @@ -17,11 +17,6 @@ fn slice_ptr_get_unchecked_range(_1: *const [u32], _2: std::ops::Range) - scope 7 (inlined std::ptr::const_ptr::::add) { } scope 8 (inlined slice_from_raw_parts::) { - let mut _8: *const (); - scope 9 (inlined std::ptr::const_ptr::::cast::<()>) { - } - scope 10 (inlined std::ptr::from_raw_parts::<[u32]>) { - } } } scope 4 (inlined std::ptr::const_ptr::::len) { @@ -41,10 +36,7 @@ fn slice_ptr_get_unchecked_range(_1: *const [u32], _2: std::ops::Range) - _6 = _1 as *const u32 (PtrToPtr); _7 = Offset(_6, _3); StorageDead(_6); - StorageLive(_8); - _8 = _7 as *const () (PtrToPtr); - _0 = *const [u32] from (_8, _5); - StorageDead(_8); + _0 = *const [u32] from (_7, _5); StorageDead(_7); StorageDead(_5); return; diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir index 18728d543ad8b..eabecaed05178 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir @@ -25,7 +25,7 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) { debug data => _4; debug len => _5; - let _7: *const [u8]; + let _6: *const [u8]; scope 7 (inlined core::ub_checks::check_language_ub) { scope 8 (inlined core::ub_checks::check_language_ub::runtime) { } @@ -37,14 +37,6 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { scope 11 (inlined slice_from_raw_parts::) { debug data => _4; debug len => _5; - let mut _6: *const (); - scope 12 (inlined std::ptr::const_ptr::::cast::<()>) { - debug self => _4; - } - scope 13 (inlined std::ptr::from_raw_parts::<[u8]>) { - debug data_pointer => _6; - debug metadata => _5; - } } } } @@ -60,13 +52,10 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { StorageDead(_2); StorageLive(_5); _5 = ((*_1).1: usize); - StorageLive(_6); - _6 = _4 as *const () (PtrToPtr); - _7 = *const [u8] from (_6, _5); - StorageDead(_6); + _6 = *const [u8] from (_4, _5); StorageDead(_5); StorageDead(_4); - _0 = &(*_7); + _0 = &(*_6); return; } } diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir index 18728d543ad8b..eabecaed05178 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir @@ -25,7 +25,7 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) { debug data => _4; debug len => _5; - let _7: *const [u8]; + let _6: *const [u8]; scope 7 (inlined core::ub_checks::check_language_ub) { scope 8 (inlined core::ub_checks::check_language_ub::runtime) { } @@ -37,14 +37,6 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { scope 11 (inlined slice_from_raw_parts::) { debug data => _4; debug len => _5; - let mut _6: *const (); - scope 12 (inlined std::ptr::const_ptr::::cast::<()>) { - debug self => _4; - } - scope 13 (inlined std::ptr::from_raw_parts::<[u8]>) { - debug data_pointer => _6; - debug metadata => _5; - } } } } @@ -60,13 +52,10 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { StorageDead(_2); StorageLive(_5); _5 = ((*_1).1: usize); - StorageLive(_6); - _6 = _4 as *const () (PtrToPtr); - _7 = *const [u8] from (_6, _5); - StorageDead(_6); + _6 = *const [u8] from (_4, _5); StorageDead(_5); StorageDead(_4); - _0 = &(*_7); + _0 = &(*_6); return; } }