Skip to content

Commit b2c54cb

Browse files
committed
coverage: Move unused-function helpers closer to where they are used
1 parent b8688f9 commit b2c54cb

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::DefId;
1111
use rustc_index::IndexVec;
1212
use rustc_middle::bug;
13+
use rustc_middle::mir;
1314
use rustc_middle::mir::coverage::CodeRegion;
1415
use rustc_middle::ty::{self, TyCtxt};
1516
use rustc_span::Symbol;
@@ -351,17 +352,47 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
351352

352353
let codegenned_def_ids = tcx.codegened_and_inlined_items(());
353354

354-
for non_codegenned_def_id in
355-
eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id))
356-
{
355+
// For each `DefId` that should have coverage instrumentation but wasn't
356+
// codegenned, add it to the function coverage map as an unused function.
357+
for def_id in eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id)) {
357358
// Skip any function that didn't have coverage data added to it by the
358359
// coverage instrumentor.
359-
let body = tcx.instance_mir(ty::InstanceDef::Item(non_codegenned_def_id));
360+
let body = tcx.instance_mir(ty::InstanceDef::Item(def_id));
360361
let Some(function_coverage_info) = body.function_coverage_info.as_deref() else {
361362
continue;
362363
};
363364

364-
debug!("generating unused fn: {:?}", non_codegenned_def_id);
365-
cx.define_unused_fn(non_codegenned_def_id, function_coverage_info);
365+
debug!("generating unused fn: {def_id:?}");
366+
let instance = declare_unused_fn(tcx, def_id);
367+
add_unused_function_coverage(cx, instance, function_coverage_info);
368+
}
369+
}
370+
371+
fn declare_unused_fn<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::Instance<'tcx> {
372+
ty::Instance::new(
373+
def_id,
374+
ty::GenericArgs::for_item(tcx, def_id, |param, _| {
375+
if let ty::GenericParamDefKind::Lifetime = param.kind {
376+
tcx.lifetimes.re_erased.into()
377+
} else {
378+
tcx.mk_param_from_def(param)
379+
}
380+
}),
381+
)
382+
}
383+
384+
fn add_unused_function_coverage<'tcx>(
385+
cx: &CodegenCx<'_, 'tcx>,
386+
instance: ty::Instance<'tcx>,
387+
function_coverage_info: &'tcx mir::coverage::FunctionCoverageInfo,
388+
) {
389+
// An unused function's mappings will automatically be rewritten to map to
390+
// zero, because none of its counters/expressions are marked as seen.
391+
let function_coverage = FunctionCoverage::unused(instance, function_coverage_info);
392+
393+
if let Some(coverage_context) = cx.coverage_context() {
394+
coverage_context.function_coverage_map.borrow_mut().insert(instance, function_coverage);
395+
} else {
396+
bug!("Could not get the `coverage_context`");
366397
}
367398
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+2-37
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ use rustc_codegen_ssa::traits::{
1111
StaticMethods,
1212
};
1313
use rustc_data_structures::fx::FxHashMap;
14-
use rustc_hir::def_id::DefId;
1514
use rustc_llvm::RustString;
1615
use rustc_middle::bug;
17-
use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo};
16+
use rustc_middle::mir::coverage::CoverageKind;
1817
use rustc_middle::mir::Coverage;
1918
use rustc_middle::ty::layout::HasTyCtxt;
20-
use rustc_middle::ty::{self, GenericArgs, Instance, TyCtxt};
19+
use rustc_middle::ty::Instance;
2120

2221
use std::cell::RefCell;
2322

@@ -69,11 +68,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
6968
bug!("Could not get the `coverage_context`");
7069
}
7170
}
72-
73-
fn define_unused_fn(&self, def_id: DefId, function_coverage_info: &'tcx FunctionCoverageInfo) {
74-
let instance = declare_unused_fn(self.tcx, def_id);
75-
add_unused_function_coverage(self, instance, function_coverage_info);
76-
}
7771
}
7872

7973
impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
@@ -138,35 +132,6 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
138132
}
139133
}
140134

141-
fn declare_unused_fn<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
142-
Instance::new(
143-
def_id,
144-
GenericArgs::for_item(tcx, def_id, |param, _| {
145-
if let ty::GenericParamDefKind::Lifetime = param.kind {
146-
tcx.lifetimes.re_erased.into()
147-
} else {
148-
tcx.mk_param_from_def(param)
149-
}
150-
}),
151-
)
152-
}
153-
154-
fn add_unused_function_coverage<'tcx>(
155-
cx: &CodegenCx<'_, 'tcx>,
156-
instance: Instance<'tcx>,
157-
function_coverage_info: &'tcx FunctionCoverageInfo,
158-
) {
159-
// An unused function's mappings will automatically be rewritten to map to
160-
// zero, because none of its counters/expressions are marked as seen.
161-
let function_coverage = FunctionCoverage::unused(instance, function_coverage_info);
162-
163-
if let Some(coverage_context) = cx.coverage_context() {
164-
coverage_context.function_coverage_map.borrow_mut().insert(instance, function_coverage);
165-
} else {
166-
bug!("Could not get the `coverage_context`");
167-
}
168-
}
169-
170135
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
171136
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
172137
/// containing the function name, with the specific variable name and linkage

0 commit comments

Comments
 (0)