Skip to content

Commit e3f66b2

Browse files
committed
coverage: Overhaul the search for unused functions
1 parent 5ddc4f2 commit e3f66b2

File tree

1 file changed

+68
-62
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+68
-62
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+68-62
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use crate::llvm;
66

77
use itertools::Itertools as _;
88
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
9-
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
10-
use rustc_hir::def::DefKind;
11-
use rustc_hir::def_id::DefId;
9+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
10+
use rustc_hir::def_id::{DefId, LocalDefId};
1211
use rustc_index::IndexVec;
1312
use rustc_middle::bug;
1413
use rustc_middle::mir;
@@ -335,16 +334,9 @@ fn save_function_record(
335334
);
336335
}
337336

338-
/// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for
339-
/// the functions that went through codegen; such as public functions and "used" functions
340-
/// (functions referenced by other "used" or public items). Any other functions considered unused,
341-
/// or "Unreachable", were still parsed and processed through the MIR stage, but were not
342-
/// codegenned. (Note that `-Clink-dead-code` can force some unused code to be codegenned, but
343-
/// that flag is known to cause other errors, when combined with `-C instrument-coverage`; and
344-
/// `-Clink-dead-code` will not generate code for unused generic functions.)
345-
///
346-
/// We can find the unused functions (including generic functions) by the set difference of all MIR
347-
/// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`codegenned_and_inlined_items`).
337+
/// Each CGU will normally only emit coverage metadata for the functions that it actually generates.
338+
/// But since we don't want unused functions to disappear from coverage reports, we also scan for
339+
/// functions that were instrumented but are not participating in codegen.
348340
///
349341
/// These unused functions don't need to be codegenned, but we do need to add them to the function
350342
/// coverage map (in a single designated CGU) so that we still emit coverage mappings for them.
@@ -354,78 +346,92 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
354346
assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
355347

356348
let tcx = cx.tcx;
349+
let usage = prepare_usage_sets(tcx);
350+
351+
let is_unused_fn = |def_id: LocalDefId| -> bool {
352+
let def_id = def_id.to_def_id();
353+
354+
// To be eligible for "unused function" mappings, a definition must:
355+
// - Be function-like
356+
// - Not participate directly in codegen
357+
// - Not have any coverage statements inlined into codegenned functions
358+
tcx.def_kind(def_id).is_fn_like()
359+
&& !usage.all_mono_items.contains(&def_id)
360+
&& !usage.used_via_inlining.contains(&def_id)
361+
};
357362

358-
let eligible_def_ids = tcx.mir_keys(()).iter().filter_map(|local_def_id| {
359-
let def_id = local_def_id.to_def_id();
360-
let kind = tcx.def_kind(def_id);
361-
// `mir_keys` will give us `DefId`s for all kinds of things, not
362-
// just "functions", like consts, statics, etc. Filter those out.
363-
if !matches!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Closure) {
364-
return None;
365-
}
363+
// Scan for unused functions that were instrumented for coverage.
364+
for def_id in tcx.mir_keys(()).iter().copied().filter(|&def_id| is_unused_fn(def_id)) {
365+
// Get the coverage info from MIR, skipping functions that were never instrumented.
366+
let body = tcx.optimized_mir(def_id);
367+
let Some(function_coverage_info) = body.function_coverage_info.as_deref() else { continue };
366368

367369
// FIXME(79651): Consider trying to filter out dummy instantiations of
368370
// unused generic functions from library crates, because they can produce
369371
// "unused instantiation" in coverage reports even when they are actually
370372
// used by some downstream crate in the same binary.
371373

372-
Some(local_def_id.to_def_id())
373-
});
374-
375-
let codegenned_def_ids = codegenned_and_inlined_items(tcx);
376-
377-
// For each `DefId` that should have coverage instrumentation but wasn't
378-
// codegenned, add it to the function coverage map as an unused function.
379-
for def_id in eligible_def_ids.filter(|id| !codegenned_def_ids.contains(id)) {
380-
// Skip any function that didn't have coverage data added to it by the
381-
// coverage instrumentor.
382-
let body = tcx.instance_mir(ty::InstanceDef::Item(def_id));
383-
let Some(function_coverage_info) = body.function_coverage_info.as_deref() else {
384-
continue;
385-
};
386-
387374
debug!("generating unused fn: {def_id:?}");
388375
add_unused_function_coverage(cx, def_id, function_coverage_info);
389376
}
390377
}
391378

