rustc_mir_transform/coverage/
mod.rs

1use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo};
2use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind};
3use rustc_middle::ty::TyCtxt;
4use tracing::{debug, debug_span, trace};
5
6use crate::coverage::counters::BcbCountersData;
7use crate::coverage::graph::CoverageGraph;
8use crate::coverage::mappings::ExtractedMappings;
9
10mod counters;
11mod expansion;
12mod graph;
13mod hir_info;
14mod mappings;
15pub(super) mod query;
16mod spans;
17#[cfg(test)]
18mod tests;
19mod unexpand;
20
21/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
22/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
23/// to construct the coverage map.
24pub(super) struct InstrumentCoverage;
25
26impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
27    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
28        sess.instrument_coverage()
29    }
30
31    fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
32        let mir_source = mir_body.source;
33
34        // This pass runs after MIR promotion, but before promoted MIR starts to
35        // be transformed, so it should never see promoted MIR.
36        assert!(mir_source.promoted.is_none());
37
38        let def_id = mir_source.def_id().expect_local();
39
40        if !tcx.is_eligible_for_coverage(def_id) {
41            trace!("InstrumentCoverage skipped for {def_id:?} (not eligible)");
42            return;
43        }
44
45        // An otherwise-eligible function is still skipped if its start block
46        // is known to be unreachable.
47        match mir_body.basic_blocks[mir::START_BLOCK].terminator().kind {
48            TerminatorKind::Unreachable => {
49                trace!("InstrumentCoverage skipped for unreachable `START_BLOCK`");
50                return;
51            }
52            _ => {}
53        }
54
55        instrument_function_for_coverage(tcx, mir_body);
56    }
57
58    fn is_required(&self) -> bool {
59        false
60    }
61}
62
63fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
64    let def_id = mir_body.source.def_id();
65    let _span = debug_span!("instrument_function_for_coverage", ?def_id).entered();
66
67    let hir_info = hir_info::extract_hir_info(tcx, def_id.expect_local());
68
69    // Build the coverage graph, which is a simplified view of the MIR control-flow
70    // graph that ignores some details not relevant to coverage instrumentation.
71    let graph = CoverageGraph::from_mir(mir_body);
72
73    ////////////////////////////////////////////////////
74    // Extract coverage spans and other mapping info from MIR.
75    let ExtractedMappings { mappings } =
76        mappings::extract_mappings_from_mir(tcx, mir_body, &hir_info, &graph);
77    if mappings.is_empty() {
78        // No spans could be converted into valid mappings, so skip this function.
79        debug!("no spans could be converted into valid mappings; skipping");
80        return;
81    }
82
83    // Use the coverage graph to prepare intermediate data that will eventually
84    // be used to assign physical counters and counter expressions to points in
85    // the control-flow graph.
86    let BcbCountersData { node_flow_data, priority_list } =
87        counters::prepare_bcb_counters_data(&graph);
88
89    // Inject coverage statements into MIR.
90    inject_coverage_statements(mir_body, &graph);
91
92    mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
93        function_source_hash: hir_info.function_source_hash,
94
95        node_flow_data,
96        priority_list,
97
98        mappings,
99    }));
100}
101
102/// Inject any necessary coverage statements into MIR, so that they influence codegen.
103fn inject_coverage_statements<'tcx>(mir_body: &mut mir::Body<'tcx>, graph: &CoverageGraph) {
104    for (bcb, data) in graph.iter_enumerated() {
105        let target_bb = data.leader_bb();
106        inject_statement(mir_body, CoverageKind::VirtualCounter { bcb }, target_bb);
107    }
108}
109
110fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb: BasicBlock) {
111    debug!("  injecting statement {counter_kind:?} for {bb:?}");
112    let data = &mut mir_body[bb];
113    let source_info = data.terminator().source_info;
114    let statement = Statement::new(source_info, StatementKind::Coverage(counter_kind));
115    data.statements.insert(0, statement);
116}