[code coverage] Fix missing dead code in modules that are never called

The issue here is that the logic used to determine which CGU to put the
dead function stubs in doesn't handle cases where a module is never
assigned to a CGU.

The partitioning logic also caused issues in #85461 where inline
functions were duplicated into multiple CGUs resulting in duplicate
symbols.

This commit fixes the issue by removing the complex logic used to assign
dead code stubs to CGUs and replaces it with a much simplier model: we
pick one CGU to hold all the dead code stubs. We pick a CGU which has
exported items which increases the likelihood the linker won't throw
away our dead functions and we pick the smallest to minimize the impact
on compilation times for crates with very large CGUs.

Fixes #86177
Fixes #85718
Fixes #79622
This commit is contained in:
Wesley Wiser 2021-12-20 16:36:56 -05:00
parent 341d65d975
commit ef57f249a2
8 changed files with 76 additions and 121 deletions

View file

@ -201,6 +201,24 @@ pub fn partition<'tcx>(
partitioner.internalize_symbols(cx, &mut post_inlining);
}
let instrument_dead_code =
tcx.sess.instrument_coverage() && !tcx.sess.instrument_coverage_except_unused_functions();
if instrument_dead_code {
// Find the smallest CGU that has exported symbols and put the dead
// function stubs in that CGU. We look for exported symbols to increase
// the likelyhood the linker won't throw away the dead functions.
let mut cgus_with_exported_symbols: Vec<_> = post_inlining
.codegen_units
.iter_mut()
.filter(|cgu| cgu.items().iter().any(|(_, (linkage, _))| *linkage == Linkage::External))
.collect();
cgus_with_exported_symbols.sort_by_key(|cgu| cgu.size_estimate());
let dead_code_cgu = cgus_with_exported_symbols.last_mut().unwrap();
dead_code_cgu.make_code_coverage_dead_code_cgu();
}
// Finally, sort by codegen unit name, so that we get deterministic results.
let PostInliningPartitioning {
codegen_units: mut result,