Skip to content

Commit e9d6d0f

Browse files
committed
Add a debug-info cost to MIR inlining
1 parent 548e14b commit e9d6d0f

9 files changed

+175
-465
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 = 1;
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-abort.mir

+30-67
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,64 +35,45 @@ 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 unreachable];
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 unreachable];
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 unreachable];
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 unreachable];
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 unreachable];
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 unreachable];
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
}
75+
76+
bb7: {
77+
unreachable;
78+
}
11679
}

0 commit comments

Comments
 (0)