Skip to content

Commit 96d545a

Browse files
committed
coverage: Avoid referring to "coverage spans" in counter creation
The counter-creation code needs to know which BCB nodes require counters, but isn't interested in why, so treat that as an opaque detail.
1 parent 26b2b8d commit 96d545a

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ pub(super) struct CoverageCounters {
7474
}
7575

7676
impl CoverageCounters {
77-
/// Makes [`BcbCounter`] `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
78-
/// indirectly associated with coverage spans, and accumulates additional `Expression`s
79-
/// representing intermediate values.
77+
/// Ensures that each BCB node needing a counter has one, by creating physical
78+
/// counters or counter expressions for nodes and edges as required.
8079
pub(super) fn make_bcb_counters(
8180
basic_coverage_blocks: &CoverageGraph,
82-
bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool,
81+
bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool,
8382
) -> Self {
8483
let num_bcbs = basic_coverage_blocks.num_nodes();
8584

@@ -91,8 +90,7 @@ impl CoverageCounters {
9190
expressions_memo: FxHashMap::default(),
9291
};
9392

94-
MakeBcbCounters::new(&mut this, basic_coverage_blocks)
95-
.make_bcb_counters(bcb_has_coverage_spans);
93+
MakeBcbCounters::new(&mut this, basic_coverage_blocks).make_bcb_counters(bcb_needs_counter);
9694

9795
this
9896
}
@@ -241,10 +239,7 @@ impl CoverageCounters {
241239
}
242240
}
243241

244-
/// Traverse the `CoverageGraph` and add either a `Counter` or `Expression` to every BCB, to be
245-
/// injected with coverage spans. `Expressions` have no runtime overhead, so if a viable expression
246-
/// (adding or subtracting two other counters or expressions) can compute the same result as an
247-
/// embedded counter, an `Expression` should be used.
242+
/// Helper struct that allows counter creation to inspect the BCB graph.
248243
struct MakeBcbCounters<'a> {
249244
coverage_counters: &'a mut CoverageCounters,
250245
basic_coverage_blocks: &'a CoverageGraph,
@@ -264,30 +259,21 @@ impl<'a> MakeBcbCounters<'a> {
264259
/// One way to predict which branch executes the least is by considering loops. A loop is exited
265260
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
266261
/// always executed less than the branch that does not exit the loop.
267-
fn make_bcb_counters(&mut self, bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool) {
262+
fn make_bcb_counters(&mut self, bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool) {
268263
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");
269264

270-
// Walk the `CoverageGraph`. For each `BasicCoverageBlock` node with an associated
271-
// coverage span, add a counter. If the `BasicCoverageBlock` branches, add a counter or
272-
// expression to each branch `BasicCoverageBlock` (if the branch BCB has only one incoming
273-
// edge) or edge from the branching BCB to the branch BCB (if the branch BCB has multiple
274-
// incoming edges).
265+
// Traverse the coverage graph, ensuring that every node that needs a
266+
// coverage counter has one.
275267
//
276-
// The `TraverseCoverageGraphWithLoops` traversal ensures that, when a loop is encountered,
277-
// all `BasicCoverageBlock` nodes in the loop are visited before visiting any node outside
278-
// the loop. The `traversal` state includes a `context_stack`, providing a way to know if
279-
// the current BCB is in one or more nested loops or not.
268+
// The traversal tries to ensure that, when a loop is encountered, all
269+
// nodes within the loop are visited before visiting any nodes outside
270+
// the loop. It also keeps track of which loop(s) the traversal is
271+
// currently inside.
280272
let mut traversal = TraverseCoverageGraphWithLoops::new(self.basic_coverage_blocks);
281273
while let Some(bcb) = traversal.next() {
282-
if bcb_has_coverage_spans(bcb) {
283-
debug!("{:?} has at least one coverage span. Get or make its counter", bcb);
274+
let _span = debug_span!("traversal", ?bcb).entered();
275+
if bcb_needs_counter(bcb) {
284276
self.make_node_and_branch_counters(&traversal, bcb);
285-
} else {
286-
debug!(
287-
"{:?} does not have any coverage spans. A counter will only be added if \
288-
and when a covered BCB has an expression dependency.",
289-
bcb,
290-
);
291277
}
292278
}
293279

@@ -298,6 +284,7 @@ impl<'a> MakeBcbCounters<'a> {
298284
);
299285
}
300286

287+
#[instrument(level = "debug", skip(self, traversal))]
301288
fn make_node_and_branch_counters(
302289
&mut self,
303290
traversal: &TraverseCoverageGraphWithLoops<'_>,

0 commit comments

Comments
 (0)