Skip to content

Commit dd71582

Browse files
committed
coverage: Consistently remove unused counter IDs from expressions/mappings
1 parent 822d67b commit dd71582

File tree

10 files changed

+200
-186
lines changed

10 files changed

+200
-186
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

+44-31
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'tcx> FunctionCoverageCollector<'tcx> {
102102
// have zero as both of their operands, and will therefore always have
103103
// a value of zero. Other expressions that refer to these as operands
104104
// can have those operands replaced with `CovTerm::Zero`.
105-
let mut zero_expressions = FxIndexSet::default();
105+
let mut zero_expressions = ZeroExpressions::new();
106106

107107
// Simplify a copy of each expression based on lower-numbered expressions,
108108
// and then update the set of always-zero expressions if necessary.
@@ -131,16 +131,16 @@ impl<'tcx> FunctionCoverageCollector<'tcx> {
131131
)
132132
};
133133

134-
// If an operand refers to an expression that is always zero, then
135-
// that operand can be replaced with `CovTerm::Zero`.
136-
let maybe_set_operand_to_zero = |operand: &mut CovTerm| match *operand {
137-
CovTerm::Expression(id) => {
134+
// If an operand refers to a counter or expression that is always
135+
// zero, then that operand can be replaced with `CovTerm::Zero`.
136+
let maybe_set_operand_to_zero = |operand: &mut CovTerm| {
137+
if let CovTerm::Expression(id) = *operand {
138138
assert_operand_expression_is_lower(id);
139-
if zero_expressions.contains(&id) {
140-
*operand = CovTerm::Zero;
141-
}
142139
}
143-
_ => (),
140+
141+
if is_zero_term(&self.counters_seen, &zero_expressions, *operand) {
142+
*operand = CovTerm::Zero;
143+
}
144144
};
145145
maybe_set_operand_to_zero(&mut lhs);
146146
maybe_set_operand_to_zero(&mut rhs);
@@ -159,7 +159,7 @@ impl<'tcx> FunctionCoverageCollector<'tcx> {
159159
}
160160
}
161161

162-
ZeroExpressions(zero_expressions)
162+
zero_expressions
163163
}
164164

165165
pub(crate) fn into_finished(self) -> FunctionCoverage<'tcx> {
@@ -205,19 +205,14 @@ impl<'tcx> FunctionCoverage<'tcx> {
205205
// thing on the Rust side unless we're confident we can do much better.
206206
// (See `CounterExpressionsMinimizer` in `CoverageMappingWriter.cpp`.)
207207

208-
let counter_from_operand = |operand: CovTerm| match operand {
209-
CovTerm::Expression(id) if self.zero_expressions.contains(id) => Counter::ZERO,
210-
_ => Counter::from_term(operand),
211-
};
212-
213208
self.function_coverage_info.expressions.iter().map(move |&Expression { lhs, op, rhs }| {
214209
CounterExpression {
215-
lhs: counter_from_operand(lhs),
210+
lhs: self.counter_for_term(lhs),
216211
kind: match op {
217212
Op::Add => ExprKind::Add,
218213
Op::Subtract => ExprKind::Subtract,
219214
},
220-
rhs: counter_from_operand(rhs),
215+
rhs: self.counter_for_term(rhs),
221216
}
222217
})
223218
}
@@ -227,25 +222,20 @@ impl<'tcx> FunctionCoverage<'tcx> {
227222
pub(crate) fn counter_regions(
228223
&self,
229224
) -> impl Iterator<Item = (Counter, &CodeRegion)> + ExactSizeIterator {
230-
// Historically, mappings were stored directly in counter/expression
231-
// statements in MIR, and MIR optimizations would sometimes remove them.
232-
// That's mostly no longer true, so now we detect cases where that would
233-
// have happened, and zero out the corresponding mappings here instead.
234-
let counter_for_term = move |term: CovTerm| {
235-
let force_to_zero = match term {
236-
CovTerm::Counter(id) => !self.counters_seen.contains(id),
237-
CovTerm::Expression(id) => self.zero_expressions.contains(id),
238-
CovTerm::Zero => false,
239-
};
240-
if force_to_zero { Counter::ZERO } else { Counter::from_term(term) }
241-
};
242-
243225
self.function_coverage_info.mappings.iter().map(move |mapping| {
244226
let &Mapping { term, ref code_region } = mapping;
245-
let counter = counter_for_term(term);
227+
let counter = self.counter_for_term(term);
246228
(counter, code_region)
247229
})
248230
}
231+
232+
fn counter_for_term(&self, term: CovTerm) -> Counter {
233+
if is_zero_term(&self.counters_seen, &self.zero_expressions, term) {
234+
Counter::ZERO
235+
} else {
236+
Counter::from_term(term)
237+
}
238+
}
249239
}
250240

