Skip to content

Commit 10f5c64

Browse files
authored
Unrolled build for rust-lang#130263
Rollup merge of rust-lang#130263 - Zalathar:sums, r=compiler-errors coverage: Simplify creation of sum counters A small and self-contained improvement, extracted from some larger changes that I'm still working on. Ultimately I want to avoid creating these sum counter-expressions in some cases (in favour of just adding physical counters directly to the nodes we care about), so a good incremental move towards that is splitting the “gather edge counters” step out from the ”build a sum of those counters” step. Creating an extra intermediate vector should have negligible cost (and coverage isn't exercised by the benchmark suite anyway). The removed logging is redundant with the `#[instrument(..)]` logging we already have on the underlying method calls.
2 parents 8c0ec05 + 2344133 commit 10f5c64

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

+27-33
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ impl CoverageCounters {
155155
BcbCounter::Expression { id }
156156
}
157157

158-
/// Variant of `make_expression` that makes `lhs` optional and assumes [`Op::Add`].
158+
/// Creates a counter that is the sum of the given counters.
159159
///
160-
/// This is useful when using [`Iterator::fold`] to build an arbitrary-length sum.
161-
fn make_sum_expression(&mut self, lhs: Option<BcbCounter>, rhs: BcbCounter) -> BcbCounter {
162-
let Some(lhs) = lhs else { return rhs };
163-
self.make_expression(lhs, Op::Add, rhs)
160+
/// Returns `None` if the given list of counters was empty.
161+
fn make_sum(&mut self, counters: &[BcbCounter]) -> Option<BcbCounter> {
162+
counters
163+
.iter()
164+
.copied()
165+
.reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
164166
}
165167

166168
pub(super) fn num_counters(&self) -> usize {
@@ -315,20 +317,17 @@ impl<'a> MakeBcbCounters<'a> {
315317
// For each out-edge other than the one that was chosen to get an expression,
316318
// ensure that it has a counter (existing counter/expression or a new counter),
317319
// and accumulate the corresponding counters into a single sum expression.
318-
let sum_of_all_other_out_edges: BcbCounter = {
319-
let _span = debug_span!("sum_of_all_other_out_edges", ?expression_to_bcb).entered();
320-
successors
321-
.iter()
322-
.copied()
323-
// Skip the chosen edge, since we'll calculate its count from this sum.
324-
.filter(|&to_bcb| to_bcb != expression_to_bcb)
325-
.fold(None, |accum, to_bcb| {
326-
let _span = debug_span!("to_bcb", ?accum, ?to_bcb).entered();
327-
let edge_counter = self.get_or_make_edge_counter(from_bcb, to_bcb);
328-
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
329-
})
330-
.expect("there must be at least one other out-edge")
331-
};
320+
let other_out_edge_counters = successors
321+
.iter()
322+
.copied()
323+
// Skip the chosen edge, since we'll calculate its count from this sum.
324+
.filter(|&to_bcb| to_bcb != expression_to_bcb)
325+
.map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
326+
.collect::<Vec<_>>();
327+
let sum_of_all_other_out_edges: BcbCounter = self
328+
.coverage_counters
329+
.make_sum(&other_out_edge_counters)
330+
.expect("there must be at least one other out-edge");
332331

333332
// Now create an expression for the chosen edge, by taking the counter
334333
// for its source node and subtracting the sum of its sibling out-edges.
@@ -375,20 +374,15 @@ impl<'a> MakeBcbCounters<'a> {
375374

376375
// A BCB with multiple incoming edges can compute its count by ensuring that counters
377376
// exist for each of those edges, and then adding them up to get a total count.
378-
let sum_of_in_edges: BcbCounter = {
379-
let _span = debug_span!("sum_of_in_edges", ?bcb).entered();
380-
// We avoid calling `self.bcb_predecessors` here so that we can
381-
// call methods on `&mut self` inside the fold.
382-
self.basic_coverage_blocks.predecessors[bcb]
383-
.iter()
384-
.copied()
385-
.fold(None, |accum, from_bcb| {
386-
let _span = debug_span!("from_bcb", ?accum, ?from_bcb).entered();
387-
let edge_counter = self.get_or_make_edge_counter(from_bcb, bcb);
388-
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
389-
})
390-
.expect("there must be at least one in-edge")
391-
};
377+
let in_edge_counters = self.basic_coverage_blocks.predecessors[bcb]
378+
.iter()
379+
.copied()
380+
.map(|from_bcb| self.get_or_make_edge_counter(from_bcb, bcb))
381+
.collect::<Vec<_>>();
382+
let sum_of_in_edges: BcbCounter = self
383+
.coverage_counters
384+
.make_sum(&in_edge_counters)
385+
.expect("there must be at least one in-edge");
392386

393387
debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}");
394388
self.coverage_counters.set_bcb_counter(bcb, sum_of_in_edges)

0 commit comments

Comments
 (0)