Rollup merge of #116589 - Zalathar:successors, r=oli-obk
coverage: Unbox and simplify `bcb_filtered_successors` This is a small cleanup in the coverage instrumentor's graph-building code. --- This function already has access to the MIR body, so instead of taking a reference to a terminator, it's simpler and easier to pass in a basic block index. There is no need to box the returned iterator if we instead add appropriate lifetime captures, and make `short_circuit_preorder` generic over the type of iterator it expects. We can also greatly simplify the function's implementation by observing that the only difference between its two cases is whether we take all of a BB's successors, or just the first one. --- `@rustbot` label +A-code-coverage
This commit is contained in:
commit
f4de82c2b3
2 changed files with 38 additions and 80 deletions
|
@ -1,8 +1,9 @@
|
||||||
|
use rustc_data_structures::captures::Captures;
|
||||||
use rustc_data_structures::graph::dominators::{self, Dominators};
|
use rustc_data_structures::graph::dominators::{self, Dominators};
|
||||||
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
|
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
|
||||||
use rustc_index::bit_set::BitSet;
|
use rustc_index::bit_set::BitSet;
|
||||||
use rustc_index::{IndexSlice, IndexVec};
|
use rustc_index::{IndexSlice, IndexVec};
|
||||||
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, Terminator, TerminatorKind};
|
use rustc_middle::mir::{self, BasicBlock, TerminatorKind};
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
@ -36,9 +37,8 @@ impl CoverageGraph {
|
||||||
}
|
}
|
||||||
let bcb_data = &bcbs[bcb];
|
let bcb_data = &bcbs[bcb];
|
||||||
let mut bcb_successors = Vec::new();
|
let mut bcb_successors = Vec::new();
|
||||||
for successor in
|
for successor in bcb_filtered_successors(&mir_body, bcb_data.last_bb())
|
||||||
bcb_filtered_successors(&mir_body, &bcb_data.terminator(mir_body).kind)
|
.filter_map(|successor_bb| bb_to_bcb[successor_bb])
|
||||||
.filter_map(|successor_bb| bb_to_bcb[successor_bb])
|
|
||||||
{
|
{
|
||||||
if !seen[successor] {
|
if !seen[successor] {
|
||||||
seen[successor] = true;
|
seen[successor] = true;
|
||||||
|
@ -80,10 +80,9 @@ impl CoverageGraph {
|
||||||
// intentionally omits unwind paths.
|
// intentionally omits unwind paths.
|
||||||
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
|
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
|
||||||
// `catch_unwind()` handlers.
|
// `catch_unwind()` handlers.
|
||||||
let mir_cfg_without_unwind = ShortCircuitPreorder::new(&mir_body, bcb_filtered_successors);
|
|
||||||
|
|
||||||
let mut basic_blocks = Vec::new();
|
let mut basic_blocks = Vec::new();
|
||||||
for (bb, data) in mir_cfg_without_unwind {
|
for bb in short_circuit_preorder(mir_body, bcb_filtered_successors) {
|
||||||
if let Some(last) = basic_blocks.last() {
|
if let Some(last) = basic_blocks.last() {
|
||||||
let predecessors = &mir_body.basic_blocks.predecessors()[bb];
|
let predecessors = &mir_body.basic_blocks.predecessors()[bb];
|
||||||
if predecessors.len() > 1 || !predecessors.contains(last) {
|
if predecessors.len() > 1 || !predecessors.contains(last) {
|
||||||
|
@ -109,7 +108,7 @@ impl CoverageGraph {
|
||||||
}
|
}
|
||||||
basic_blocks.push(bb);
|
basic_blocks.push(bb);
|
||||||
|
|
||||||
let term = data.terminator();
|
let term = mir_body[bb].terminator();
|
||||||
|
|
||||||
match term.kind {
|
match term.kind {
|
||||||
TerminatorKind::Return { .. }
|
TerminatorKind::Return { .. }
|
||||||
|
@ -316,11 +315,6 @@ impl BasicCoverageBlockData {
|
||||||
pub fn last_bb(&self) -> BasicBlock {
|
pub fn last_bb(&self) -> BasicBlock {
|
||||||
*self.basic_blocks.last().unwrap()
|
*self.basic_blocks.last().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn terminator<'a, 'tcx>(&self, mir_body: &'a mir::Body<'tcx>) -> &'a Terminator<'tcx> {
|
|
||||||
&mir_body[self.last_bb()].terminator()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a successor from a branching BasicCoverageBlock (such as the arms of a `SwitchInt`)
|
/// Represents a successor from a branching BasicCoverageBlock (such as the arms of a `SwitchInt`)
|
||||||
|
@ -362,26 +356,28 @@ impl std::fmt::Debug for BcbBranch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the `Terminator`s non-unwind successors.
|
// Returns the subset of a block's successors that are relevant to the coverage
|
||||||
|
// graph, i.e. those that do not represent unwinds or unreachable branches.
|
||||||
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
|
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
|
||||||
// `catch_unwind()` handlers.
|
// `catch_unwind()` handlers.
|
||||||
fn bcb_filtered_successors<'a, 'tcx>(
|
fn bcb_filtered_successors<'a, 'tcx>(
|
||||||
body: &'a mir::Body<'tcx>,
|
body: &'a mir::Body<'tcx>,
|
||||||
term_kind: &'a TerminatorKind<'tcx>,
|
bb: BasicBlock,
|
||||||
) -> Box<dyn Iterator<Item = BasicBlock> + 'a> {
|
) -> impl Iterator<Item = BasicBlock> + Captures<'a> + Captures<'tcx> {
|
||||||
Box::new(
|
let terminator = body[bb].terminator();
|
||||||
match &term_kind {
|
|
||||||
// SwitchInt successors are never unwind, and all of them should be traversed.
|
let take_n_successors = match terminator.kind {
|
||||||
TerminatorKind::SwitchInt { ref targets, .. } => {
|
// SwitchInt successors are never unwinds, so all of them should be traversed.
|
||||||
None.into_iter().chain(targets.all_targets().into_iter().copied())
|
TerminatorKind::SwitchInt { .. } => usize::MAX,
|
||||||
}
|
// For all other kinds, return only the first successor (if any), ignoring any
|
||||||
// For all other kinds, return only the first successor, if any, and ignore unwinds.
|
// unwind successors.
|
||||||
// NOTE: `chain(&[])` is required to coerce the `option::iter` (from
|
_ => 1,
|
||||||
// `next().into_iter()`) into the `mir::Successors` aliased type.
|
};
|
||||||
_ => term_kind.successors().next().into_iter().chain((&[]).into_iter().copied()),
|
|
||||||
}
|
terminator
|
||||||
.filter(move |&successor| body[successor].terminator().kind != TerminatorKind::Unreachable),
|
.successors()
|
||||||
)
|
.take(take_n_successors)
|
||||||
|
.filter(move |&successor| body[successor].terminator().kind != TerminatorKind::Unreachable)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maintains separate worklists for each loop in the BasicCoverageBlock CFG, plus one for the
|
/// Maintains separate worklists for each loop in the BasicCoverageBlock CFG, plus one for the
|
||||||
|
@ -553,66 +549,28 @@ pub(super) fn find_loop_backedges(
|
||||||
backedges
|
backedges
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ShortCircuitPreorder<
|
fn short_circuit_preorder<'a, 'tcx, F, Iter>(
|
||||||
'a,
|
|
||||||
'tcx,
|
|
||||||
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
|
|
||||||
> {
|
|
||||||
body: &'a mir::Body<'tcx>,
|
body: &'a mir::Body<'tcx>,
|
||||||
visited: BitSet<BasicBlock>,
|
|
||||||
worklist: Vec<BasicBlock>,
|
|
||||||
filtered_successors: F,
|
filtered_successors: F,
|
||||||
}
|
) -> impl Iterator<Item = BasicBlock> + Captures<'a> + Captures<'tcx>
|
||||||
|
where
|
||||||
impl<
|
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Iter,
|
||||||
'a,
|
Iter: Iterator<Item = BasicBlock>,
|
||||||
'tcx,
|
|
||||||
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
|
|
||||||
> ShortCircuitPreorder<'a, 'tcx, F>
|
|
||||||
{
|
{
|
||||||
pub fn new(
|
let mut visited = BitSet::new_empty(body.basic_blocks.len());
|
||||||
body: &'a mir::Body<'tcx>,
|
let mut worklist = vec![mir::START_BLOCK];
|
||||||
filtered_successors: F,
|
|
||||||
) -> ShortCircuitPreorder<'a, 'tcx, F> {
|
|
||||||
let worklist = vec![mir::START_BLOCK];
|
|
||||||
|
|
||||||
ShortCircuitPreorder {
|
std::iter::from_fn(move || {
|
||||||
body,
|
while let Some(bb) = worklist.pop() {
|
||||||
visited: BitSet::new_empty(body.basic_blocks.len()),
|
if !visited.insert(bb) {
|
||||||
worklist,
|
|
||||||
filtered_successors,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<
|
|
||||||
'a,
|
|
||||||
'tcx,
|
|
||||||
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
|
|
||||||
> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
|
|
||||||
{
|
|
||||||
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
||||||
while let Some(idx) = self.worklist.pop() {
|
|
||||||
if !self.visited.insert(idx) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = &self.body[idx];
|
worklist.extend(filtered_successors(body, bb));
|
||||||
|
|
||||||
if let Some(ref term) = data.terminator {
|
return Some(bb);
|
||||||
self.worklist.extend((self.filtered_successors)(&self.body, &term.kind));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Some((idx, data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
})
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
||||||
let size = self.body.basic_blocks.len() - self.visited.count();
|
|
||||||
(size, Some(size))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,7 +241,7 @@ fn print_coverage_graphviz(
|
||||||
" {:?} [label=\"{:?}: {}\"];\n{}",
|
" {:?} [label=\"{:?}: {}\"];\n{}",
|
||||||
bcb,
|
bcb,
|
||||||
bcb,
|
bcb,
|
||||||
bcb_data.terminator(mir_body).kind.name(),
|
mir_body[bcb_data.last_bb()].terminator().kind.name(),
|
||||||
basic_coverage_blocks
|
basic_coverage_blocks
|
||||||
.successors(bcb)
|
.successors(bcb)
|
||||||
.map(|successor| { format!(" {:?} -> {:?};", bcb, successor) })
|
.map(|successor| { format!(" {:?} -> {:?};", bcb, successor) })
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue