Implemented CoverageGraph of BasicCoverageBlocks
This commit is contained in:
parent
c7ae4c2cb6
commit
b5020648fe
61 changed files with 565 additions and 495 deletions
|
@ -1,4 +1,4 @@
|
|||
use super::graph::BasicCoverageBlocks;
|
||||
use super::graph::CoverageGraph;
|
||||
use super::spans::CoverageSpan;
|
||||
|
||||
use crate::util::pretty;
|
||||
|
@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
|
|||
pub(crate) fn dump_coverage_spanview(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
mir_body: &mir::Body<'tcx>,
|
||||
basic_coverage_blocks: &BasicCoverageBlocks,
|
||||
basic_coverage_blocks: &CoverageGraph,
|
||||
pass_name: &str,
|
||||
coverage_spans: &Vec<CoverageSpan>,
|
||||
) {
|
||||
|
@ -28,20 +28,20 @@ pub(crate) fn dump_coverage_spanview(
|
|||
.expect("Unexpected IO error dumping coverage spans as HTML");
|
||||
}
|
||||
|
||||
/// Converts the computed `BasicCoverageBlock`s into `SpanViewable`s.
|
||||
/// Converts the computed `BasicCoverageBlockData`s into `SpanViewable`s.
|
||||
fn span_viewables(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
mir_body: &mir::Body<'tcx>,
|
||||
basic_coverage_blocks: &BasicCoverageBlocks,
|
||||
basic_coverage_blocks: &CoverageGraph,
|
||||
coverage_spans: &Vec<CoverageSpan>,
|
||||
) -> Vec<SpanViewable> {
|
||||
let mut span_viewables = Vec::new();
|
||||
for coverage_span in coverage_spans {
|
||||
let tooltip = coverage_span.format_coverage_statements(tcx, mir_body);
|
||||
let CoverageSpan { span, bcb_leader_bb: bb, .. } = coverage_span;
|
||||
let bcb = &basic_coverage_blocks[*bb];
|
||||
let id = bcb.id();
|
||||
let leader_bb = bcb.leader_bb();
|
||||
let CoverageSpan { span, bcb, .. } = coverage_span;
|
||||
let bcb_data = &basic_coverage_blocks[*bcb];
|
||||
let id = bcb_data.id();
|
||||
let leader_bb = bcb_data.leader_bb();
|
||||
span_viewables.push(SpanViewable { bb: leader_bb, span: *span, id, tooltip });
|
||||
}
|
||||
span_viewables
|
||||
|
|
|
@ -1,152 +1,100 @@
|
|||
use rustc_data_structures::graph::dominators::{self, Dominators};
|
||||
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes};
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, TerminatorKind};
|
||||
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, Terminator, TerminatorKind};
|
||||
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
const ID_SEPARATOR: &str = ",";
|
||||
|
||||
/// A BasicCoverageBlock (BCB) represents the maximal-length sequence of CFG (MIR) BasicBlocks
|
||||
/// without conditional branches.
|
||||
///
|
||||
/// The BCB allows coverage analysis to be performed on a simplified projection of the underlying
|
||||
/// MIR CFG, without altering the original CFG. Note that running the MIR `SimplifyCfg` transform,
|
||||
/// is not sufficient, and therefore not necessary, since the BCB-based CFG projection is a more
|
||||
/// aggressive simplification. For example:
|
||||
///
|
||||
/// * The BCB CFG projection ignores (trims) branches not relevant to coverage, such as unwind-
|
||||
/// related code that is injected by the Rust compiler but has no physical source code to
|
||||
/// count. This also means a BasicBlock with a `Call` terminator can be merged into its
|
||||
/// primary successor target block, in the same BCB.
|
||||
/// * Some BasicBlock terminators support Rust-specific concerns--like borrow-checking--that are
|
||||
/// not relevant to coverage analysis. `FalseUnwind`, for example, can be treated the same as
|
||||
/// a `Goto`, and merged with its successor into the same BCB.
|
||||
///
|
||||
/// Each BCB with at least one computed `CoverageSpan` will have no more than one `Counter`.
|
||||
/// In some cases, a BCB's execution count can be computed by `CounterExpression`. Additional
|
||||
/// disjoint `CoverageSpan`s in a BCB can also be counted by `CounterExpression` (by adding `ZERO`
|
||||
/// to the BCB's primary counter or expression).
|
||||
///
|
||||
/// Dominator/dominated relationships (which are fundamental to the coverage analysis algorithm)
|
||||
/// between two BCBs can be computed using the `mir::Body` `dominators()` with any `BasicBlock`
|
||||
/// member of each BCB. (For consistency, BCB's use the first `BasicBlock`, also referred to as the
|
||||
/// `bcb_leader_bb`.)
|
||||
///
|
||||
/// The BCB CFG projection is critical to simplifying the coverage analysis by ensuring graph
|
||||
/// path-based queries (`is_dominated_by()`, `predecessors`, `successors`, etc.) have branch
|
||||
/// (control flow) significance.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct BasicCoverageBlock {
|
||||
pub blocks: Vec<BasicBlock>,
|
||||
/// A coverage-specific simplification of the MIR control flow graph (CFG). The `CoverageGraph`s
|
||||
/// nodes are `BasicCoverageBlock`s, which encompass one or more MIR `BasicBlock`s, plus a
|
||||
/// `CoverageKind` counter (to be added by `CoverageCounters::make_bcb_counters`), and an optional
|
||||
/// set of additional counters--if needed--to count incoming edges, if there are more than one.
|
||||
/// (These "edge counters" are eventually converted into new MIR `BasicBlock`s.)
|
||||
pub(crate) struct CoverageGraph {
|
||||
bcbs: IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
|
||||
bb_to_bcb: IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
|
||||
pub successors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
|
||||
pub predecessors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
|
||||
dominators: Option<Dominators<BasicCoverageBlock>>,
|
||||
}
|
||||
|
||||
impl BasicCoverageBlock {
|
||||
pub fn leader_bb(&self) -> BasicBlock {
|
||||
self.blocks[0]
|
||||
}
|
||||
|
||||
pub fn id(&self) -> String {
|
||||
format!(
|
||||
"@{}",
|
||||
self.blocks
|
||||
.iter()
|
||||
.map(|bb| bb.index().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(ID_SEPARATOR)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct BasicCoverageBlocks {
|
||||
vec: IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
|
||||
}
|
||||
|
||||
impl BasicCoverageBlocks {
|
||||
impl CoverageGraph {
|
||||
pub fn from_mir(mir_body: &mir::Body<'tcx>) -> Self {
|
||||
let (bcbs, bb_to_bcb) = Self::compute_basic_coverage_blocks(mir_body);
|
||||
|
||||
// Pre-transform MIR `BasicBlock` successors and predecessors into the BasicCoverageBlock
|
||||
// equivalents. Note that since the BasicCoverageBlock graph has been fully simplified, the
|
||||
// each predecessor of a BCB leader_bb should be in a unique BCB, and each successor of a
|
||||
// BCB last_bb should bin in its own unique BCB. Therefore, collecting the BCBs using
|
||||
// `bb_to_bcb` should work without requiring a deduplication step.
|
||||
|
||||
let successors = IndexVec::from_fn_n(
|
||||
|bcb| {
|
||||
let bcb_data = &bcbs[bcb];
|
||||
let bcb_successors =
|
||||
bcb_filtered_successors(&mir_body, &bcb_data.terminator(mir_body).kind)
|
||||
.filter_map(|&successor_bb| bb_to_bcb[successor_bb])
|
||||
.collect::<Vec<_>>();
|
||||
debug_assert!({
|
||||
let mut sorted = bcb_successors.clone();
|
||||
sorted.sort_unstable();
|
||||
let initial_len = sorted.len();
|
||||
sorted.dedup();
|
||||
sorted.len() == initial_len
|
||||
});
|
||||
bcb_successors
|
||||
},
|
||||
bcbs.len(),
|
||||
);
|
||||
|
||||
let mut predecessors = IndexVec::from_elem_n(Vec::new(), bcbs.len());
|
||||
for (bcb, bcb_successors) in successors.iter_enumerated() {
|
||||
for &successor in bcb_successors {
|
||||
predecessors[successor].push(bcb);
|
||||
}
|
||||
}
|
||||
|
||||
let mut basic_coverage_blocks =
|
||||
BasicCoverageBlocks { vec: IndexVec::from_elem_n(None, mir_body.basic_blocks().len()) };
|
||||
basic_coverage_blocks.extract_from_mir(mir_body);
|
||||
Self { bcbs, bb_to_bcb, successors, predecessors, dominators: None };
|
||||
let dominators = dominators::dominators(&basic_coverage_blocks);
|
||||
basic_coverage_blocks.dominators = Some(dominators);
|
||||
basic_coverage_blocks
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &BasicCoverageBlock> {
|
||||
self.vec.iter().filter_map(|bcb| bcb.as_ref())
|
||||
}
|
||||
fn compute_basic_coverage_blocks(
|
||||
mir_body: &mir::Body<'tcx>,
|
||||
) -> (
|
||||
IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
|
||||
IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
|
||||
) {
|
||||
let num_basic_blocks = mir_body.num_nodes();
|
||||
let mut bcbs = IndexVec::with_capacity(num_basic_blocks);
|
||||
let mut bb_to_bcb = IndexVec::from_elem_n(None, num_basic_blocks);
|
||||
|
||||
pub fn num_nodes(&self) -> usize {
|
||||
self.vec.len()
|
||||
}
|
||||
|
||||
pub fn extract_from_mir(&mut self, mir_body: &mir::Body<'tcx>) {
|
||||
// Traverse the CFG but ignore anything following an `unwind`
|
||||
let cfg_without_unwind = ShortCircuitPreorder::new(&mir_body, |term_kind| {
|
||||
let mut successors = term_kind.successors();
|
||||
match &term_kind {
|
||||
// SwitchInt successors are never unwind, and all of them should be traversed.
|
||||
|
||||
// NOTE: TerminatorKind::FalseEdge targets from SwitchInt don't appear to be
|
||||
// helpful in identifying unreachable code. I did test the theory, but the following
|
||||
// changes were not beneficial. (I assumed that replacing some constants with
|
||||
// non-deterministic variables might effect which blocks were targeted by a
|
||||
// `FalseEdge` `imaginary_target`. It did not.)
|
||||
//
|
||||
// Also note that, if there is a way to identify BasicBlocks that are part of the
|
||||
// MIR CFG, but not actually reachable, here are some other things to consider:
|
||||
//
|
||||
// Injecting unreachable code regions will probably require computing the set
|
||||
// difference between the basic blocks found without filtering out unreachable
|
||||
// blocks, and the basic blocks found with the filter; then computing the
|
||||
// `CoverageSpans` without the filter; and then injecting `Counter`s or
|
||||
// `CounterExpression`s for blocks that are not unreachable, or injecting
|
||||
// `Unreachable` code regions otherwise. This seems straightforward, but not
|
||||
// trivial.
|
||||
//
|
||||
// Alternatively, we might instead want to leave the unreachable blocks in
|
||||
// (bypass the filter here), and inject the counters. This will result in counter
|
||||
// values of zero (0) for unreachable code (and, notably, the code will be displayed
|
||||
// with a red background by `llvm-cov show`).
|
||||
//
|
||||
// TerminatorKind::SwitchInt { .. } => {
|
||||
// let some_imaginary_target = successors.clone().find_map(|&successor| {
|
||||
// let term = mir_body.basic_blocks()[successor].terminator();
|
||||
// if let TerminatorKind::FalseEdge { imaginary_target, .. } = term.kind {
|
||||
// if mir_body.predecessors()[imaginary_target].len() == 1 {
|
||||
// return Some(imaginary_target);
|
||||
// }
|
||||
// }
|
||||
// None
|
||||
// });
|
||||
// if let Some(imaginary_target) = some_imaginary_target {
|
||||
// box successors.filter(move |&&successor| successor != imaginary_target)
|
||||
// } else {
|
||||
// box successors
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Note this also required changing the closure signature for the
|
||||
// `ShortCurcuitPreorder` to:
|
||||
//
|
||||
// F: Fn(&'tcx TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = &BasicBlock> + 'a>,
|
||||
TerminatorKind::SwitchInt { .. } => successors,
|
||||
|
||||
// For all other kinds, return only the first successor, if any, and ignore unwinds
|
||||
_ => successors.next().into_iter().chain(&[]),
|
||||
}
|
||||
});
|
||||
|
||||
// Walk the CFG using a Preorder traversal, which starts from `START_BLOCK` and follows
|
||||
// Walk the MIR CFG using a Preorder traversal, which starts from `START_BLOCK` and follows
|
||||
// each block terminator's `successors()`. Coverage spans must map to actual source code,
|
||||
// so compiler generated blocks and paths can be ignored. To that end the CFG traversal
|
||||
// so compiler generated blocks and paths can be ignored. To that end, the CFG traversal
|
||||
// intentionally omits unwind paths.
|
||||
let mut blocks = Vec::new();
|
||||
for (bb, data) in cfg_without_unwind {
|
||||
if let Some(last) = blocks.last() {
|
||||
let mir_cfg_without_unwind = ShortCircuitPreorder::new(&mir_body, bcb_filtered_successors);
|
||||
|
||||
let mut basic_blocks = Vec::new();
|
||||
for (bb, data) in mir_cfg_without_unwind {
|
||||
if let Some(last) = basic_blocks.last() {
|
||||
let predecessors = &mir_body.predecessors()[bb];
|
||||
if predecessors.len() > 1 || !predecessors.contains(last) {
|
||||
// The `bb` has more than one _incoming_ edge, and should start its own
|
||||
// `BasicCoverageBlock`. (Note, the `blocks` vector does not yet include `bb`;
|
||||
// it contains a sequence of one or more sequential blocks with no intermediate
|
||||
// branches in or out. Save these as a new `BasicCoverageBlock` before starting
|
||||
// the new one.)
|
||||
self.add_basic_coverage_block(blocks.split_off(0));
|
||||
// `BasicCoverageBlockData`. (Note, the `basic_blocks` vector does not yet
|
||||
// include `bb`; it contains a sequence of one or more sequential basic_blocks
|
||||
// with no intermediate branches in or out. Save these as a new
|
||||
// `BasicCoverageBlockData` before starting the new one.)
|
||||
Self::add_basic_coverage_block(
|
||||
&mut bcbs,
|
||||
&mut bb_to_bcb,
|
||||
basic_blocks.split_off(0),
|
||||
);
|
||||
debug!(
|
||||
" because {}",
|
||||
if predecessors.len() > 1 {
|
||||
|
@ -157,27 +105,40 @@ impl BasicCoverageBlocks {
|
|||
);
|
||||
}
|
||||
}
|
||||
blocks.push(bb);
|
||||
basic_blocks.push(bb);
|
||||
|
||||
let term = data.terminator();
|
||||
|
||||
match term.kind {
|
||||
TerminatorKind::Return { .. }
|
||||
// FIXME(richkadel): Add test(s) for `Abort` coverage.
|
||||
| TerminatorKind::Abort
|
||||
// FIXME(richkadel): Add test(s) for `Assert` coverage.
|
||||
// Should `Assert` be handled like `FalseUnwind` instead? Since we filter out unwind
|
||||
// branches when creating the BCB CFG, aren't `Assert`s (without unwinds) just like
|
||||
// `FalseUnwinds` (which are kind of like `Goto`s)?
|
||||
| TerminatorKind::Assert { .. }
|
||||
// FIXME(richkadel): Add test(s) for `Yield` coverage, and confirm coverage is
|
||||
// sensible for code using the `yield` keyword.
|
||||
| TerminatorKind::Yield { .. }
|
||||
// FIXME(richkadel): Also add coverage tests using async/await, and threading.
|
||||
|
||||
| TerminatorKind::SwitchInt { .. } => {
|
||||
// The `bb` has more than one _outgoing_ edge, or exits the function. Save the
|
||||
// current sequence of `blocks` gathered to this point, as a new
|
||||
// `BasicCoverageBlock`.
|
||||
self.add_basic_coverage_block(blocks.split_off(0));
|
||||
// current sequence of `basic_blocks` gathered to this point, as a new
|
||||
// `BasicCoverageBlockData`.
|
||||
Self::add_basic_coverage_block(
|
||||
&mut bcbs,
|
||||
&mut bb_to_bcb,
|
||||
basic_blocks.split_off(0),
|
||||
);
|
||||
debug!(" because term.kind = {:?}", term.kind);
|
||||
// Note that this condition is based on `TerminatorKind`, even though it
|
||||
// theoretically boils down to `successors().len() != 1`; that is, either zero
|
||||
// (e.g., `Return`, `Abort`) or multiple successors (e.g., `SwitchInt`), but
|
||||
// since the Coverage graph (the BCB CFG projection) ignores things like unwind
|
||||
// branches (which exist in the `Terminator`s `successors()` list) checking the
|
||||
// number of successors won't work.
|
||||
// since the BCB CFG ignores things like unwind branches (which exist in the
|
||||
// `Terminator`s `successors()` list) checking the number of successors won't
|
||||
// work.
|
||||
}
|
||||
TerminatorKind::Goto { .. }
|
||||
| TerminatorKind::Resume
|
||||
|
@ -192,45 +153,222 @@ impl BasicCoverageBlocks {
|
|||
}
|
||||
}
|
||||
|
||||
if !blocks.is_empty() {
|
||||
// process any remaining blocks into a final `BasicCoverageBlock`
|
||||
self.add_basic_coverage_block(blocks.split_off(0));
|
||||
debug!(" because the end of the CFG was reached while traversing");
|
||||
if !basic_blocks.is_empty() {
|
||||
// process any remaining basic_blocks into a final `BasicCoverageBlockData`
|
||||
Self::add_basic_coverage_block(&mut bcbs, &mut bb_to_bcb, basic_blocks.split_off(0));
|
||||
debug!(" because the end of the MIR CFG was reached while traversing");
|
||||
}
|
||||
|
||||
(bcbs, bb_to_bcb)
|
||||
}
|
||||
|
||||
fn add_basic_coverage_block(&mut self, blocks: Vec<BasicBlock>) {
|
||||
let leader_bb = blocks[0];
|
||||
let bcb = BasicCoverageBlock { blocks };
|
||||
debug!("adding BCB: {:?}", bcb);
|
||||
self.vec[leader_bb] = Some(bcb);
|
||||
fn add_basic_coverage_block(
|
||||
bcbs: &mut IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
|
||||
bb_to_bcb: &mut IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
|
||||
basic_blocks: Vec<BasicBlock>,
|
||||
) {
|
||||
let bcb = BasicCoverageBlock::from_usize(bcbs.len());
|
||||
for &bb in basic_blocks.iter() {
|
||||
bb_to_bcb[bb] = Some(bcb);
|
||||
}
|
||||
let bcb_data = BasicCoverageBlockData::from(basic_blocks);
|
||||
debug!("adding bcb{}: {:?}", bcb.index(), bcb_data);
|
||||
bcbs.push(bcb_data);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn iter_enumerated(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (BasicCoverageBlock, &BasicCoverageBlockData)> {
|
||||
self.bcbs.iter_enumerated()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bcb_from_bb(&self, bb: BasicBlock) -> Option<BasicCoverageBlock> {
|
||||
if bb.index() < self.bb_to_bcb.len() { self.bb_to_bcb[bb] } else { None }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_dominated_by(&self, node: BasicCoverageBlock, dom: BasicCoverageBlock) -> bool {
|
||||
self.dominators.as_ref().unwrap().is_dominated_by(node, dom)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn dominators(&self) -> &Dominators<BasicCoverageBlock> {
|
||||
self.dominators.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Index<BasicBlock> for BasicCoverageBlocks {
|
||||
type Output = BasicCoverageBlock;
|
||||
impl Index<BasicCoverageBlock> for CoverageGraph {
|
||||
type Output = BasicCoverageBlockData;
|
||||
|
||||
fn index(&self, index: BasicBlock) -> &Self::Output {
|
||||
self.vec[index].as_ref().expect("is_some if BasicBlock is a BasicCoverageBlock leader")
|
||||
#[inline]
|
||||
fn index(&self, index: BasicCoverageBlock) -> &BasicCoverageBlockData {
|
||||
&self.bcbs[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl IndexMut<BasicCoverageBlock> for CoverageGraph {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: BasicCoverageBlock) -> &mut BasicCoverageBlockData {
|
||||
&mut self.bcbs[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl graph::DirectedGraph for CoverageGraph {
|
||||
type Node = BasicCoverageBlock;
|
||||
}
|
||||
|
||||
impl graph::WithNumNodes for CoverageGraph {
|
||||
#[inline]
|
||||
fn num_nodes(&self) -> usize {
|
||||
self.bcbs.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl graph::WithStartNode for CoverageGraph {
|
||||
#[inline]
|
||||
fn start_node(&self) -> Self::Node {
|
||||
self.bcb_from_bb(mir::START_BLOCK)
|
||||
.expect("mir::START_BLOCK should be in a BasicCoverageBlock")
|
||||
}
|
||||
}
|
||||
|
||||
type BcbSuccessors<'graph> = std::slice::Iter<'graph, BasicCoverageBlock>;
|
||||
|
||||
impl<'graph> graph::GraphSuccessors<'graph> for CoverageGraph {
|
||||
type Item = BasicCoverageBlock;
|
||||
type Iter = std::iter::Cloned<BcbSuccessors<'graph>>;
|
||||
}
|
||||
|
||||
impl graph::WithSuccessors for CoverageGraph {
|
||||
#[inline]
|
||||
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
|
||||
self.successors[node].iter().cloned()
|
||||
}
|
||||
}
|
||||
|
||||
impl graph::GraphPredecessors<'graph> for CoverageGraph {
|
||||
type Item = BasicCoverageBlock;
|
||||
type Iter = std::vec::IntoIter<BasicCoverageBlock>;
|
||||
}
|
||||
|
||||
impl graph::WithPredecessors for CoverageGraph {
|
||||
#[inline]
|
||||
fn predecessors(&self, node: Self::Node) -> <Self as graph::GraphPredecessors<'_>>::Iter {
|
||||
self.predecessors[node].clone().into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
rustc_index::newtype_index! {
|
||||
/// A node in the [control-flow graph][CFG] of CoverageGraph.
|
||||
pub(crate) struct BasicCoverageBlock {
|
||||
DEBUG_FORMAT = "bcb{}",
|
||||
}
|
||||
}
|
||||
|
||||
/// A BasicCoverageBlockData (BCB) represents the maximal-length sequence of MIR BasicBlocks without
|
||||
/// conditional branches, and form a new, simplified, coverage-specific Control Flow Graph, without
|
||||
/// altering the original MIR CFG.
|
||||
///
|
||||
/// Note that running the MIR `SimplifyCfg` transform is not sufficient (and therefore not
|
||||
/// necessary). The BCB-based CFG is a more aggressive simplification. For example:
|
||||
///
|
||||
/// * The BCB CFG ignores (trims) branches not relevant to coverage, such as unwind-related code,
|
||||
/// that is injected by the Rust compiler but has no physical source code to count. This also
|
||||
/// means a BasicBlock with a `Call` terminator can be merged into its primary successor target
|
||||
/// block, in the same BCB.
|
||||
/// * Some BasicBlock terminators support Rust-specific concerns--like borrow-checking--that are
|
||||
/// not relevant to coverage analysis. `FalseUnwind`, for example, can be treated the same as
|
||||
/// a `Goto`, and merged with its successor into the same BCB.
|
||||
///
|
||||
/// Each BCB with at least one computed `CoverageSpan` will have no more than one `Counter`.
|
||||
/// In some cases, a BCB's execution count can be computed by `Expression`. Additional
|
||||
/// disjoint `CoverageSpan`s in a BCB can also be counted by `Expression` (by adding `ZERO`
|
||||
/// to the BCB's primary counter or expression).
|
||||
///
|
||||
/// The BCB CFG is critical to simplifying the coverage analysis by ensuring graph path-based
|
||||
/// queries (`is_dominated_by()`, `predecessors`, `successors`, etc.) have branch (control flow)
|
||||
/// significance.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct BasicCoverageBlockData {
|
||||
pub basic_blocks: Vec<BasicBlock>,
|
||||
}
|
||||
|
||||
impl BasicCoverageBlockData {
|
||||
pub fn from(basic_blocks: Vec<BasicBlock>) -> Self {
|
||||
assert!(basic_blocks.len() > 0);
|
||||
Self { basic_blocks }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn leader_bb(&self) -> BasicBlock {
|
||||
self.basic_blocks[0]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn last_bb(&self) -> BasicBlock {
|
||||
*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()
|
||||
}
|
||||
|
||||
pub fn id(&self) -> String {
|
||||
format!(
|
||||
"@{}",
|
||||
self.basic_blocks
|
||||
.iter()
|
||||
.map(|bb| bb.index().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(ID_SEPARATOR)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn bcb_filtered_successors<'a, 'tcx>(
|
||||
body: &'tcx &'a mir::Body<'tcx>,
|
||||
term_kind: &'tcx TerminatorKind<'tcx>,
|
||||
) -> Box<dyn Iterator<Item = &'a BasicBlock> + 'a> {
|
||||
let mut successors = term_kind.successors();
|
||||
box match &term_kind {
|
||||
// SwitchInt successors are never unwind, and all of them should be traversed.
|
||||
TerminatorKind::SwitchInt { .. } => successors,
|
||||
// For all other kinds, return only the first successor, if any, and ignore unwinds.
|
||||
// NOTE: `chain(&[])` is required to coerce the `option::iter` (from
|
||||
// `next().into_iter()`) into the `mir::Successors` aliased type.
|
||||
_ => successors.next().into_iter().chain(&[]),
|
||||
}
|
||||
.filter(move |&&successor| body[successor].terminator().kind != TerminatorKind::Unreachable)
|
||||
}
|
||||
|
||||
pub struct ShortCircuitPreorder<
|
||||
'a,
|
||||
'tcx,
|
||||
F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>,
|
||||
F: Fn(
|
||||
&'tcx &'a mir::Body<'tcx>,
|
||||
&'tcx TerminatorKind<'tcx>,
|
||||
) -> Box<dyn Iterator<Item = &'a BasicBlock> + 'a>,
|
||||
> {
|
||||
body: &'a mir::Body<'tcx>,
|
||||
body: &'tcx &'a mir::Body<'tcx>,
|
||||
visited: BitSet<BasicBlock>,
|
||||
worklist: Vec<BasicBlock>,
|
||||
filtered_successors: F,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>>
|
||||
ShortCircuitPreorder<'a, 'tcx, F>
|
||||
impl<
|
||||
'a,
|
||||
'tcx,
|
||||
F: Fn(
|
||||
&'tcx &'a mir::Body<'tcx>,
|
||||
&'tcx TerminatorKind<'tcx>,
|
||||
) -> Box<dyn Iterator<Item = &'a BasicBlock> + 'a>,
|
||||
> ShortCircuitPreorder<'a, 'tcx, F>
|
||||
{
|
||||
pub fn new(
|
||||
body: &'a mir::Body<'tcx>,
|
||||
body: &'tcx &'a mir::Body<'tcx>,
|
||||
filtered_successors: F,
|
||||
) -> ShortCircuitPreorder<'a, 'tcx, F> {
|
||||
let worklist = vec![mir::START_BLOCK];
|
||||
|
@ -244,8 +382,14 @@ impl<'a, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>>
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a: 'tcx, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>> Iterator
|
||||
for ShortCircuitPreorder<'a, 'tcx, F>
|
||||
impl<
|
||||
'a: 'tcx,
|
||||
'tcx,
|
||||
F: Fn(
|
||||
&'tcx &'a mir::Body<'tcx>,
|
||||
&'tcx TerminatorKind<'tcx>,
|
||||
) -> Box<dyn Iterator<Item = &'a BasicBlock> + 'a>,
|
||||
> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
|
||||
{
|
||||
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
||||
|
||||
|
@ -258,7 +402,7 @@ impl<'a: 'tcx, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>>
|
|||
let data = &self.body[idx];
|
||||
|
||||
if let Some(ref term) = data.terminator {
|
||||
self.worklist.extend((self.filtered_successors)(&term.kind));
|
||||
self.worklist.extend((self.filtered_successors)(&self.body, &term.kind));
|
||||
}
|
||||
|
||||
return Some((idx, data));
|
||||
|
|
|
@ -6,13 +6,14 @@ mod graph;
|
|||
mod spans;
|
||||
|
||||
use counters::CoverageCounters;
|
||||
use graph::BasicCoverageBlocks;
|
||||
use graph::CoverageGraph;
|
||||
use spans::{CoverageSpan, CoverageSpans};
|
||||
|
||||
use crate::transform::MirPass;
|
||||
use crate::util::pretty;
|
||||
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_data_structures::graph::WithNumNodes;
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_index::vec::IndexVec;
|
||||
|
@ -73,7 +74,7 @@ struct Instrumentor<'a, 'tcx> {
|
|||
tcx: TyCtxt<'tcx>,
|
||||
mir_body: &'a mut mir::Body<'tcx>,
|
||||
body_span: Span,
|
||||
basic_coverage_blocks: BasicCoverageBlocks,
|
||||
basic_coverage_blocks: CoverageGraph,
|
||||
coverage_counters: CoverageCounters,
|
||||
}
|
||||
|
||||
|
@ -82,7 +83,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
let hir_body = hir_body(tcx, mir_body.source.def_id());
|
||||
let body_span = hir_body.value.span;
|
||||
let function_source_hash = hash_mir_source(tcx, hir_body);
|
||||
let basic_coverage_blocks = BasicCoverageBlocks::from_mir(mir_body);
|
||||
let basic_coverage_blocks = CoverageGraph::from_mir(mir_body);
|
||||
Self {
|
||||
pass_name,
|
||||
tcx,
|
||||
|
@ -103,7 +104,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
debug!("instrumenting {:?}, span: {}", def_id, source_map.span_to_string(body_span));
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Compute `CoverageSpan`s from the `BasicCoverageBlocks`.
|
||||
// Compute `CoverageSpan`s from the `CoverageGraph`.
|
||||
let coverage_spans = CoverageSpans::generate_coverage_spans(
|
||||
&self.mir_body,
|
||||
body_span,
|
||||
|
@ -135,9 +136,11 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
let source_file = source_map.lookup_source_file(body_span.lo());
|
||||
let file_name = Symbol::intern(&source_file.name.to_string());
|
||||
|
||||
let mut bb_counters = IndexVec::from_elem_n(None, self.mir_body.basic_blocks().len());
|
||||
for CoverageSpan { span, bcb_leader_bb: bb, .. } in coverage_spans {
|
||||
if let Some(&counter_operand) = bb_counters[bb].as_ref() {
|
||||
let mut bcb_counters = IndexVec::from_elem_n(None, self.basic_coverage_blocks.num_nodes());
|
||||
for covspan in coverage_spans {
|
||||
let bcb = covspan.bcb;
|
||||
let span = covspan.span;
|
||||
if let Some(&counter_operand) = bcb_counters[bcb].as_ref() {
|
||||
let expression = self.coverage_counters.make_expression(
|
||||
counter_operand,
|
||||
Op::Add,
|
||||
|
@ -149,6 +152,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
span,
|
||||
source_map.span_to_snippet(span).expect("Error getting source for span"),
|
||||
);
|
||||
let bb = self.basic_coverage_blocks[bcb].leader_bb();
|
||||
let code_region = make_code_region(file_name, &source_file, span, body_span);
|
||||
inject_statement(self.mir_body, expression, bb, Some(code_region));
|
||||
} else {
|
||||
|
@ -160,7 +164,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
source_map.span_to_snippet(span).expect("Error getting source for span"),
|
||||
);
|
||||
let counter_operand = counter.as_operand_id();
|
||||
bb_counters[bb] = Some(counter_operand);
|
||||
bcb_counters[bcb] = Some(counter_operand);
|
||||
let bb = self.basic_coverage_blocks[bcb].leader_bb();
|
||||
let code_region = make_code_region(file_name, &source_file, span, body_span);
|
||||
inject_statement(self.mir_body, counter, bb, Some(code_region));
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
use super::debug::term_type;
|
||||
use super::graph::{BasicCoverageBlock, BasicCoverageBlocks};
|
||||
use super::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph};
|
||||
|
||||
use crate::util::spanview::source_range_no_file;
|
||||
|
||||
use rustc_data_structures::graph::dominators::Dominators;
|
||||
use rustc_data_structures::graph::WithNumNodes;
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_middle::mir::{
|
||||
|
@ -69,7 +68,7 @@ impl CoverageStatement {
|
|||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct CoverageSpan {
|
||||
pub span: Span,
|
||||
pub bcb_leader_bb: BasicBlock,
|
||||
pub bcb: BasicCoverageBlock,
|
||||
pub coverage_statements: Vec<CoverageStatement>,
|
||||
pub is_closure: bool,
|
||||
}
|
||||
|
@ -78,7 +77,7 @@ impl CoverageSpan {
|
|||
pub fn for_statement(
|
||||
statement: &Statement<'tcx>,
|
||||
span: Span,
|
||||
bcb: &BasicCoverageBlock,
|
||||
bcb: BasicCoverageBlock,
|
||||
bb: BasicBlock,
|
||||
stmt_index: usize,
|
||||
) -> Self {
|
||||
|
@ -92,16 +91,16 @@ impl CoverageSpan {
|
|||
|
||||
Self {
|
||||
span,
|
||||
bcb_leader_bb: bcb.leader_bb(),
|
||||
bcb,
|
||||
coverage_statements: vec![CoverageStatement::Statement(bb, span, stmt_index)],
|
||||
is_closure,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_terminator(span: Span, bcb: &BasicCoverageBlock, bb: BasicBlock) -> Self {
|
||||
pub fn for_terminator(span: Span, bcb: BasicCoverageBlock, bb: BasicBlock) -> Self {
|
||||
Self {
|
||||
span,
|
||||
bcb_leader_bb: bcb.leader_bb(),
|
||||
bcb,
|
||||
coverage_statements: vec![CoverageStatement::Terminator(bb, span)],
|
||||
is_closure: false,
|
||||
}
|
||||
|
@ -132,7 +131,7 @@ impl CoverageSpan {
|
|||
|
||||
#[inline]
|
||||
pub fn is_in_same_bcb(&self, other: &Self) -> bool {
|
||||
self.bcb_leader_bb == other.bcb_leader_bb
|
||||
self.bcb == other.bcb
|
||||
}
|
||||
|
||||
pub fn format_coverage_statements(
|
||||
|
@ -164,15 +163,12 @@ pub struct CoverageSpans<'a, 'tcx> {
|
|||
/// The MIR, used to look up `BasicBlockData`.
|
||||
mir_body: &'a mir::Body<'tcx>,
|
||||
|
||||
/// A snapshot of the MIR CFG dominators before injecting any coverage statements.
|
||||
dominators: Dominators<BasicBlock>,
|
||||
|
||||
/// A `Span` covering the function body of the MIR (typically from left curly brace to right
|
||||
/// curly brace).
|
||||
body_span: Span,
|
||||
|
||||
/// The BasicCoverageBlock Control Flow Graph (BCB CFG).
|
||||
basic_coverage_blocks: &'a BasicCoverageBlocks,
|
||||
basic_coverage_blocks: &'a CoverageGraph,
|
||||
|
||||
/// The initial set of `CoverageSpan`s, sorted by `Span` (`lo` and `hi`) and by relative
|
||||
/// dominance between the `BasicCoverageBlock`s of equal `Span`s.
|
||||
|
@ -213,12 +209,10 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
pub(crate) fn generate_coverage_spans(
|
||||
mir_body: &'a mir::Body<'tcx>,
|
||||
body_span: Span,
|
||||
basic_coverage_blocks: &'a BasicCoverageBlocks,
|
||||
basic_coverage_blocks: &'a CoverageGraph,
|
||||
) -> Vec<CoverageSpan> {
|
||||
let dominators = mir_body.dominators();
|
||||
let mut coverage_spans = CoverageSpans {
|
||||
mir_body,
|
||||
dominators,
|
||||
body_span,
|
||||
basic_coverage_blocks,
|
||||
sorted_spans_iter: None,
|
||||
|
@ -246,7 +240,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
/// The basic steps are:
|
||||
///
|
||||
/// 1. Extract an initial set of spans from the `Statement`s and `Terminator`s of each
|
||||
/// `BasicCoverageBlock`.
|
||||
/// `BasicCoverageBlockData`.
|
||||
/// 2. Sort the spans by span.lo() (starting position). Spans that start at the same position
|
||||
/// are sorted with longer spans before shorter spans; and equal spans are sorted
|
||||
/// (deterministically) based on "dominator" relationship (if any).
|
||||
|
@ -263,8 +257,8 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
/// to be).
|
||||
fn mir_to_initial_sorted_coverage_spans(&self) -> Vec<CoverageSpan> {
|
||||
let mut initial_spans = Vec::<CoverageSpan>::with_capacity(self.mir_body.num_nodes() * 2);
|
||||
for bcb in self.basic_coverage_blocks.iter() {
|
||||
for coverage_span in self.bcb_to_initial_coverage_spans(bcb) {
|
||||
for (bcb, bcb_data) in self.basic_coverage_blocks.iter_enumerated() {
|
||||
for coverage_span in self.bcb_to_initial_coverage_spans(bcb, bcb_data) {
|
||||
initial_spans.push(coverage_span);
|
||||
}
|
||||
}
|
||||
|
@ -285,7 +279,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
// dominators always come after the dominated equal spans). When later
|
||||
// comparing two spans in order, the first will either dominate the second,
|
||||
// or they will have no dominator relationship.
|
||||
self.dominators.rank_partial_cmp(b.bcb_leader_bb, a.bcb_leader_bb)
|
||||
self.basic_coverage_blocks.dominators().rank_partial_cmp(b.bcb, a.bcb)
|
||||
}
|
||||
} else {
|
||||
// Sort hi() in reverse order so shorter spans are attempted after longer spans.
|
||||
|
@ -357,13 +351,13 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
let mut has_coverage = BitSet::new_empty(basic_coverage_blocks.num_nodes());
|
||||
for covspan in &refined_spans {
|
||||
if !covspan.span.is_empty() {
|
||||
has_coverage.insert(covspan.bcb_leader_bb);
|
||||
has_coverage.insert(covspan.bcb);
|
||||
}
|
||||
}
|
||||
refined_spans.retain(|covspan| {
|
||||
!(covspan.span.is_empty()
|
||||
&& is_goto(&mir_body[covspan.bcb_leader_bb].terminator().kind)
|
||||
&& has_coverage.contains(covspan.bcb_leader_bb))
|
||||
&& is_goto(&basic_coverage_blocks[covspan.bcb].terminator(mir_body).kind)
|
||||
&& has_coverage.contains(covspan.bcb))
|
||||
});
|
||||
|
||||
// Remove `CoverageSpan`s derived from closures, originally added to ensure the coverage
|
||||
|
@ -374,12 +368,17 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
}
|
||||
|
||||
// Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
|
||||
// the `BasicBlock`(s) in the given `BasicCoverageBlock`. One `CoverageSpan` is generated
|
||||
// the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
|
||||
// for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will
|
||||
// merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple
|
||||
// `Statement`s and/or `Terminator`s.)
|
||||
fn bcb_to_initial_coverage_spans(&self, bcb: &BasicCoverageBlock) -> Vec<CoverageSpan> {
|
||||
bcb.blocks
|
||||
fn bcb_to_initial_coverage_spans(
|
||||
&self,
|
||||
bcb: BasicCoverageBlock,
|
||||
bcb_data: &'a BasicCoverageBlockData,
|
||||
) -> Vec<CoverageSpan> {
|
||||
bcb_data
|
||||
.basic_blocks
|
||||
.iter()
|
||||
.map(|bbref| {
|
||||
let bb = *bbref;
|
||||
|
@ -636,7 +635,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
|
|||
}
|
||||
|
||||
fn span_bcb_is_dominated_by(&self, covspan: &CoverageSpan, dom_covspan: &CoverageSpan) -> bool {
|
||||
self.dominators.is_dominated_by(covspan.bcb_leader_bb, dom_covspan.bcb_leader_bb)
|
||||
self.basic_coverage_blocks.is_dominated_by(covspan.bcb, dom_covspan.bcb)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue