Skip to content

Commit 2a3b4a0

Browse files
committed
coverage: Extract subtracted_sum in counter creation
1 parent 3b38264 commit 2a3b4a0

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ impl CoverageCounters {
182182
.reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
183183
}
184184

185+
/// Creates a counter whose value is `lhs - SUM(rhs)`.
186+
fn make_subtracted_sum(&mut self, lhs: BcbCounter, rhs: &[BcbCounter]) -> BcbCounter {
187+
let Some(rhs_sum) = self.make_sum(rhs) else { return lhs };
188+
self.make_expression(lhs, Op::Subtract, rhs_sum)
189+
}
190+
185191
pub(super) fn num_counters(&self) -> usize {
186192
self.counter_increment_sites.len()
187193
}
@@ -338,24 +344,18 @@ impl<'a> CountersBuilder<'a> {
338344
};
339345

340346
// For each out-edge other than the one that was chosen to get an expression,
341-
// ensure that it has a counter (existing counter/expression or a new counter),
342-
// and accumulate the corresponding counters into a single sum expression.
347+
// ensure that it has a counter (existing counter/expression or a new counter).
343348
let other_out_edge_counters = successors
344349
.iter()
345350
.copied()
346351
// Skip the chosen edge, since we'll calculate its count from this sum.
347352
.filter(|&edge_target_bcb| edge_target_bcb != target_bcb)
348353
.map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
349354
.collect::<Vec<_>>();
350-
let Some(sum_of_all_other_out_edges) = self.counters.make_sum(&other_out_edge_counters)
351-
else {
352-
return;
353-
};
354355

355356
// Now create an expression for the chosen edge, by taking the counter
356357
// for its source node and subtracting the sum of its sibling out-edges.
357-
let expression =
358-
self.counters.make_expression(node_counter, Op::Subtract, sum_of_all_other_out_edges);
358+
let expression = self.counters.make_subtracted_sum(node_counter, &other_out_edge_counters);
359359

360360
debug!("{target_bcb:?} gets an expression: {expression:?}");
361361
self.counters.set_edge_counter(from_bcb, target_bcb, expression);

tests/coverage/branch/guard.cov-map

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: guard::branch_match_guard
2-
Raw bytes (85): 0x[01, 01, 06, 19, 0d, 05, 09, 0f, 15, 13, 11, 17, 0d, 05, 09, 0d, 01, 0c, 01, 01, 10, 1d, 03, 0b, 00, 0c, 15, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 19, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 1d, 00, 14, 00, 19, 20, 11, 09, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 17, 03, 0e, 02, 0a, 0b, 04, 01, 00, 02]
2+
Raw bytes (85): 0x[01, 01, 06, 19, 0d, 05, 09, 0f, 15, 13, 11, 17, 0d, 05, 09, 0d, 01, 0c, 01, 01, 10, 02, 03, 0b, 00, 0c, 15, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 19, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 09, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 17, 03, 0e, 02, 0a, 0b, 04, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 6
@@ -11,7 +11,8 @@ Number of expressions: 6
1111
- expression 5 operands: lhs = Counter(1), rhs = Counter(2)
1212
Number of file 0 mappings: 13
1313
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
14-
- Code(Counter(7)) at (prev + 3, 11) to (start + 0, 12)
14+
- Code(Expression(0, Sub)) at (prev + 3, 11) to (start + 0, 12)
15+
= (c6 - c3)
1516
- Code(Counter(5)) at (prev + 1, 20) to (start + 2, 10)
1617
- Code(Counter(3)) at (prev + 3, 14) to (start + 0, 15)
1718
- Code(Counter(6)) at (prev + 0, 20) to (start + 0, 25)
@@ -20,7 +21,8 @@ Number of file 0 mappings: 13
2021
false = (c6 - c3)
2122
- Code(Counter(3)) at (prev + 0, 29) to (start + 2, 10)
2223
- Code(Counter(4)) at (prev + 3, 14) to (start + 0, 15)
23-
- Code(Counter(7)) at (prev + 0, 20) to (start + 0, 25)
24+
- Code(Expression(0, Sub)) at (prev + 0, 20) to (start + 0, 25)
25+
= (c6 - c3)
2426
- Branch { true: Counter(4), false: Counter(2) } at (prev + 0, 20) to (start + 0, 30)
2527
true = c4
2628
false = c2
@@ -29,5 +31,5 @@ Number of file 0 mappings: 13
2931
= (c1 + c2)
3032
- Code(Expression(2, Add)) at (prev + 4, 1) to (start + 0, 2)
3133
= ((((c1 + c2) + c3) + c4) + c5)
32-
Highest counter ID seen: c7
34+
Highest counter ID seen: c6
3335

0 commit comments

Comments
 (0)