Skip to content

Commit a2ae6fa

Browse files
committed
Auto merge of #116708 - cjgillot:cheap-stmt, r=<try>
Reduce inlining cost for statement r? `@ghost`
2 parents 284cb71 + 486a7e8 commit a2ae6fa

22 files changed

+5192
-292
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::mir::visit::*;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
44

5-
const INSTR_COST: usize = 5;
5+
const INSTR_COST: usize = 1;
66
const CALL_PENALTY: usize = 25;
77
const LANDINGPAD_PENALTY: usize = 50;
88
const RESUME_PENALTY: usize = 45;

library/core/src/ptr/non_null.rs

+3
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,13 @@ impl<T: ?Sized> NonNull<T> {
341341
/// ```
342342
#[stable(feature = "nonnull", since = "1.25.0")]
343343
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
344+
#[rustc_allow_const_fn_unstable(const_ptr_is_null)]
344345
#[rustc_never_returns_null_ptr]
345346
#[must_use]
346347
#[inline(always)]
347348
pub const fn as_ptr(self) -> *mut T {
349+
// SAFETY: By definition of this type.
350+
unsafe { crate::intrinsics::assume(!self.pointer.is_null()) };
348351
self.pointer as *mut T
349352
}
350353

tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff

+149-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,82 @@
1212
+ scope 3 (inlined std::ptr::drop_in_place::<Vec<A>> - shim(Some(Vec<A>))) {
1313
+ let mut _6: &mut std::vec::Vec<A>;
1414
+ let mut _7: ();
15+
+ scope 4 (inlined <Vec<A> as Drop>::drop) {
16+
+ debug self => _6;
17+
+ let mut _8: *mut [A];
18+
+ let mut _9: *mut A;
19+
+ let mut _10: usize;
20+
+ scope 5 {
21+
+ scope 6 (inlined Vec::<A>::as_mut_ptr) {
22+
+ debug self => _6;
23+
+ let mut _11: &alloc::raw_vec::RawVec<A>;
24+
+ scope 7 (inlined alloc::raw_vec::RawVec::<A>::ptr) {
25+
+ debug self => _11;
26+
+ let mut _18: std::ptr::NonNull<A>;
27+
+ scope 8 (inlined Unique::<A>::as_ptr) {
28+
+ debug ((self: Unique<A>).0: std::ptr::NonNull<A>) => _18;
29+
+ debug ((self: Unique<A>).1: std::marker::PhantomData<A>) => const ZeroSized: PhantomData<A>;
30+
+ scope 9 (inlined NonNull::<A>::as_ptr) {
31+
+ debug self => _18;
32+
+ let mut _12: bool;
33+
+ let mut _13: bool;
34+
+ let mut _14: *const A;
35+
+ scope 10 {
36+
+ scope 11 (inlined std::ptr::const_ptr::<impl *const A>::is_null) {
37+
+ debug self => _14;
38+
+ let mut _15: *const u8;
39+
+ scope 12 {
40+
+ scope 13 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
41+
+ debug ptr => _15;
42+
+ let mut _16: usize;
43+
+ scope 14 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
44+
+ debug self => _15;
45+
+ let mut _17: *const ();
46+
+ scope 15 {
47+
+ scope 16 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
48+
+ debug self => _15;
49+
+ }
50+
+ }
51+
+ }
52+
+ }
53+
+ }
54+
+ }
55+
+ }
56+
+ }
57+
+ }
58+
+ }
59+
+ }
60+
+ scope 17 (inlined slice_from_raw_parts_mut::<A>) {
61+
+ debug data => _9;
62+
+ debug len => _10;
63+
+ let mut _19: *mut ();
64+
+ scope 18 (inlined std::ptr::mut_ptr::<impl *mut A>::cast::<()>) {
65+
+ debug self => _9;
66+
+ }
67+
+ scope 19 (inlined std::ptr::from_raw_parts_mut::<[A]>) {
68+
+ debug data_address => _19;
69+
+ debug metadata => _10;
70+
+ let mut _20: std::ptr::metadata::PtrRepr<[A]>;
71+
+ let mut _21: std::ptr::metadata::PtrComponents<[A]>;
72+
+ let mut _22: *const ();
73+
+ scope 20 {
74+
+ }
75+
+ }
76+
+ }
77+
+ scope 21 (inlined std::ptr::drop_in_place::<[A]> - shim(Some([A]))) {
78+
+ let mut _23: usize;
79+
+ let mut _24: usize;
80+
+ let mut _25: *mut A;
81+
+ let mut _26: bool;
82+
+ }
83+
+ }
84+
+ }
1585
+ }
1686
}
1787
scope 2 {
18-
+ scope 4 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
19-
+ let mut _8: isize;
20-
+ let mut _9: isize;
88+
+ scope 22 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
89+
+ let mut _27: isize;
90+
+ let mut _28: isize;
2191
+ }
2292
}
2393

