Skip to content

Commit 5a9538a

Browse files
committed
Functions inlined into reachable functions are reachable
Consider functions to be reachable for code coverage purposes, either when they reach the code generation directly, or indirectly as inlined part of another function.
1 parent 1796cc0 commit 5a9538a

File tree

3 files changed

+34
-1
lines changed
  • compiler
    • rustc_codegen_llvm/src/coverageinfo
    • rustc_middle/src/query
    • rustc_mir/src/monomorphize/partitioning

3 files changed

+34
-1
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn add_unreachable_coverage<'tcx>(
284284
let all_def_ids: DefIdSet =
285285
tcx.mir_keys(LOCAL_CRATE).iter().map(|local_def_id| local_def_id.to_def_id()).collect();
286286

287-
let (codegenned_def_ids, _) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
287+
let codegenned_def_ids = tcx.codegened_and_inlined_items(LOCAL_CRATE);
288288

289289
let mut unreachable_def_ids_by_file: FxHashMap<Symbol, Vec<DefId>> = FxHashMap::default();
290290
for &non_codegenned_def_id in all_def_ids.difference(codegenned_def_ids) {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,14 @@ rustc_queries! {
13971397
query is_codegened_item(def_id: DefId) -> bool {
13981398
desc { |tcx| "determining whether `{}` needs codegen", tcx.def_path_str(def_id) }
13991399
}
1400+
1401+
/// All items participating in code generation together with items inlined into them.
1402+
query codegened_and_inlined_items(_: CrateNum)
1403+
-> &'tcx DefIdSet {
1404+
eval_always
1405+
desc { "codegened_and_inlined_items" }
1406+
}
1407+
14001408
query codegen_unit(_: Symbol) -> &'tcx CodegenUnit<'tcx> {
14011409
desc { "codegen_unit" }
14021410
}

compiler/rustc_mir/src/monomorphize/partitioning/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,33 @@ fn collect_and_partition_mono_items<'tcx>(
424424
(tcx.arena.alloc(mono_items), codegen_units)
425425
}
426426

427+
fn codegened_and_inlined_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx DefIdSet {
428+
let (items, cgus) = tcx.collect_and_partition_mono_items(cnum);
429+
let mut visited = DefIdSet::default();
430+
let mut result = items.clone();
431+
432+
for cgu in cgus {
433+
for (item, _) in cgu.items() {
434+
if let MonoItem::Fn(ref instance) = item {
435+
let did = instance.def_id();
436+
if !visited.insert(did) {
437+
continue;
438+
}
439+
for scope in &tcx.instance_mir(instance.def).source_scopes {
440+
if let Some((ref inlined, _)) = scope.inlined {
441+
result.insert(inlined.def_id());
442+
}
443+
}
444+
}
445+
}
446+
}
447+
448+
tcx.arena.alloc(result)
449+
}
450+
427451
pub fn provide(providers: &mut Providers) {
428452
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
453+
providers.codegened_and_inlined_items = codegened_and_inlined_items;
429454

430455
providers.is_codegened_item = |tcx, def_id| {
431456
let (all_mono_items, _) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);

0 commit comments

Comments
 (0)