251241
/// Set of expression IDs that are known to always evaluate to zero.
@@ -254,7 +244,30 @@ impl<'tcx> FunctionCoverage<'tcx> {
254244
struct ZeroExpressions(FxIndexSet<ExpressionId>);
255245

256246
impl ZeroExpressions {
247+
fn new() -> Self {
248+
Self(FxIndexSet::default())
249+
}
250+
251+
fn insert(&mut self, id: ExpressionId) {
252+
self.0.insert(id);
253+
}
254+
257255
fn contains(&self, id: ExpressionId) -> bool {
258256
self.0.contains(&id)
259257
}
260258
}
259+
260+
/// Returns `true` if the given term is known to have a value of zero, taking
261+
/// into account knowledge of which counters are unused and which expressions
262+
/// are always zero.
263+
fn is_zero_term(
264+
counters_seen: &BitSet<CounterId>,
265+
zero_expressions: &ZeroExpressions,
266+
term: CovTerm,
267+
) -> bool {
268+
match term {
269+
CovTerm::Zero => true,
270+
CovTerm::Counter(id) => !counters_seen.contains(id),
271+
CovTerm::Expression(id) => zero_expressions.contains(id),
272+
}
273+
}

tests/coverage-map/fn_sig_into_try.cov-map

+15-15
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@ Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 10, 1) to (start + 4, 2)
88

