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