rustc_codegen_llvm/coverageinfo/mapgen/
unused.rs

1use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
2use rustc_data_structures::fx::FxHashSet;
3use rustc_hir::def_id::{DefId, LocalDefId};
4use rustc_middle::mir;
5use rustc_middle::mir::mono::MonoItemPartitions;
6use rustc_middle::ty::{self, TyCtxt};
7use rustc_span::def_id::DefIdSet;
8
9use crate::common::CodegenCx;
10use crate::coverageinfo::mapgen::covfun::{CovfunRecord, prepare_covfun_record};
11use crate::llvm;
12
13/// Each CGU will normally only emit coverage metadata for the functions that it actually generates.
14/// But since we don't want unused functions to disappear from coverage reports, we also scan for
15/// functions that were instrumented but are not participating in codegen.
16///
17/// These unused functions don't need to be codegenned, but we do need to add them to the function
18/// coverage map (in a single designated CGU) so that we still emit coverage mappings for them.
19/// We also end up adding their symbol names to a special global array that LLVM will include in
20/// its embedded coverage data.
21pub(crate) fn prepare_covfun_records_for_unused_functions<'tcx>(
22    cx: &CodegenCx<'_, 'tcx>,
23    covfun_records: &mut Vec<CovfunRecord<'tcx>>,
24) {
25    assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
26
27    let mut unused_instances = gather_unused_function_instances(cx);
28    // Sort the unused instances by symbol name, so that their order isn't hash-sensitive.
29    unused_instances.sort_by_key(|instance| instance.symbol_name);
30
31    // Try to create a covfun record for each unused function.
32    let mut name_globals = Vec::with_capacity(unused_instances.len());
33    covfun_records.extend(unused_instances.into_iter().filter_map(|unused| try {
34        let record = prepare_covfun_record(cx.tcx, unused.instance, false)?;
35        // If successful, also store its symbol name in a global constant.
36        name_globals.push(cx.const_str(unused.symbol_name.name).0);
37        record
38    }));
39
40    // Store the names of unused functions in a specially-named global array.
41    // LLVM's `InstrProfilling` pass will detect this array, and include the
42    // referenced names in its `__llvm_prf_names` section.
43    // (See `llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp`.)
44    if !name_globals.is_empty() {
45        let initializer = cx.const_array(cx.type_ptr(), &name_globals);
46
47        let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), c"__llvm_coverage_names");
48        llvm::set_global_constant(array, true);
49        llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
50        llvm::set_initializer(array, initializer);
51    }
52}
53
54/// Holds a dummy function instance along with its symbol name, to avoid having
55/// to repeatedly query for the name.
56struct UnusedInstance<'tcx> {
57    instance: ty::Instance<'tcx>,
58    symbol_name: ty::SymbolName<'tcx>,
59}
60
61fn gather_unused_function_instances<'tcx>(cx: &CodegenCx<'_, 'tcx>) -> Vec<UnusedInstance<'tcx>> {
62    assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
63
64    let tcx = cx.tcx;
65    let usage = prepare_usage_sets(tcx);
66
67    let is_unused_fn = |def_id: LocalDefId| -> bool {
68        // Usage sets expect `DefId`, so convert from `LocalDefId`.
69        let d: DefId = LocalDefId::to_def_id(def_id);
70        // To be potentially eligible for "unused function" mappings, a definition must:
71        // - Be eligible for coverage instrumentation
72        // - Not participate directly in codegen (or have lost all its coverage statements)
73        // - Not have any coverage statements inlined into codegenned functions
74        tcx.is_eligible_for_coverage(def_id)
75            && (!usage.all_mono_items.contains(&d) || usage.missing_own_coverage.contains(&d))
76            && !usage.used_via_inlining.contains(&d)
77    };
78
79    // FIXME(#79651): Consider trying to filter out dummy instantiations of
80    // unused generic functions from library crates, because they can produce
81    // "unused instantiation" in coverage reports even when they are actually
82    // used by some downstream crate in the same binary.
83
84    tcx.mir_keys(())
85        .iter()
86        .copied()
87        .filter(|&def_id| is_unused_fn(def_id))
88        .map(|def_id| make_dummy_instance(tcx, def_id))
89        .map(|instance| UnusedInstance { instance, symbol_name: tcx.symbol_name(instance) })
90        .collect::<Vec<_>>()
91}
92
93struct UsageSets<'tcx> {
94    all_mono_items: &'tcx DefIdSet,
95    used_via_inlining: FxHashSet<DefId>,
96    missing_own_coverage: FxHashSet<DefId>,
97}
98
99/// Prepare sets of definitions that are relevant to deciding whether something
100/// is an "unused function" for coverage purposes.
101fn prepare_usage_sets<'tcx>(tcx: TyCtxt<'tcx>) -> UsageSets<'tcx> {
102    let MonoItemPartitions { all_mono_items, codegen_units, .. } =
103        tcx.collect_and_partition_mono_items(());
104
105    // Obtain a MIR body for each function participating in codegen, via an
106    // arbitrary instance.
107    let mut def_ids_seen = FxHashSet::default();
108    let def_and_mir_for_all_mono_fns = codegen_units
109        .iter()
110        .flat_map(|cgu| cgu.items().keys())
111        .filter_map(|item| match item {
112            mir::mono::MonoItem::Fn(instance) => Some(instance),
113            mir::mono::MonoItem::Static(_) | mir::mono::MonoItem::GlobalAsm(_) => None,
114        })
115        // We only need one arbitrary instance per definition.
116        .filter(move |instance| def_ids_seen.insert(instance.def_id()))
117        .map(|instance| {
118            // We don't care about the instance, just its underlying MIR.
119            let body = tcx.instance_mir(instance.def);
120            (instance.def_id(), body)
121        });
122
123    // Functions whose coverage statements were found inlined into other functions.
124    let mut used_via_inlining = FxHashSet::default();
125    // Functions that were instrumented, but had all of their coverage statements
126    // removed by later MIR transforms (e.g. UnreachablePropagation).
127    let mut missing_own_coverage = FxHashSet::default();
128
129    for (def_id, body) in def_and_mir_for_all_mono_fns {
130        let mut saw_own_coverage = false;
131
132        // Inspect every coverage statement in the function's MIR.
133        for stmt in body
134            .basic_blocks
135            .iter()
136            .flat_map(|block| &block.statements)
137            .filter(|stmt| matches!(stmt.kind, mir::StatementKind::Coverage(_)))
138        {
139            if let Some(inlined) = stmt.source_info.scope.inlined_instance(&body.source_scopes) {
140                // This coverage statement was inlined from another function.
141                used_via_inlining.insert(inlined.def_id());
142            } else {
143                // Non-inlined coverage statements belong to the enclosing function.
144                saw_own_coverage = true;
145            }
146        }
147
148        if !saw_own_coverage && body.function_coverage_info.is_some() {
149            missing_own_coverage.insert(def_id);
150        }
151    }
152
153    UsageSets { all_mono_items, used_via_inlining, missing_own_coverage }
154}
155
156fn make_dummy_instance<'tcx>(tcx: TyCtxt<'tcx>, local_def_id: LocalDefId) -> ty::Instance<'tcx> {
157    let def_id = local_def_id.to_def_id();
158
159    // Make a dummy instance that fills in all generics with placeholders.
160    ty::Instance::new_raw(
161        def_id,
162        ty::GenericArgs::for_item(tcx, def_id, |param, _| {
163            if let ty::GenericParamDefKind::Lifetime = param.kind {
164                tcx.lifetimes.re_erased.into()
165            } else {
166                tcx.mk_param_from_def(param)
167            }
168        }),
169    )
170}