mod.rs - source (original) (raw)

rustc_mir_transform/coverage/

mod.rs

1mod counters;
2mod graph;
3mod mappings;
4pub(super) mod query;
5mod spans;
6#[cfg(test)]
7mod tests;
8mod unexpand;
9
10use rustc_hir as hir;
11use rustc_hir::intravisit::{Visitor, walk_expr};
12use rustc_middle::hir::nested_filter;
13use rustc_middle::mir::coverage::{
14    CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind,
15};
16use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind};
17use rustc_middle::ty::TyCtxt;
18use rustc_span::Span;
19use rustc_span::def_id::LocalDefId;
20use tracing::{debug, debug_span, trace};
21
22use crate::coverage::counters::BcbCountersData;
23use crate::coverage::graph::CoverageGraph;
24use crate::coverage::mappings::ExtractedMappings;
25
26/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
27/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
28/// to construct the coverage map.
29pub(super) struct InstrumentCoverage;
30
31impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
32    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
33        sess.instrument_coverage()
34    }
35
36    fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
37        let mir_source = mir_body.source;
38
39        // This pass runs after MIR promotion, but before promoted MIR starts to
40        // be transformed, so it should never see promoted MIR.
41        assert!(mir_source.promoted.is_none());
42
43        let def_id = mir_source.def_id().expect_local();
44
45        if !tcx.is_eligible_for_coverage(def_id) {
46            trace!("InstrumentCoverage skipped for {def_id:?} (not eligible)");
47            return;
48        }
49
50        // An otherwise-eligible function is still skipped if its start block
51        // is known to be unreachable.
52        match mir_body.basic_blocks[mir::START_BLOCK].terminator().kind {
53            TerminatorKind::Unreachable => {
54                trace!("InstrumentCoverage skipped for unreachable `START_BLOCK`");
55                return;
56            }
57            _ => {}
58        }
59
60        instrument_function_for_coverage(tcx, mir_body);
61    }
62
63    fn is_required(&self) -> bool {
64        false
65    }
66}
67
68fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
69    let def_id = mir_body.source.def_id();
70    let _span = debug_span!("instrument_function_for_coverage", ?def_id).entered();
71
72    let hir_info = extract_hir_info(tcx, def_id.expect_local());
73
74    // Build the coverage graph, which is a simplified view of the MIR control-flow
75    // graph that ignores some details not relevant to coverage instrumentation.
76    let graph = CoverageGraph::from_mir(mir_body);
77
78    ////////////////////////////////////////////////////
79    // Extract coverage spans and other mapping info from MIR.
80    let extracted_mappings =
81        mappings::extract_all_mapping_info_from_mir(tcx, mir_body, &hir_info, &graph);
82
83    let mappings = create_mappings(&extracted_mappings);
84    if mappings.is_empty() {
85        // No spans could be converted into valid mappings, so skip this function.
86        debug!("no spans could be converted into valid mappings; skipping");
87        return;
88    }
89
90    // Use the coverage graph to prepare intermediate data that will eventually
91    // be used to assign physical counters and counter expressions to points in
92    // the control-flow graph.
93    let BcbCountersData { node_flow_data, priority_list } =
94        counters::prepare_bcb_counters_data(&graph);
95
96    // Inject coverage statements into MIR.
97    inject_coverage_statements(mir_body, &graph);
98    inject_mcdc_statements(mir_body, &graph, &extracted_mappings);
99
100    let mcdc_num_condition_bitmaps = extracted_mappings
101        .mcdc_mappings
102        .iter()
103        .map(|&(mappings::MCDCDecision { decision_depth, .. }, _)| decision_depth)
104        .max()
105        .map_or(0, |max| usize::from(max) + 1);
106
107    mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
108        function_source_hash: hir_info.function_source_hash,
109
110        node_flow_data,
111        priority_list,
112
113        mappings,
114
115        mcdc_bitmap_bits: extracted_mappings.mcdc_bitmap_bits,
116        mcdc_num_condition_bitmaps,
117    }));
118}
119
120/// For each coverage span extracted from MIR, create a corresponding mapping.
121///
122/// FIXME(Zalathar): This used to be where BCBs in the extracted mappings were
123/// resolved to a `CovTerm`. But that is now handled elsewhere, so this
124/// function can potentially be simplified even further.
125fn create_mappings(extracted_mappings: &ExtractedMappings) -> Vec<Mapping> {
126    // Fully destructure the mappings struct to make sure we don't miss any kinds.
127    let ExtractedMappings {
128        code_mappings,
129        branch_pairs,
130        mcdc_bitmap_bits: _,
131        mcdc_degraded_branches,
132        mcdc_mappings,
133    } = extracted_mappings;
134    let mut mappings = Vec::new();
135
136    mappings.extend(code_mappings.iter().map(
137        // Ordinary code mappings are the simplest kind.
138        |&mappings::CodeMapping { span, bcb }| {
139            let kind = MappingKind::Code { bcb };
140            Mapping { kind, span }
141        },
142    ));
143
144    mappings.extend(branch_pairs.iter().map(
145        |&mappings::BranchPair { span, true_bcb, false_bcb }| {
146            let kind = MappingKind::Branch { true_bcb, false_bcb };
147            Mapping { kind, span }
148        },
149    ));
150
151    // MCDC branch mappings are appended with their decisions in case decisions were ignored.
152    mappings.extend(mcdc_degraded_branches.iter().map(
153        |&mappings::MCDCBranch {
154             span,
155             true_bcb,
156             false_bcb,
157             condition_info: _,
158             true_index: _,
159             false_index: _,
160         }| { Mapping { kind: MappingKind::Branch { true_bcb, false_bcb }, span } },
161    ));
162
163    for (decision, branches) in mcdc_mappings {
164        // FIXME(#134497): Previously it was possible for some of these branch
165        // conversions to fail, in which case the remaining branches in the
166        // decision would be degraded to plain `MappingKind::Branch`.
167        // The changes in #134497 made that failure impossible, because the
168        // fallible step was deferred to codegen. But the corresponding code
169        // in codegen wasn't updated to detect the need for a degrade step.
170        let conditions = branches
171            .into_iter()
172            .map(
173                |&mappings::MCDCBranch {
174                     span,
175                     true_bcb,
176                     false_bcb,
177                     condition_info,
178                     true_index: _,
179                     false_index: _,
180                 }| {
181                    Mapping {
182                        kind: MappingKind::MCDCBranch {
183                            true_bcb,
184                            false_bcb,
185                            mcdc_params: condition_info,
186                        },
187                        span,
188                    }
189                },
190            )
191            .collect::<Vec<_>>();
192
193        // LLVM requires end index for counter mapping regions.
194        let kind = MappingKind::MCDCDecision(DecisionInfo {
195            bitmap_idx: (decision.bitmap_idx + decision.num_test_vectors) as u32,
196            num_conditions: u16::try_from(conditions.len()).unwrap(),
197        });
198        let span = decision.span;
199        mappings.extend(std::iter::once(Mapping { kind, span }).chain(conditions.into_iter()));
200    }
201
202    mappings
203}
204
205/// Inject any necessary coverage statements into MIR, so that they influence codegen.
206fn inject_coverage_statements<'tcx>(mir_body: &mut mir::Body<'tcx>, graph: &CoverageGraph) {
207    for (bcb, data) in graph.iter_enumerated() {
208        let target_bb = data.leader_bb();
209        inject_statement(mir_body, CoverageKind::VirtualCounter { bcb }, target_bb);
210    }
211}
212
213/// For each conditions inject statements to update condition bitmap after it has been evaluated.
214/// For each decision inject statements to update test vector bitmap after it has been evaluated.
215fn inject_mcdc_statements<'tcx>(
216    mir_body: &mut mir::Body<'tcx>,
217    graph: &CoverageGraph,
218    extracted_mappings: &ExtractedMappings,
219) {
220    for (decision, conditions) in &extracted_mappings.mcdc_mappings {
221        // Inject test vector update first because `inject_statement` always insert new statement at head.
222        for &end in &decision.end_bcbs {
223            let end_bb = graph[end].leader_bb();
224            inject_statement(
225                mir_body,
226                CoverageKind::TestVectorBitmapUpdate {
227                    bitmap_idx: decision.bitmap_idx as u32,
228                    decision_depth: decision.decision_depth,
229                },
230                end_bb,
231            );
232        }
233
234        for &mappings::MCDCBranch {
235            span: _,
236            true_bcb,
237            false_bcb,
238            condition_info: _,
239            true_index,
240            false_index,
241        } in conditions
242        {
243            for (index, bcb) in [(false_index, false_bcb), (true_index, true_bcb)] {
244                let bb = graph[bcb].leader_bb();
245                inject_statement(
246                    mir_body,
247                    CoverageKind::CondBitmapUpdate {
248                        index: index as u32,
249                        decision_depth: decision.decision_depth,
250                    },
251                    bb,
252                );
253            }
254        }
255    }
256}
257
258fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb: BasicBlock) {
259    debug!("  injecting statement {counter_kind:?} for {bb:?}");
260    let data = &mut mir_body[bb];
261    let source_info = data.terminator().source_info;
262    let statement = Statement { source_info, kind: StatementKind::Coverage(counter_kind) };
263    data.statements.insert(0, statement);
264}
265
266/// Function information extracted from HIR by the coverage instrumentor.
267#[derive(Debug)]
268struct ExtractedHirInfo {
269    function_source_hash: u64,
270    is_async_fn: bool,
271    /// The span of the function's signature, if available.
272    /// Must have the same context and filename as the body span.
273    fn_sig_span: Option<Span>,
274    body_span: Span,
275    /// "Holes" are regions within the function body (or its expansions) that
276    /// should not be included in coverage spans for this function
277    /// (e.g. closures and nested items).
278    hole_spans: Vec<Span>,
279}
280
281fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo {
282    // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
283    // to HIR for it.
284
285    // HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body.
286    if tcx.is_synthetic_mir(def_id) {
287        return extract_hir_info(tcx, tcx.local_parent(def_id));
288    }
289
290    let hir_node = tcx.hir_node_by_def_id(def_id);
291    let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
292    let hir_body = tcx.hir_body(fn_body_id);
293
294    let maybe_fn_sig = hir_node.fn_sig();
295    let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async());
296
297    let mut body_span = hir_body.value.span;
298
299    use hir::{Closure, Expr, ExprKind, Node};
300    // Unexpand a closure's body span back to the context of its declaration.
301    // This helps with closure bodies that consist of just a single bang-macro,
302    // and also with closure bodies produced by async desugaring.
303    if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) =
304        hir_node
305    {
306        body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span);
307    }
308
309    // The actual signature span is only used if it has the same context and
310    // filename as the body, and precedes the body.
311    let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
312        let source_map = tcx.sess.source_map();
313        let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
314
315        fn_sig_span.eq_ctxt(body_span)
316            && fn_sig_span.hi() <= body_span.lo()
317            && file_idx(fn_sig_span) == file_idx(body_span)
318    });
319
320    let function_source_hash = hash_mir_source(tcx, hir_body);
321
322    let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
323
324    ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
325}
326
327fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
328    // FIXME(cjgillot) Stop hashing HIR manually here.
329    let owner = hir_body.id().hir_id.owner;
330    tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
331}
332
333fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
334    struct HolesVisitor<'tcx> {
335        tcx: TyCtxt<'tcx>,
336        hole_spans: Vec<Span>,
337    }
338
339    impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> {
340        /// We have special handling for nested items, but we still want to
341        /// traverse into nested bodies of things that are not considered items,
342        /// such as "anon consts" (e.g. array lengths).
343        type NestedFilter = nested_filter::OnlyBodies;
344
345        fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
346            self.tcx
347        }
348
349        /// We override `visit_nested_item` instead of `visit_item` because we
350        /// only need the item's span, not the item itself.
351        fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
352            let span = self.tcx.def_span(id.owner_id.def_id);
353            self.visit_hole_span(span);
354            // Having visited this item, we don't care about its children,
355            // so don't call `walk_item`.
356        }
357
358        // We override `visit_expr` instead of the more specific expression
359        // visitors, so that we have direct access to the expression span.
360        fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
361            match expr.kind {
362                hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => {
363                    self.visit_hole_span(expr.span);
364                    // Having visited this expression, we don't care about its
365                    // children, so don't call `walk_expr`.
366                }
367
368                // For other expressions, recursively visit as normal.
369                _ => walk_expr(self, expr),
370            }
371        }
372    }
373    impl HolesVisitor<'_> {
374        fn visit_hole_span(&mut self, hole_span: Span) {
375            self.hole_spans.push(hole_span);
376        }
377    }
378
379    let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
380
381    visitor.visit_body(hir_body);
382    visitor.hole_spans
383}