Injecting expressions in place of counters where helpful
Implementing the Graph traits for the BasicCoverageBlock graph. optimized replacement of counters with expressions plus new BCB graphviz * Avoid adding coverage to unreachable blocks. * Special case for Goto at the end of the body. Make it non-reportable. Improved debugging and formatting options (from env) Don't automatically add counters to BCBs without CoverageSpans. They may still get counters but only if there are dependencies from other BCBs that have spans, I think. Make CodeRegions optional for Counters too. It is possible to inject counters (`llvm.instrprof.increment` intrinsic calls without corresponding code regions in the coverage map. An expression can still uses these counter values. Refactored instrument_coverage.rs -> instrument_coverage/mod.rs, and then broke up the mod into multiple files. Compiling with coverage, with the expression optimization, works on the json5format crate and its dependencies. Refactored debug features from mod.rs to debug.rs
This commit is contained in:
parent
3291d28e9a
commit
198ba3bd1c
75 changed files with 1984 additions and 603 deletions
|
@ -1,7 +1,15 @@
|
|||
use super::Error;
|
||||
|
||||
use super::debug;
|
||||
use super::graph;
|
||||
use super::spans;
|
||||
|
||||
use debug::DebugCounters;
|
||||
use debug::{DebugCounters, NESTED_INDENT};
|
||||
use graph::{BasicCoverageBlock, BcbBranch, CoverageGraph, TraverseCoverageGraphWithLoops};
|
||||
use spans::CoverageSpan;
|
||||
|
||||
use rustc_data_structures::graph::WithNumNodes;
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_middle::mir::coverage::*;
|
||||
|
||||
/// Manages the counter and expression indexes/IDs to generate `CoverageKind` components for MIR
|
||||
|
@ -29,7 +37,19 @@ impl CoverageCounters {
|
|||
self.debug_counters.enable();
|
||||
}
|
||||
|
||||
pub fn make_counter<F>(&mut self, debug_block_label_fn: F) -> CoverageKind
|
||||
/// Makes `CoverageKind` `Counter`s and `Expressions` for the `BasicCoverageBlocks` directly or
|
||||
/// indirectly associated with `CoverageSpans`, and returns additional `Expression`s
|
||||
/// representing intermediate values.
|
||||
pub fn make_bcb_counters(
|
||||
&mut self,
|
||||
basic_coverage_blocks: &mut CoverageGraph,
|
||||
coverage_spans: &Vec<CoverageSpan>,
|
||||
) -> Result<Vec<CoverageKind>, Error> {
|
||||
let mut bcb_counters = BcbCounters::new(self, basic_coverage_blocks);
|
||||
bcb_counters.make_bcb_counters(coverage_spans)
|
||||
}
|
||||
|
||||
fn make_counter<F>(&mut self, debug_block_label_fn: F) -> CoverageKind
|
||||
where
|
||||
F: Fn() -> Option<String>,
|
||||
{
|
||||
|
@ -43,7 +63,7 @@ impl CoverageCounters {
|
|||
counter
|
||||
}
|
||||
|
||||
pub fn make_expression<F>(
|
||||
fn make_expression<F>(
|
||||
&mut self,
|
||||
lhs: ExpressionOperandId,
|
||||
op: Op,
|
||||
|
@ -61,6 +81,17 @@ impl CoverageCounters {
|
|||
expression
|
||||
}
|
||||
|
||||
pub fn make_identity_counter(&mut self, counter_operand: ExpressionOperandId) -> CoverageKind {
|
||||
let some_debug_block_label = if self.debug_counters.is_enabled() {
|
||||
self.debug_counters.some_block_label(counter_operand).cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.make_expression(counter_operand, Op::Add, ExpressionOperandId::ZERO, || {
|
||||
some_debug_block_label.clone()
|
||||
})
|
||||
}
|
||||
|
||||
/// Counter IDs start from one and go up.
|
||||
fn next_counter(&mut self) -> CounterValueReference {
|
||||
assert!(self.next_counter_id < u32::MAX - self.num_expressions);
|
||||
|
@ -79,3 +110,490 @@ impl CoverageCounters {
|
|||
InjectedExpressionId::from(next)
|
||||
}
|
||||
}
|
||||
|
||||
/// Traverse the `CoverageGraph` and add either a `Counter` or `Expression` to every BCB, to be
|
||||
/// injected with `CoverageSpan`s. `Expressions` have no runtime overhead, so if a viable expression
|
||||
/// (adding or subtracting two other counters or expressions) can compute the same result as an
|
||||
/// embedded counter, an `Expression` should be used.
|
||||
struct BcbCounters<'a> {
|
||||
coverage_counters: &'a mut CoverageCounters,
|
||||
basic_coverage_blocks: &'a mut CoverageGraph,
|
||||
}
|
||||
|
||||
impl<'a> BcbCounters<'a> {
|
||||
fn new(
|
||||
coverage_counters: &'a mut CoverageCounters,
|
||||
basic_coverage_blocks: &'a mut CoverageGraph,
|
||||
) -> Self {
|
||||
Self { coverage_counters, basic_coverage_blocks }
|
||||
}
|
||||
|
||||
/// If two `CoverageGraph` branch from another `BasicCoverageBlock`, one of the branches
|
||||
/// can be counted by `Expression` by subtracting the other branch from the branching
|
||||
/// block. Otherwise, the `BasicCoverageBlock` executed the least should have the `Counter`.
|
||||
/// One way to predict which branch executes the least is by considering loops. A loop is exited
|
||||
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
|
||||
/// always executed less than the branch that does not exit the loop.
|
||||
///
|
||||
/// Returns any non-code-span expressions created to represent intermediate values (such as to
|
||||
/// add two counters so the result can be subtracted from another counter), or an Error with
|
||||
/// message for subsequent debugging.
|
||||
fn make_bcb_counters(
|
||||
&mut self,
|
||||
coverage_spans: &Vec<CoverageSpan>,
|
||||
) -> Result<Vec<CoverageKind>, Error> {
|
||||
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");
|
||||
let num_bcbs = self.basic_coverage_blocks.num_nodes();
|
||||
let mut collect_intermediate_expressions = Vec::with_capacity(num_bcbs);
|
||||
|
||||
let mut bcbs_with_coverage = BitSet::new_empty(num_bcbs);
|
||||
for covspan in coverage_spans {
|
||||
bcbs_with_coverage.insert(covspan.bcb);
|
||||
}
|
||||
|
||||
// FIXME(richkadel): Add more comments to explain the logic here and in the rest of this
|
||||
// function, and refactor this function to break it up into smaller functions that are
|
||||
// easier to understand.
|
||||
|
||||
let mut traversal = TraverseCoverageGraphWithLoops::new(&self.basic_coverage_blocks);
|
||||
while let Some(bcb) = traversal.next(self.basic_coverage_blocks) {
|
||||
if bcbs_with_coverage.contains(bcb) {
|
||||
debug!("{:?} has at least one `CoverageSpan`. Get or make its counter", bcb);
|
||||
let branching_counter_operand =
|
||||
self.get_or_make_counter_operand(bcb, &mut collect_intermediate_expressions)?;
|
||||
|
||||
if self.bcb_needs_branch_counters(bcb) {
|
||||
self.make_branch_counters(
|
||||
&mut traversal,
|
||||
bcb,
|
||||
branching_counter_operand,
|
||||
&mut collect_intermediate_expressions,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
debug!(
|
||||
"{:?} does not have any `CoverageSpan`s. A counter will only be added if \
|
||||
and when a covered BCB has an expression dependency.",
|
||||
bcb,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if traversal.is_complete() {
|
||||
Ok(collect_intermediate_expressions)
|
||||
} else {
|
||||
Error::from_string(format!(
|
||||
"`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}",
|
||||
traversal.unvisited(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn make_branch_counters(
|
||||
&mut self,
|
||||
traversal: &mut TraverseCoverageGraphWithLoops,
|
||||
branching_bcb: BasicCoverageBlock,
|
||||
branching_counter_operand: ExpressionOperandId,
|
||||
collect_intermediate_expressions: &mut Vec<CoverageKind>,
|
||||
) -> Result<(), Error> {
|
||||
let branches = self.bcb_branches(branching_bcb);
|
||||
debug!(
|
||||
"{:?} has some branch(es) without counters:\n {}",
|
||||
branching_bcb,
|
||||
branches
|
||||
.iter()
|
||||
.map(|branch| {
|
||||
format!("{:?}: {:?}", branch, branch.counter(&self.basic_coverage_blocks))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n "),
|
||||
);
|
||||
|
||||
let expression_branch = self.choose_preferred_expression_branch(traversal, &branches);
|
||||
// Assign a Counter or Expression to each branch, plus additional
|
||||
// `Expression`s, as needed, to sum up intermediate results.
|
||||
let mut some_sumup_counter_operand = None;
|
||||
for branch in branches {
|
||||
if branch != expression_branch {
|
||||
let branch_counter_operand = if branch.is_only_path_to_target() {
|
||||
debug!(
|
||||
" {:?} has only one incoming edge (from {:?}), so adding a \
|
||||
counter",
|
||||
branch, branching_bcb
|
||||
);
|
||||
self.get_or_make_counter_operand(
|
||||
branch.target_bcb,
|
||||
collect_intermediate_expressions,
|
||||
)?
|
||||
} else {
|
||||
debug!(" {:?} has multiple incoming edges, so adding an edge counter", branch);
|
||||
self.get_or_make_edge_counter_operand(
|
||||
branching_bcb,
|
||||
branch.target_bcb,
|
||||
collect_intermediate_expressions,
|
||||
)?
|
||||
};
|
||||
if let Some(sumup_counter_operand) =
|
||||
some_sumup_counter_operand.replace(branch_counter_operand)
|
||||
{
|
||||
let intermediate_expression = self.coverage_counters.make_expression(
|
||||
branch_counter_operand,
|
||||
Op::Add,
|
||||
sumup_counter_operand,
|
||||
|| None,
|
||||
);
|
||||
debug!(
|
||||
" [new intermediate expression: {}]",
|
||||
self.format_counter(&intermediate_expression)
|
||||
);
|
||||
let intermediate_expression_operand = intermediate_expression.as_operand_id();
|
||||
collect_intermediate_expressions.push(intermediate_expression);
|
||||
some_sumup_counter_operand.replace(intermediate_expression_operand);
|
||||
}
|
||||
}
|
||||
}
|
||||
let sumup_counter_operand =
|
||||
some_sumup_counter_operand.expect("sumup_counter_operand should have a value");
|
||||
debug!(
|
||||
"Making an expression for the selected expression_branch: {:?} \
|
||||
(expression_branch predecessors: {:?})",
|
||||
expression_branch,
|
||||
self.bcb_predecessors(expression_branch.target_bcb),
|
||||
);
|
||||
let expression = self.coverage_counters.make_expression(
|
||||
branching_counter_operand,
|
||||
Op::Subtract,
|
||||
sumup_counter_operand,
|
||||
|| Some(format!("{:?}", expression_branch)),
|
||||
);
|
||||
debug!("{:?} gets an expression: {}", expression_branch, self.format_counter(&expression));
|
||||
let bcb = expression_branch.target_bcb;
|
||||
if expression_branch.is_only_path_to_target() {
|
||||
self.basic_coverage_blocks[bcb].set_counter(expression)?;
|
||||
} else {
|
||||
self.basic_coverage_blocks[bcb].set_edge_counter_from(branching_bcb, expression)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_or_make_counter_operand(
|
||||
&mut self,
|
||||
bcb: BasicCoverageBlock,
|
||||
collect_intermediate_expressions: &mut Vec<CoverageKind>,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
self.recursive_get_or_make_counter_operand(bcb, collect_intermediate_expressions, 1)
|
||||
}
|
||||
|
||||
fn recursive_get_or_make_counter_operand(
|
||||
&mut self,
|
||||
bcb: BasicCoverageBlock,
|
||||
collect_intermediate_expressions: &mut Vec<CoverageKind>,
|
||||
debug_indent_level: usize,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
Ok({
|
||||
if let Some(counter_kind) = self.basic_coverage_blocks[bcb].counter() {
|
||||
debug!(
|
||||
"{}{:?} already has a counter: {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
bcb,
|
||||
self.format_counter(counter_kind),
|
||||
);
|
||||
counter_kind.as_operand_id()
|
||||
} else {
|
||||
let one_path_to_target = self.bcb_has_one_path_to_target(bcb);
|
||||
if one_path_to_target || self.bcb_predecessors(bcb).contains(&bcb) {
|
||||
let counter_kind =
|
||||
self.coverage_counters.make_counter(|| Some(format!("{:?}", bcb)));
|
||||
if one_path_to_target {
|
||||
debug!(
|
||||
"{}{:?} gets a new counter: {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
bcb,
|
||||
self.format_counter(&counter_kind),
|
||||
);
|
||||
} else {
|
||||
debug!(
|
||||
"{}{:?} has itself as its own predecessor. It can't be part of its own \
|
||||
Expression sum, so it will get its own new counter: {}. (Note, the \
|
||||
compiled code will generate an infinite loop.)",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
bcb,
|
||||
self.format_counter(&counter_kind),
|
||||
);
|
||||
}
|
||||
self.basic_coverage_blocks[bcb].set_counter(counter_kind)?
|
||||
} else {
|
||||
let mut predecessors = self.bcb_predecessors(bcb).clone().into_iter();
|
||||
debug!(
|
||||
"{}{:?} has multiple incoming edges and will get an expression that sums \
|
||||
them up...",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
bcb,
|
||||
);
|
||||
let first_edge_counter_operand = self
|
||||
.recursive_get_or_make_edge_counter_operand(
|
||||
predecessors.next().unwrap(),
|
||||
bcb,
|
||||
collect_intermediate_expressions,
|
||||
debug_indent_level + 1,
|
||||
)?;
|
||||
let mut some_sumup_edge_counter_operand = None;
|
||||
for predecessor in predecessors {
|
||||
let edge_counter_operand = self
|
||||
.recursive_get_or_make_edge_counter_operand(
|
||||
predecessor,
|
||||
bcb,
|
||||
collect_intermediate_expressions,
|
||||
debug_indent_level + 1,
|
||||
)?;
|
||||
if let Some(sumup_edge_counter_operand) =
|
||||
some_sumup_edge_counter_operand.replace(edge_counter_operand)
|
||||
{
|
||||
let intermediate_expression = self.coverage_counters.make_expression(
|
||||
sumup_edge_counter_operand,
|
||||
Op::Add,
|
||||
edge_counter_operand,
|
||||
|| None,
|
||||
);
|
||||
debug!(
|
||||
"{}new intermediate expression: {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
self.format_counter(&intermediate_expression)
|
||||
);
|
||||
let intermediate_expression_operand =
|
||||
intermediate_expression.as_operand_id();
|
||||
collect_intermediate_expressions.push(intermediate_expression);
|
||||
some_sumup_edge_counter_operand
|
||||
.replace(intermediate_expression_operand);
|
||||
}
|
||||
}
|
||||
let counter_kind = self.coverage_counters.make_expression(
|
||||
first_edge_counter_operand,
|
||||
Op::Add,
|
||||
some_sumup_edge_counter_operand.unwrap(),
|
||||
|| Some(format!("{:?}", bcb)),
|
||||
);
|
||||
debug!(
|
||||
"{}{:?} gets a new counter (sum of predecessor counters): {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
bcb,
|
||||
self.format_counter(&counter_kind)
|
||||
);
|
||||
self.basic_coverage_blocks[bcb].set_counter(counter_kind)?
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_or_make_edge_counter_operand(
|
||||
&mut self,
|
||||
from_bcb: BasicCoverageBlock,
|
||||
to_bcb: BasicCoverageBlock,
|
||||
collect_intermediate_expressions: &mut Vec<CoverageKind>,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
self.recursive_get_or_make_edge_counter_operand(
|
||||
from_bcb,
|
||||
to_bcb,
|
||||
collect_intermediate_expressions,
|
||||
1,
|
||||
)
|
||||
}
|
||||
|
||||
fn recursive_get_or_make_edge_counter_operand(
|
||||
&mut self,
|
||||
from_bcb: BasicCoverageBlock,
|
||||
to_bcb: BasicCoverageBlock,
|
||||
collect_intermediate_expressions: &mut Vec<CoverageKind>,
|
||||
debug_indent_level: usize,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
Ok({
|
||||
let successors = self.bcb_successors(from_bcb).iter();
|
||||
if successors.len() > 1 {
|
||||
if let Some(counter_kind) =
|
||||
self.basic_coverage_blocks[to_bcb].edge_counter_from(from_bcb)
|
||||
{
|
||||
debug!(
|
||||
"{}Edge {:?}->{:?} already has a counter: {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
from_bcb,
|
||||
to_bcb,
|
||||
self.format_counter(counter_kind)
|
||||
);
|
||||
counter_kind.as_operand_id()
|
||||
} else {
|
||||
let counter_kind = self
|
||||
.coverage_counters
|
||||
.make_counter(|| Some(format!("{:?}->{:?}", from_bcb, to_bcb)));
|
||||
debug!(
|
||||
"{}Edge {:?}->{:?} gets a new counter: {}",
|
||||
NESTED_INDENT.repeat(debug_indent_level),
|
||||
from_bcb,
|
||||
to_bcb,
|
||||
self.format_counter(&counter_kind)
|
||||
);
|
||||
self.basic_coverage_blocks[to_bcb]
|
||||
.set_edge_counter_from(from_bcb, counter_kind)?
|
||||
}
|
||||
} else {
|
||||
self.recursive_get_or_make_counter_operand(
|
||||
from_bcb,
|
||||
collect_intermediate_expressions,
|
||||
debug_indent_level + 1,
|
||||
)?
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Select a branch for the expression, either the recommended `reloop_branch`, or
|
||||
/// if none was found, select any branch.
|
||||
fn choose_preferred_expression_branch(
|
||||
&self,
|
||||
traversal: &TraverseCoverageGraphWithLoops,
|
||||
branches: &Vec<BcbBranch>,
|
||||
) -> BcbBranch {
|
||||
let branch_needs_a_counter =
|
||||
|branch: &BcbBranch| branch.counter(&self.basic_coverage_blocks).is_none();
|
||||
|
||||
let some_reloop_branch = self.find_some_reloop_branch(traversal, &branches);
|
||||
if let Some(reloop_branch_without_counter) =
|
||||
some_reloop_branch.filter(branch_needs_a_counter)
|
||||
{
|
||||
debug!(
|
||||
"Selecting reloop_branch={:?} that still needs a counter, to get the \
|
||||
`Expression`",
|
||||
reloop_branch_without_counter
|
||||
);
|
||||
reloop_branch_without_counter
|
||||
} else {
|
||||
let &branch_without_counter = branches
|
||||
.iter()
|
||||
.find(|&&branch| branch.counter(&self.basic_coverage_blocks).is_none())
|
||||
.expect(
|
||||
"needs_branch_counters was `true` so there should be at least one \
|
||||
branch",
|
||||
);
|
||||
debug!(
|
||||
"Selecting any branch={:?} that still needs a counter, to get the \
|
||||
`Expression` because there was no `reloop_branch`, or it already had a \
|
||||
counter",
|
||||
branch_without_counter
|
||||
);
|
||||
branch_without_counter
|
||||
}
|
||||
}
|
||||
|
||||
/// At most one of the branches (or its edge, from the branching_bcb,
|
||||
/// if the branch has multiple incoming edges) can have a counter computed by
|
||||
/// expression.
|
||||
///
|
||||
/// If at least one of the branches leads outside of a loop (`found_loop_exit` is
|
||||
/// true), and at least one other branch does not exit the loop (the first of which
|
||||
/// is captured in `some_reloop_branch`), it's likely any reloop branch will be
|
||||
/// executed far more often than loop exit branch, making the reloop branch a better
|
||||
/// candidate for an expression.
|
||||
fn find_some_reloop_branch(
|
||||
&self,
|
||||
traversal: &TraverseCoverageGraphWithLoops,
|
||||
branches: &Vec<BcbBranch>,
|
||||
) -> Option<BcbBranch> {
|
||||
let branch_needs_a_counter =
|
||||
|branch: &BcbBranch| branch.counter(&self.basic_coverage_blocks).is_none();
|
||||
|
||||
let mut some_reloop_branch: Option<BcbBranch> = None;
|
||||
for context in traversal.context_stack.iter().rev() {
|
||||
if let Some((backedge_from_bcbs, _)) = &context.loop_backedges {
|
||||
let mut found_loop_exit = false;
|
||||
for &branch in branches.iter() {
|
||||
if backedge_from_bcbs.iter().any(|&backedge_from_bcb| {
|
||||
self.bcb_is_dominated_by(backedge_from_bcb, branch.target_bcb)
|
||||
}) {
|
||||
if let Some(reloop_branch) = some_reloop_branch {
|
||||
if reloop_branch.counter(&self.basic_coverage_blocks).is_none() {
|
||||
// we already found a candidate reloop_branch that still
|
||||
// needs a counter
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// The path from branch leads back to the top of the loop. Set this
|
||||
// branch as the `reloop_branch`. If this branch already has a
|
||||
// counter, and we find another reloop branch that doesn't have a
|
||||
// counter yet, that branch will be selected as the `reloop_branch`
|
||||
// instead.
|
||||
some_reloop_branch = Some(branch);
|
||||
} else {
|
||||
// The path from branch leads outside this loop
|
||||
found_loop_exit = true;
|
||||
}
|
||||
if found_loop_exit
|
||||
&& some_reloop_branch.filter(branch_needs_a_counter).is_some()
|
||||
{
|
||||
// Found both a branch that exits the loop and a branch that returns
|
||||
// to the top of the loop (`reloop_branch`), and the `reloop_branch`
|
||||
// doesn't already have a counter.
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found_loop_exit {
|
||||
debug!(
|
||||
"No branches exit the loop, so any branch without an existing \
|
||||
counter can have the `Expression`."
|
||||
);
|
||||
break;
|
||||
}
|
||||
if some_reloop_branch.is_some() {
|
||||
debug!(
|
||||
"Found a branch that exits the loop and a branch the loops back to \
|
||||
the top of the loop (`reloop_branch`). The `reloop_branch` will \
|
||||
get the `Expression`, as long as it still needs a counter."
|
||||
);
|
||||
break;
|
||||
}
|
||||
// else all branches exited this loop context, so run the same checks with
|
||||
// the outer loop(s)
|
||||
}
|
||||
}
|
||||
some_reloop_branch
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_predecessors(&self, bcb: BasicCoverageBlock) -> &Vec<BasicCoverageBlock> {
|
||||
&self.basic_coverage_blocks.predecessors[bcb]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_successors(&self, bcb: BasicCoverageBlock) -> &Vec<BasicCoverageBlock> {
|
||||
&self.basic_coverage_blocks.successors[bcb]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_branches(&self, from_bcb: BasicCoverageBlock) -> Vec<BcbBranch> {
|
||||
self.bcb_successors(from_bcb)
|
||||
.iter()
|
||||
.map(|&to_bcb| BcbBranch::from_to(from_bcb, to_bcb, &self.basic_coverage_blocks))
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn bcb_needs_branch_counters(&self, bcb: BasicCoverageBlock) -> bool {
|
||||
let branch_needs_a_counter =
|
||||
|branch: &BcbBranch| branch.counter(&self.basic_coverage_blocks).is_none();
|
||||
let branches = self.bcb_branches(bcb);
|
||||
branches.len() > 1 && branches.iter().any(branch_needs_a_counter)
|
||||
}
|
||||
|
||||
/// Returns true if the BasicCoverageBlock has zero or one incoming edge. (If zero, it should be
|
||||
/// the entry point for the function.)
|
||||
#[inline]
|
||||
fn bcb_has_one_path_to_target(&self, bcb: BasicCoverageBlock) -> bool {
|
||||
self.bcb_predecessors(bcb).len() <= 1
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_is_dominated_by(&self, node: BasicCoverageBlock, dom: BasicCoverageBlock) -> bool {
|
||||
self.basic_coverage_blocks.is_dominated_by(node, dom)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format_counter(&self, counter_kind: &CoverageKind) -> String {
|
||||
self.coverage_counters.debug_counters.format_counter(counter_kind)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ use rustc_middle::ty::TyCtxt;
|
|||
|
||||
use std::lazy::SyncOnceCell;
|
||||
|
||||
pub const NESTED_INDENT: &str = " ";
|
||||
|
||||
const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
|
||||
|
||||
pub(crate) fn debug_options<'a>() -> &'a DebugOptions {
|
||||
|
@ -24,20 +26,29 @@ pub(crate) fn debug_options<'a>() -> &'a DebugOptions {
|
|||
/// Parses and maintains coverage-specific debug options captured from the environment variable
|
||||
/// "RUSTC_COVERAGE_DEBUG_OPTIONS", if set. Options can be set on the command line by, for example:
|
||||
///
|
||||
/// $ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=block cargo build
|
||||
/// $ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=block,allow_unused_expressions=n cargo build
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct DebugOptions {
|
||||
pub allow_unused_expressions: bool,
|
||||
counter_format: ExpressionFormat,
|
||||
}
|
||||
|
||||
impl DebugOptions {
|
||||
fn new() -> Self {
|
||||
let mut allow_unused_expressions = true;
|
||||
let mut counter_format = ExpressionFormat::default();
|
||||
|
||||
if let Ok(env_debug_options) = std::env::var(RUSTC_COVERAGE_DEBUG_OPTIONS) {
|
||||
for setting_str in env_debug_options.replace(" ", "").replace("-", "_").split(",") {
|
||||
let mut setting = setting_str.splitn(2, "=");
|
||||
match setting.next() {
|
||||
Some(option) if option == "allow_unused_expressions" => {
|
||||
allow_unused_expressions = bool_option_val(option, setting.next());
|
||||
debug!(
|
||||
"{} env option `allow_unused_expressions` is set to {}",
|
||||
RUSTC_COVERAGE_DEBUG_OPTIONS, allow_unused_expressions
|
||||
);
|
||||
}
|
||||
Some(option) if option == "counter_format" => {
|
||||
if let Some(strval) = setting.next() {
|
||||
counter_format = counter_format_option_val(strval);
|
||||
|
@ -66,7 +77,26 @@ impl DebugOptions {
|
|||
}
|
||||
}
|
||||
|
||||
Self { counter_format }
|
||||
Self { allow_unused_expressions, counter_format }
|
||||
}
|
||||
}
|
||||
|
||||
fn bool_option_val(option: &str, some_strval: Option<&str>) -> bool {
|
||||
if let Some(val) = some_strval {
|
||||
if vec!["yes", "y", "on", "true"].contains(&val) {
|
||||
true
|
||||
} else if vec!["no", "n", "off", "false"].contains(&val) {
|
||||
false
|
||||
} else {
|
||||
bug!(
|
||||
"Unsupported value `{}` for option `{}` in environment variable {}",
|
||||
option,
|
||||
val,
|
||||
RUSTC_COVERAGE_DEBUG_OPTIONS
|
||||
)
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,6 +177,14 @@ impl DebugCounters {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn some_block_label(&self, operand: ExpressionOperandId) -> Option<&String> {
|
||||
self.some_counters.as_ref().map_or(None, |counters| {
|
||||
counters
|
||||
.get(&operand)
|
||||
.map_or(None, |debug_counter| debug_counter.some_block_label.as_ref())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn format_counter(&self, counter_kind: &CoverageKind) -> String {
|
||||
match *counter_kind {
|
||||
CoverageKind::Counter { .. } => {
|
||||
|
@ -242,16 +280,22 @@ impl DebugCounter {
|
|||
pub(crate) struct GraphvizData {
|
||||
some_bcb_to_coverage_spans_with_counters:
|
||||
Option<FxHashMap<BasicCoverageBlock, Vec<(CoverageSpan, CoverageKind)>>>,
|
||||
some_bcb_to_dependency_counters: Option<FxHashMap<BasicCoverageBlock, Vec<CoverageKind>>>,
|
||||
some_edge_to_counter: Option<FxHashMap<(BasicCoverageBlock, BasicBlock), CoverageKind>>,
|
||||
}
|
||||
|
||||
impl GraphvizData {
|
||||
pub fn new() -> Self {
|
||||
Self { some_bcb_to_coverage_spans_with_counters: None, some_edge_to_counter: None }
|
||||
Self {
|
||||
some_bcb_to_coverage_spans_with_counters: None,
|
||||
some_bcb_to_dependency_counters: None,
|
||||
some_edge_to_counter: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enable(&mut self) {
|
||||
self.some_bcb_to_coverage_spans_with_counters = Some(FxHashMap::default());
|
||||
self.some_bcb_to_dependency_counters = Some(FxHashMap::default());
|
||||
self.some_edge_to_counter = Some(FxHashMap::default());
|
||||
}
|
||||
|
||||
|
@ -287,6 +331,187 @@ impl GraphvizData {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_bcb_dependency_counter(
|
||||
&mut self,
|
||||
bcb: BasicCoverageBlock,
|
||||
counter_kind: &CoverageKind,
|
||||
) {
|
||||
if let Some(bcb_to_dependency_counters) = self.some_bcb_to_dependency_counters.as_mut() {
|
||||
bcb_to_dependency_counters
|
||||
.entry(bcb)
|
||||
.or_insert_with(|| Vec::new())
|
||||
.push(counter_kind.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_bcb_dependency_counters(
|
||||
&self,
|
||||
bcb: BasicCoverageBlock,
|
||||
) -> Option<&Vec<CoverageKind>> {
|
||||
if let Some(bcb_to_dependency_counters) = self.some_bcb_to_dependency_counters.as_ref() {
|
||||
bcb_to_dependency_counters.get(&bcb)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_edge_counter(
|
||||
&mut self,
|
||||
from_bcb: BasicCoverageBlock,
|
||||
to_bb: BasicBlock,
|
||||
counter_kind: &CoverageKind,
|
||||
) {
|
||||
if let Some(edge_to_counter) = self.some_edge_to_counter.as_mut() {
|
||||
edge_to_counter.insert((from_bcb, to_bb), counter_kind.clone()).expect_none(
|
||||
"invalid attempt to insert more than one edge counter for the same edge",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_edge_counter(
|
||||
&self,
|
||||
from_bcb: BasicCoverageBlock,
|
||||
to_bb: BasicBlock,
|
||||
) -> Option<&CoverageKind> {
|
||||
if let Some(edge_to_counter) = self.some_edge_to_counter.as_ref() {
|
||||
edge_to_counter.get(&(from_bcb, to_bb))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// If enabled, this struct captures additional data used to track whether expressions were used,
|
||||
/// directly or indirectly, to compute the coverage counts for all `CoverageSpan`s, and any that are
|
||||
/// _not_ used are retained in the `unused_expressions` Vec, to be included in debug output (logs
|
||||
/// and/or a `CoverageGraph` graphviz output).
|
||||
pub(crate) struct UsedExpressions {
|
||||
some_used_expression_operands:
|
||||
Option<FxHashMap<ExpressionOperandId, Vec<InjectedExpressionId>>>,
|
||||
some_unused_expressions:
|
||||
Option<Vec<(CoverageKind, Option<BasicCoverageBlock>, BasicCoverageBlock)>>,
|
||||
}
|
||||
|
||||
impl UsedExpressions {
|
||||
pub fn new() -> Self {
|
||||
Self { some_used_expression_operands: None, some_unused_expressions: None }
|
||||
}
|
||||
|
||||
pub fn enable(&mut self) {
|
||||
self.some_used_expression_operands = Some(FxHashMap::default());
|
||||
self.some_unused_expressions = Some(Vec::new());
|
||||
}
|
||||
|
||||
pub fn is_enabled(&mut self) -> bool {
|
||||
self.some_used_expression_operands.is_some()
|
||||
}
|
||||
|
||||
pub fn add_expression_operands(&mut self, expression: &CoverageKind) {
|
||||
if let Some(used_expression_operands) = self.some_used_expression_operands.as_mut() {
|
||||
if let CoverageKind::Expression { id, lhs, rhs, .. } = *expression {
|
||||
used_expression_operands.entry(lhs).or_insert_with(|| Vec::new()).push(id);
|
||||
used_expression_operands.entry(rhs).or_insert_with(|| Vec::new()).push(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expression_is_used(&mut self, expression: &CoverageKind) -> bool {
|
||||
if let Some(used_expression_operands) = self.some_used_expression_operands.as_ref() {
|
||||
used_expression_operands.contains_key(&expression.as_operand_id())
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_unused_expression_if_not_found(
|
||||
&mut self,
|
||||
expression: &CoverageKind,
|
||||
edge_from_bcb: Option<BasicCoverageBlock>,
|
||||
target_bcb: BasicCoverageBlock,
|
||||
) {
|
||||
if let Some(used_expression_operands) = self.some_used_expression_operands.as_ref() {
|
||||
if !used_expression_operands.contains_key(&expression.as_operand_id()) {
|
||||
self.some_unused_expressions.as_mut().unwrap().push((
|
||||
expression.clone(),
|
||||
edge_from_bcb,
|
||||
target_bcb,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the list of unused counters (if any) as a tuple with the counter (`CoverageKind`),
|
||||
/// optional `from_bcb` (if it was an edge counter), and `target_bcb`.
|
||||
pub fn get_unused_expressions(
|
||||
&self,
|
||||
) -> Vec<(CoverageKind, Option<BasicCoverageBlock>, BasicCoverageBlock)> {
|
||||
if let Some(unused_expressions) = self.some_unused_expressions.as_ref() {
|
||||
unused_expressions.clone()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// If enabled, validate that every BCB or edge counter not directly associated with a coverage
|
||||
/// span is at least indirectly associated (it is a dependency of a BCB counter that _is_
|
||||
/// associated with a coverage span).
|
||||
pub fn validate(
|
||||
&mut self,
|
||||
bcb_counters_without_direct_coverage_spans: &Vec<(
|
||||
Option<BasicCoverageBlock>,
|
||||
BasicCoverageBlock,
|
||||
CoverageKind,
|
||||
)>,
|
||||
) {
|
||||
if self.is_enabled() {
|
||||
let mut not_validated = bcb_counters_without_direct_coverage_spans
|
||||
.iter()
|
||||
.map(|(_, _, counter_kind)| counter_kind)
|
||||
.collect::<Vec<_>>();
|
||||
let mut validating_count = 0;
|
||||
while not_validated.len() != validating_count {
|
||||
let to_validate = not_validated.split_off(0);
|
||||
validating_count = to_validate.len();
|
||||
for counter_kind in to_validate {
|
||||
if self.expression_is_used(counter_kind) {
|
||||
self.add_expression_operands(counter_kind);
|
||||
} else {
|
||||
not_validated.push(counter_kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alert_on_unused_expressions(&self, debug_counters: &DebugCounters) {
|
||||
if let Some(unused_expressions) = self.some_unused_expressions.as_ref() {
|
||||
for (counter_kind, edge_from_bcb, target_bcb) in unused_expressions {
|
||||
let unused_counter_message = if let Some(from_bcb) = edge_from_bcb.as_ref() {
|
||||
format!(
|
||||
"non-coverage edge counter found without a dependent expression, in \
|
||||
{:?}->{:?}; counter={}",
|
||||
from_bcb,
|
||||
target_bcb,
|
||||
debug_counters.format_counter(&counter_kind),
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"non-coverage counter found without a dependent expression, in {:?}; \
|
||||
counter={}",
|
||||
target_bcb,
|
||||
debug_counters.format_counter(&counter_kind),
|
||||
)
|
||||
};
|
||||
|
||||
if debug_options().allow_unused_expressions {
|
||||
debug!("WARNING: {}", unused_counter_message);
|
||||
} else {
|
||||
bug!("{}", unused_counter_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the MIR pass `CoverageSpan`-specific spanview dump file.
|
||||
|
@ -337,6 +562,8 @@ pub(crate) fn dump_coverage_graphviz(
|
|||
basic_coverage_blocks: &CoverageGraph,
|
||||
debug_counters: &DebugCounters,
|
||||
graphviz_data: &GraphvizData,
|
||||
intermediate_expressions: &Vec<CoverageKind>,
|
||||
debug_used_expressions: &UsedExpressions,
|
||||
) {
|
||||
let mir_source = mir_body.source;
|
||||
let def_id = mir_source.def_id();
|
||||
|
@ -347,21 +574,62 @@ pub(crate) fn dump_coverage_graphviz(
|
|||
debug_counters,
|
||||
&basic_coverage_blocks[bcb],
|
||||
graphviz_data.get_bcb_coverage_spans_with_counters(bcb),
|
||||
graphviz_data.get_bcb_dependency_counters(bcb),
|
||||
// intermediate_expressions are injected into the mir::START_BLOCK, so
|
||||
// include them in the first BCB.
|
||||
if bcb.index() == 0 { Some(&intermediate_expressions) } else { None },
|
||||
)
|
||||
};
|
||||
let edge_labels = |from_bcb| {
|
||||
let from_bcb_data = &basic_coverage_blocks[from_bcb];
|
||||
let from_terminator = from_bcb_data.terminator(mir_body);
|
||||
from_terminator
|
||||
.kind
|
||||
.fmt_successor_labels()
|
||||
let mut edge_labels = from_terminator.kind.fmt_successor_labels();
|
||||
edge_labels.retain(|label| label.to_string() != "unreachable");
|
||||
let edge_counters = from_terminator
|
||||
.successors()
|
||||
.map(|&successor_bb| graphviz_data.get_edge_counter(from_bcb, successor_bb));
|
||||
edge_labels
|
||||
.iter()
|
||||
.map(|label| label.to_string())
|
||||
.zip(edge_counters)
|
||||
.map(|(label, some_counter)| {
|
||||
if let Some(counter) = some_counter {
|
||||
format!("{}\n{}", label, debug_counters.format_counter(counter))
|
||||
} else {
|
||||
label.to_string()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let graphviz_name = format!("Cov_{}_{}", def_id.krate.index(), def_id.index.index());
|
||||
let graphviz_writer =
|
||||
let mut graphviz_writer =
|
||||
GraphvizWriter::new(basic_coverage_blocks, &graphviz_name, node_content, edge_labels);
|
||||
let unused_expressions = debug_used_expressions.get_unused_expressions();
|
||||
if unused_expressions.len() > 0 {
|
||||
graphviz_writer.set_graph_label(&format!(
|
||||
"Unused expressions:\n {}",
|
||||
unused_expressions
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(|(counter_kind, edge_from_bcb, target_bcb)| {
|
||||
if let Some(from_bcb) = edge_from_bcb.as_ref() {
|
||||
format!(
|
||||
"{:?}->{:?}: {}",
|
||||
from_bcb,
|
||||
target_bcb,
|
||||
debug_counters.format_counter(&counter_kind),
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"{:?}: {}",
|
||||
target_bcb,
|
||||
debug_counters.format_counter(&counter_kind),
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n ")
|
||||
));
|
||||
}
|
||||
let mut file = pretty::create_dump_file(tcx, "dot", None, pass_name, &0, mir_source)
|
||||
.expect("Unexpected error creating BasicCoverageBlock graphviz DOT file");
|
||||
graphviz_writer
|
||||
|
@ -375,9 +643,22 @@ fn bcb_to_string_sections(
|
|||
debug_counters: &DebugCounters,
|
||||
bcb_data: &BasicCoverageBlockData,
|
||||
some_coverage_spans_with_counters: Option<&Vec<(CoverageSpan, CoverageKind)>>,
|
||||
some_dependency_counters: Option<&Vec<CoverageKind>>,
|
||||
some_intermediate_expressions: Option<&Vec<CoverageKind>>,
|
||||
) -> Vec<String> {
|
||||
let len = bcb_data.basic_blocks.len();
|
||||
let mut sections = Vec::new();
|
||||
if let Some(collect_intermediate_expressions) = some_intermediate_expressions {
|
||||
sections.push(
|
||||
collect_intermediate_expressions
|
||||
.iter()
|
||||
.map(|expression| {
|
||||
format!("Intermediate {}", debug_counters.format_counter(expression))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
);
|
||||
}
|
||||
if let Some(coverage_spans_with_counters) = some_coverage_spans_with_counters {
|
||||
sections.push(
|
||||
coverage_spans_with_counters
|
||||
|
@ -393,6 +674,19 @@ fn bcb_to_string_sections(
|
|||
.join("\n"),
|
||||
);
|
||||
}
|
||||
if let Some(dependency_counters) = some_dependency_counters {
|
||||
sections.push(format!(
|
||||
"Non-coverage counters:\n {}",
|
||||
dependency_counters
|
||||
.iter()
|
||||
.map(|counter| debug_counters.format_counter(counter))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" \n"),
|
||||
));
|
||||
}
|
||||
if let Some(counter_kind) = &bcb_data.counter_kind {
|
||||
sections.push(format!("{:?}", counter_kind));
|
||||
}
|
||||
let non_term_blocks = bcb_data.basic_blocks[0..len - 1]
|
||||
.iter()
|
||||
.map(|&bb| format!("{:?}: {}", bb, term_type(&mir_body[bb].terminator().kind)))
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
use super::Error;
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::graph::dominators::{self, Dominators};
|
||||
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes};
|
||||
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_middle::mir::coverage::*;
|
||||
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, Terminator, TerminatorKind};
|
||||
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
@ -183,6 +187,13 @@ impl CoverageGraph {
|
|||
self.bcbs.iter_enumerated()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn iter_enumerated_mut(
|
||||
&mut self,
|
||||
) -> impl Iterator<Item = (BasicCoverageBlock, &mut BasicCoverageBlockData)> {
|
||||
self.bcbs.iter_enumerated_mut()
|
||||
}
|
||||
|
||||
#[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 }
|
||||
|
@ -293,12 +304,14 @@ rustc_index::newtype_index! {
|
|||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct BasicCoverageBlockData {
|
||||
pub basic_blocks: Vec<BasicBlock>,
|
||||
pub counter_kind: Option<CoverageKind>,
|
||||
edge_from_bcbs: Option<FxHashMap<BasicCoverageBlock, CoverageKind>>,
|
||||
}
|
||||
|
||||
impl BasicCoverageBlockData {
|
||||
pub fn from(basic_blocks: Vec<BasicBlock>) -> Self {
|
||||
assert!(basic_blocks.len() > 0);
|
||||
Self { basic_blocks }
|
||||
Self { basic_blocks, counter_kind: None, edge_from_bcbs: None }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -316,6 +329,91 @@ impl BasicCoverageBlockData {
|
|||
&mir_body[self.last_bb()].terminator()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn set_counter(
|
||||
&mut self,
|
||||
counter_kind: CoverageKind,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
debug_assert!(
|
||||
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
|
||||
// have an expression (to be injected into an existing `BasicBlock` represented by this
|
||||
// `BasicCoverageBlock`).
|
||||
self.edge_from_bcbs.is_none() || counter_kind.is_expression(),
|
||||
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
|
||||
);
|
||||
let operand = counter_kind.as_operand_id();
|
||||
let expect_none = self.counter_kind.replace(counter_kind);
|
||||
if expect_none.is_some() {
|
||||
return Error::from_string(format!(
|
||||
"attempt to set a BasicCoverageBlock coverage counter more than once; \
|
||||
{:?} already had counter {:?}",
|
||||
self,
|
||||
expect_none.unwrap(),
|
||||
));
|
||||
}
|
||||
Ok(operand)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn counter(&self) -> Option<&CoverageKind> {
|
||||
self.counter_kind.as_ref()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn take_counter(&mut self) -> Option<CoverageKind> {
|
||||
self.counter_kind.take()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn set_edge_counter_from(
|
||||
&mut self,
|
||||
from_bcb: BasicCoverageBlock,
|
||||
counter_kind: CoverageKind,
|
||||
) -> Result<ExpressionOperandId, Error> {
|
||||
if level_enabled!(tracing::Level::DEBUG) {
|
||||
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
|
||||
// have an expression (to be injected into an existing `BasicBlock` represented by this
|
||||
// `BasicCoverageBlock`).
|
||||
if !self.counter_kind.as_ref().map_or(true, |c| c.is_expression()) {
|
||||
return Error::from_string(format!(
|
||||
"attempt to add an incoming edge counter from {:?} when the target BCB already \
|
||||
has a `Counter`",
|
||||
from_bcb
|
||||
));
|
||||
}
|
||||
}
|
||||
let operand = counter_kind.as_operand_id();
|
||||
let expect_none = self
|
||||
.edge_from_bcbs
|
||||
.get_or_insert_with(|| FxHashMap::default())
|
||||
.insert(from_bcb, counter_kind);
|
||||
if expect_none.is_some() {
|
||||
return Error::from_string(format!(
|
||||
"attempt to set an edge counter more than once; from_bcb: \
|
||||
{:?} already had counter {:?}",
|
||||
from_bcb,
|
||||
expect_none.unwrap(),
|
||||
));
|
||||
}
|
||||
Ok(operand)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn edge_counter_from(&self, from_bcb: BasicCoverageBlock) -> Option<&CoverageKind> {
|
||||
if let Some(edge_from_bcbs) = &self.edge_from_bcbs {
|
||||
edge_from_bcbs.get(&from_bcb)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn take_edge_counters(
|
||||
&mut self,
|
||||
) -> Option<impl Iterator<Item = (BasicCoverageBlock, CoverageKind)>> {
|
||||
self.edge_from_bcbs.take().map_or(None, |m| Some(m.into_iter()))
|
||||
}
|
||||
|
||||
pub fn id(&self) -> String {
|
||||
format!(
|
||||
"@{}",
|
||||
|
@ -328,6 +426,56 @@ impl BasicCoverageBlockData {
|
|||
}
|
||||
}
|
||||
|
||||
/// Represents a successor from a branching BasicCoverageBlock (such as the arms of a `SwitchInt`)
|
||||
/// as either the successor BCB itself, if it has only one incoming edge, or the successor _plus_
|
||||
/// the specific branching BCB, representing the edge between the two. The latter case
|
||||
/// distinguishes this incoming edge from other incoming edges to the same `target_bcb`.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) struct BcbBranch {
|
||||
pub edge_from_bcb: Option<BasicCoverageBlock>,
|
||||
pub target_bcb: BasicCoverageBlock,
|
||||
}
|
||||
|
||||
impl BcbBranch {
|
||||
pub fn from_to(
|
||||
from_bcb: BasicCoverageBlock,
|
||||
to_bcb: BasicCoverageBlock,
|
||||
basic_coverage_blocks: &CoverageGraph,
|
||||
) -> Self {
|
||||
let edge_from_bcb = if basic_coverage_blocks.predecessors[to_bcb].len() > 1 {
|
||||
Some(from_bcb)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Self { edge_from_bcb, target_bcb: to_bcb }
|
||||
}
|
||||
|
||||
pub fn counter<'a>(
|
||||
&self,
|
||||
basic_coverage_blocks: &'a CoverageGraph,
|
||||
) -> Option<&'a CoverageKind> {
|
||||
if let Some(from_bcb) = self.edge_from_bcb {
|
||||
basic_coverage_blocks[self.target_bcb].edge_counter_from(from_bcb)
|
||||
} else {
|
||||
basic_coverage_blocks[self.target_bcb].counter()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_only_path_to_target(&self) -> bool {
|
||||
self.edge_from_bcb.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for BcbBranch {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(from_bcb) = self.edge_from_bcb {
|
||||
write!(fmt, "{:?}->{:?}", from_bcb, self.target_bcb)
|
||||
} else {
|
||||
write!(fmt, "{:?}", self.target_bcb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn bcb_filtered_successors<'a, 'tcx>(
|
||||
body: &'tcx &'a mir::Body<'tcx>,
|
||||
term_kind: &'tcx TerminatorKind<'tcx>,
|
||||
|
@ -344,6 +492,175 @@ fn bcb_filtered_successors<'a, 'tcx>(
|
|||
.filter(move |&&successor| body[successor].terminator().kind != TerminatorKind::Unreachable)
|
||||
}
|
||||
|
||||
/// Maintains separate worklists for each loop in the BasicCoverageBlock CFG, plus one for the
|
||||
/// CoverageGraph outside all loops. This supports traversing the BCB CFG in a way that
|
||||
/// ensures a loop is completely traversed before processing Blocks after the end of the loop.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TraversalContext {
|
||||
/// From one or more backedges returning to a loop header.
|
||||
pub loop_backedges: Option<(Vec<BasicCoverageBlock>, BasicCoverageBlock)>,
|
||||
|
||||
/// worklist, to be traversed, of CoverageGraph in the loop with the given loop
|
||||
/// backedges, such that the loop is the inner inner-most loop containing these
|
||||
/// CoverageGraph
|
||||
pub worklist: Vec<BasicCoverageBlock>,
|
||||
}
|
||||
|
||||
pub(crate) struct TraverseCoverageGraphWithLoops {
|
||||
pub backedges: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
|
||||
pub context_stack: Vec<TraversalContext>,
|
||||
visited: BitSet<BasicCoverageBlock>,
|
||||
}
|
||||
|
||||
impl TraverseCoverageGraphWithLoops {
|
||||
pub fn new(basic_coverage_blocks: &CoverageGraph) -> Self {
|
||||
let start_bcb = basic_coverage_blocks.start_node();
|
||||
let backedges = find_loop_backedges(basic_coverage_blocks);
|
||||
let mut context_stack = Vec::new();
|
||||
context_stack.push(TraversalContext { loop_backedges: None, worklist: vec![start_bcb] });
|
||||
// `context_stack` starts with a `TraversalContext` for the main function context (beginning
|
||||
// with the `start` BasicCoverageBlock of the function). New worklists are pushed to the top
|
||||
// of the stack as loops are entered, and popped off of the stack when a loop's worklist is
|
||||
// exhausted.
|
||||
let visited = BitSet::new_empty(basic_coverage_blocks.num_nodes());
|
||||
Self { backedges, context_stack, visited }
|
||||
}
|
||||
|
||||
pub fn next(&mut self, basic_coverage_blocks: &CoverageGraph) -> Option<BasicCoverageBlock> {
|
||||
debug!(
|
||||
"TraverseCoverageGraphWithLoops::next - context_stack: {:?}",
|
||||
self.context_stack.iter().rev().collect::<Vec<_>>()
|
||||
);
|
||||
while let Some(next_bcb) = {
|
||||
// Strip contexts with empty worklists from the top of the stack
|
||||
while self.context_stack.last().map_or(false, |context| context.worklist.is_empty()) {
|
||||
self.context_stack.pop();
|
||||
}
|
||||
// Pop the next bcb off of the current context_stack. If none, all BCBs were visited.
|
||||
self.context_stack.last_mut().map_or(None, |context| context.worklist.pop())
|
||||
} {
|
||||
if !self.visited.insert(next_bcb) {
|
||||
debug!("Already visited: {:?}", next_bcb);
|
||||
continue;
|
||||
}
|
||||
debug!("Visiting {:?}", next_bcb);
|
||||
if self.backedges[next_bcb].len() > 0 {
|
||||
debug!("{:?} is a loop header! Start a new TraversalContext...", next_bcb);
|
||||
self.context_stack.push(TraversalContext {
|
||||
loop_backedges: Some((self.backedges[next_bcb].clone(), next_bcb)),
|
||||
worklist: Vec::new(),
|
||||
});
|
||||
}
|
||||
self.extend_worklist(basic_coverage_blocks, next_bcb);
|
||||
return Some(next_bcb);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn extend_worklist(
|
||||
&mut self,
|
||||
basic_coverage_blocks: &CoverageGraph,
|
||||
bcb: BasicCoverageBlock,
|
||||
) {
|
||||
let successors = &basic_coverage_blocks.successors[bcb];
|
||||
debug!("{:?} has {} successors:", bcb, successors.len());
|
||||
for &successor in successors {
|
||||
if successor == bcb {
|
||||
debug!(
|
||||
"{:?} has itself as its own successor. (Note, the compiled code will \
|
||||
generate an infinite loop.)",
|
||||
bcb
|
||||
);
|
||||
// Don't re-add this successor to the worklist. We are already processing it.
|
||||
break;
|
||||
}
|
||||
for context in self.context_stack.iter_mut().rev() {
|
||||
// Add successors of the current BCB to the appropriate context. Successors that
|
||||
// stay within a loop are added to the BCBs context worklist. Successors that
|
||||
// exit the loop (they are not dominated by the loop header) must be reachable
|
||||
// from other BCBs outside the loop, and they will be added to a different
|
||||
// worklist.
|
||||
//
|
||||
// Branching blocks (with more than one successor) must be processed before
|
||||
// blocks with only one successor, to prevent unnecessarily complicating
|
||||
// `Expression`s by creating a Counter in a `BasicCoverageBlock` that the
|
||||
// branching block would have given an `Expression` (or vice versa).
|
||||
let (some_successor_to_add, some_loop_header) =
|
||||
if let Some((_, loop_header)) = context.loop_backedges {
|
||||
if basic_coverage_blocks.is_dominated_by(successor, loop_header) {
|
||||
(Some(successor), Some(loop_header))
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
} else {
|
||||
(Some(successor), None)
|
||||
};
|
||||
if let Some(successor_to_add) = some_successor_to_add {
|
||||
if basic_coverage_blocks.successors[successor_to_add].len() > 1 {
|
||||
debug!(
|
||||
"{:?} successor is branching. Prioritize it at the beginning of \
|
||||
the {}",
|
||||
successor_to_add,
|
||||
if let Some(loop_header) = some_loop_header {
|
||||
format!("worklist for the loop headed by {:?}", loop_header)
|
||||
} else {
|
||||
String::from("non-loop worklist")
|
||||
},
|
||||
);
|
||||
context.worklist.insert(0, successor_to_add);
|
||||
} else {
|
||||
debug!(
|
||||
"{:?} successor is non-branching. Defer it to the end of the {}",
|
||||
successor_to_add,
|
||||
if let Some(loop_header) = some_loop_header {
|
||||
format!("worklist for the loop headed by {:?}", loop_header)
|
||||
} else {
|
||||
String::from("non-loop worklist")
|
||||
},
|
||||
);
|
||||
context.worklist.push(successor_to_add);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_complete(&self) -> bool {
|
||||
self.visited.count() == self.visited.domain_size()
|
||||
}
|
||||
|
||||
pub fn unvisited(&self) -> Vec<BasicCoverageBlock> {
|
||||
let mut unvisited_set: BitSet<BasicCoverageBlock> =
|
||||
BitSet::new_filled(self.visited.domain_size());
|
||||
unvisited_set.subtract(&self.visited);
|
||||
unvisited_set.iter().collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
fn find_loop_backedges(
|
||||
basic_coverage_blocks: &CoverageGraph,
|
||||
) -> IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>> {
|
||||
let num_bcbs = basic_coverage_blocks.num_nodes();
|
||||
let mut backedges = IndexVec::from_elem_n(Vec::<BasicCoverageBlock>::new(), num_bcbs);
|
||||
|
||||
// Identify loops by their backedges
|
||||
for (bcb, _) in basic_coverage_blocks.iter_enumerated() {
|
||||
for &successor in &basic_coverage_blocks.successors[bcb] {
|
||||
if basic_coverage_blocks.is_dominated_by(bcb, successor) {
|
||||
let loop_header = successor;
|
||||
let backedge_from_bcb = bcb;
|
||||
debug!(
|
||||
"Found BCB backedge: {:?} -> loop_header: {:?}",
|
||||
backedge_from_bcb, loop_header
|
||||
);
|
||||
backedges[loop_header].push(backedge_from_bcb);
|
||||
}
|
||||
}
|
||||
}
|
||||
backedges
|
||||
}
|
||||
|
||||
pub struct ShortCircuitPreorder<
|
||||
'a,
|
||||
'tcx,
|
||||
|
|
|
@ -6,7 +6,7 @@ mod graph;
|
|||
mod spans;
|
||||
|
||||
use counters::CoverageCounters;
|
||||
use graph::CoverageGraph;
|
||||
use graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph};
|
||||
use spans::{CoverageSpan, CoverageSpans};
|
||||
|
||||
use crate::transform::MirPass;
|
||||
|
@ -21,11 +21,26 @@ use rustc_middle::hir;
|
|||
use rustc_middle::hir::map::blocks::FnLikeNode;
|
||||
use rustc_middle::ich::StableHashingContext;
|
||||
use rustc_middle::mir::coverage::*;
|
||||
use rustc_middle::mir::{self, BasicBlock, Coverage, Statement, StatementKind};
|
||||
use rustc_middle::mir::{
|
||||
self, BasicBlock, BasicBlockData, Coverage, SourceInfo, Statement, StatementKind, Terminator,
|
||||
TerminatorKind,
|
||||
};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_span::def_id::DefId;
|
||||
use rustc_span::{CharPos, Pos, SourceFile, Span, Symbol};
|
||||
|
||||
/// A simple error message wrapper for `coverage::Error`s.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Error {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn from_string<T>(message: String) -> Result<T, Error> {
|
||||
Err(Self { message })
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
|
||||
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
|
||||
/// to construct the coverage map.
|
||||
|
@ -104,6 +119,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
debug!("instrumenting {:?}, span: {}", def_id, source_map.span_to_string(body_span));
|
||||
|
||||
let mut graphviz_data = debug::GraphvizData::new();
|
||||
let mut debug_used_expressions = debug::UsedExpressions::new();
|
||||
|
||||
let dump_graphviz = tcx.sess.opts.debugging_opts.dump_mir_graphviz;
|
||||
if dump_graphviz {
|
||||
|
@ -111,6 +127,10 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
self.coverage_counters.enable_debug();
|
||||
}
|
||||
|
||||
if dump_graphviz || level_enabled!(tracing::Level::DEBUG) {
|
||||
debug_used_expressions.enable();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Compute `CoverageSpan`s from the `CoverageGraph`.
|
||||
let coverage_spans = CoverageSpans::generate_coverage_spans(
|
||||
|
@ -129,7 +149,53 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
);
|
||||
}
|
||||
|
||||
self.inject_coverage_span_counters(coverage_spans, &mut graphviz_data);
|
||||
////////////////////////////////////////////////////
|
||||
// Create an optimized mix of `Counter`s and `Expression`s for the `CoverageGraph`. Ensure
|
||||
// every `CoverageSpan` has a `Counter` or `Expression` assigned to its `BasicCoverageBlock`
|
||||
// and all `Expression` dependencies (operands) are also generated, for any other
|
||||
// `BasicCoverageBlock`s not already associated with a `CoverageSpan`.
|
||||
//
|
||||
// Intermediate expressions (used to compute other `Expression` values), which have no
|
||||
// direct associate to any `BasicCoverageBlock`, are returned in the method `Result`.
|
||||
let intermediate_expressions_or_error = self
|
||||
.coverage_counters
|
||||
.make_bcb_counters(&mut self.basic_coverage_blocks, &coverage_spans);
|
||||
|
||||
let (result, intermediate_expressions) = match intermediate_expressions_or_error {
|
||||
Ok(intermediate_expressions) => {
|
||||
// If debugging, add any intermediate expressions (which are not associated with any
|
||||
// BCB) to the `debug_used_expressions` map.
|
||||
if debug_used_expressions.is_enabled() {
|
||||
for intermediate_expression in &intermediate_expressions {
|
||||
debug_used_expressions.add_expression_operands(intermediate_expression);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Remove the counter or edge counter from of each `CoverageSpan`s associated
|
||||
// `BasicCoverageBlock`, and inject a `Coverage` statement into the MIR.
|
||||
self.inject_coverage_span_counters(
|
||||
coverage_spans,
|
||||
&mut graphviz_data,
|
||||
&mut debug_used_expressions,
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// For any remaining `BasicCoverageBlock` counters (that were not associated with
|
||||
// any `CoverageSpan`), inject `Coverage` statements (_without_ code region `Span`s)
|
||||
// to ensure `BasicCoverageBlock` counters that other `Expression`s may depend on
|
||||
// are in fact counted, even though they don't directly contribute to counting
|
||||
// their own independent code region's coverage.
|
||||
self.inject_indirect_counters(&mut graphviz_data, &mut debug_used_expressions);
|
||||
|
||||
// Intermediate expressions will be injected as the final step, after generating
|
||||
// debug output, if any.
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
(Ok(()), intermediate_expressions)
|
||||
}
|
||||
Err(e) => (Err(e), Vec::new()),
|
||||
};
|
||||
|
||||
if graphviz_data.is_enabled() {
|
||||
// Even if there was an error, a partial CoverageGraph can still generate a useful
|
||||
|
@ -141,19 +207,40 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
&self.basic_coverage_blocks,
|
||||
&self.coverage_counters.debug_counters,
|
||||
&graphviz_data,
|
||||
&intermediate_expressions,
|
||||
&debug_used_expressions,
|
||||
);
|
||||
}
|
||||
|
||||
if let Err(e) = result {
|
||||
bug!("Error processing: {:?}: {:?}", self.mir_body.source.def_id(), e)
|
||||
};
|
||||
|
||||
// Depending on current `debug_options()`, `alert_on_unused_expressions()` could panic, so
|
||||
// this check is performed as late as possible, to allow other debug output (logs and dump
|
||||
// files), which might be helpful in analyzing unused expressions, to still be generated.
|
||||
debug_used_expressions.alert_on_unused_expressions(&self.coverage_counters.debug_counters);
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Finally, inject the intermediate expressions collected along the way.
|
||||
for intermediate_expression in intermediate_expressions {
|
||||
inject_intermediate_expression(self.mir_body, intermediate_expression);
|
||||
}
|
||||
}
|
||||
|
||||
/// Inject a counter for each `CoverageSpan`. There can be multiple `CoverageSpan`s for a given
|
||||
/// BCB, but only one actual counter needs to be incremented per BCB. `bcb_counters` maps each
|
||||
/// BCB, but only one actual counter needs to be incremented per BCB. `bb_counters` maps each
|
||||
/// `bcb` to its `Counter`, when injected. Subsequent `CoverageSpan`s for a BCB that already has
|
||||
/// a `Counter` will inject an `Expression` instead, and compute its value by adding `ZERO` to
|
||||
/// the BCB `Counter` value.
|
||||
///
|
||||
/// If debugging, add every BCB `Expression` associated with a `CoverageSpan`s to the
|
||||
/// `used_expression_operands` map.
|
||||
fn inject_coverage_span_counters(
|
||||
&mut self,
|
||||
coverage_spans: Vec<CoverageSpan>,
|
||||
graphviz_data: &mut debug::GraphvizData,
|
||||
debug_used_expressions: &mut debug::UsedExpressions,
|
||||
) {
|
||||
let tcx = self.tcx;
|
||||
let source_map = tcx.sess.source_map();
|
||||
|
@ -165,40 +252,194 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|||
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,
|
||||
ExpressionOperandId::ZERO,
|
||||
|| Some(format!("{:?}", bcb)),
|
||||
);
|
||||
debug!(
|
||||
"Injecting counter expression {:?} at: {:?}:\n{}\n==========",
|
||||
expression,
|
||||
span,
|
||||
source_map.span_to_snippet(span).expect("Error getting source for span"),
|
||||
);
|
||||
graphviz_data.add_bcb_coverage_span_with_counter(bcb, &covspan, &expression);
|
||||
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));
|
||||
let counter_kind = if let Some(&counter_operand) = bcb_counters[bcb].as_ref() {
|
||||
self.coverage_counters.make_identity_counter(counter_operand)
|
||||
} else if let Some(counter_kind) = self.bcb_data_mut(bcb).take_counter() {
|
||||
bcb_counters[bcb] = Some(counter_kind.as_operand_id());
|
||||
debug_used_expressions.add_expression_operands(&counter_kind);
|
||||
counter_kind
|
||||
} else {
|
||||
let counter = self.coverage_counters.make_counter(|| Some(format!("{:?}", bcb)));
|
||||
debug!(
|
||||
"Injecting counter {:?} at: {:?}:\n{}\n==========",
|
||||
counter,
|
||||
span,
|
||||
source_map.span_to_snippet(span).expect("Error getting source for span"),
|
||||
);
|
||||
let counter_operand = counter.as_operand_id();
|
||||
bcb_counters[bcb] = Some(counter_operand);
|
||||
graphviz_data.add_bcb_coverage_span_with_counter(bcb, &covspan, &counter);
|
||||
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));
|
||||
bug!("Every BasicCoverageBlock should have a Counter or Expression");
|
||||
};
|
||||
graphviz_data.add_bcb_coverage_span_with_counter(bcb, &covspan, &counter_kind);
|
||||
let some_code_region = if self.is_code_region_redundant(bcb, span, body_span) {
|
||||
None
|
||||
} else {
|
||||
Some(make_code_region(file_name, &source_file, span, body_span))
|
||||
};
|
||||
inject_statement(self.mir_body, counter_kind, self.bcb_last_bb(bcb), some_code_region);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the type of `BasicCoverageBlock` (specifically, it's `BasicBlock`s
|
||||
/// `TerminatorKind`) with the given `Span` (relative to the `body_span`) is known to produce
|
||||
/// a redundant coverage count.
|
||||
///
|
||||
/// There is at least one case for this, and if it's not handled, the last line in a function
|
||||
/// will be double-counted.
|
||||
///
|
||||
/// If this method returns `true`, the counter (which other `Expressions` may depend on) is
|
||||
/// still injected, but without an associated code region.
|
||||
fn is_code_region_redundant(
|
||||
&self,
|
||||
bcb: BasicCoverageBlock,
|
||||
span: Span,
|
||||
body_span: Span,
|
||||
) -> bool {
|
||||
if span.hi() == body_span.hi() {
|
||||
// All functions execute a `Return`-terminated `BasicBlock`, regardless of how the
|
||||
// function returns; but only some functions also _can_ return after a `Goto` block
|
||||
// that ends on the closing brace of the function (with the `Return`). When this
|
||||
// happens, the last character is counted 2 (or possibly more) times, when we know
|
||||
// the function returned only once (of course). By giving all `Goto` terminators at
|
||||
// the end of a function a `non-reportable` code region, they are still counted
|
||||
// if appropriate, but they don't increment the line counter, as long as their is
|
||||
// also a `Return` on that last line.
|
||||
if let TerminatorKind::Goto { .. } = self.bcb_terminator(bcb).kind {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// `inject_coverage_span_counters()` looped through the `CoverageSpan`s and injected the
|
||||
/// counter from the `CoverageSpan`s `BasicCoverageBlock`, removing it from the BCB in the
|
||||
/// process (via `take_counter()`).
|
||||
///
|
||||
/// Any other counter associated with a `BasicCoverageBlock`, or its incoming edge, but not
|
||||
/// associated with a `CoverageSpan`, should only exist if the counter is a `Expression`
|
||||
/// dependency (one of the expression operands). Collect them, and inject the additional
|
||||
/// counters into the MIR, without a reportable coverage span.
|
||||
fn inject_indirect_counters(
|
||||
&mut self,
|
||||
graphviz_data: &mut debug::GraphvizData,
|
||||
debug_used_expressions: &mut debug::UsedExpressions,
|
||||
) {
|
||||
let mut bcb_counters_without_direct_coverage_spans = Vec::new();
|
||||
for (target_bcb, target_bcb_data) in self.basic_coverage_blocks.iter_enumerated_mut() {
|
||||
if let Some(counter_kind) = target_bcb_data.take_counter() {
|
||||
bcb_counters_without_direct_coverage_spans.push((None, target_bcb, counter_kind));
|
||||
}
|
||||
if let Some(edge_counters) = target_bcb_data.take_edge_counters() {
|
||||
for (from_bcb, counter_kind) in edge_counters {
|
||||
bcb_counters_without_direct_coverage_spans.push((
|
||||
Some(from_bcb),
|
||||
target_bcb,
|
||||
counter_kind,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If debug is enabled, validate that every BCB or edge counter not directly associated
|
||||
// with a coverage span is at least indirectly associated (it is a dependency of a BCB
|
||||
// counter that _is_ associated with a coverage span).
|
||||
debug_used_expressions.validate(&bcb_counters_without_direct_coverage_spans);
|
||||
|
||||
for (edge_from_bcb, target_bcb, counter_kind) in bcb_counters_without_direct_coverage_spans
|
||||
{
|
||||
debug_used_expressions.add_unused_expression_if_not_found(
|
||||
&counter_kind,
|
||||
edge_from_bcb,
|
||||
target_bcb,
|
||||
);
|
||||
|
||||
match counter_kind {
|
||||
CoverageKind::Counter { .. } => {
|
||||
let inject_to_bb = if let Some(from_bcb) = edge_from_bcb {
|
||||
// The MIR edge starts `from_bb` (the outgoing / last BasicBlock in
|
||||
// `from_bcb`) and ends at `to_bb` (the incoming / first BasicBlock in the
|
||||
// `target_bcb`; also called the `leader_bb`).
|
||||
let from_bb = self.bcb_last_bb(from_bcb);
|
||||
let to_bb = self.bcb_leader_bb(target_bcb);
|
||||
|
||||
let new_bb = inject_edge_counter_basic_block(self.mir_body, from_bb, to_bb);
|
||||
graphviz_data.set_edge_counter(from_bcb, new_bb, &counter_kind);
|
||||
debug!(
|
||||
"Edge {:?} (last {:?}) -> {:?} (leader {:?}) requires a new MIR \
|
||||
BasicBlock {:?}, for unclaimed edge counter {}",
|
||||
edge_from_bcb,
|
||||
from_bb,
|
||||
target_bcb,
|
||||
to_bb,
|
||||
new_bb,
|
||||
self.format_counter(&counter_kind),
|
||||
);
|
||||
new_bb
|
||||
} else {
|
||||
let target_bb = self.bcb_last_bb(target_bcb);
|
||||
graphviz_data.add_bcb_dependency_counter(target_bcb, &counter_kind);
|
||||
debug!(
|
||||
"{:?} ({:?}) gets a new Coverage statement for unclaimed counter {}",
|
||||
target_bcb,
|
||||
target_bb,
|
||||
self.format_counter(&counter_kind),
|
||||
);
|
||||
target_bb
|
||||
};
|
||||
|
||||
inject_statement(self.mir_body, counter_kind, inject_to_bb, None);
|
||||
}
|
||||
CoverageKind::Expression { .. } => {
|
||||
inject_intermediate_expression(self.mir_body, counter_kind)
|
||||
}
|
||||
_ => bug!("CoverageKind should be a counter"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_leader_bb(&self, bcb: BasicCoverageBlock) -> BasicBlock {
|
||||
self.bcb_data(bcb).leader_bb()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_last_bb(&self, bcb: BasicCoverageBlock) -> BasicBlock {
|
||||
self.bcb_data(bcb).last_bb()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_terminator(&self, bcb: BasicCoverageBlock) -> &Terminator<'tcx> {
|
||||
self.bcb_data(bcb).terminator(self.mir_body)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_data(&self, bcb: BasicCoverageBlock) -> &BasicCoverageBlockData {
|
||||
&self.basic_coverage_blocks[bcb]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bcb_data_mut(&mut self, bcb: BasicCoverageBlock) -> &mut BasicCoverageBlockData {
|
||||
&mut self.basic_coverage_blocks[bcb]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format_counter(&self, counter_kind: &CoverageKind) -> String {
|
||||
self.coverage_counters.debug_counters.format_counter(counter_kind)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_edge_counter_basic_block(
|
||||
mir_body: &mut mir::Body<'tcx>,
|
||||
from_bb: BasicBlock,
|
||||
to_bb: BasicBlock,
|
||||
) -> BasicBlock {
|
||||
let span = mir_body[from_bb].terminator().source_info.span.shrink_to_hi();
|
||||
let new_bb = mir_body.basic_blocks_mut().push(BasicBlockData {
|
||||
statements: vec![], // counter will be injected here
|
||||
terminator: Some(Terminator {
|
||||
source_info: SourceInfo::outermost(span),
|
||||
kind: TerminatorKind::Goto { target: to_bb },
|
||||
}),
|
||||
is_cleanup: false,
|
||||
});
|
||||
let edge_ref = mir_body[from_bb]
|
||||
.terminator_mut()
|
||||
.successors_mut()
|
||||
.find(|successor| **successor == to_bb)
|
||||
.expect("from_bb should have a successor for to_bb");
|
||||
*edge_ref = new_bb;
|
||||
new_bb
|
||||
}
|
||||
|
||||
fn inject_statement(
|
||||
|
@ -223,6 +464,20 @@ fn inject_statement(
|
|||
data.statements.push(statement);
|
||||
}
|
||||
|
||||
// Non-code expressions are injected into the coverage map, without generating executable code.
|
||||
fn inject_intermediate_expression(mir_body: &mut mir::Body<'tcx>, expression: CoverageKind) {
|
||||
debug_assert!(if let CoverageKind::Expression { .. } = expression { true } else { false });
|
||||
debug!(" injecting non-code expression {:?}", expression);
|
||||
let inject_in_bb = mir::START_BLOCK;
|
||||
let data = &mut mir_body[inject_in_bb];
|
||||
let source_info = data.terminator().source_info;
|
||||
let statement = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Coverage(box Coverage { kind: expression, code_region: None }),
|
||||
};
|
||||
data.statements.push(statement);
|
||||
}
|
||||
|
||||
/// Convert the Span into its file name, start line and column, and end line and column
|
||||
fn make_code_region(
|
||||
file_name: Symbol,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue