Skip to content

Commit 7414060

Browse files
committed
update the format of liveness debug dumps to be more readable
1 parent 24442ff commit 7414060

File tree

5 files changed

+84
-70
lines changed

5 files changed

+84
-70
lines changed

src/librustc_mir/transform/nll/mod.rs

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::util::nodemap::FxHashMap;
1616
use rustc_data_structures::indexed_vec::Idx;
1717
use std::collections::BTreeSet;
1818
use std::fmt;
19-
use util::liveness::{self, LivenessResult, LivenessMode};
19+
use util::liveness::{self, LivenessMode, LivenessResult, LocalSet};
2020

2121
use util as mir_util;
2222
use self::mir_util::PassWhere;
@@ -51,15 +51,21 @@ impl MirPass for NLL {
5151

5252
// Compute what is live where.
5353
let liveness = &LivenessResults {
54-
regular: liveness::liveness_of_locals(mir, LivenessMode {
55-
include_regular_use: true,
56-
include_drops: false,
57-
}),
58-
59-
drop: liveness::liveness_of_locals(mir, LivenessMode {
60-
include_regular_use: false,
61-
include_drops: true,
62-
})
54+
regular: liveness::liveness_of_locals(
55+
mir,
56+
LivenessMode {
57+
include_regular_use: true,
58+
include_drops: false,
59+
},
60+
),
61+
62+
drop: liveness::liveness_of_locals(
63+
mir,
64+
LivenessMode {
65+
include_regular_use: false,
66+
include_drops: true,
67+
},
68+
),
6369
};
6470

6571
// Create the region inference context, generate the constraints,
@@ -94,9 +100,11 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
94100
.indices()
95101
.flat_map(|bb| {
96102
let mut results = vec![];
97-
liveness.regular.simulate_block(&mir, bb, |location, local_set| {
98-
results.push((location, local_set.clone()));
99-
});
103+
liveness
104+
.regular
105+
.simulate_block(&mir, bb, |location, local_set| {
106+
results.push((location, local_set.clone()));
107+
});
100108
results
101109
})
102110
.collect();
@@ -105,9 +113,11 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
105113
.indices()
106114
.flat_map(|bb| {
107115
let mut results = vec![];
108-
liveness.drop.simulate_block(&mir, bb, |location, local_set| {
109-
results.push((location, local_set.clone()));
110-
});
116+
liveness
117+
.drop
118+
.simulate_block(&mir, bb, |location, local_set| {
119+
results.push((location, local_set.clone()));
120+
});
111121
results
112122
})
113123
.collect();
@@ -122,17 +132,21 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
122132
// Before each basic block, dump out the values
123133
// that are live on entry to the basic block.
124134
PassWhere::BeforeBlock(bb) => {
125-
writeln!(out, " | Variables regular-live on entry to the block {:?}: {:?}",
126-
bb, liveness.regular.ins[bb])?;
127-
writeln!(out, " | Variables drop-live on entry to the block {:?}: {:?}",
128-
bb, liveness.drop.ins[bb])?;
135+
let s = live_variable_set(&liveness.regular.ins[bb], &liveness.drop.ins[bb]);
136+
writeln!(out, " | Live variables on entry to {:?}: {}", bb, s)?;
129137
}
130138

131139
PassWhere::InCFG(location) => {
132-
let local_set = &regular_liveness_per_location[&location];
133-
writeln!(out, " | Regular-Live variables here: {:?}", local_set)?;
134-
let local_set = &drop_liveness_per_location[&location];
135-
writeln!(out, " | Drop-Live variables here: {:?}", local_set)?;
140+
let s = live_variable_set(
141+
&regular_liveness_per_location[&location],
142+
&drop_liveness_per_location[&location],
143+
);
144+
writeln!(
145+
out,
146+
" | Live variables at {:?}: {}",
147+
location,
148+
s
149+
)?;
136150
}
137151

138152
PassWhere::AfterCFG => {}
@@ -184,3 +198,26 @@ impl ToRegionIndex for RegionKind {
184198
}
185199
}
186200
}
201+
202+
fn live_variable_set(regular: &LocalSet, drops: &LocalSet) -> String {
203+
// sort and deduplicate:
204+
let all_locals: BTreeSet<_> = regular.iter().chain(drops.iter()).collect();
205+
206+
// construct a string with each local, including `(drop)` if it is
207+
// only dropped, versus a regular use.
208+
let mut string = String::new();
209+
for local in all_locals {
210+
string.push_str(&format!("{:?}", local));
211+
212+
if !regular.contains(&local) {
213+
assert!(drops.contains(&local));
214+
string.push_str(" (drop)");
215+
}
216+
217+
string.push_str(", ");
218+
}
219+
220+
let len = if string.is_empty() { 0 } else { string.len() - 2 };
221+
222+
format!("[{}]", &string[..len])
223+
}

