Skip to content

Reduce inlining cost for statement #116708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/cost_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};

const INSTR_COST: usize = 5;
const INSTR_COST: usize = 1;
const CALL_PENALTY: usize = 25;
const LANDINGPAD_PENALTY: usize = 50;
const RESUME_PENALTY: usize = 45;
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,13 @@ impl<T: ?Sized> NonNull<T> {
/// ```
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
#[rustc_allow_const_fn_unstable(const_ptr_is_null)]
#[rustc_never_returns_null_ptr]
#[must_use]
#[inline(always)]
pub const fn as_ptr(self) -> *mut T {
// SAFETY: By definition of this type.
unsafe { crate::intrinsics::assume(!self.pointer.is_null()) };
self.pointer as *mut T
}

Expand Down
161 changes: 149 additions & 12 deletions tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,82 @@
+ scope 3 (inlined std::ptr::drop_in_place::<Vec<A>> - shim(Some(Vec<A>))) {
+ let mut _6: &mut std::vec::Vec<A>;
+ let mut _7: ();
+ scope 4 (inlined <Vec<A> as Drop>::drop) {
+ debug self => _6;
+ let mut _8: *mut [A];
+ let mut _9: *mut A;
+ let mut _10: usize;
+ scope 5 {
+ scope 6 (inlined Vec::<A>::as_mut_ptr) {
+ debug self => _6;
+ let mut _11: &alloc::raw_vec::RawVec<A>;
+ scope 7 (inlined alloc::raw_vec::RawVec::<A>::ptr) {
+ debug self => _11;
+ let mut _18: std::ptr::NonNull<A>;
+ scope 8 (inlined Unique::<A>::as_ptr) {
+ debug ((self: Unique<A>).0: std::ptr::NonNull<A>) => _18;
+ debug ((self: Unique<A>).1: std::marker::PhantomData<A>) => const ZeroSized: PhantomData<A>;
+ scope 9 (inlined NonNull::<A>::as_ptr) {
+ debug self => _18;
+ let mut _12: bool;
+ let mut _13: bool;
+ let mut _14: *const A;
+ scope 10 {
+ scope 11 (inlined std::ptr::const_ptr::<impl *const A>::is_null) {
+ debug self => _14;
+ let mut _15: *const u8;
+ scope 12 {
+ scope 13 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
+ debug ptr => _15;
+ let mut _16: usize;
+ scope 14 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
+ debug self => _15;
+ let mut _17: *const ();
+ scope 15 {
+ scope 16 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
+ debug self => _15;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ scope 17 (inlined slice_from_raw_parts_mut::<A>) {
+ debug data => _9;
+ debug len => _10;
+ let mut _19: *mut ();
+ scope 18 (inlined std::ptr::mut_ptr::<impl *mut A>::cast::<()>) {
+ debug self => _9;
+ }
+ scope 19 (inlined std::ptr::from_raw_parts_mut::<[A]>) {
+ debug data_address => _19;
+ debug metadata => _10;
+ let mut _20: std::ptr::metadata::PtrRepr<[A]>;
+ let mut _21: std::ptr::metadata::PtrComponents<[A]>;
+ let mut _22: *const ();
+ scope 20 {
+ }
+ }
+ }
+ scope 21 (inlined std::ptr::drop_in_place::<[A]> - shim(Some([A]))) {
+ let mut _23: usize;
+ let mut _24: usize;
+ let mut _25: *mut A;
+ let mut _26: bool;
+ }
+ }
+ }
+ }
}
scope 2 {
+ scope 4 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
+ let mut _8: isize;
+ let mut _9: isize;
+ scope 22 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
+ let mut _27: isize;
+ let mut _28: isize;
+ }
}

Expand All @@ -29,7 +99,58 @@
+ StorageLive(_6);
+ StorageLive(_7);
+ _6 = &mut (*_4);
+ _7 = <Vec<A> as Drop>::drop(move _6) -> [return: bb2, unwind unreachable];
+ StorageLive(_8);
+ StorageLive(_25);
+ StorageLive(_9);
+ StorageLive(_11);
+ _11 = &((*_6).0: alloc::raw_vec::RawVec<A>);
+ StorageLive(_18);
+ _18 = ((((*_6).0: alloc::raw_vec::RawVec<A>).0: std::ptr::Unique<A>).0: std::ptr::NonNull<A>);
+ StorageLive(_14);
+ StorageLive(_12);
+ StorageLive(_13);
+ _14 = (_18.0: *const A);
+ StorageLive(_15);
+ _15 = _14 as *const u8 (PtrToPtr);
+ StorageLive(_16);
+ StorageLive(_17);
+ _17 = _15 as *const () (PtrToPtr);
+ _16 = move _17 as usize (Transmute);
+ StorageDead(_17);
+ _13 = Eq(move _16, const 0_usize);
+ StorageDead(_16);
+ StorageDead(_15);
+ _12 = Not(move _13);
+ StorageDead(_13);
+ assume(move _12);
+ StorageDead(_12);
+ _9 = _14 as *mut A (PtrToPtr);
+ StorageDead(_14);
+ StorageDead(_18);
+ StorageDead(_11);
+ StorageLive(_10);
+ _10 = ((*_6).1: usize);
+ StorageLive(_19);
+ _19 = _9 as *mut () (PtrToPtr);
+ StorageLive(_20);
+ StorageLive(_21);
+ StorageLive(_22);
+ _22 = _19 as *const () (PointerCoercion(MutToConstPointer));
+ _21 = std::ptr::metadata::PtrComponents::<[A]> { data_address: move _22, metadata: _10 };
+ StorageDead(_22);
+ _20 = std::ptr::metadata::PtrRepr::<[A]> { const_ptr: move _21 };
+ StorageDead(_21);
+ _8 = (_20.1: *mut [A]);
+ StorageDead(_20);
+ StorageDead(_19);
+ StorageDead(_10);
+ StorageDead(_9);
+ StorageLive(_23);
+ StorageLive(_24);
+ StorageLive(_26);
+ _23 = Len((*_8));
+ _24 = const 0_usize;
+ goto -> bb4;
}

bb1: {
Expand All @@ -40,25 +161,41 @@
StorageLive(_5);
_5 = _2;
- _0 = std::ptr::drop_in_place::<Option<B>>(move _5) -> [return: bb2, unwind unreachable];
+ StorageLive(_8);
+ StorageLive(_9);
+ _8 = discriminant((*_5));
+ switchInt(move _8) -> [0: bb3, otherwise: bb4];
+ StorageLive(_27);
+ StorageLive(_28);
+ _27 = discriminant((*_5));
+ switchInt(move _27) -> [0: bb5, otherwise: bb6];
}

bb2: {
+ StorageDead(_26);
+ StorageDead(_24);
+ StorageDead(_23);
+ StorageDead(_25);
+ StorageDead(_8);
+ drop(((*_4).0: alloc::raw_vec::RawVec<A>)) -> [return: bb1, unwind unreachable];
+ }
+
+ bb3: {
+ StorageDead(_9);
+ StorageDead(_8);
+ _25 = &raw mut (*_8)[_24];
+ _24 = Add(move _24, const 1_usize);
+ drop((*_25)) -> [return: bb4, unwind unreachable];
+ }
+
+ bb4: {
+ _26 = Eq(_24, _23);
+ switchInt(move _26) -> [0: bb3, otherwise: bb2];
+ }
+
+ bb5: {
+ StorageDead(_28);
+ StorageDead(_27);
StorageDead(_5);
return;
+ }
+
+ bb4: {
+ drop((((*_5) as Some).0: B)) -> [return: bb3, unwind unreachable];
+ bb6: {
+ drop((((*_5) as Some).0: B)) -> [return: bb5, unwind unreachable];
}
}

Loading