392-
/// All items participating in code generation together with (instrumented)
393-
/// items inlined into them.
394-
fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
395-
let (items, cgus) = tcx.collect_and_partition_mono_items(());
396-
let mut visited = DefIdSet::default();
397-
let mut result = items.clone();
398-
399-
for cgu in cgus {
400-
for item in cgu.items().keys() {
401-
if let mir::mono::MonoItem::Fn(ref instance) = item {
402-
let did = instance.def_id();
403-
if !visited.insert(did) {
404-
continue;
405-
}
406-
let body = tcx.instance_mir(instance.def);
407-
for block in body.basic_blocks.iter() {
408-
for statement in &block.statements {
409-
let mir::StatementKind::Coverage(_) = statement.kind else { continue };
410-
let scope = statement.source_info.scope;
411-
if let Some(inlined) = scope.inlined_instance(&body.source_scopes) {
412-
result.insert(inlined.def_id());
413-
}
414-
}
415-
}
379+
struct UsageSets<'tcx> {
380+
all_mono_items: &'tcx DefIdSet,
381+
used_via_inlining: FxHashSet<DefId>,
382+
}
383+
384+
/// Prepare sets of definitions that are relevant to deciding whether something
385+
/// is an "unused function" for coverage purposes.
386+
fn prepare_usage_sets<'tcx>(tcx: TyCtxt<'tcx>) -> UsageSets<'tcx> {
387+
let (all_mono_items, cgus) = tcx.collect_and_partition_mono_items(());
388+
389+
// Obtain a MIR body for each function participating in codegen, via an
390+
// arbitrary instance.
391+
let mut def_ids_seen = FxHashSet::default();
392+
let def_and_mir_for_all_mono_fns = cgus
393+
.iter()
394+
.flat_map(|cgu| cgu.items().keys())
395+
.filter_map(|item| match item {
396+
mir::mono::MonoItem::Fn(instance) => Some(instance),
397+
mir::mono::MonoItem::Static(_) | mir::mono::MonoItem::GlobalAsm(_) => None,
398+
})
399+
// We only need one arbitrary instance per definition.
400+
.filter(move |instance| def_ids_seen.insert(instance.def_id()))
401+
.map(|instance| {
402+
// We don't care about the instance, just its underlying MIR.
403+
let body = tcx.instance_mir(instance.def);
404+
(instance.def_id(), body)
405+
});
406+
407+
// Functions whose coverage statments were found inlined into other functions.
408+
let mut used_via_inlining = FxHashSet::default();
409+
410+
for (_def_id, body) in def_and_mir_for_all_mono_fns {
411+
// Inspect every coverage statement in the function's MIR.
412+
for stmt in body
413+
.basic_blocks
414+
.iter()
415+
.flat_map(|block| &block.statements)
416+
.filter(|stmt| matches!(stmt.kind, mir::StatementKind::Coverage(_)))
417+
{
418+
if let Some(inlined) = stmt.source_info.scope.inlined_instance(&body.source_scopes) {
419+
// This coverage statement was inlined from another function.
420+
used_via_inlining.insert(inlined.def_id());
416421
}
417422
}
418423
}
419424

420-
result
425+
UsageSets { all_mono_items, used_via_inlining }
421426
}
422427

423428
fn add_unused_function_coverage<'tcx>(
424429
cx: &CodegenCx<'_, 'tcx>,
425-
def_id: DefId,
430+
def_id: LocalDefId,
426431
function_coverage_info: &'tcx mir::coverage::FunctionCoverageInfo,
427432
) {
428433
let tcx = cx.tcx;
434+
let def_id = def_id.to_def_id();
429435

430436
// Make a dummy instance that fills in all generics with placeholders.
431437
let instance = ty::Instance::new(

0 commit comments

Comments
 (0)