Skip to content

Commit bf1f254

Browse files
committed
coverage: Don't create counters for code that was removed by MIR opts
1 parent 20d051e commit bf1f254

10 files changed

+103
-134
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,30 @@ fn make_node_flow_priority_list(
8080
pub(crate) fn transcribe_counters(
8181
old: &NodeCounters<BasicCoverageBlock>,
8282
bcb_needs_counter: &DenseBitSet<BasicCoverageBlock>,
83+
bcbs_seen: &DenseBitSet<BasicCoverageBlock>,
8384
) -> CoverageCounters {
8485
let mut new = CoverageCounters::with_num_bcbs(bcb_needs_counter.domain_size());
8586

8687
for bcb in bcb_needs_counter.iter() {
88+
if !bcbs_seen.contains(bcb) {
89+
// This BCB's code was removed by MIR opts, so its counter is always zero.
90+
new.set_node_counter(bcb, CovTerm::Zero);
91+
continue;
92+
}
93+
8794
// Our counter-creation algorithm doesn't guarantee that a node's list
8895
// of terms starts or ends with a positive term, so partition the
8996
// counters into "positive" and "negative" lists for easier handling.
90-
let (mut pos, mut neg): (Vec<_>, Vec<_>) =
91-
old.counter_terms[bcb].iter().partition_map(|&CounterTerm { node, op }| match op {
97+
let (mut pos, mut neg): (Vec<_>, Vec<_>) = old.counter_terms[bcb]
98+
.iter()
99+
// Filter out any BCBs that were removed by MIR opts;
100+
// this treats them as having an execution count of 0.
101+
.filter(|term| bcbs_seen.contains(term.node))
102+
.partition_map(|&CounterTerm { node, op }| match op {
92103
Op::Add => Either::Left(node),
93104
Op::Subtract => Either::Right(node),
94105
});
95106

96-
if pos.is_empty() {
97-
// If we somehow end up with no positive terms, fall back to
98-
// creating a physical counter. There's no known way for this
99-
// to happen, but we can avoid an ICE if it does.
100-
debug_assert!(false, "{bcb:?} has no positive counter terms");
101-
pos = vec![bcb];
102-
neg = vec![];
103-
}
104-
105107
// These intermediate sorts are not strictly necessary, but were helpful
106108
// in reducing churn when switching to the current counter-creation scheme.
107109
// They also help to slightly decrease the overall size of the expression
@@ -119,7 +121,7 @@ pub(crate) fn transcribe_counters(
119121
pos.sort();
120122
neg.sort();
121123

122-
let pos_counter = new.make_sum(&pos).expect("`pos` should not be empty");
124+
let pos_counter = new.make_sum(&pos).unwrap_or(CovTerm::Zero);
123125
let new_counter = new.make_subtracted_sum(pos_counter, &neg);
124126
new.set_node_counter(bcb, new_counter);
125127
}

compiler/rustc_mir_transform/src/coverage/query.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ fn coverage_ids_info<'tcx>(
127127
}
128128
}
129129

130+
// FIXME(Zalathar): It should be possible to sort `priority_list[1..]` by
131+
// `!bcbs_seen.contains(bcb)` to simplify the mappings even further, at the
132+
// expense of some churn in the tests. When doing so, also consider removing
133+
// the sorts in `transcribe_counters`.
130134
let node_counters = make_node_counters(&fn_cov_info.node_flow_data, &fn_cov_info.priority_list);
131-
let coverage_counters = transcribe_counters(&node_counters, &bcb_needs_counter);
135+
let coverage_counters = transcribe_counters(&node_counters, &bcb_needs_counter, &bcbs_seen);
132136

133137
let mut counters_seen = DenseBitSet::new_empty(coverage_counters.node_counters.len());
134138
let mut expressions_seen = DenseBitSet::new_filled(coverage_counters.expressions.len());

tests/coverage/assert_not.cov-map

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
Function name: assert_not::main
2-
Raw bytes (31): 0x[01, 01, 01, 0d, 00, 05, 01, 06, 01, 01, 12, 05, 02, 05, 00, 14, 09, 01, 05, 00, 14, 0d, 01, 05, 00, 16, 02, 01, 01, 00, 02]
2+
Raw bytes (29): 0x[01, 01, 00, 05, 01, 06, 01, 01, 12, 05, 02, 05, 00, 14, 09, 01, 05, 00, 14, 0d, 01, 05, 00, 16, 0d, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
5-
Number of expressions: 1
6-
- expression 0 operands: lhs = Counter(3), rhs = Zero
5+
Number of expressions: 0
76
Number of file 0 mappings: 5
87
- Code(Counter(0)) at (prev + 6, 1) to (start + 1, 18)
98
- Code(Counter(1)) at (prev + 2, 5) to (start + 0, 20)
109
- Code(Counter(2)) at (prev + 1, 5) to (start + 0, 20)
1110
- Code(Counter(3)) at (prev + 1, 5) to (start + 0, 22)
12-
- Code(Expression(0, Sub)) at (prev + 1, 1) to (start + 0, 2)
13-
= (c3 - Zero)
11+
- Code(Counter(3)) at (prev + 1, 1) to (start + 0, 2)
1412
Highest counter ID seen: c3
1513

tests/coverage/bad_counter_ids.cov-map

+9-15
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ Number of file 0 mappings: 2
99
Highest counter ID seen: c0
1010

1111
Function name: bad_counter_ids::eq_bad_message
12-
Raw bytes (21): 0x[01, 01, 01, 01, 00, 03, 01, 29, 01, 02, 0f, 02, 02, 20, 00, 2b, 00, 01, 01, 00, 02]
12+
Raw bytes (19): 0x[01, 01, 00, 03, 01, 29, 01, 02, 0f, 01, 02, 20, 00, 2b, 00, 01, 01, 00, 02]
1313
Number of files: 1
1414
- file 0 => global file 1
15-
Number of expressions: 1
16-
- expression 0 operands: lhs = Counter(0), rhs = Zero
15+
Number of expressions: 0
1716
Number of file 0 mappings: 3
1817
- Code(Counter(0)) at (prev + 41, 1) to (start + 2, 15)
19-
- Code(Expression(0, Sub)) at (prev + 2, 32) to (start + 0, 43)
20-
= (c0 - Zero)
18+
- Code(Counter(0)) at (prev + 2, 32) to (start + 0, 43)
2119
- Code(Zero) at (prev + 1, 1) to (start + 0, 2)
2220
Highest counter ID seen: c0
2321

@@ -64,27 +62,23 @@ Number of file 0 mappings: 3
6462
Highest counter ID seen: c1
6563

6664
Function name: bad_counter_ids::ne_good
67-
Raw bytes (16): 0x[01, 01, 01, 01, 00, 02, 01, 1a, 01, 02, 1f, 02, 03, 01, 00, 02]
65+
Raw bytes (14): 0x[01, 01, 00, 02, 01, 1a, 01, 02, 1f, 01, 03, 01, 00, 02]
6866
Number of files: 1
6967
- file 0 => global file 1
70-
Number of expressions: 1
71-
- expression 0 operands: lhs = Counter(0), rhs = Zero
68+
Number of expressions: 0
7269
Number of file 0 mappings: 2
7370
- Code(Counter(0)) at (prev + 26, 1) to (start + 2, 31)
74-
- Code(Expression(0, Sub)) at (prev + 3, 1) to (start + 0, 2)
75-
= (c0 - Zero)
71+
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
7672
Highest counter ID seen: c0
7773

7874
Function name: bad_counter_ids::ne_good_message
79-
Raw bytes (21): 0x[01, 01, 01, 01, 00, 03, 01, 1f, 01, 02, 0f, 00, 02, 20, 00, 2b, 02, 01, 01, 00, 02]
75+
Raw bytes (19): 0x[01, 01, 00, 03, 01, 1f, 01, 02, 0f, 00, 02, 20, 00, 2b, 01, 01, 01, 00, 02]
8076
Number of files: 1
8177
- file 0 => global file 1
82-
Number of expressions: 1
83-
- expression 0 operands: lhs = Counter(0), rhs = Zero
78+
Number of expressions: 0
8479
Number of file 0 mappings: 3
8580
- Code(Counter(0)) at (prev + 31, 1) to (start + 2, 15)
8681
- Code(Zero) at (prev + 2, 32) to (start + 0, 43)
87-
- Code(Expression(0, Sub)) at (prev + 1, 1) to (start + 0, 2)
88-
= (c0 - Zero)
82+
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
8983
Highest counter ID seen: c0
9084

tests/coverage/fn_sig_into_try.cov-map

+9-15
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,38 @@ Number of file 0 mappings: 1
88
Highest counter ID seen: c0
99

1010
Function name: fn_sig_into_try::b
11-
Raw bytes (26): 0x[01, 01, 01, 01, 00, 04, 01, 11, 01, 03, 0f, 00, 03, 0f, 00, 10, 02, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
11+
Raw bytes (24): 0x[01, 01, 00, 04, 01, 11, 01, 03, 0f, 00, 03, 0f, 00, 10, 01, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
1212
Number of files: 1
1313
- file 0 => global file 1
14-
Number of expressions: 1
15-
- expression 0 operands: lhs = Counter(0), rhs = Zero
14+
Number of expressions: 0
1615
Number of file 0 mappings: 4
1716
- Code(Counter(0)) at (prev + 17, 1) to (start + 3, 15)
1817
- Code(Zero) at (prev + 3, 15) to (start + 0, 16)
19-
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
20-
= (c0 - Zero)
18+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
2119
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
2220
Highest counter ID seen: c0
2321

2422
Function name: fn_sig_into_try::c
25-
Raw bytes (26): 0x[01, 01, 01, 01, 00, 04, 01, 18, 01, 03, 17, 00, 03, 17, 00, 18, 02, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
23+
Raw bytes (24): 0x[01, 01, 00, 04, 01, 18, 01, 03, 17, 00, 03, 17, 00, 18, 01, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
2624
Number of files: 1
2725
- file 0 => global file 1
28-
Number of expressions: 1
29-
- expression 0 operands: lhs = Counter(0), rhs = Zero
26+
Number of expressions: 0
3027
Number of file 0 mappings: 4
3128
- Code(Counter(0)) at (prev + 24, 1) to (start + 3, 23)
3229
- Code(Zero) at (prev + 3, 23) to (start + 0, 24)
33-
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
34-
= (c0 - Zero)
30+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
3531
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
3632
Highest counter ID seen: c0
3733

3834
Function name: fn_sig_into_try::d
39-
Raw bytes (26): 0x[01, 01, 01, 01, 00, 04, 01, 1f, 01, 04, 0f, 00, 04, 0f, 00, 10, 02, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
35+
Raw bytes (24): 0x[01, 01, 00, 04, 01, 1f, 01, 04, 0f, 00, 04, 0f, 00, 10, 01, 01, 05, 00, 0c, 01, 01, 01, 00, 02]
4036
Number of files: 1
4137
- file 0 => global file 1
42-
Number of expressions: 1
43-
- expression 0 operands: lhs = Counter(0), rhs = Zero
38+
Number of expressions: 0
4439
Number of file 0 mappings: 4
4540
- Code(Counter(0)) at (prev + 31, 1) to (start + 4, 15)
4641
- Code(Zero) at (prev + 4, 15) to (start + 0, 16)
47-
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
48-
= (c0 - Zero)
42+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
4943
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
5044
Highest counter ID seen: c0
5145

tests/coverage/inline-dead.cov-map

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ Number of file 0 mappings: 2
3232
Highest counter ID seen: c0
3333

3434
Function name: inline_dead::main::{closure#0}
35-
Raw bytes (23): 0x[01, 01, 02, 07, 00, 01, 00, 03, 01, 07, 17, 01, 16, 00, 01, 17, 00, 18, 02, 01, 05, 00, 06]
35+
Raw bytes (19): 0x[01, 01, 00, 03, 01, 07, 17, 01, 16, 00, 01, 17, 00, 18, 01, 01, 05, 00, 06]
3636
Number of files: 1
3737
- file 0 => global file 1
38-
Number of expressions: 2
39-
- expression 0 operands: lhs = Expression(1, Add), rhs = Zero
40-
- expression 1 operands: lhs = Counter(0), rhs = Zero
38+
Number of expressions: 0
4139
Number of file 0 mappings: 3
4240
- Code(Counter(0)) at (prev + 7, 23) to (start + 1, 22)
4341
- Code(Zero) at (prev + 1, 23) to (start + 0, 24)
44-
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 6)
45-
= ((c0 + Zero) - Zero)
42+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 6)
4643
Highest counter ID seen: c0
4744

tests/coverage/issue-84561.cov-map

+14-15
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ Number of file 0 mappings: 1
5959
Highest counter ID seen: c0
6060

6161
Function name: issue_84561::test3
62-
Raw bytes (317): 0x[01, 01, 1c, 1d, 21, 25, 29, 21, 25, 2d, 31, 21, 17, 25, 2d, 41, 45, 49, 4d, 51, 55, 33, 51, 49, 4d, 33, 37, 49, 4d, 51, 59, 55, 59, 55, 59, 47, 5d, 55, 59, 61, 65, 71, 75, 69, 6d, 69, 6d, 69, 5f, 6d, 00, 67, 79, 71, 75, 79, 7d, 7d, 81, 01, 33, 01, 08, 01, 03, 1c, 05, 04, 09, 01, 1c, 09, 02, 05, 04, 1f, 0d, 05, 05, 00, 1f, 11, 01, 05, 00, 1f, 15, 01, 09, 01, 1c, 19, 02, 05, 00, 1f, 1d, 01, 05, 00, 0f, 02, 00, 20, 00, 30, 21, 01, 05, 03, 0f, 25, 03, 20, 00, 30, 29, 00, 33, 00, 41, 06, 00, 4b, 00, 5a, 0a, 01, 05, 00, 0f, 2d, 05, 09, 03, 10, 31, 05, 0d, 00, 1b, 0e, 02, 0d, 00, 1c, 12, 04, 09, 05, 06, 35, 06, 05, 03, 06, 39, 04, 05, 03, 06, 3d, 04, 09, 04, 06, 41, 05, 08, 00, 0f, 45, 01, 09, 03, 0a, 1a, 05, 09, 03, 0a, 33, 05, 08, 00, 0f, 51, 01, 09, 00, 13, 22, 03, 0d, 00, 1d, 26, 03, 09, 00, 13, 2e, 03, 0d, 00, 1d, 47, 03, 05, 00, 0f, 47, 01, 0c, 00, 13, 5d, 01, 0d, 00, 13, 42, 02, 0d, 00, 13, 61, 04, 05, 02, 13, 65, 03, 0d, 00, 13, 4a, 02, 0d, 00, 13, 67, 03, 05, 00, 0f, 69, 01, 0c, 00, 13, 6d, 01, 0d, 03, 0e, 71, 04, 0d, 00, 13, 56, 02, 0d, 00, 17, 56, 01, 14, 00, 1b, 00, 01, 15, 00, 1b, 5a, 02, 15, 00, 1b, 75, 04, 0d, 00, 13, 62, 03, 09, 00, 19, 79, 02, 05, 00, 0f, 6a, 03, 09, 00, 22, 7d, 02, 05, 00, 0f, 6e, 03, 09, 00, 2c, 81, 01, 02, 01, 00, 02]
62+
Raw bytes (315): 0x[01, 01, 1b, 1d, 21, 25, 29, 21, 25, 2d, 31, 21, 17, 25, 2d, 41, 45, 49, 4d, 51, 55, 33, 51, 49, 4d, 33, 37, 49, 4d, 51, 59, 55, 59, 55, 59, 47, 5d, 55, 59, 61, 65, 71, 75, 69, 6d, 69, 6d, 69, 6d, 63, 79, 71, 75, 79, 7d, 7d, 81, 01, 33, 01, 08, 01, 03, 1c, 05, 04, 09, 01, 1c, 09, 02, 05, 04, 1f, 0d, 05, 05, 00, 1f, 11, 01, 05, 00, 1f, 15, 01, 09, 01, 1c, 19, 02, 05, 00, 1f, 1d, 01, 05, 00, 0f, 02, 00, 20, 00, 30, 21, 01, 05, 03, 0f, 25, 03, 20, 00, 30, 29, 00, 33, 00, 41, 06, 00, 4b, 00, 5a, 0a, 01, 05, 00, 0f, 2d, 05, 09, 03, 10, 31, 05, 0d, 00, 1b, 0e, 02, 0d, 00, 1c, 12, 04, 09, 05, 06, 35, 06, 05, 03, 06, 39, 04, 05, 03, 06, 3d, 04, 09, 04, 06, 41, 05, 08, 00, 0f, 45, 01, 09, 03, 0a, 1a, 05, 09, 03, 0a, 33, 05, 08, 00, 0f, 51, 01, 09, 00, 13, 22, 03, 0d, 00, 1d, 26, 03, 09, 00, 13, 2e, 03, 0d, 00, 1d, 47, 03, 05, 00, 0f, 47, 01, 0c, 00, 13, 5d, 01, 0d, 00, 13, 42, 02, 0d, 00, 13, 61, 04, 05, 02, 13, 65, 03, 0d, 00, 13, 4a, 02, 0d, 00, 13, 63, 03, 05, 00, 0f, 69, 01, 0c, 00, 13, 6d, 01, 0d, 03, 0e, 71, 04, 0d, 00, 13, 5a, 02, 0d, 00, 17, 5a, 01, 14, 00, 1b, 00, 01, 15, 00, 1b, 5a, 02, 15, 00, 1b, 75, 04, 0d, 00, 13, 5e, 03, 09, 00, 19, 79, 02, 05, 00, 0f, 66, 03, 09, 00, 22, 7d, 02, 05, 00, 0f, 6a, 03, 09, 00, 2c, 81, 01, 02, 01, 00, 02]
6363
Number of files: 1
6464
- file 0 => global file 1
65-
Number of expressions: 28
65+
Number of expressions: 27
6666
- expression 0 operands: lhs = Counter(7), rhs = Counter(8)
6767
- expression 1 operands: lhs = Counter(9), rhs = Counter(10)
6868
- expression 2 operands: lhs = Counter(8), rhs = Counter(9)
@@ -85,12 +85,11 @@ Number of expressions: 28
8585
- expression 19 operands: lhs = Counter(28), rhs = Counter(29)
8686
- expression 20 operands: lhs = Counter(26), rhs = Counter(27)
8787
- expression 21 operands: lhs = Counter(26), rhs = Counter(27)
88-
- expression 22 operands: lhs = Counter(26), rhs = Expression(23, Add)
89-
- expression 23 operands: lhs = Counter(27), rhs = Zero
90-
- expression 24 operands: lhs = Expression(25, Add), rhs = Counter(30)
91-
- expression 25 operands: lhs = Counter(28), rhs = Counter(29)
92-
- expression 26 operands: lhs = Counter(30), rhs = Counter(31)
93-
- expression 27 operands: lhs = Counter(31), rhs = Counter(32)
88+
- expression 22 operands: lhs = Counter(26), rhs = Counter(27)
89+
- expression 23 operands: lhs = Expression(24, Add), rhs = Counter(30)
90+
- expression 24 operands: lhs = Counter(28), rhs = Counter(29)
91+
- expression 25 operands: lhs = Counter(30), rhs = Counter(31)
92+
- expression 26 operands: lhs = Counter(31), rhs = Counter(32)
9493
Number of file 0 mappings: 51
9594
- Code(Counter(0)) at (prev + 8, 1) to (start + 3, 28)
9695
- Code(Counter(1)) at (prev + 4, 9) to (start + 1, 28)
@@ -142,26 +141,26 @@ Number of file 0 mappings: 51
142141
- Code(Counter(25)) at (prev + 3, 13) to (start + 0, 19)
143142
- Code(Expression(18, Sub)) at (prev + 2, 13) to (start + 0, 19)
144143
= (c24 - c25)
145-
- Code(Expression(25, Add)) at (prev + 3, 5) to (start + 0, 15)
144+
- Code(Expression(24, Add)) at (prev + 3, 5) to (start + 0, 15)
146145
= (c28 + c29)
147146
- Code(Counter(26)) at (prev + 1, 12) to (start + 0, 19)
148147
- Code(Counter(27)) at (prev + 1, 13) to (start + 3, 14)
149148
- Code(Counter(28)) at (prev + 4, 13) to (start + 0, 19)
150-
- Code(Expression(21, Sub)) at (prev + 2, 13) to (start + 0, 23)
149+
- Code(Expression(22, Sub)) at (prev + 2, 13) to (start + 0, 23)
151150
= (c26 - c27)
152-
- Code(Expression(21, Sub)) at (prev + 1, 20) to (start + 0, 27)
151+
- Code(Expression(22, Sub)) at (prev + 1, 20) to (start + 0, 27)
153152
= (c26 - c27)
154153
- Code(Zero) at (prev + 1, 21) to (start + 0, 27)
155154
- Code(Expression(22, Sub)) at (prev + 2, 21) to (start + 0, 27)
156-
= (c26 - (c27 + Zero))
155+
= (c26 - c27)
157156
- Code(Counter(29)) at (prev + 4, 13) to (start + 0, 19)
158-
- Code(Expression(24, Sub)) at (prev + 3, 9) to (start + 0, 25)
157+
- Code(Expression(23, Sub)) at (prev + 3, 9) to (start + 0, 25)
159158
= ((c28 + c29) - c30)
160159
- Code(Counter(30)) at (prev + 2, 5) to (start + 0, 15)
161-
- Code(Expression(26, Sub)) at (prev + 3, 9) to (start + 0, 34)
160+
- Code(Expression(25, Sub)) at (prev + 3, 9) to (start + 0, 34)
162161
= (c30 - c31)
163162
- Code(Counter(31)) at (prev + 2, 5) to (start + 0, 15)
164-
- Code(Expression(27, Sub)) at (prev + 3, 9) to (start + 0, 44)
163+
- Code(Expression(26, Sub)) at (prev + 3, 9) to (start + 0, 44)
165164
= (c31 - c32)
166165
- Code(Counter(32)) at (prev + 2, 1) to (start + 0, 2)
167166
Highest counter ID seen: c32

0 commit comments

Comments
 (0)