Skip to content

Commit a1a8c4a

Browse files
committed
Lower inling cost for a statement.
1 parent cd6be0c commit a1a8c4a

File tree

4 files changed

+109
-78
lines changed

4 files changed

+109
-78
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 = 3;
66
const CALL_PENALTY: usize = 25;
77
const LANDINGPAD_PENALTY: usize = 50;
88
const RESUME_PENALTY: usize = 45;

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir

+1-55
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,12 @@ fn step_forward(_1: u32, _2: usize) -> u32 {
44
debug x => _1;
55
debug n => _2;
66
let mut _0: u32;
7-
scope 1 (inlined <u32 as Step>::forward) {
8-
debug start => _1;
9-
debug n => _2;
10-
let _3: std::option::Option<u32>;
11-
let mut _4: &std::option::Option<u32>;
12-
let mut _7: bool;
13-
let mut _8: u32;
14-
scope 2 {
15-
}
16-
scope 3 (inlined Option::<u32>::is_none) {
17-
debug self => _4;
18-
let mut _6: bool;
19-
scope 4 (inlined Option::<u32>::is_some) {
20-
debug self => _4;
21-
let mut _5: isize;
22-
}
23-
}
24-
scope 5 (inlined core::num::<impl u32>::wrapping_add) {
25-
debug self => _1;
26-
debug rhs => _8;
27-
}
28-
}
297

308
bb0: {
31-
StorageLive(_7);
32-
StorageLive(_4);
33-
StorageLive(_3);
34-
_3 = <u32 as Step>::forward_checked(_1, _2) -> [return: bb1, unwind continue];
9+
_0 = <u32 as Step>::forward(move _1, move _2) -> [return: bb1, unwind continue];
3510
}
3611

3712
bb1: {
38-
_4 = &_3;
39-
StorageLive(_6);
40-
StorageLive(_5);
41-
_5 = discriminant(_3);
42-
_6 = Eq(_5, const 1_isize);
43-
StorageDead(_5);
44-
_7 = Not(move _6);
45-
StorageDead(_6);
46-
switchInt(move _7) -> [0: bb2, otherwise: bb3];
47-
}
48-
49-
bb2: {
50-
StorageDead(_3);
51-
StorageDead(_4);
52-
goto -> bb4;
53-
}
54-
55-
bb3: {
56-
StorageDead(_3);
57-
StorageDead(_4);
58-
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const _, const 1_u32) -> [success: bb4, unwind continue];
59-
}
60-
61-
bb4: {
62-
StorageDead(_7);
63-
StorageLive(_8);
64-
_8 = _2 as u32 (IntToInt);
65-
_0 = Add(_1, _8);
66-
StorageDead(_8);
6713
return;
6814
}
6915
}

tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir

+74-22
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,31 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
77
let mut _3: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
88
let mut _4: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
99
let mut _5: &mut std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
10-
let mut _6: std::option::Option<U>;
11-
let mut _7: isize;
12-
let _9: ();
10+
let mut _10: std::option::Option<U>;
11+
let mut _14: isize;
12+
let _16: ();
1313
scope 1 {
1414
debug iter => _4;
15-
let _8: U;
15+
let _15: U;
1616
scope 2 {
17-
debug x => _8;
17+
debug x => _15;
18+
}
19+
scope 4 (inlined <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next) {
20+
debug self => _5;
21+
let mut _6: &mut impl Iterator<Item = T>;
22+
let mut _7: std::option::Option<T>;
23+
let mut _8: &mut impl Fn(T) -> U;
24+
scope 5 (inlined Option::<T>::map::<U, &mut impl Fn(T) -> U>) {
25+
debug self => _7;
26+
debug f => _8;
27+
let mut _9: isize;
28+
let _11: T;
29+
let mut _12: (T,);
30+
let mut _13: U;
31+
scope 6 {
32+
debug x => _11;
33+
}
34+
}
1835
}
1936
}
2037
scope 3 (inlined <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as IntoIterator>::into_iter) {
@@ -32,47 +49,82 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
3249
}
3350

3451
bb2: {
35-
StorageLive(_6);
36-
StorageLive(_5);
52+
StorageLive(_10);
3753
_5 = &mut _4;
38-
_6 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(move _5) -> [return: bb3, unwind: bb9];
54+
StorageLive(_8);
55+
StorageLive(_7);
56+
StorageLive(_6);
57+
_6 = &mut (_4.0: impl Iterator<Item = T>);
58+
_7 = <impl Iterator<Item = T> as Iterator>::next(move _6) -> [return: bb3, unwind: bb13];
3959
}
4060

