Skip to content

Commit 5208d65

Browse files
committed
coverage: Pass around basic blocks instead of references
For code that already has access to the MIR body, it's simpler and easier to pass around basic block indices, and look up details in the body only where they are needed.
1 parent 02f1db7 commit 5208d65

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

compiler/rustc_mir_transform/src/coverage/graph.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::graph::dominators::{self, Dominators};
22
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
33
use rustc_index::bit_set::BitSet;
44
use rustc_index::{IndexSlice, IndexVec};
5-
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, Terminator, TerminatorKind};
5+
use rustc_middle::mir::{self, BasicBlock, TerminatorKind};
66

77
use std::cmp::Ordering;
88
use std::ops::{Index, IndexMut};
@@ -36,9 +36,8 @@ impl CoverageGraph {
3636
}
3737
let bcb_data = &bcbs[bcb];
3838
let mut bcb_successors = Vec::new();
39-
for successor in
40-
bcb_filtered_successors(&mir_body, &bcb_data.terminator(mir_body).kind)
41-
.filter_map(|successor_bb| bb_to_bcb[successor_bb])
39+
for successor in bcb_filtered_successors(&mir_body, bcb_data.last_bb())
40+
.filter_map(|successor_bb| bb_to_bcb[successor_bb])
4241
{
4342
if !seen[successor] {
4443
seen[successor] = true;
@@ -83,7 +82,7 @@ impl CoverageGraph {
8382
let mir_cfg_without_unwind = ShortCircuitPreorder::new(&mir_body, bcb_filtered_successors);
8483

8584
let mut basic_blocks = Vec::new();
86-
for (bb, data) in mir_cfg_without_unwind {
85+
for bb in mir_cfg_without_unwind {
8786
if let Some(last) = basic_blocks.last() {
8887
let predecessors = &mir_body.basic_blocks.predecessors()[bb];
8988
if predecessors.len() > 1 || !predecessors.contains(last) {
@@ -109,7 +108,7 @@ impl CoverageGraph {
109108
}
110109
basic_blocks.push(bb);
111110

112-
let term = data.terminator();
111+
let term = mir_body[bb].terminator();
113112

114113
match term.kind {
115114
TerminatorKind::Return { .. }
@@ -316,11 +315,6 @@ impl BasicCoverageBlockData {
316315
pub fn last_bb(&self) -> BasicBlock {
317316
*self.basic_blocks.last().unwrap()
318317
}
319-
320-
#[inline(always)]
321-
pub fn terminator<'a, 'tcx>(&self, mir_body: &'a mir::Body<'tcx>) -> &'a Terminator<'tcx> {
322-
&mir_body[self.last_bb()].terminator()
323-
}
324318
}
325319

326320
/// Represents a successor from a branching BasicCoverageBlock (such as the arms of a `SwitchInt`)
@@ -362,13 +356,15 @@ impl std::fmt::Debug for BcbBranch {
362356
}
363357
}
364358

365-
// Returns the `Terminator`s non-unwind successors.
359+
// Returns the subset of a block's successors that are relevant for the coverage
360+
// graph, i.e. those that do not represent unwinds or unreachable branches.
366361
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
367362
// `catch_unwind()` handlers.
368363
fn bcb_filtered_successors<'a, 'tcx>(
369364
body: &'a mir::Body<'tcx>,
370-
term_kind: &'a TerminatorKind<'tcx>,
365+
bb: BasicBlock,
371366
) -> Box<dyn Iterator<Item = BasicBlock> + 'a> {
367+
let term_kind = &body[bb].terminator().kind;
372368
Box::new(
373369
match &term_kind {
374370
// SwitchInt successors are never unwind, and all of them should be traversed.
@@ -562,10 +558,7 @@ struct ShortCircuitPreorder<'a, 'tcx, F> {
562558

563559
impl<'a, 'tcx, F> ShortCircuitPreorder<'a, 'tcx, F>
564560
where
565-
F: Fn(
566-
&'a mir::Body<'tcx>,
567-
&'a TerminatorKind<'tcx>,
568-
) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
561+
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
569562
{
570563
fn new(body: &'a mir::Body<'tcx>, filtered_successors: F) -> Self {
571564
Self {
@@ -579,26 +572,19 @@ where
579572

580573
impl<'a, 'tcx, F> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
581574
where
582-
F: Fn(
583-
&'a mir::Body<'tcx>,
584-
&'a TerminatorKind<'tcx>,
585-
) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
575+
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
586576
{
587-
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
577+
type Item = BasicBlock;
588578

589-
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
590-
while let Some(idx) = self.worklist.pop() {
591-
if !self.visited.insert(idx) {
579+
fn next(&mut self) -> Option<BasicBlock> {
580+
while let Some(bb) = self.worklist.pop() {
581+
if !self.visited.insert(bb) {
592582
continue;
593583
}
594584

595-
let data = &self.body[idx];
596-
597-
if let Some(ref term) = data.terminator {
598-
self.worklist.extend((self.filtered_successors)(&self.body, &term.kind));
599-
}
585+
self.worklist.extend((self.filtered_successors)(self.body, bb));
600586

601-
return Some((idx, data));
587+
return Some(bb);
602588
}
603589

604590
None

compiler/rustc_mir_transform/src/coverage/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn print_coverage_graphviz(
241241
" {:?} [label=\"{:?}: {}\"];\n{}",
242242
bcb,
243243
bcb,
244-
bcb_data.terminator(mir_body).kind.name(),
244+
mir_body[bcb_data.last_bb()].terminator().kind.name(),
245245
basic_coverage_blocks
246246
.successors(bcb)
247247
.map(|successor| { format!(" {:?} -> {:?};", bcb, successor) })

0 commit comments

Comments
 (0)