@@ -29,7 +99,58 @@
2999
+ StorageLive(_6);
30100
+ StorageLive(_7);
31101
+ _6 = &mut (*_4);
32-
+ _7 = <Vec<A> as Drop>::drop(move _6) -> [return: bb2, unwind unreachable];
102+
+ StorageLive(_8);
103+
+ StorageLive(_25);
104+
+ StorageLive(_9);
105+
+ StorageLive(_11);
106+
+ _11 = &((*_6).0: alloc::raw_vec::RawVec<A>);
107+
+ StorageLive(_18);
108+
+ _18 = ((((*_6).0: alloc::raw_vec::RawVec<A>).0: std::ptr::Unique<A>).0: std::ptr::NonNull<A>);
109+
+ StorageLive(_14);
110+
+ StorageLive(_12);
111+
+ StorageLive(_13);
112+
+ _14 = (_18.0: *const A);
113+
+ StorageLive(_15);
114+
+ _15 = _14 as *const u8 (PtrToPtr);
115+
+ StorageLive(_16);
116+
+ StorageLive(_17);
117+
+ _17 = _15 as *const () (PtrToPtr);
118+
+ _16 = move _17 as usize (Transmute);
119+
+ StorageDead(_17);
120+
+ _13 = Eq(move _16, const 0_usize);
121+
+ StorageDead(_16);
122+
+ StorageDead(_15);
123+
+ _12 = Not(move _13);
124+
+ StorageDead(_13);
125+
+ assume(move _12);
126+
+ StorageDead(_12);
127+
+ _9 = _14 as *mut A (PtrToPtr);
128+
+ StorageDead(_14);
129+
+ StorageDead(_18);
130+
+ StorageDead(_11);
131+
+ StorageLive(_10);
132+
+ _10 = ((*_6).1: usize);
133+
+ StorageLive(_19);
134+
+ _19 = _9 as *mut () (PtrToPtr);
135+
+ StorageLive(_20);
136+
+ StorageLive(_21);
137+
+ StorageLive(_22);
138+
+ _22 = _19 as *const () (PointerCoercion(MutToConstPointer));
139+
+ _21 = std::ptr::metadata::PtrComponents::<[A]> { data_address: move _22, metadata: _10 };
140+
+ StorageDead(_22);
141+
+ _20 = std::ptr::metadata::PtrRepr::<[A]> { const_ptr: move _21 };
142+
+ StorageDead(_21);
143+
+ _8 = (_20.1: *mut [A]);
144+
+ StorageDead(_20);
145+
+ StorageDead(_19);
146+
+ StorageDead(_10);
147+
+ StorageDead(_9);
148+
+ StorageLive(_23);
149+
+ StorageLive(_24);
150+
+ StorageLive(_26);
151+
+ _23 = Len((*_8));
152+
+ _24 = const 0_usize;
153+
+ goto -> bb4;
33154
}
34155

35156
bb1: {
@@ -40,25 +161,41 @@
40161
StorageLive(_5);
41162
_5 = _2;
42163
- _0 = std::ptr::drop_in_place::<Option<B>>(move _5) -> [return: bb2, unwind unreachable];
43-
+ StorageLive(_8);
44-
+ StorageLive(_9);
45-
+ _8 = discriminant((*_5));
46-
+ switchInt(move _8) -> [0: bb3, otherwise: bb4];
164+
+ StorageLive(_27);
165+
+ StorageLive(_28);
166+
+ _27 = discriminant((*_5));
167+
+ switchInt(move _27) -> [0: bb5, otherwise: bb6];
47168
}
48169

49170
bb2: {
171+
+ StorageDead(_26);
172+
+ StorageDead(_24);
173+
+ StorageDead(_23);
174+
+ StorageDead(_25);
175+
+ StorageDead(_8);
50176
+ drop(((*_4).0: alloc::raw_vec::RawVec<A>)) -> [return: bb1, unwind unreachable];
51177
+ }
52178
+
53179
+ bb3: {
54-
+ StorageDead(_9);
55-
+ StorageDead(_8);
180+
+ _25 = &raw mut (*_8)[_24];
181+
+ _24 = Add(move _24, const 1_usize);
182+
+ drop((*_25)) -> [return: bb4, unwind unreachable];
183+
+ }
184+
+
185+
+ bb4: {
186+
+ _26 = Eq(_24, _23);
187+
+ switchInt(move _26) -> [0: bb3, otherwise: bb2];
188+
+ }
189+
+
190+
+ bb5: {
191+
+ StorageDead(_28);
192+
+ StorageDead(_27);
56193
StorageDead(_5);
57194
return;
58195
+ }
59196
+
60-
+ bb4: {
61-
+ drop((((*_5) as Some).0: B)) -> [return: bb3, unwind unreachable];
197+
+ bb6: {
198+
+ drop((((*_5) as Some).0: B)) -> [return: bb5, unwind unreachable];
62199
}
63200
}
64201

0 commit comments

Comments
 (0)