src/test/mir-opt/nll/liveness-call-subtlety.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,20 @@ fn main() {
2626
//
2727
// END RUST SOURCE
2828
// START rustc.node12.nll.0.mir
29-
// | Variables regular-live on entry to the block bb0: []
30-
// | Variables drop-live on entry to the block bb0: []
29+
// | Live variables on entry to bb0: []
3130
// bb0: {
32-
// | Regular-Live variables here: []
33-
// | Drop-Live variables here: []
31+
// | Live variables at bb0[0]: []
3432
// StorageLive(_1);
35-
// | Regular-Live variables here: []
36-
// | Drop-Live variables here: []
33+
// | Live variables at bb0[1]: []
3734
// _1 = const <std::boxed::Box<T>>::new(const 22usize) -> bb1;
3835
// }
3936
// END rustc.node12.nll.0.mir
4037
// START rustc.node12.nll.0.mir
41-
// | Variables regular-live on entry to the block bb1: []
42-
// | Variables drop-live on entry to the block bb1: [_1]
38+
// | Live variables on entry to bb1: [_1 (drop)]
4339
// bb1: {
44-
// | Regular-Live variables here: []
45-
// | Drop-Live variables here: [_1]
40+
// | Live variables at bb1[0]: [_1 (drop)]
4641
// StorageLive(_2);
47-
// | Regular-Live variables here: []
48-
// | Drop-Live variables here: [_1]
42+
// | Live variables at bb1[1]: [_1 (drop)]
4943
// _2 = const can_panic() -> [return: bb2, unwind: bb4];
5044
// }
5145
// END rustc.node12.nll.0.mir

src/test/mir-opt/nll/liveness-drop-intra-block.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,17 @@ fn main() {
2525

2626
// END RUST SOURCE
2727
// START rustc.node12.nll.0.mir
28-
// | Variables regular-live on entry to the block bb1: []
29-
// | Variables drop-live on entry to the block bb1: []
28+
// | Live variables on entry to bb1: []
3029
// bb1: {
31-
// | Regular-Live variables here: []
32-
// | Drop-Live variables here: []
30+
// | Live variables at bb1[0]: []
3331
// _1 = const 55usize;
34-
// | Regular-Live variables here: [_1]
35-
// | Drop-Live variables here: []
32+
// | Live variables at bb1[1]: [_1]
3633
// StorageLive(_3);
37-
// | Regular-Live variables here: [_1]
38-
// | Drop-Live variables here: []
34+
// | Live variables at bb1[2]: [_1]
3935
// StorageLive(_4);
40-
// | Regular-Live variables here: [_1]
41-
// | Drop-Live variables here: []
36+
// | Live variables at bb1[3]: [_1]
4237
// _4 = _1;
43-
// | Regular-Live variables here: [_4]
44-
// | Drop-Live variables here: []
38+
// | Live variables at bb1[4]: [_4]
4539
// _3 = const use_x(_4) -> bb2;
4640
// }
4741
// END rustc.node12.nll.0.mir

src/test/mir-opt/nll/liveness-interblock.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,20 @@ fn main() {
2929

3030
// END RUST SOURCE
3131
// START rustc.node18.nll.0.mir
32-
// | Variables regular-live on entry to the block bb2: [_1]
33-
// | Variables drop-live on entry to the block bb2: []
32+
// | Live variables on entry to bb2: [_1]
3433
// bb2: {
35-
// | Regular-Live variables here: [_1]
36-
// | Drop-Live variables here: []
34+
// | Live variables at bb2[0]: [_1]
3735
// StorageLive(_4);
38-
// | Regular-Live variables here: [_1]
39-
// | Drop-Live variables here: []
36+
// | Live variables at bb2[1]: [_1]
4037
// _4 = _1;
41-
// | Regular-Live variables here: [_4]
42-
// | Drop-Live variables here: []
38+
// | Live variables at bb2[2]: [_4]
4339
// _3 = const make_live(_4) -> bb4;
4440
// }
4541
// END rustc.node18.nll.0.mir
4642
// START rustc.node18.nll.0.mir
47-
// | Variables regular-live on entry to the block bb3: []
48-
// | Variables drop-live on entry to the block bb3: []
43+
// | Live variables on entry to bb3: []
4944
// bb3: {
50-
// | Regular-Live variables here: []
51-
// | Drop-Live variables here: []
45+
// | Live variables at bb3[0]: []
5246
// _5 = const make_dead() -> bb5;
5347
// }
5448
// END rustc.node18.nll.0.mir

src/test/mir-opt/nll/region-liveness-basic.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,19 @@ fn main() {
3838
// END rustc.node12.nll.0.mir
3939
// START rustc.node12.nll.0.mir
4040
// bb1: {
41-
// | Regular-Live variables here: [_1, _3]
42-
// | Drop-Live variables here: []
41+
// | Live variables at bb1[0]: [_1, _3]
4342
// _2 = &'_#0r _1[_3];
44-
// | Regular-Live variables here: [_2]
45-
// | Drop-Live variables here: []
43+
// | Live variables at bb1[1]: [_2]
4644
// switchInt(const true) -> [0u8: bb3, otherwise: bb2];
4745
// }
4846
// END rustc.node12.nll.0.mir
4947
// START rustc.node12.nll.0.mir
5048
// bb2: {
51-
// | Regular-Live variables here: [_2]
52-
// | Drop-Live variables here: []
49+
// | Live variables at bb2[0]: [_2]
5350
// StorageLive(_7);
54-
// | Regular-Live variables here: [_2]
55-
// | Drop-Live variables here: []
51+
// | Live variables at bb2[1]: [_2]
5652
// _7 = (*_2);
57-
// | Regular-Live variables here: [_7]
58-
// | Drop-Live variables here: []
53+
// | Live variables at bb2[2]: [_7]
5954
// _6 = const use_x(_7) -> bb4;
6055
// }
6156
// END rustc.node12.nll.0.mir

0 commit comments

Comments
 (0)