Skip to content

Commit 4199d77

Browse files
committed
coverage: Change query codegened_and_inlined_items to a plain function
This query has a name that sounds general-purpose, but in fact it has coverage-specific semantics, and (fortunately) is only used by coverage code. Because it is only ever called once (from one designated CGU), it doesn't need to be a query, and we can change it to a regular function instead.
1 parent b2c54cb commit 4199d77

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::bug;
1313
use rustc_middle::mir;
1414
use rustc_middle::mir::coverage::CodeRegion;
1515
use rustc_middle::ty::{self, TyCtxt};
16+
use rustc_span::def_id::DefIdSet;
1617
use rustc_span::Symbol;
1718

1819
/// Generates and exports the Coverage Map.
@@ -313,8 +314,7 @@ fn save_function_record(
313314
/// `-Clink-dead-code` will not generate code for unused generic functions.)
314315
///
315316
/// We can find the unused functions (including generic functions) by the set difference of all MIR
316-
/// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`tcx` query
317-
/// `codegened_and_inlined_items`).
317+
/// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`codegenned_and_inlined_items`).
318318
///
319319
/// These unused functions don't need to be codegenned, but we do need to add them to the function
320320
/// coverage map (in a single designated CGU) so that we still emit coverage mappings for them.
@@ -350,7 +350,7 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
350350
})
351351
.collect();
352352

353-
let codegenned_def_ids = tcx.codegened_and_inlined_items(());
353+
let codegenned_def_ids = codegenned_and_inlined_items(tcx);
354354

355355
// For each `DefId` that should have coverage instrumentation but wasn't
356356
// codegenned, add it to the function coverage map as an unused function.
@@ -368,6 +368,37 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
368368
}
369369
}
370370

371+
/// All items participating in code generation together with (instrumented)
372+
/// items inlined into them.
373+
fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
374+
let (items, cgus) = tcx.collect_and_partition_mono_items(());
375+
let mut visited = DefIdSet::default();
376+
let mut result = items.clone();
377+
378+
for cgu in cgus {
379+
for item in cgu.items().keys() {
380+
if let mir::mono::MonoItem::Fn(ref instance) = item {
381+
let did = instance.def_id();
382+
if !visited.insert(did) {
383+
continue;
384+
}
385+
let body = tcx.instance_mir(instance.def);
386+
for block in body.basic_blocks.iter() {
387+
for statement in &block.statements {
388+
let mir::StatementKind::Coverage(_) = statement.kind else { continue };
389+
let scope = statement.source_info.scope;
390+
if let Some(inlined) = scope.inlined_instance(&body.source_scopes) {
391+
result.insert(inlined.def_id());
392+
}
393+
}
394+
}
395+
}
396+
}
397+
}
398+
399+
result
400+
}
401+
371402
fn declare_unused_fn<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::Instance<'tcx> {
372403
ty::Instance::new(
373404
def_id,

compiler/rustc_middle/src/query/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1882,12 +1882,6 @@ rustc_queries! {
18821882
desc { |tcx| "determining whether `{}` needs codegen", tcx.def_path_str(def_id) }
18831883
}
18841884

1885-
/// All items participating in code generation together with items inlined into them.
1886-
query codegened_and_inlined_items(_: ()) -> &'tcx DefIdSet {
1887-
eval_always
1888-
desc { "collecting codegened and inlined items" }
1889-
}
1890-
18911885
query codegen_unit(sym: Symbol) -> &'tcx CodegenUnit<'tcx> {
18921886
desc { "getting codegen unit `{sym}`" }
18931887
}

compiler/rustc_monomorphize/src/partitioning.rs

-31
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
105105
use rustc_hir::definitions::DefPathDataName;
106106
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
107107
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
108-
use rustc_middle::mir;
109108
use rustc_middle::mir::mono::{
110109
CodegenUnit, CodegenUnitNameBuilder, InstantiationMode, Linkage, MonoItem, MonoItemData,
111110
Visibility,
@@ -1279,38 +1278,8 @@ fn dump_mono_items_stats<'tcx>(
12791278
Ok(())
12801279
}
12811280

1282-
fn codegened_and_inlined_items(tcx: TyCtxt<'_>, (): ()) -> &DefIdSet {
1283-
let (items, cgus) = tcx.collect_and_partition_mono_items(());
1284-
let mut visited = DefIdSet::default();
1285-
let mut result = items.clone();
1286-
1287-
for cgu in cgus {
1288-
for item in cgu.items().keys() {
1289-
if let MonoItem::Fn(ref instance) = item {
1290-
let did = instance.def_id();
1291-
if !visited.insert(did) {
1292-
continue;
1293-
}
1294-
let body = tcx.instance_mir(instance.def);
1295-
for block in body.basic_blocks.iter() {
1296-
for statement in &block.statements {
1297-
let mir::StatementKind::Coverage(_) = statement.kind else { continue };
1298-
let scope = statement.source_info.scope;
1299-
if let Some(inlined) = scope.inlined_instance(&body.source_scopes) {
1300-
result.insert(inlined.def_id());
1301-
}
1302-
}
1303-
}
1304-
}
1305-
}
1306-
}
1307-
1308-
tcx.arena.alloc(result)
1309-
}
1310-
13111281
pub fn provide(providers: &mut Providers) {
13121282
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
1313-
providers.codegened_and_inlined_items = codegened_and_inlined_items;
13141283

13151284
providers.is_codegened_item = |tcx, def_id| {
13161285
let (all_mono_items, _) = tcx.collect_and_partition_mono_items(());

0 commit comments

Comments
 (0)