99
Function name: fn_sig_into_try::b
10-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 10, 01, 02, 0f, 00, 02, 0f, 00, 10, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
10+
Raw bytes (28): 0x[01, 01, 02, 01, 00, 00, 02, 04, 01, 10, 01, 02, 0f, 00, 02, 0f, 00, 10, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
1111
Number of files: 1
1212
- file 0 => global file 1
1313
Number of expressions: 2
14-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
15-
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
14+
- expression 0 operands: lhs = Counter(0), rhs = Zero
15+
- expression 1 operands: lhs = Zero, rhs = Expression(0, Sub)
1616
Number of file 0 mappings: 4
1717
- Code(Counter(0)) at (prev + 16, 1) to (start + 2, 15)
1818
- Code(Zero) at (prev + 2, 15) to (start + 0, 16)
1919
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
20-
= (c0 - c1)
20+
= (c0 - Zero)
2121
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
22-
= (c1 + (c0 - c1))
22+
= (Zero + (c0 - Zero))
2323

2424
Function name: fn_sig_into_try::c
25-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 16, 01, 02, 17, 00, 02, 17, 00, 18, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
25+
Raw bytes (28): 0x[01, 01, 02, 01, 00, 00, 02, 04, 01, 16, 01, 02, 17, 00, 02, 17, 00, 18, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
2626
Number of files: 1
2727
- file 0 => global file 1
2828
Number of expressions: 2
29-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
30-
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
29+
- expression 0 operands: lhs = Counter(0), rhs = Zero
30+
- expression 1 operands: lhs = Zero, rhs = Expression(0, Sub)
3131
Number of file 0 mappings: 4
3232
- Code(Counter(0)) at (prev + 22, 1) to (start + 2, 23)
3333
- Code(Zero) at (prev + 2, 23) to (start + 0, 24)
3434
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
35-
= (c0 - c1)
35+
= (c0 - Zero)
3636
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
37-
= (c1 + (c0 - c1))
37+
= (Zero + (c0 - Zero))
3838

3939
Function name: fn_sig_into_try::d
40-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 1c, 01, 03, 0f, 00, 03, 0f, 00, 10, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
40+
Raw bytes (28): 0x[01, 01, 02, 01, 00, 00, 02, 04, 01, 1c, 01, 03, 0f, 00, 03, 0f, 00, 10, 02, 01, 05, 00, 0c, 07, 01, 01, 00, 02]
4141
Number of files: 1
4242
- file 0 => global file 1
4343
Number of expressions: 2
44-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
45-
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
44+
- expression 0 operands: lhs = Counter(0), rhs = Zero
45+
- expression 1 operands: lhs = Zero, rhs = Expression(0, Sub)
4646
Number of file 0 mappings: 4
4747
- Code(Counter(0)) at (prev + 28, 1) to (start + 3, 15)
4848
- Code(Zero) at (prev + 3, 15) to (start + 0, 16)
4949
- Code(Expression(0, Sub)) at (prev + 1, 5) to (start + 0, 12)
50-
= (c0 - c1)
50+
= (c0 - Zero)
5151
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
52-
= (c1 + (c0 - c1))
52+
= (Zero + (c0 - Zero))
5353

tests/coverage-map/status-quo/bad_counter_ids.cov-map

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ Number of file 0 mappings: 2
2424
- Code(Zero) at (prev + 3, 1) to (start + 0, 2)
2525

2626
Function name: bad_counter_ids::eq_bad_message
27-
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 28, 01, 02, 0f, 02, 02, 20, 00, 2b, 00, 01, 01, 00, 02]
27+
Raw bytes (21): 0x[01, 01, 01, 01, 00, 03, 01, 28, 01, 02, 0f, 02, 02, 20, 00, 2b, 00, 01, 01, 00, 02]
2828
Number of files: 1
2929
- file 0 => global file 1
3030
Number of expressions: 1
31-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
31+
- expression 0 operands: lhs = Counter(0), rhs = Zero
3232
Number of file 0 mappings: 3
3333
- Code(Counter(0)) at (prev + 40, 1) to (start + 2, 15)
3434
- Code(Expression(0, Sub)) at (prev + 2, 32) to (start + 0, 43)
35-
= (c0 - c1)
35+
= (c0 - Zero)
3636
- Code(Zero) at (prev + 1, 1) to (start + 0, 2)
3737

3838
Function name: bad_counter_ids::eq_good
@@ -74,25 +74,25 @@ Number of file 0 mappings: 3
7474
- Code(Zero) at (prev + 1, 1) to (start + 0, 2)
7575

7676
Function name: bad_counter_ids::ne_good
77-
Raw bytes (16): 0x[01, 01, 01, 01, 05, 02, 01, 19, 01, 02, 1f, 02, 03, 01, 00, 02]
77+
Raw bytes (16): 0x[01, 01, 01, 01, 00, 02, 01, 19, 01, 02, 1f, 02, 03, 01, 00, 02]
7878
Number of files: 1
7979
- file 0 => global file 1
8080
Number of expressions: 1
81-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
81+
- expression 0 operands: lhs = Counter(0), rhs = Zero
8282
Number of file 0 mappings: 2
8383
- Code(Counter(0)) at (prev + 25, 1) to (start + 2, 31)
8484
- Code(Expression(0, Sub)) at (prev + 3, 1) to (start + 0, 2)
85-
= (c0 - c1)
85+
= (c0 - Zero)
8686

8787
Function name: bad_counter_ids::ne_good_message
88-
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 1e, 01, 02, 0f, 00, 02, 20, 00, 2b, 02, 01, 01, 00, 02]
88+
Raw bytes (21): 0x[01, 01, 01, 01, 00, 03, 01, 1e, 01, 02, 0f, 00, 02, 20, 00, 2b, 02, 01, 01, 00, 02]
8989
Number of files: 1
9090
- file 0 => global file 1
9191
Number of expressions: 1
92-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
92+
- expression 0 operands: lhs = Counter(0), rhs = Zero
9393
Number of file 0 mappings: 3
9494
- Code(Counter(0)) at (prev + 30, 1) to (start + 2, 15)
9595
- Code(Zero) at (prev + 2, 32) to (start + 0, 43)
9696
- Code(Expression(0, Sub)) at (prev + 1, 1) to (start + 0, 2)
97-
= (c0 - c1)
97+
= (c0 - Zero)
9898