4161
bb3: {
42-
StorageDead(_5);
43-
_7 = discriminant(_6);
44-
switchInt(move _7) -> [0: bb4, 1: bb6, otherwise: bb8];
62+
StorageDead(_6);
63+
_8 = &mut (_4.1: impl Fn(T) -> U);
64+
StorageLive(_9);
65+
StorageLive(_11);
66+
_9 = discriminant(_7);
67+
switchInt(move _9) -> [0: bb4, 1: bb5, otherwise: bb12];
4568
}
4669

4770
bb4: {
48-
StorageDead(_6);
49-
drop(_4) -> [return: bb5, unwind continue];
71+
_10 = Option::<U>::None;
72+
goto -> bb7;
5073
}
5174

5275
bb5: {
53-
StorageDead(_4);
54-
return;
76+
_11 = move ((_7 as Some).0: T);
77+
StorageLive(_13);
78+
StorageLive(_12);
79+
_12 = (move _11,);
80+
_13 = <&mut impl Fn(T) -> U as FnOnce<(T,)>>::call_once(move _8, move _12) -> [return: bb6, unwind: bb13];
5581
}
5682

5783
bb6: {
58-
_8 = move ((_6 as Some).0: U);
59-
_9 = opaque::<U>(move _8) -> [return: bb7, unwind: bb9];
84+
StorageDead(_12);
85+
_10 = Option::<U>::Some(move _13);
86+
StorageDead(_13);
87+
goto -> bb7;
6088
}
6189

6290
bb7: {
63-
StorageDead(_6);
64-
goto -> bb2;
91+
StorageDead(_11);
92+
StorageDead(_9);
93+
StorageDead(_7);
94+
StorageDead(_8);
95+
_14 = discriminant(_10);
96+
switchInt(move _14) -> [0: bb8, 1: bb10, otherwise: bb12];
6597
}
6698

6799
bb8: {
100+
StorageDead(_10);
101+
drop(_4) -> [return: bb9, unwind continue];
102+
}
103+
104+
bb9: {
105+
StorageDead(_4);
106+
return;
107+
}
108+
109+
bb10: {
110+
_15 = move ((_10 as Some).0: U);
111+
_16 = opaque::<U>(move _15) -> [return: bb11, unwind: bb13];
112+
}
113+
114+
bb11: {
115+
StorageDead(_10);
116+
goto -> bb2;
117+
}
118+
119+
bb12: {
68120
unreachable;
69121
}
70122

71-
bb9 (cleanup): {
72-
drop(_4) -> [return: bb10, unwind terminate(cleanup)];
123+
bb13 (cleanup): {
124+
drop(_4) -> [return: bb14, unwind terminate(cleanup)];
73125
}
74126

75-
bb10 (cleanup): {
127+
bb14 (cleanup): {
76128
resume;
77129
}
78130
}

tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
1414
debug dst => _1;
1515
debug src => _2;
1616
scope 17 {
17+
scope 18 (inlined std::ptr::write::runtime::<u32>) {
18+
debug dst => _1;
19+
let mut _4: *const u32;
20+
scope 19 (inlined intrinsics::is_aligned_and_not_null::<u32>) {
21+
debug ptr => _4;
22+
scope 20 (inlined std::ptr::const_ptr::<impl *const u32>::is_null) {
23+
debug self => _4;
24+
let mut _5: *const u8;
25+
scope 21 {
26+
scope 22 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
27+
debug ptr => _5;
28+
scope 23 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
29+
debug self => _5;
30+
scope 24 {
31+
scope 25 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
32+
debug self => _5;
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
scope 26 (inlined std::ptr::const_ptr::<impl *const u32>::is_aligned) {
40+
debug self => _4;
41+
scope 27 (inlined align_of::<u32>) {
42+
}
43+
}
44+
}
45+
}
1746
}
1847
}
1948
}
@@ -57,7 +86,11 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
5786
StorageLive(_3);
5887
_0 = (*_1);
5988
StorageDead(_3);
89+
StorageLive(_4);
90+
StorageLive(_5);
6091
(*_1) = _2;
92+
StorageDead(_5);
93+
StorageDead(_4);
6194
return;
6295
}
6396
}

0 commit comments

Comments
 (0)