Skip to content

Commit dbc37a9

Browse files
committed
Auto merge of #83307 - richkadel:cov-unused-functions-1.1, r=tmandry
coverage bug fixes and optimization support Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to address multiple, somewhat related issues. Fixed a significant flaw in prior coverage solution: Every counter generated a new counter variable, but there should have only been one counter variable per function. This appears to have bloated .profraw files significantly. (For a small program, it increased the size by about 40%. I have not tested large programs, but there is anecdotal evidence that profraw files were way too large. This is a good fix, regardless, but hopefully it also addresses related issues. Fixes: #82144 Invalid LLVM coverage data produced when compiled with -C opt-level=1 Existing tests now work up to at least `opt-level=3`. This required a detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR when compiled with coverage, and a lot of trial and error with codegen adjustments. The biggest hurdle was figuring out how to continue to support coverage results for unused functions and generics. Rust's coverage results have three advantages over Clang's coverage results: 1. Rust's coverage map does not include any overlapping code regions, making coverage counting unambiguous. 2. Rust generates coverage results (showing zero counts) for all unused functions, including generics. (Clang does not generate coverage for uninstantiated template functions.) 3. Rust's unused functions produce minimal stubbed functions in LLVM IR, sufficient for including in the coverage results; while Clang must generate the complete LLVM IR for each unused function, even though it will never be called. This PR removes the previous hack of attempting to inject coverage into some other existing function instance, and generates dedicated instances for each unused function. This change, and a few other adjustments (similar to what is required for `-C link-dead-code`, but with lower impact), makes it possible to support LLVM optimizations. Fixes: #79651 Coverage report: "Unexecuted instantiation:..." for a generic function from multiple crates Fixed by removing the aforementioned hack. Some "Unexecuted instantiation" notices are unavoidable, as explained in the `used_crate.rs` test, but `-Zinstrument-coverage` has new options to back off support for either unused generics, or all unused functions, which avoids the notice, at the cost of less coverage of unused functions. Fixes: #82875 Invalid LLVM coverage data produced with crate brotli_decompressor Fixed by disabling the LLVM function attribute that forces inlining, if `-Z instrument-coverage` is enabled. This attribute is applied to Rust functions with `#[inline(always)], and in some cases, the forced inlining breaks coverage instrumentation and reports. FYI: `@wesleywiser` r? `@tmandry`
2 parents 26c7e55 + 0859cec commit dbc37a9

File tree

62 files changed

+3065
-342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3065
-342
lines changed

compiler/rustc_codegen_llvm/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn compile_codegen_unit(
143143

144144
// Finalize code coverage by injecting the coverage map. Note, the coverage map will
145145
// also be added to the `llvm.used` variable, created next.
146-
if cx.sess().opts.debugging_opts.instrument_coverage {
146+
if cx.sess().instrument_coverage() {
147147
cx.coverageinfo_finalize();
148148
}
149149

compiler/rustc_codegen_llvm/src/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub struct CodegenCx<'ll, 'tcx> {
7979
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
8080
pub isize_ty: &'ll Type,
8181

82-
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'tcx>>,
82+
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
8383
pub dbg_cx: Option<debuginfo::CrateDebugContext<'ll, 'tcx>>,
8484

8585
eh_personality: Cell<Option<&'ll Value>>,
@@ -280,7 +280,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
280280

281281
let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
282282

283-
let coverage_cx = if tcx.sess.opts.debugging_opts.instrument_coverage {
283+
let coverage_cx = if tcx.sess.instrument_coverage() {
284284
let covctx = coverageinfo::CrateCoverageContext::new();
285285
Some(covctx)
286286
} else {
@@ -331,7 +331,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
331331
}
332332

333333
#[inline]
334-
pub fn coverage_context(&'a self) -> Option<&'a coverageinfo::CrateCoverageContext<'tcx>> {
334+
pub fn coverage_context(&'a self) -> Option<&'a coverageinfo::CrateCoverageContext<'ll, 'tcx>> {
335335
self.coverage_cx.as_ref()
336336
}
337337
}
@@ -712,7 +712,7 @@ impl CodegenCx<'b, 'tcx> {
712712
ifn!("llvm.va_end", fn(i8p) -> void);
713713
ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
714714

715-
if self.sess().opts.debugging_opts.instrument_coverage {
715+
if self.sess().instrument_coverage() {
716716
ifn!("llvm.instrprof.increment", fn(i8p, t_i64, t_i32, t_i32) -> void);
717717
}
718718

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+97-131
Large diffs are not rendered by default.

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+153-15
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,102 @@
11
use crate::llvm;
22

3+
use crate::abi::{Abi, FnAbi};
34
use crate::builder::Builder;
45
use crate::common::CodegenCx;
56

67
use libc::c_uint;
78
use llvm::coverageinfo::CounterMappingRegion;
89
use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, FunctionCoverage};
910
use rustc_codegen_ssa::traits::{
10-
BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, MiscMethods, StaticMethods,
11+
BaseTypeMethods, BuilderMethods, ConstMethods, CoverageInfoBuilderMethods, CoverageInfoMethods,
12+
MiscMethods, StaticMethods,
1113
};
1214
use rustc_data_structures::fx::FxHashMap;
15+
use rustc_hir as hir;
16+
use rustc_hir::def_id::DefId;
1317
use rustc_llvm::RustString;
18+
use rustc_middle::bug;
1419
use rustc_middle::mir::coverage::{
1520
CodeRegion, CounterValueReference, ExpressionOperandId, InjectedExpressionId, Op,
1621
};
22+
use rustc_middle::ty;
23+
use rustc_middle::ty::layout::FnAbiExt;
24+
use rustc_middle::ty::subst::InternalSubsts;
1725
use rustc_middle::ty::Instance;
1826

1927
use std::cell::RefCell;
2028
use std::ffi::CString;
2129

30+
use std::iter;
2231
use tracing::debug;
2332

2433
pub mod mapgen;
2534

35+
const UNUSED_FUNCTION_COUNTER_ID: CounterValueReference = CounterValueReference::START;
36+
2637
const VAR_ALIGN_BYTES: usize = 8;
2738

2839
/// A context object for maintaining all state needed by the coverageinfo module.
29-
pub struct CrateCoverageContext<'tcx> {
40+
pub struct CrateCoverageContext<'ll, 'tcx> {
3041
// Coverage data for each instrumented function identified by DefId.
3142
pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>>,
43+
pub(crate) pgo_func_name_var_map: RefCell<FxHashMap<Instance<'tcx>, &'ll llvm::Value>>,
3244
}
3345

34-
impl<'tcx> CrateCoverageContext<'tcx> {
46+
impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
3547
pub fn new() -> Self {
36-
Self { function_coverage_map: Default::default() }
48+
Self {
49+
function_coverage_map: Default::default(),
50+
pgo_func_name_var_map: Default::default(),
51+
}
3752
}
3853

3954
pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
4055
self.function_coverage_map.replace(FxHashMap::default())
4156
}
4257
}
4358

44-
impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
59+
impl CoverageInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
4560
fn coverageinfo_finalize(&self) {
4661
mapgen::finalize(self)
4762
}
48-
}
4963

50-
impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
51-
/// Calls llvm::createPGOFuncNameVar() with the given function instance's mangled function name.
52-
/// The LLVM API returns an llvm::GlobalVariable containing the function name, with the specific
53-
/// variable name and linkage required by LLVM InstrProf source-based coverage instrumentation.
54-
fn create_pgo_func_name_var(&self, instance: Instance<'tcx>) -> Self::Value {
55-
let llfn = self.cx.get_fn(instance);
56-
let mangled_fn_name = CString::new(self.tcx.symbol_name(instance).name)
57-
.expect("error converting function name to C string");
58-
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
64+
fn get_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
65+
if let Some(coverage_context) = self.coverage_context() {
66+
debug!("getting pgo_func_name_var for instance={:?}", instance);
67+
let mut pgo_func_name_var_map = coverage_context.pgo_func_name_var_map.borrow_mut();
68+
pgo_func_name_var_map
69+
.entry(instance)
70+
.or_insert_with(|| create_pgo_func_name_var(self, instance))
71+
} else {
72+
bug!("Could not get the `coverage_context`");
73+
}
5974
}
6075

76+
/// Functions with MIR-based coverage are normally codegenned _only_ if
77+
/// called. LLVM coverage tools typically expect every function to be
78+
/// defined (even if unused), with at least one call to LLVM intrinsic
79+
/// `instrprof.increment`.
80+
///
81+
/// Codegen a small function that will never be called, with one counter
82+
/// that will never be incremented.
83+
///
84+
/// For used/called functions, the coverageinfo was already added to the
85+
/// `function_coverage_map` (keyed by function `Instance`) during codegen.
86+
/// But in this case, since the unused function was _not_ previously
87+
/// codegenned, collect the coverage `CodeRegion`s from the MIR and add
88+
/// them. The first `CodeRegion` is used to add a single counter, with the
89+
/// same counter ID used in the injected `instrprof.increment` intrinsic
90+
/// call. Since the function is never called, all other `CodeRegion`s can be
91+
/// added as `unreachable_region`s.
92+
fn define_unused_fn(&self, def_id: DefId) {
93+
let instance = declare_unused_fn(self, &def_id);
94+
codegen_unused_fn_and_counter(self, instance);
95+
add_unused_function_coverage(self, instance, def_id);
96+
}
97+
}
98+
99+
impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
61100
fn set_function_source_hash(
62101
&mut self,
63102
instance: Instance<'tcx>,
@@ -145,6 +184,104 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
145184
}
146185
}
147186

187+
fn declare_unused_fn(cx: &CodegenCx<'ll, 'tcx>, def_id: &DefId) -> Instance<'tcx> {
188+
let tcx = cx.tcx;
189+
190+
let instance = Instance::new(
191+
*def_id,
192+
InternalSubsts::for_item(tcx, *def_id, |param, _| {
193+
if let ty::GenericParamDefKind::Lifetime = param.kind {
194+
tcx.lifetimes.re_erased.into()
195+
} else {
196+
tcx.mk_param_from_def(param)
197+
}
198+
}),
199+
);
200+
201+
let llfn = cx.declare_fn(
202+
&tcx.symbol_name(instance).name,
203+
&FnAbi::of_fn_ptr(
204+
cx,
205+
ty::Binder::dummy(tcx.mk_fn_sig(
206+
iter::once(tcx.mk_unit()),
207+
tcx.mk_unit(),
208+
false,
209+
hir::Unsafety::Unsafe,
210+
Abi::Rust,
211+
)),
212+
&[],
213+
),
214+
);
215+
216+
llvm::set_linkage(llfn, llvm::Linkage::WeakAnyLinkage);
217+
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
218+
219+
assert!(cx.instances.borrow_mut().insert(instance, llfn).is_none());
220+
221+
instance
222+
}
223+
224+
fn codegen_unused_fn_and_counter(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) {
225+
let llfn = cx.get_fn(instance);
226+
let mut bx = Builder::new_block(cx, llfn, "unused_function");
227+
let fn_name = bx.get_pgo_func_name_var(instance);
228+
let hash = bx.const_u64(0);
229+
let num_counters = bx.const_u32(1);
230+
let index = bx.const_u32(u32::from(UNUSED_FUNCTION_COUNTER_ID));
231+
debug!(
232+
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?},
233+
index={:?}) for unused function: {:?}",
234+
fn_name, hash, num_counters, index, instance
235+
);
236+
bx.instrprof_increment(fn_name, hash, num_counters, index);
237+
bx.ret_void();
238+
}
239+
240+
fn add_unused_function_coverage(
241+
cx: &CodegenCx<'ll, 'tcx>,
242+
instance: Instance<'tcx>,
243+
def_id: DefId,
244+
) {
245+
let tcx = cx.tcx;
246+
247+
let mut function_coverage = FunctionCoverage::unused(tcx, instance);
248+
for (index, &code_region) in tcx.covered_code_regions(def_id).iter().enumerate() {
249+
if index == 0 {
250+
// Insert at least one real counter so the LLVM CoverageMappingReader will find expected
251+
// definitions.
252+
function_coverage.add_counter(UNUSED_FUNCTION_COUNTER_ID, code_region.clone());
253+
}
254+
// Add a Zero Counter for every code region.
255+
//
256+
// Even though the first coverage region already has an actual Counter, `llvm-cov` will not
257+
// always report it. Re-adding an unreachable region (zero counter) for the same region
258+
// seems to help produce the expected coverage.
259+
function_coverage.add_unreachable_region(code_region.clone());
260+
}
261+
262+
if let Some(coverage_context) = cx.coverage_context() {
263+
coverage_context.function_coverage_map.borrow_mut().insert(instance, function_coverage);
264+
} else {
265+
bug!("Could not get the `coverage_context`");
266+
}
267+
}
268+
269+
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
270+
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
271+
/// containing the function name, with the specific variable name and linkage
272+
/// required by LLVM InstrProf source-based coverage instrumentation. Use
273+
/// `bx.get_pgo_func_name_var()` to ensure the variable is only created once per
274+
/// `Instance`.
275+
fn create_pgo_func_name_var(
276+
cx: &CodegenCx<'ll, 'tcx>,
277+
instance: Instance<'tcx>,
278+
) -> &'ll llvm::Value {
279+
let mangled_fn_name = CString::new(cx.tcx.symbol_name(instance).name)
280+
.expect("error converting function name to C string");
281+
let llfn = cx.get_fn(instance);
282+
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
283+
}
284+
148285
pub(crate) fn write_filenames_section_to_buffer<'a>(
149286
filenames: impl IntoIterator<Item = &'a CString>,
150287
buffer: &RustString,
@@ -177,6 +314,7 @@ pub(crate) fn write_mapping_to_buffer(
177314
);
178315
}
179316
}
317+
180318
pub(crate) fn hash_str(strval: &str) -> u64 {
181319
let strval = CString::new(strval).expect("null error converting hashable str to C string");
182320
unsafe { llvm::LLVMRustCoverageHashCString(strval.as_ptr()) }

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
17461746
);
17471747

17481748
// OBJECT-FILES-NO, AUDIT-ORDER
1749-
if sess.opts.cg.profile_generate.enabled() || sess.opts.debugging_opts.instrument_coverage {
1749+
if sess.opts.cg.profile_generate.enabled() || sess.instrument_coverage() {
17501750
cmd.pgo_gen();
17511751
}
17521752

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ fn exported_symbols_provider_local(
188188
}
189189
}
190190

191-
if tcx.sess.opts.debugging_opts.instrument_coverage
192-
|| tcx.sess.opts.cg.profile_generate.enabled()
193-
{
191+
if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {
194192
// These are weak symbols that point to the profile version and the
195193
// profile name, which need to be treated as exported so LTO doesn't nix
196194
// them.

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl ModuleConfig {
176176

177177
// The rustc option `-Zinstrument_coverage` injects intrinsic calls to
178178
// `llvm.instrprof.increment()`, which requires the LLVM `instrprof` pass.
179-
if sess.opts.debugging_opts.instrument_coverage {
179+
if sess.instrument_coverage() {
180180
passes.push("instrprof".to_owned());
181181
}
182182
passes

compiler/rustc_codegen_ssa/src/coverageinfo/map.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,44 @@ pub struct Expression {
3131
pub struct FunctionCoverage<'tcx> {
3232
instance: Instance<'tcx>,
3333
source_hash: u64,
34+
is_used: bool,
3435
counters: IndexVec<CounterValueReference, Option<CodeRegion>>,
3536
expressions: IndexVec<InjectedExpressionIndex, Option<Expression>>,
3637
unreachable_regions: Vec<CodeRegion>,
3738
}
3839

3940
impl<'tcx> FunctionCoverage<'tcx> {
41+
/// Creates a new set of coverage data for a used (called) function.
4042
pub fn new(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Self {
43+
Self::create(tcx, instance, true)
44+
}
45+
46+
/// Creates a new set of coverage data for an unused (never called) function.
47+
pub fn unused(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Self {
48+
Self::create(tcx, instance, false)
49+
}
50+
51+
fn create(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, is_used: bool) -> Self {
4152
let coverageinfo = tcx.coverageinfo(instance.def_id());
4253
debug!(
43-
"FunctionCoverage::new(instance={:?}) has coverageinfo={:?}",
44-
instance, coverageinfo
54+
"FunctionCoverage::new(instance={:?}) has coverageinfo={:?}. is_used={}",
55+
instance, coverageinfo, is_used
4556
);
4657
Self {
4758
instance,
4859
source_hash: 0, // will be set with the first `add_counter()`
60+
is_used,
4961
counters: IndexVec::from_elem_n(None, coverageinfo.num_counters as usize),
5062
expressions: IndexVec::from_elem_n(None, coverageinfo.num_expressions as usize),
5163
unreachable_regions: Vec::new(),
5264
}
5365
}
5466

67+
/// Returns true for a used (called) function, and false for an unused function.
68+
pub fn is_used(&self) -> bool {
69+
self.is_used
70+
}
71+
5572
/// Sets the function source hash value. If called multiple times for the same function, all
5673
/// calls should have the same hash value.
5774
pub fn set_function_source_hash(&mut self, source_hash: u64) {
@@ -128,8 +145,8 @@ impl<'tcx> FunctionCoverage<'tcx> {
128145
&'a self,
129146
) -> (Vec<CounterExpression>, impl Iterator<Item = (Counter, &'a CodeRegion)>) {
130147
assert!(
131-
self.source_hash != 0,
132-
"No counters provided the source_hash for function: {:?}",
148+
self.source_hash != 0 || !self.is_used,
149+
"No counters provided the source_hash for used function: {:?}",
133150
self.instance
134151
);
135152

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3333

3434
let coverageinfo = bx.tcx().coverageinfo(instance.def_id());
3535

36-
let fn_name = bx.create_pgo_func_name_var(instance);
36+
let fn_name = bx.get_pgo_func_name_var(instance);
3737
let hash = bx.const_u64(function_source_hash);
3838
let num_counters = bx.const_u32(coverageinfo.num_counters);
3939
let index = bx.const_u32(u32::from(id));

0 commit comments

Comments
 (0)