Skip to content

Commit 15360b3

Browse files
committed
coverage: Store a graph reference in the graph traversal struct
Having to keep passing in a graph reference was a holdover from when the graph was partly mutated during traversal. As of #114354 that is no longer necessary, so we can simplify the traversal code by storing a graph reference as a field in `TraverseCoverageGraphWithLoops`.
1 parent ea3fb7b commit 15360b3

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

Diff for: compiler/rustc_mir_transform/src/coverage/counters.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,13 @@ impl<'a> MakeBcbCounters<'a> {
245245
// the loop. The `traversal` state includes a `context_stack`, providing a way to know if
246246
// the current BCB is in one or more nested loops or not.
247247
let mut traversal = TraverseCoverageGraphWithLoops::new(&self.basic_coverage_blocks);
248-
while let Some(bcb) = traversal.next(self.basic_coverage_blocks) {
248+
while let Some(bcb) = traversal.next() {
249249
if bcb_has_coverage_spans(bcb) {
250250
debug!("{:?} has at least one coverage span. Get or make its counter", bcb);
251251
let branching_counter_operand = self.get_or_make_counter_operand(bcb)?;
252252

253253
if self.bcb_needs_branch_counters(bcb) {
254-
self.make_branch_counters(&mut traversal, bcb, branching_counter_operand)?;
254+
self.make_branch_counters(&traversal, bcb, branching_counter_operand)?;
255255
}
256256
} else {
257257
debug!(
@@ -274,7 +274,7 @@ impl<'a> MakeBcbCounters<'a> {
274274

275275
fn make_branch_counters(
276276
&mut self,
277-
traversal: &mut TraverseCoverageGraphWithLoops,
277+
traversal: &TraverseCoverageGraphWithLoops<'_>,
278278
branching_bcb: BasicCoverageBlock,
279279
branching_counter_operand: Operand,
280280
) -> Result<(), Error> {
@@ -507,7 +507,7 @@ impl<'a> MakeBcbCounters<'a> {
507507
/// found, select any branch.
508508
fn choose_preferred_expression_branch(
509509
&self,
510-
traversal: &TraverseCoverageGraphWithLoops,
510+
traversal: &TraverseCoverageGraphWithLoops<'_>,
511511
branches: &[BcbBranch],
512512
) -> BcbBranch {
513513
let good_reloop_branch = self.find_good_reloop_branch(traversal, &branches);
@@ -537,7 +537,7 @@ impl<'a> MakeBcbCounters<'a> {
537537
/// will tend to be executed more times than a loop-exit branch.
538538
fn find_good_reloop_branch(
539539
&self,
540-
traversal: &TraverseCoverageGraphWithLoops,
540+
traversal: &TraverseCoverageGraphWithLoops<'_>,
541541
branches: &[BcbBranch],
542542
) -> Option<BcbBranch> {
543543
// Consider each loop on the current traversal context stack, top-down.

Diff for: compiler/rustc_mir_transform/src/coverage/graph.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,16 @@ pub(super) struct TraversalContext {
393393
worklist: VecDeque<BasicCoverageBlock>,
394394
}
395395

396-
pub(super) struct TraverseCoverageGraphWithLoops {
396+
pub(super) struct TraverseCoverageGraphWithLoops<'a> {
397+
basic_coverage_blocks: &'a CoverageGraph,
398+
397399
backedges: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
398400
context_stack: Vec<TraversalContext>,
399401
visited: BitSet<BasicCoverageBlock>,
400402
}
401403

402-
impl TraverseCoverageGraphWithLoops {
403-
pub fn new(basic_coverage_blocks: &CoverageGraph) -> Self {
404+
impl<'a> TraverseCoverageGraphWithLoops<'a> {
405+
pub(super) fn new(basic_coverage_blocks: &'a CoverageGraph) -> Self {
404406
let backedges = find_loop_backedges(basic_coverage_blocks);
405407

406408
let worklist = VecDeque::from([basic_coverage_blocks.start_node()]);
@@ -411,7 +413,7 @@ impl TraverseCoverageGraphWithLoops {
411413
// of the stack as loops are entered, and popped off of the stack when a loop's worklist is
412414
// exhausted.
413415
let visited = BitSet::new_empty(basic_coverage_blocks.num_nodes());
414-
Self { backedges, context_stack, visited }
416+
Self { basic_coverage_blocks, backedges, context_stack, visited }
415417
}
416418

417419
/// For each loop on the loop context stack (top-down), yields a list of BCBs
@@ -424,7 +426,7 @@ impl TraverseCoverageGraphWithLoops {
424426
.map(|(from_bcbs, _to_bcb)| from_bcbs.as_slice())
425427
}
426428

427-
pub fn next(&mut self, basic_coverage_blocks: &CoverageGraph) -> Option<BasicCoverageBlock> {
429+
pub(super) fn next(&mut self) -> Option<BasicCoverageBlock> {
428430
debug!(
429431
"TraverseCoverageGraphWithLoops::next - context_stack: {:?}",
430432
self.context_stack.iter().rev().collect::<Vec<_>>()
@@ -445,7 +447,7 @@ impl TraverseCoverageGraphWithLoops {
445447
worklist: VecDeque::new(),
446448
});
447449
}
448-
self.extend_worklist(basic_coverage_blocks, bcb);
450+
self.extend_worklist(bcb);
449451
return Some(bcb);
450452
} else {
451453
// Strip contexts with empty worklists from the top of the stack
@@ -456,11 +458,8 @@ impl TraverseCoverageGraphWithLoops {
456458
None
457459
}
458460

459-
pub fn extend_worklist(
460-
&mut self,
461-
basic_coverage_blocks: &CoverageGraph,
462-
bcb: BasicCoverageBlock,
463-
) {
461+
pub fn extend_worklist(&mut self, bcb: BasicCoverageBlock) {
462+
let Self { basic_coverage_blocks, .. } = *self;
464463
let successors = &basic_coverage_blocks.successors[bcb];
465464
debug!("{:?} has {} successors:", bcb, successors.len());
466465
for &successor in successors {

Diff for: compiler/rustc_mir_transform/src/coverage/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ fn test_traverse_coverage_with_loops() {
628628
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
629629
let mut traversed_in_order = Vec::new();
630630
let mut traversal = graph::TraverseCoverageGraphWithLoops::new(&basic_coverage_blocks);
631-
while let Some(bcb) = traversal.next(&basic_coverage_blocks) {
631+
while let Some(bcb) = traversal.next() {
632632
traversed_in_order.push(bcb);
633633
}
634634

0 commit comments

Comments
 (0)