Skip to content

Commit a40a62a

Browse files
committed
Auto merge of #123011 - scottmcm:inline-debuginfo-cost, r=<try>
Add a debug-info cost to MIR inlining Inspired by all the MIR I keep looking at that's inlined away like 7 different function calls so the body is 2 statements but 30 debug-infos. r? ghost
2 parents 536606b + ce93553 commit a40a62a

9 files changed

+249
-678
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ const CALL_PENALTY: usize = 25;
77
const LANDINGPAD_PENALTY: usize = 50;
88
const RESUME_PENALTY: usize = 45;
99

10+
// While debug info has no runtime cost, when inlining we do need to copy it
11+
// into the caller MIR, which means we should give it a small cost to represent
12+
// the compile-time impact of doing that, since just because something has few
13+
// instructions doesn't necessarily mean it's cheap for us to inline.
14+
// The backend can, of course, still inline such things later should it so wish.
15+
const DEBUG_INFO_COST: usize = 2;
16+
1017
/// Verify that the callee body is compatible with the caller.
1118
#[derive(Clone)]
1219
pub(crate) struct CostChecker<'b, 'tcx> {
@@ -31,6 +38,12 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
3138
self.cost
3239
}
3340

41+
// The MIR inliner doesn't actually call `visit_body`, so it doesn't work
42+
// to put this in the visitor below.
43+
pub fn before_body(&mut self, body: &Body<'tcx>) {
44+
self.cost += body.var_debug_info.len() * DEBUG_INFO_COST;
45+
}
46+
3447
fn instantiate_ty(&self, v: Ty<'tcx>) -> Ty<'tcx> {
3548
if let Some(instance) = self.instance {
3649
instance.instantiate_mir(self.tcx, ty::EarlyBinder::bind(&v))

compiler/rustc_mir_transform/src/inline.rs

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ impl<'tcx> Inliner<'tcx> {
506506
let mut checker =
507507
CostChecker::new(self.tcx, self.param_env, Some(callsite.callee), callee_body);
508508

509+
checker.before_body(callee_body);
510+
509511
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
510512
let mut work_list = vec![START_BLOCK];
511513
let mut visited = BitSet::new_empty(callee_body.basic_blocks.len());

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

+20-57
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,17 @@ fn int_range(_1: usize, _2: usize) -> () {
77
let mut _3: std::ops::Range<usize>;
88
let mut _4: std::ops::Range<usize>;
99
let mut _5: &mut std::ops::Range<usize>;
10-
let mut _13: std::option::Option<usize>;
11-
let _15: ();
10+
let mut _6: std::option::Option<usize>;
11+
let mut _7: isize;
12+
let _9: ();
1213
scope 1 {
1314
debug iter => _4;
14-
let _14: usize;
15+
let _8: usize;
1516
scope 2 {
16-
debug i => _14;
17+
debug i => _8;
1718
}
1819
scope 4 (inlined iter::range::<impl Iterator for std::ops::Range<usize>>::next) {
1920
debug self => _5;
20-
scope 5 (inlined <std::ops::Range<usize> as iter::range::RangeIteratorImpl>::spec_next) {
21-
debug self => _5;
22-
let mut _6: &usize;
23-
let mut _7: &usize;
24-
let mut _10: bool;
25-
let _11: usize;
26-
let mut _12: usize;
27-
scope 6 {
28-
debug old => _11;
29-
scope 7 {
30-
}
31-
}
32-
scope 8 (inlined std::cmp::impls::<impl PartialOrd for usize>::lt) {
33-
debug self => _6;
34-
debug other => _7;
35-
let mut _8: usize;
36-
let mut _9: usize;
37-
}
38-
}
3921
}
4022
}
4123
scope 3 (inlined <std::ops::Range<usize> as IntoIterator>::into_iter) {
@@ -50,54 +32,35 @@ fn int_range(_1: usize, _2: usize) -> () {
5032
}
5133

5234
bb1: {
53-
StorageLive(_13);
54-
_5 = &mut _4;
55-
StorageLive(_11);
56-
StorageLive(_10);
5735
StorageLive(_6);
58-
_6 = &(_4.0: usize);
59-
StorageLive(_7);
60-
_7 = &(_4.1: usize);
61-
StorageLive(_8);
62-
_8 = (_4.0: usize);
63-
StorageLive(_9);
64-
_9 = (_4.1: usize);
65-
_10 = Lt(move _8, move _9);
66-
StorageDead(_9);
67-
StorageDead(_8);
68-
switchInt(move _10) -> [0: bb2, otherwise: bb3];
36+
StorageLive(_5);
37+
_5 = &mut _4;
38+
_6 = <std::ops::Range<usize> as iter::range::RangeIteratorImpl>::spec_next(move _5) -> [return: bb2, unwind continue];
6939
}
7040

7141
bb2: {
72-
StorageDead(_7);
73-
StorageDead(_6);
74-
StorageDead(_10);
75-
StorageDead(_11);
76-
StorageDead(_13);
77-
StorageDead(_4);
78-
return;
42+
StorageDead(_5);
43+
_7 = discriminant(_6);
44+
switchInt(move _7) -> [0: bb3, 1: bb4, otherwise: bb6];
7945
}
8046

8147
bb3: {
82-
StorageDead(_7);
8348
StorageDead(_6);
84-
_11 = (_4.0: usize);
85-
StorageLive(_12);
86-
_12 = <usize as Step>::forward_unchecked(_11, const 1_usize) -> [return: bb4, unwind continue];
49+
StorageDead(_4);
50+
return;
8751
}
8852

8953
bb4: {
90-
(_4.0: usize) = move _12;
91-
StorageDead(_12);
92-
_13 = Option::<usize>::Some(_11);
93-
StorageDead(_10);
94-
StorageDead(_11);
95-
_14 = ((_13 as Some).0: usize);
96-
_15 = opaque::<usize>(move _14) -> [return: bb5, unwind continue];
54+
_8 = ((_6 as Some).0: usize);
55+
_9 = opaque::<usize>(move _8) -> [return: bb5, unwind continue];
9756
}
9857

9958
bb5: {
100-
StorageDead(_13);
59+
StorageDead(_6);
10160
goto -> bb1;
10261
}
62+
63+
bb6: {
64+
unreachable;
65+
}
10366
}

tests/mir-opt/pre-codegen/range_iter.forward_loop.PreCodegen.after.panic-unwind.mir

+32-69
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,19 @@ fn forward_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
88
let mut _4: std::ops::Range<u32>;
99
let mut _5: std::ops::Range<u32>;
1010
let mut _6: &mut std::ops::Range<u32>;
11-
let mut _14: std::option::Option<u32>;
12-
let mut _16: &impl Fn(u32);
13-
let mut _17: (u32,);
14-
let _18: ();
11+
let mut _7: std::option::Option<u32>;
12+
let mut _8: isize;
13+
let mut _10: &impl Fn(u32);
14+
let mut _11: (u32,);
15+
let _12: ();
1516
scope 1 {
1617
debug iter => _5;
17-
let _15: u32;
18+
let _9: u32;
1819
scope 2 {
19-
debug x => _15;
20+
debug x => _9;
2021
}
2122
scope 4 (inlined iter::range::<impl Iterator for std::ops::Range<u32>>::next) {
2223
debug self => _6;
23-
scope 5 (inlined <std::ops::Range<u32> as iter::range::RangeIteratorImpl>::spec_next) {
24-
debug self => _6;
25-
let mut _7: &u32;
26-
let mut _8: &u32;
27-
let mut _11: bool;
28-
let _12: u32;
29-
let mut _13: u32;
30-
scope 6 {
31-
debug old => _12;
32-
scope 7 {
33-
}
34-
}
35-
scope 8 (inlined std::cmp::impls::<impl PartialOrd for u32>::lt) {
36-
debug self => _7;
37-
debug other => _8;
38-
let mut _9: u32;
39-
let mut _10: u32;
40-
}
41-
}
4224
}
4325
}
4426
scope 3 (inlined <std::ops::Range<u32> as IntoIterator>::into_iter) {
@@ -53,72 +35,53 @@ fn forward_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
5335
}
5436

5537
bb1: {
56-
StorageLive(_14);
57-
_6 = &mut _5;
58-
StorageLive(_12);
59-
StorageLive(_11);
6038
StorageLive(_7);
61-
_7 = &(_5.0: u32);
62-
StorageLive(_8);
63-
_8 = &(_5.1: u32);
64-
StorageLive(_9);
65-
_9 = (_5.0: u32);
66-
StorageLive(_10);
67-
_10 = (_5.1: u32);
68-
_11 = Lt(move _9, move _10);
69-
StorageDead(_10);
70-
StorageDead(_9);
71-
switchInt(move _11) -> [0: bb2, otherwise: bb4];
39+
StorageLive(_6);
40+
_6 = &mut _5;
41+
_7 = <std::ops::Range<u32> as iter::range::RangeIteratorImpl>::spec_next(move _6) -> [return: bb2, unwind: bb8];
7242
}
7343

7444
bb2: {
75-
StorageDead(_8);
76-
StorageDead(_7);
77-
StorageDead(_11);
78-
StorageDead(_12);
79-
StorageDead(_14);
80-
StorageDead(_5);
81-
drop(_3) -> [return: bb3, unwind continue];
45+
StorageDead(_6);
46+
_8 = discriminant(_7);
47+
switchInt(move _8) -> [0: bb3, 1: bb5, otherwise: bb7];
8248
}
8349

8450
bb3: {
85-
return;
51+
StorageDead(_7);
52+
StorageDead(_5);
53+
drop(_3) -> [return: bb4, unwind continue];
8654
}
8755

8856
bb4: {
89-
StorageDead(_8);
90-
StorageDead(_7);
91-
_12 = (_5.0: u32);
92-
StorageLive(_13);
93-
_13 = <u32 as Step>::forward_unchecked(_12, const 1_usize) -> [return: bb5, unwind: bb7];
57+
return;
9458
}
9559

9660
bb5: {
97-
(_5.0: u32) = move _13;
98-
StorageDead(_13);
99-
_14 = Option::<u32>::Some(_12);
100-
StorageDead(_11);
101-
StorageDead(_12);
102-
_15 = ((_14 as Some).0: u32);
103-
StorageLive(_16);
104-
_16 = &_3;
105-
StorageLive(_17);
106-
_17 = (_15,);
107-
_18 = <impl Fn(u32) as Fn<(u32,)>>::call(move _16, move _17) -> [return: bb6, unwind: bb7];
61+
_9 = ((_7 as Some).0: u32);
62+
StorageLive(_10);
63+
_10 = &_3;
64+
StorageLive(_11);
65+
_11 = (_9,);
66+
_12 = <impl Fn(u32) as Fn<(u32,)>>::call(move _10, move _11) -> [return: bb6, unwind: bb8];
10867
}
10968

11069
bb6: {
111-
StorageDead(_17);
112-
StorageDead(_16);
113-
StorageDead(_14);
70+
StorageDead(_11);
71+
StorageDead(_10);
72+
StorageDead(_7);
11473
goto -> bb1;
11574
}
11675

117-
bb7 (cleanup): {
118-
drop(_3) -> [return: bb8, unwind terminate(cleanup)];
76+
bb7: {
77+
unreachable;
11978
}
12079

12180
bb8 (cleanup): {
81+
drop(_3) -> [return: bb9, unwind terminate(cleanup)];
82+
}
83+
84+
bb9 (cleanup): {
12285
resume;
12386
}
12487
}

tests/mir-opt/pre-codegen/range_iter.range_iter_next.PreCodegen.after.panic-unwind.mir

+1-61
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,13 @@ fn range_iter_next(_1: &mut std::ops::Range<u32>) -> Option<u32> {
55
let mut _0: std::option::Option<u32>;
66
scope 1 (inlined iter::range::<impl Iterator for std::ops::Range<u32>>::next) {
77
debug self => _1;
8-
scope 2 (inlined <std::ops::Range<u32> as iter::range::RangeIteratorImpl>::spec_next) {
9-
debug self => _1;
10-
let mut _2: &u32;
11-
let mut _3: &u32;
12-
let mut _6: bool;
13-
let _7: u32;
14-
let mut _8: u32;
15-
scope 3 {
16-
debug old => _7;
17-
scope 4 {
18-
}
19-
}
20-
scope 5 (inlined std::cmp::impls::<impl PartialOrd for u32>::lt) {
21-
debug self => _2;
22-
debug other => _3;
23-
let mut _4: u32;
24-
let mut _5: u32;
25-
}
26-
}
278
}
289

2910
bb0: {
30-
StorageLive(_7);
31-
StorageLive(_6);
32-
StorageLive(_2);
33-
_2 = &((*_1).0: u32);
34-
StorageLive(_3);
35-
_3 = &((*_1).1: u32);
36-
StorageLive(_4);
37-
_4 = ((*_1).0: u32);
38-
StorageLive(_5);
39-
_5 = ((*_1).1: u32);
40-
_6 = Lt(move _4, move _5);
41-
StorageDead(_5);
42-
StorageDead(_4);
43-
switchInt(move _6) -> [0: bb1, otherwise: bb2];
11+
_0 = <std::ops::Range<u32> as iter::range::RangeIteratorImpl>::spec_next(move _1) -> [return: bb1, unwind continue];
4412
}
4513

4614
bb1: {
47-
StorageDead(_3);
48-
StorageDead(_2);
49-
_0 = const Option::<u32>::None;
50-
goto -> bb4;
51-
}
52-
53-
bb2: {
54-
StorageDead(_3);
55-
StorageDead(_2);
56-
_7 = ((*_1).0: u32);
57-
StorageLive(_8);
58-
_8 = <u32 as Step>::forward_unchecked(_7, const 1_usize) -> [return: bb3, unwind continue];
59-
}
60-
61-
bb3: {
62-
((*_1).0: u32) = move _8;
63-
StorageDead(_8);
64-
_0 = Option::<u32>::Some(_7);
65-
goto -> bb4;
66-
}
67-
68-
bb4: {
69-
StorageDead(_6);
70-
StorageDead(_7);
7115
return;
7216
}
7317
}
74-
75-
ALLOC0 (size: 8, align: 4) {
76-
00 00 00 00 __ __ __ __ │ ....░░░░
77-
}

0 commit comments

Comments
 (0)