tests/coverage-map/status-quo/inline-dead.cov-map

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ Number of file 0 mappings: 1
77
- Code(Zero) at (prev + 25, 1) to (start + 2, 2)
88

99
Function name: inline_dead::live::<false>
10-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 10, 01, 01, 09, 00, 02, 09, 00, 0f, 02, 02, 09, 00, 0a, 07, 02, 01, 00, 02]
10+
Raw bytes (28): 0x[01, 01, 02, 01, 00, 00, 02, 04, 01, 10, 01, 01, 09, 00, 02, 09, 00, 0f, 02, 02, 09, 00, 0a, 07, 02, 01, 00, 02]
1111
Number of files: 1
1212
- file 0 => global file 1
1313
Number of expressions: 2
14-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
15-
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
14+
- expression 0 operands: lhs = Counter(0), rhs = Zero
15+
- expression 1 operands: lhs = Zero, rhs = Expression(0, Sub)
1616
Number of file 0 mappings: 4
1717
- Code(Counter(0)) at (prev + 16, 1) to (start + 1, 9)
1818
- Code(Zero) at (prev + 2, 9) to (start + 0, 15)
1919
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 10)
20-
= (c0 - c1)
20+
= (c0 - Zero)
2121
- Code(Expression(1, Add)) at (prev + 2, 1) to (start + 0, 2)
22-
= (c1 + (c0 - c1))
22+
= (Zero + (c0 - Zero))
2323

2424
Function name: inline_dead::main
2525
Raw bytes (14): 0x[01, 01, 00, 02, 01, 04, 01, 03, 0d, 01, 07, 06, 02, 02]
@@ -31,15 +31,15 @@ Number of file 0 mappings: 2
3131
- Code(Counter(0)) at (prev + 7, 6) to (start + 2, 2)
3232

3333
Function name: inline_dead::main::{closure#0}
34-
Raw bytes (23): 0x[01, 01, 02, 09, 06, 01, 05, 03, 01, 07, 17, 00, 18, 00, 02, 0d, 00, 0e, 03, 02, 05, 00, 06]
34+
Raw bytes (23): 0x[01, 01, 02, 00, 06, 01, 00, 03, 01, 07, 17, 00, 18, 00, 02, 0d, 00, 0e, 03, 02, 05, 00, 06]
3535
Number of files: 1
3636
- file 0 => global file 1
3737
Number of expressions: 2
38-
- expression 0 operands: lhs = Counter(2), rhs = Expression(1, Sub)
39-
- expression 1 operands: lhs = Counter(0), rhs = Counter(1)
38+
- expression 0 operands: lhs = Zero, rhs = Expression(1, Sub)
39+
- expression 1 operands: lhs = Counter(0), rhs = Zero
4040
Number of file 0 mappings: 3
4141
- Code(Counter(0)) at (prev + 7, 23) to (start + 0, 24)
4242
- Code(Zero) at (prev + 2, 13) to (start + 0, 14)
4343
- Code(Expression(0, Add)) at (prev + 2, 5) to (start + 0, 6)
44-
= (c2 + (c0 - c1))
44+
= (Zero + (c0 - Zero))
4545

0 commit comments

Comments
 (0)