2020-06-04 04:19:34 +00:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
2020-06-18 20:29:43 +00:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-06-22 22:54:28 +00:00
|
|
|
use rustc_middle::hir;
|
2020-06-18 20:29:43 +00:00
|
|
|
use rustc_middle::ich::StableHashingContext;
|
2020-08-02 03:03:59 +00:00
|
|
|
use rustc_middle::mir;
|
2020-06-22 06:29:08 +00:00
|
|
|
use rustc_middle::mir::coverage::*;
|
2020-08-15 11:42:13 +00:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
|
|
|
use rustc_middle::mir::{BasicBlock, Coverage, CoverageInfo, Location, Statement, StatementKind};
|
2020-06-23 02:21:56 +00:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2020-08-15 11:42:13 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-06-04 04:19:34 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
2020-08-15 11:42:13 +00:00
|
|
|
use rustc_span::{FileName, Pos, RealFileName, Span, Symbol};
|
2020-06-04 04:19:34 +00:00
|
|
|
|
2020-06-10 16:54:02 +00:00
|
|
|
/// Inserts call to count_code_region() as a placeholder to be replaced during code generation with
|
|
|
|
/// the intrinsic llvm.instrprof.increment.
|
2020-06-04 04:19:34 +00:00
|
|
|
pub struct InstrumentCoverage;
|
|
|
|
|
2020-06-22 06:29:08 +00:00
|
|
|
/// The `query` provider for `CoverageInfo`, requested by `codegen_intrinsic_call()` when
|
2020-06-23 02:21:56 +00:00
|
|
|
/// constructing the arguments for `llvm.instrprof.increment`.
|
2020-07-05 20:00:14 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2020-06-22 06:29:08 +00:00
|
|
|
providers.coverageinfo = |tcx, def_id| coverageinfo_from_mir(tcx, def_id);
|
|
|
|
}
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
struct CoverageVisitor {
|
|
|
|
info: CoverageInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<'_> for CoverageVisitor {
|
|
|
|
fn visit_coverage(&mut self, coverage: &Coverage, _location: Location) {
|
|
|
|
match coverage.kind {
|
|
|
|
CoverageKind::Counter { id, .. } => {
|
|
|
|
let counter_id = u32::from(id);
|
|
|
|
self.info.num_counters = std::cmp::max(self.info.num_counters, counter_id + 1);
|
|
|
|
}
|
|
|
|
CoverageKind::Expression { id, .. } => {
|
|
|
|
let expression_index = u32::MAX - u32::from(id);
|
|
|
|
self.info.num_expressions =
|
|
|
|
std::cmp::max(self.info.num_expressions, expression_index + 1);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 06:29:08 +00:00
|
|
|
fn coverageinfo_from_mir<'tcx>(tcx: TyCtxt<'tcx>, mir_def_id: DefId) -> CoverageInfo {
|
|
|
|
let mir_body = tcx.optimized_mir(mir_def_id);
|
|
|
|
|
|
|
|
// The `num_counters` argument to `llvm.instrprof.increment` is the number of injected
|
2020-07-27 23:25:08 +00:00
|
|
|
// counters, with each counter having a counter ID from `0..num_counters-1`. MIR optimization
|
2020-06-22 06:29:08 +00:00
|
|
|
// may split and duplicate some BasicBlock sequences. Simply counting the calls may not
|
2020-07-27 23:25:08 +00:00
|
|
|
// work; but computing the num_counters by adding `1` to the highest counter_id (for a given
|
2020-06-22 06:29:08 +00:00
|
|
|
// instrumented function) is valid.
|
2020-07-02 18:27:15 +00:00
|
|
|
//
|
|
|
|
// `num_expressions` is the number of counter expressions added to the MIR body. Both
|
|
|
|
// `num_counters` and `num_expressions` are used to initialize new vectors, during backend
|
2020-07-27 23:25:08 +00:00
|
|
|
// code generate, to lookup counters and expressions by simple u32 indexes.
|
2020-08-15 11:42:13 +00:00
|
|
|
let mut coverage_visitor =
|
|
|
|
CoverageVisitor { info: CoverageInfo { num_counters: 0, num_expressions: 0 } };
|
2020-06-23 02:21:56 +00:00
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
coverage_visitor.visit_body(mir_body);
|
|
|
|
coverage_visitor.info
|
2020-06-18 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 04:19:34 +00:00
|
|
|
impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
|
2020-06-18 20:29:43 +00:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, mir_body: &mut mir::Body<'tcx>) {
|
2020-07-27 23:25:08 +00:00
|
|
|
// If the InstrumentCoverage pass is called on promoted MIRs, skip them.
|
|
|
|
// See: https://github.com/rust-lang/rust/pull/73011#discussion_r438317601
|
|
|
|
if src.promoted.is_none() {
|
|
|
|
Instrumentor::new(tcx, src, mir_body).inject_counters();
|
2020-06-04 04:19:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
struct Instrumentor<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
mir_def_id: DefId,
|
|
|
|
mir_body: &'a mut mir::Body<'tcx>,
|
|
|
|
hir_body: &'tcx rustc_hir::Body<'tcx>,
|
|
|
|
function_source_hash: Option<u64>,
|
|
|
|
num_counters: u32,
|
|
|
|
num_expressions: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, mir_body: &'a mut mir::Body<'tcx>) -> Self {
|
|
|
|
let mir_def_id = src.def_id();
|
|
|
|
let hir_body = hir_body(tcx, mir_def_id);
|
|
|
|
Self {
|
|
|
|
tcx,
|
|
|
|
mir_def_id,
|
|
|
|
mir_body,
|
|
|
|
hir_body,
|
|
|
|
function_source_hash: None,
|
|
|
|
num_counters: 0,
|
|
|
|
num_expressions: 0,
|
|
|
|
}
|
2020-06-18 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
/// Counter IDs start from zero and go up.
|
2020-08-15 11:42:13 +00:00
|
|
|
fn next_counter(&mut self) -> CounterValueReference {
|
2020-07-02 18:27:15 +00:00
|
|
|
assert!(self.num_counters < u32::MAX - self.num_expressions);
|
2020-06-22 06:48:39 +00:00
|
|
|
let next = self.num_counters;
|
2020-06-18 20:29:43 +00:00
|
|
|
self.num_counters += 1;
|
2020-08-15 11:42:13 +00:00
|
|
|
CounterValueReference::from(next)
|
2020-06-18 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
/// Expression IDs start from u32::MAX and go down because a CounterExpression can reference
|
2020-07-27 23:25:08 +00:00
|
|
|
/// (add or subtract counts) of both Counter regions and CounterExpression regions. The counter
|
|
|
|
/// expression operand IDs must be unique across both types.
|
2020-08-15 11:42:13 +00:00
|
|
|
fn next_expression(&mut self) -> InjectedExpressionIndex {
|
2020-07-02 18:27:15 +00:00
|
|
|
assert!(self.num_counters < u32::MAX - self.num_expressions);
|
|
|
|
let next = u32::MAX - self.num_expressions;
|
|
|
|
self.num_expressions += 1;
|
2020-08-15 11:42:13 +00:00
|
|
|
InjectedExpressionIndex::from(next)
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn function_source_hash(&mut self) -> u64 {
|
|
|
|
match self.function_source_hash {
|
|
|
|
Some(hash) => hash,
|
|
|
|
None => {
|
|
|
|
let hash = hash_mir_source(self.tcx, self.hir_body);
|
|
|
|
self.function_source_hash.replace(hash);
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_counters(&mut self) {
|
|
|
|
let body_span = self.hir_body.value.span;
|
2020-07-27 23:25:08 +00:00
|
|
|
debug!("instrumenting {:?}, span: {:?}", self.mir_def_id, body_span);
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2020-06-18 20:29:43 +00:00
|
|
|
// FIXME(richkadel): As a first step, counters are only injected at the top of each
|
|
|
|
// function. The complete solution will inject counters at each conditional code branch.
|
2020-08-15 11:42:13 +00:00
|
|
|
let block = rustc_middle::mir::START_BLOCK;
|
|
|
|
let counter = self.make_counter();
|
|
|
|
self.inject_statement(counter, body_span, block);
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
// FIXME(richkadel): The next step to implement source based coverage analysis will be
|
|
|
|
// instrumenting branches within functions, and some regions will be counted by "counter
|
|
|
|
// expression". The function to inject counter expression is implemented. Replace this
|
|
|
|
// "fake use" with real use.
|
|
|
|
let fake_use = false;
|
|
|
|
if fake_use {
|
|
|
|
let add = false;
|
2020-08-15 11:42:13 +00:00
|
|
|
let fake_counter = CoverageKind::Counter {
|
|
|
|
function_source_hash: self.function_source_hash(),
|
|
|
|
id: CounterValueReference::from_u32(1),
|
|
|
|
};
|
|
|
|
let fake_expression = CoverageKind::Expression {
|
|
|
|
id: InjectedExpressionIndex::from(u32::MAX - 1),
|
|
|
|
lhs: ExpressionOperandId::from_u32(1),
|
|
|
|
op: Op::Add,
|
|
|
|
rhs: ExpressionOperandId::from_u32(2),
|
|
|
|
};
|
|
|
|
|
|
|
|
let lhs = fake_counter.as_operand_id();
|
2020-07-27 23:25:08 +00:00
|
|
|
let op = if add { Op::Add } else { Op::Subtract };
|
2020-08-15 11:42:13 +00:00
|
|
|
let rhs = fake_expression.as_operand_id();
|
2020-07-27 23:25:08 +00:00
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
let block = rustc_middle::mir::START_BLOCK;
|
2020-07-27 23:25:08 +00:00
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
let expression = self.make_expression(lhs, op, rhs);
|
|
|
|
self.inject_statement(expression, body_span, block);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
2020-06-18 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
fn make_counter(&mut self) -> CoverageKind {
|
|
|
|
CoverageKind::Counter {
|
|
|
|
function_source_hash: self.function_source_hash(),
|
|
|
|
id: self.next_counter(),
|
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 23:25:08 +00:00
|
|
|
fn make_expression(
|
2020-08-15 11:42:13 +00:00
|
|
|
&mut self,
|
|
|
|
lhs: ExpressionOperandId,
|
2020-07-02 18:27:15 +00:00
|
|
|
op: Op,
|
2020-08-15 11:42:13 +00:00
|
|
|
rhs: ExpressionOperandId,
|
|
|
|
) -> CoverageKind {
|
|
|
|
CoverageKind::Expression { id: self.next_expression(), lhs, op, rhs }
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
fn inject_statement(&mut self, coverage_kind: CoverageKind, span: Span, block: BasicBlock) {
|
|
|
|
let code_region = make_code_region(self.tcx, &span);
|
|
|
|
debug!(" injecting statement {:?} covering {:?}", coverage_kind, code_region);
|
2020-06-22 06:29:08 +00:00
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
let data = &mut self.mir_body[block];
|
|
|
|
let source_info = data.terminator().source_info;
|
|
|
|
let statement = Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::Coverage(box Coverage { kind: coverage_kind, code_region }),
|
2020-08-02 03:03:59 +00:00
|
|
|
};
|
2020-08-15 11:42:13 +00:00
|
|
|
data.statements.push(statement);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
2020-06-08 02:35:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
/// Convert the Span into its file name, start line and column, and end line and column
|
|
|
|
fn make_code_region<'tcx>(tcx: TyCtxt<'tcx>, span: &Span) -> CodeRegion {
|
|
|
|
let source_map = tcx.sess.source_map();
|
|
|
|
let start = source_map.lookup_char_pos(span.lo());
|
|
|
|
let end = if span.hi() == span.lo() {
|
|
|
|
start.clone()
|
|
|
|
} else {
|
|
|
|
let end = source_map.lookup_char_pos(span.hi());
|
|
|
|
debug_assert_eq!(
|
|
|
|
start.file.name,
|
|
|
|
end.file.name,
|
|
|
|
"Region start ({:?} -> {:?}) and end ({:?} -> {:?}) don't come from the same source file!",
|
|
|
|
span.lo(),
|
|
|
|
start,
|
|
|
|
span.hi(),
|
|
|
|
end
|
|
|
|
);
|
|
|
|
end
|
|
|
|
};
|
|
|
|
match &start.file.name {
|
|
|
|
FileName::Real(RealFileName::Named(path)) => CodeRegion {
|
|
|
|
file_name: Symbol::intern(&path.to_string_lossy()),
|
|
|
|
start_line: start.line as u32,
|
|
|
|
start_col: start.col.to_u32() + 1,
|
|
|
|
end_line: end.line as u32,
|
|
|
|
end_col: end.col.to_u32() + 1,
|
|
|
|
},
|
|
|
|
_ => bug!("start.file.name should be a RealFileName, but it was: {:?}", start.file.name),
|
2020-06-08 02:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 20:29:43 +00:00
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx rustc_hir::Body<'tcx> {
|
2020-06-23 02:21:56 +00:00
|
|
|
let hir_node = tcx.hir().get_if_local(def_id).expect("DefId is local");
|
|
|
|
let fn_body_id = hir::map::associated_body(hir_node).expect("HIR node is a function with body");
|
2020-07-02 18:27:15 +00:00
|
|
|
tcx.hir().body(fn_body_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
|
2020-06-18 20:29:43 +00:00
|
|
|
let mut hcx = tcx.create_no_span_stable_hashing_context();
|
|
|
|
hash(&mut hcx, &hir_body.value).to_smaller_hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash(
|
|
|
|
hcx: &mut StableHashingContext<'tcx>,
|
|
|
|
node: &impl HashStable<StableHashingContext<'tcx>>,
|
|
|
|
) -> Fingerprint {
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
node.hash_stable(hcx, &mut stable_hasher);
|
|
|
|
stable_hasher.finish()
|
|
|
|
}
|