From 4b03b97226391b745d3ee033c325b8976f14e397 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 17 Feb 2026 00:31:19 -0800 Subject: [PATCH] What if we discourage mir-inlining of `box_new_uninit`? --- library/alloc/src/boxed.rs | 4 + .../item-collection/opaque-return-impls.rs | 1 + ...ng_operand.test.GVN.32bit.panic-abort.diff | 93 +++---------------- ...ng_operand.test.GVN.64bit.panic-abort.diff | 93 +++---------------- 4 files changed, 35 insertions(+), 156 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 8b0546433491..ae16a8401552 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -238,6 +238,10 @@ pub struct Box< /// Monomorphic function for allocating an uninit `Box`. #[inline] +// The is a separate function to avoid doing it in every generic version, but it +// looks small to the mir inliner (particularly in panic=abort) so leave it to +// the backend to decide whether pulling it in everywhere is worth doing. +#[rustc_no_mir_inline] #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg(not(no_global_oom_handling))] fn box_new_uninit(layout: Layout) -> *mut u8 { diff --git a/tests/codegen-units/item-collection/opaque-return-impls.rs b/tests/codegen-units/item-collection/opaque-return-impls.rs index 63ee9afe34ab..1c32ddf6254b 100644 --- a/tests/codegen-units/item-collection/opaque-return-impls.rs +++ b/tests/codegen-units/item-collection/opaque-return-impls.rs @@ -45,6 +45,7 @@ pub fn foo2() -> Box { //~ MONO_ITEM fn foo2 //~ MONO_ITEM fn std::alloc::Global::alloc_impl_runtime //~ MONO_ITEM fn std::boxed::Box::::new +//~ MONO_ITEM fn std::boxed::box_new_uninit //~ MONO_ITEM fn std::ptr::NonNull::::new_unchecked::precondition_check struct Counter { diff --git a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff index ab222c968da1..a83771f740a2 100644 --- a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff +++ b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff @@ -11,8 +11,7 @@ let mut _9: *const [()]; let mut _10: std::boxed::Box<()>; let mut _11: *const (); - let mut _14: std::alloc::Layout; - let mut _20: usize; + let mut _14: usize; scope 1 { debug vp_ctx => _1; let _5: *const (); @@ -25,12 +24,12 @@ scope 4 { debug _x => _8; } - scope 18 (inlined foo) { - let mut _21: *const [()]; + scope 9 (inlined foo) { + let mut _15: *const [()]; } } - scope 16 (inlined slice_from_raw_parts::<()>) { - scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) { + scope 7 (inlined slice_from_raw_parts::<()>) { + scope 8 (inlined std::ptr::from_raw_parts::<[()], ()>) { } } } @@ -40,29 +39,6 @@ let mut _13: *mut u8; scope 6 { } - scope 7 (inlined boxed::box_new_uninit) { - let mut _15: std::result::Result, std::alloc::AllocError>; - let mut _16: isize; - let _17: std::ptr::NonNull<[u8]>; - let mut _18: !; - scope 8 { - scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) { - scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) { - scope 13 (inlined NonNull::<[u8]>::cast::) { - let mut _19: *mut [u8]; - scope 14 (inlined NonNull::<[u8]>::as_ptr) { - } - } - } - scope 15 (inlined NonNull::::as_ptr) { - } - } - } - scope 9 (inlined ::allocate) { - scope 10 (inlined std::alloc::Global::alloc_impl) { - } - } - } } bb0: { @@ -74,15 +50,7 @@ + _4 = const (); StorageLive(_12); StorageLive(_13); - StorageLive(_14); -- _14 = const <() as std::mem::SizedTypeProperties>::LAYOUT; -+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}; - StorageLive(_16); - StorageLive(_17); - StorageLive(_18); - StorageLive(_15); -- _15 = std::alloc::Global::alloc_impl_runtime(copy _14, const false) -> [return: bb5, unwind unreachable]; -+ _15 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}, const false) -> [return: bb5, unwind unreachable]; + _13 = boxed::box_new_uninit(const <() as std::mem::SizedTypeProperties>::LAYOUT) -> [return: bb2, unwind unreachable]; } bb1: { @@ -92,30 +60,8 @@ } bb2: { - unreachable; - } - - bb3: { -- _18 = handle_alloc_error(move _14) -> unwind unreachable; -+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable; - } - - bb4: { - _17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>); -- StorageLive(_19); -+ nop; - _19 = copy _17 as *mut [u8] (Transmute); - _13 = copy _19 as *mut u8 (PtrToPtr); -- StorageDead(_19); -+ nop; - StorageDead(_15); - StorageDead(_18); - StorageDead(_17); - StorageDead(_16); - StorageDead(_14); -- _12 = copy _13 as *mut () (PtrToPtr); + _12 = copy _13 as *mut () (PtrToPtr); - (*_12) = move _4; -+ _12 = copy _19 as *mut () (PtrToPtr); + (*_12) = const (); _3 = copy _13 as std::boxed::Box<()> (Transmute); StorageDead(_13); @@ -133,21 +79,21 @@ + nop; StorageLive(_7); _7 = copy _5; - StorageLive(_20); - _20 = const 1_usize; -- _6 = *const [()] from (copy _7, copy _20); + StorageLive(_14); + _14 = const 1_usize; +- _6 = *const [()] from (copy _7, copy _14); + _6 = *const [()] from (copy _5, const 1_usize); - StorageDead(_20); + StorageDead(_14); StorageDead(_7); StorageLive(_8); StorageLive(_9); _9 = copy _6; - StorageLive(_21); -- _21 = copy _9; + StorageLive(_15); +- _15 = copy _9; - _8 = copy _9 as *mut () (PtrToPtr); -+ _21 = copy _6; ++ _15 = copy _6; + _8 = copy _5 as *mut () (PtrToPtr); - StorageDead(_21); + StorageDead(_15); StorageDead(_9); _0 = const (); StorageDead(_8); @@ -157,14 +103,5 @@ + nop; drop(_3) -> [return: bb1, unwind unreachable]; } - - bb5: { - _16 = discriminant(_15); - switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2]; - } -+ } -+ -+ ALLOC0 (size: 8, align: 4) { -+ 01 00 00 00 00 00 00 00 │ ........ } diff --git a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff index 923c7bfd1d99..a83771f740a2 100644 --- a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff +++ b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff @@ -11,8 +11,7 @@ let mut _9: *const [()]; let mut _10: std::boxed::Box<()>; let mut _11: *const (); - let mut _14: std::alloc::Layout; - let mut _20: usize; + let mut _14: usize; scope 1 { debug vp_ctx => _1; let _5: *const (); @@ -25,12 +24,12 @@ scope 4 { debug _x => _8; } - scope 18 (inlined foo) { - let mut _21: *const [()]; + scope 9 (inlined foo) { + let mut _15: *const [()]; } } - scope 16 (inlined slice_from_raw_parts::<()>) { - scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) { + scope 7 (inlined slice_from_raw_parts::<()>) { + scope 8 (inlined std::ptr::from_raw_parts::<[()], ()>) { } } } @@ -40,29 +39,6 @@ let mut _13: *mut u8; scope 6 { } - scope 7 (inlined boxed::box_new_uninit) { - let mut _15: std::result::Result, std::alloc::AllocError>; - let mut _16: isize; - let _17: std::ptr::NonNull<[u8]>; - let mut _18: !; - scope 8 { - scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) { - scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) { - scope 13 (inlined NonNull::<[u8]>::cast::) { - let mut _19: *mut [u8]; - scope 14 (inlined NonNull::<[u8]>::as_ptr) { - } - } - } - scope 15 (inlined NonNull::::as_ptr) { - } - } - } - scope 9 (inlined ::allocate) { - scope 10 (inlined std::alloc::Global::alloc_impl) { - } - } - } } bb0: { @@ -74,15 +50,7 @@ + _4 = const (); StorageLive(_12); StorageLive(_13); - StorageLive(_14); -- _14 = const <() as std::mem::SizedTypeProperties>::LAYOUT; -+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}; - StorageLive(_16); - StorageLive(_17); - StorageLive(_18); - StorageLive(_15); -- _15 = std::alloc::Global::alloc_impl_runtime(copy _14, const false) -> [return: bb5, unwind unreachable]; -+ _15 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}, const false) -> [return: bb5, unwind unreachable]; + _13 = boxed::box_new_uninit(const <() as std::mem::SizedTypeProperties>::LAYOUT) -> [return: bb2, unwind unreachable]; } bb1: { @@ -92,30 +60,8 @@ } bb2: { - unreachable; - } - - bb3: { -- _18 = handle_alloc_error(move _14) -> unwind unreachable; -+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable; - } - - bb4: { - _17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>); -- StorageLive(_19); -+ nop; - _19 = copy _17 as *mut [u8] (Transmute); - _13 = copy _19 as *mut u8 (PtrToPtr); -- StorageDead(_19); -+ nop; - StorageDead(_15); - StorageDead(_18); - StorageDead(_17); - StorageDead(_16); - StorageDead(_14); -- _12 = copy _13 as *mut () (PtrToPtr); + _12 = copy _13 as *mut () (PtrToPtr); - (*_12) = move _4; -+ _12 = copy _19 as *mut () (PtrToPtr); + (*_12) = const (); _3 = copy _13 as std::boxed::Box<()> (Transmute); StorageDead(_13); @@ -133,21 +79,21 @@ + nop; StorageLive(_7); _7 = copy _5; - StorageLive(_20); - _20 = const 1_usize; -- _6 = *const [()] from (copy _7, copy _20); + StorageLive(_14); + _14 = const 1_usize; +- _6 = *const [()] from (copy _7, copy _14); + _6 = *const [()] from (copy _5, const 1_usize); - StorageDead(_20); + StorageDead(_14); StorageDead(_7); StorageLive(_8); StorageLive(_9); _9 = copy _6; - StorageLive(_21); -- _21 = copy _9; + StorageLive(_15); +- _15 = copy _9; - _8 = copy _9 as *mut () (PtrToPtr); -+ _21 = copy _6; ++ _15 = copy _6; + _8 = copy _5 as *mut () (PtrToPtr); - StorageDead(_21); + StorageDead(_15); StorageDead(_9); _0 = const (); StorageDead(_8); @@ -157,14 +103,5 @@ + nop; drop(_3) -> [return: bb1, unwind unreachable]; } - - bb5: { - _16 = discriminant(_15); - switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2]; - } -+ } -+ -+ ALLOC0 (size: 16, align: 8) { -+ 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ }