2020-08-15 11:42:13 +00:00
|
|
|
use crate::traits::*;
|
|
|
|
|
|
|
|
use rustc_middle::mir::coverage::*;
|
|
|
|
use rustc_middle::mir::Coverage;
|
2021-03-13 00:00:00 +00:00
|
|
|
use rustc_middle::mir::SourceScope;
|
2020-08-15 11:42:13 +00:00
|
|
|
|
|
|
|
use super::FunctionCx;
|
|
|
|
|
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2021-03-13 00:00:00 +00:00
|
|
|
pub fn codegen_coverage(&self, bx: &mut Bx, coverage: Coverage, scope: SourceScope) {
|
|
|
|
// Determine the instance that coverage data was originally generated for.
|
|
|
|
let scope_data = &self.mir.source_scopes[scope];
|
|
|
|
let instance = if let Some((inlined_instance, _)) = scope_data.inlined {
|
|
|
|
self.monomorphize(inlined_instance)
|
|
|
|
} else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
|
|
|
|
self.monomorphize(self.mir.source_scopes[inlined_scope].inlined.unwrap().0)
|
|
|
|
} else {
|
|
|
|
self.instance
|
|
|
|
};
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
let Coverage { kind, code_region } = coverage;
|
|
|
|
match kind {
|
|
|
|
CoverageKind::Counter { function_source_hash, id } => {
|
2021-03-13 00:00:00 +00:00
|
|
|
if bx.set_function_source_hash(instance, function_source_hash) {
|
2020-10-25 18:13:16 +00:00
|
|
|
// If `set_function_source_hash()` returned true, the coverage map is enabled,
|
|
|
|
// so continue adding the counter.
|
|
|
|
if let Some(code_region) = code_region {
|
|
|
|
// Note: Some counters do not have code regions, but may still be referenced
|
|
|
|
// from expressions. In that case, don't add the counter to the coverage map,
|
|
|
|
// but do inject the counter intrinsic.
|
2021-03-13 00:00:00 +00:00
|
|
|
bx.add_coverage_counter(instance, id, code_region);
|
2020-10-25 18:13:16 +00:00
|
|
|
}
|
2020-10-05 23:36:10 +00:00
|
|
|
|
2021-05-12 03:56:23 +00:00
|
|
|
let coverageinfo = bx.tcx().coverageinfo(instance.def);
|
2020-08-15 11:42:13 +00:00
|
|
|
|
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.
2021-03-15 23:32:45 +00:00
|
|
|
let fn_name = bx.get_pgo_func_name_var(instance);
|
2020-10-23 18:41:56 +00:00
|
|
|
let hash = bx.const_u64(function_source_hash);
|
|
|
|
let num_counters = bx.const_u32(coverageinfo.num_counters);
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 07:08:48 +00:00
|
|
|
let index = bx.const_u32(id.zero_based_index());
|
2020-10-23 18:41:56 +00:00
|
|
|
debug!(
|
|
|
|
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
|
2020-10-22 21:30:03 +00:00
|
|
|
fn_name, hash, num_counters, index,
|
2020-10-23 18:41:56 +00:00
|
|
|
);
|
2020-10-05 23:36:10 +00:00
|
|
|
bx.instrprof_increment(fn_name, hash, num_counters, index);
|
2020-10-23 18:41:56 +00:00
|
|
|
}
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
CoverageKind::Expression { id, lhs, op, rhs } => {
|
2021-03-13 00:00:00 +00:00
|
|
|
bx.add_coverage_counter_expression(instance, id, lhs, op, rhs, code_region);
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
CoverageKind::Unreachable => {
|
2020-10-05 23:36:10 +00:00
|
|
|
bx.add_coverage_unreachable(
|
2021-03-13 00:00:00 +00:00
|
|
|
instance,
|
2020-10-05 23:36:10 +00:00
|
|
|
code_region.expect("unreachable regions always have code regions"),
|
|
|
|
);
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|