2024-11-29 03:52:41 +00:00
|
|
|
use std::iter;
|
2024-10-18 10:43:37 +00:00
|
|
|
|
2023-10-03 10:40:50 +00:00
|
|
|
use itertools::Itertools as _;
|
2024-10-24 11:47:45 +00:00
|
|
|
use rustc_abi::Align;
|
|
|
|
use rustc_codegen_ssa::traits::{
|
|
|
|
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
|
|
|
|
};
|
2024-11-29 03:52:41 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
|
2024-03-22 01:59:49 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2023-09-03 05:52:49 +00:00
|
|
|
use rustc_index::IndexVec;
|
2023-09-10 06:35:37 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
|
|
|
use rustc_middle::{bug, mir};
|
2024-11-04 03:53:52 +00:00
|
|
|
use rustc_session::RemapFileNameExt;
|
|
|
|
use rustc_session::config::RemapPathScopeComponents;
|
2023-09-28 05:37:44 +00:00
|
|
|
use rustc_span::def_id::DefIdSet;
|
2024-11-29 03:52:41 +00:00
|
|
|
use rustc_span::{Span, Symbol};
|
2024-05-22 04:50:24 +00:00
|
|
|
use tracing::debug;
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
use crate::common::CodegenCx;
|
2024-12-11 03:28:55 +00:00
|
|
|
use crate::coverageinfo::llvm_cov;
|
2024-12-06 11:55:09 +00:00
|
|
|
use crate::coverageinfo::map_data::FunctionCoverage;
|
2024-11-01 09:32:20 +00:00
|
|
|
use crate::llvm;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-12-11 03:28:55 +00:00
|
|
|
mod covfun;
|
|
|
|
|
2024-10-11 10:44:36 +00:00
|
|
|
/// Generates and exports the coverage map, which is embedded in special
|
|
|
|
/// linker sections in the final binary.
|
2020-07-02 18:27:15 +00:00
|
|
|
///
|
2024-10-11 10:44:36 +00:00
|
|
|
/// Those sections are then read and understood by LLVM's `llvm-cov` tool,
|
|
|
|
/// which is distributed in the `llvm-tools` rustup component.
|
2024-07-06 12:26:42 +00:00
|
|
|
pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
|
2020-12-01 07:58:08 +00:00
|
|
|
let tcx = cx.tcx;
|
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
|
|
|
|
2024-02-13 12:00:49 +00:00
|
|
|
// Ensure that LLVM is using a version of the coverage mapping format that
|
|
|
|
// agrees with our Rust-side code. Expected versions (encoded as n-1) are:
|
2024-10-11 10:44:36 +00:00
|
|
|
// - `CovMapVersion::Version7` (6) used by LLVM 18-19
|
2024-02-13 12:00:49 +00:00
|
|
|
let covmap_version = {
|
2024-11-01 09:32:20 +00:00
|
|
|
let llvm_covmap_version = llvm_cov::mapping_version();
|
2024-10-11 10:44:36 +00:00
|
|
|
let expected_versions = 6..=6;
|
2024-02-13 12:00:49 +00:00
|
|
|
assert!(
|
|
|
|
expected_versions.contains(&llvm_covmap_version),
|
|
|
|
"Coverage mapping version exposed by `llvm-wrapper` is out of sync; \
|
|
|
|
expected {expected_versions:?} but was {llvm_covmap_version}"
|
|
|
|
);
|
|
|
|
// This is the version number that we will embed in the covmap section:
|
|
|
|
llvm_covmap_version
|
|
|
|
};
|
2020-11-25 17:45:33 +00:00
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
|
|
|
|
|
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
|
|
|
// In order to show that unused functions have coverage counts of zero (0), LLVM requires the
|
|
|
|
// functions exist. Generate synthetic functions with a (required) single counter, and add the
|
|
|
|
// MIR `Coverage` code regions to the `function_coverage_map`, before calling
|
|
|
|
// `ctx.take_function_coverage_map()`.
|
2021-12-20 21:36:56 +00:00
|
|
|
if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
|
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
|
|
|
add_unused_functions(cx);
|
|
|
|
}
|
|
|
|
|
2024-10-31 10:12:15 +00:00
|
|
|
// FIXME(#132395): Can this be none even when coverage is enabled?
|
|
|
|
let function_coverage_map = match cx.coverage_cx {
|
|
|
|
Some(ref cx) => cx.take_function_coverage_map(),
|
|
|
|
None => return,
|
|
|
|
};
|
2020-07-27 23:25:08 +00:00
|
|
|
if function_coverage_map.is_empty() {
|
2024-12-06 11:55:09 +00:00
|
|
|
// This CGU has no functions with coverage instrumentation.
|
2020-07-25 04:14:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-06 11:55:09 +00:00
|
|
|
let all_file_names = function_coverage_map
|
2024-11-04 03:53:52 +00:00
|
|
|
.iter()
|
|
|
|
.map(|(_, fn_cov)| fn_cov.function_coverage_info.body_span)
|
2024-11-29 03:52:41 +00:00
|
|
|
.map(|span| span_file_name(tcx, span));
|
|
|
|
let global_file_table = GlobalFileTable::new(all_file_names);
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2023-10-06 12:53:23 +00:00
|
|
|
// Encode all filenames referenced by coverage mappings in this CGU.
|
|
|
|
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
|
|
|
|
|
|
|
|
let filenames_size = filenames_buffer.len();
|
|
|
|
let filenames_val = cx.const_bytes(&filenames_buffer);
|
2024-11-01 09:32:20 +00:00
|
|
|
let filenames_ref = llvm_cov::hash_bytes(&filenames_buffer);
|
2023-10-06 12:53:23 +00:00
|
|
|
|
|
|
|
// Generate the coverage map header, which contains the filenames used by
|
|
|
|
// this CGU's coverage mappings, and store it in a well-known global.
|
2024-10-24 11:47:45 +00:00
|
|
|
generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
|
2023-10-06 12:53:23 +00:00
|
|
|
|
|
|
|
let mut unused_function_names = Vec::new();
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
// Encode coverage mappings and generate function records
|
2024-12-06 11:55:09 +00:00
|
|
|
for (instance, function_coverage) in function_coverage_map {
|
2024-12-11 03:28:55 +00:00
|
|
|
covfun::prepare_and_generate_covfun_record(
|
2020-11-23 20:56:07 +00:00
|
|
|
cx,
|
2024-12-11 03:28:55 +00:00
|
|
|
&global_file_table,
|
2020-11-23 20:56:07 +00:00
|
|
|
filenames_ref,
|
2024-12-11 03:28:55 +00:00
|
|
|
&mut unused_function_names,
|
|
|
|
instance,
|
|
|
|
&function_coverage,
|
2020-11-23 20:56:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:06:39 +00:00
|
|
|
// For unused functions, we need to take their mangled names and store them
|
|
|
|
// in a specially-named global array. LLVM's `InstrProfiling` pass will
|
|
|
|
// detect this global and include those names in its `__llvm_prf_names`
|
|
|
|
// section. (See `llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp`.)
|
|
|
|
if !unused_function_names.is_empty() {
|
|
|
|
assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
|
|
|
|
|
|
|
|
let name_globals = unused_function_names
|
|
|
|
.into_iter()
|
|
|
|
.map(|mangled_function_name| cx.const_str(mangled_function_name).0)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let initializer = cx.const_array(cx.type_ptr(), &name_globals);
|
|
|
|
|
2024-10-18 07:00:23 +00:00
|
|
|
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), c"__llvm_coverage_names");
|
2023-09-24 11:06:39 +00:00
|
|
|
llvm::set_global_constant(array, true);
|
|
|
|
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
|
|
|
|
llvm::set_initializer(array, initializer);
|
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 03:52:41 +00:00
|
|
|
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
|
2023-09-01 13:27:45 +00:00
|
|
|
struct GlobalFileTable {
|
2024-11-29 03:52:41 +00:00
|
|
|
/// This "raw" table doesn't include the working dir, so a filename's
|
2023-10-03 10:40:50 +00:00
|
|
|
/// global ID is its index in this set **plus one**.
|
2024-11-29 03:52:41 +00:00
|
|
|
raw_file_table: FxIndexSet<Symbol>,
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:27:45 +00:00
|
|
|
impl GlobalFileTable {
|
2024-11-29 03:52:41 +00:00
|
|
|
fn new(all_file_names: impl IntoIterator<Item = Symbol>) -> Self {
|
|
|
|
// Collect all of the filenames into a set. Filenames usually come in
|
|
|
|
// contiguous runs, so we can dedup adjacent ones to save work.
|
|
|
|
let mut raw_file_table = all_file_names.into_iter().dedup().collect::<FxIndexSet<Symbol>>();
|
2023-10-03 10:40:50 +00:00
|
|
|
|
2024-11-29 03:52:41 +00:00
|
|
|
// Sort the file table by its actual string values, not the arbitrary
|
|
|
|
// ordering of its symbols.
|
|
|
|
raw_file_table.sort_unstable_by(|a, b| a.as_str().cmp(b.as_str()));
|
2023-10-03 10:40:50 +00:00
|
|
|
|
|
|
|
Self { raw_file_table }
|
|
|
|
}
|
|
|
|
|
2024-11-29 03:52:41 +00:00
|
|
|
fn global_file_id_for_file_name(&self, file_name: Symbol) -> GlobalFileId {
|
|
|
|
let raw_id = self.raw_file_table.get_index_of(&file_name).unwrap_or_else(|| {
|
|
|
|
bug!("file name not found in prepared global file table: {file_name}");
|
2023-10-03 10:40:50 +00:00
|
|
|
});
|
|
|
|
// The raw file table doesn't include an entry for the working dir
|
|
|
|
// (which has ID 0), so add 1 to get the correct ID.
|
2024-08-29 05:56:21 +00:00
|
|
|
GlobalFileId::from_usize(raw_id + 1)
|
2023-10-03 10:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_filenames_buffer(&self, tcx: TyCtxt<'_>) -> Vec<u8> {
|
2023-01-09 08:31:34 +00:00
|
|
|
// LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
|
|
|
|
// requires setting the first filename to the compilation directory.
|
|
|
|
// Since rustc generates coverage maps with relative paths, the
|
|
|
|
// compilation directory can be combined with the relative paths
|
|
|
|
// to get absolute paths, if needed.
|
2024-11-29 03:52:41 +00:00
|
|
|
use rustc_session::RemapFileNameExt;
|
|
|
|
use rustc_session::config::RemapPathScopeComponents;
|
|
|
|
let working_dir: &str = &tcx
|
|
|
|
.sess
|
|
|
|
.opts
|
|
|
|
.working_dir
|
|
|
|
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
|
|
|
|
.to_string_lossy();
|
|
|
|
|
|
|
|
// Insert the working dir at index 0, before the other filenames.
|
|
|
|
let filenames =
|
|
|
|
iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str));
|
|
|
|
llvm_cov::write_filenames_to_buffer(filenames)
|
2023-09-01 13:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 12:36:40 +00:00
|
|
|
rustc_index::newtype_index! {
|
2024-08-29 05:56:21 +00:00
|
|
|
/// An index into the CGU's overall list of file paths. The underlying paths
|
|
|
|
/// will be embedded in the `__llvm_covmap` linker section.
|
|
|
|
struct GlobalFileId {}
|
|
|
|
}
|
|
|
|
rustc_index::newtype_index! {
|
|
|
|
/// An index into a function's list of global file IDs. That underlying list
|
|
|
|
/// of local-to-global mappings will be embedded in the function's record in
|
|
|
|
/// the `__llvm_covfun` linker section.
|
2024-11-29 03:52:41 +00:00
|
|
|
pub(crate) struct LocalFileId {}
|
2023-09-28 12:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU)
|
|
|
|
/// file IDs.
|
|
|
|
#[derive(Default)]
|
|
|
|
struct VirtualFileMapping {
|
2024-08-29 05:56:21 +00:00
|
|
|
local_to_global: IndexVec<LocalFileId, GlobalFileId>,
|
|
|
|
global_to_local: FxIndexMap<GlobalFileId, LocalFileId>,
|
2023-09-28 12:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtualFileMapping {
|
2024-08-29 05:56:21 +00:00
|
|
|
fn local_id_for_global(&mut self, global_file_id: GlobalFileId) -> LocalFileId {
|
2023-09-28 12:36:40 +00:00
|
|
|
*self
|
|
|
|
.global_to_local
|
|
|
|
.entry(global_file_id)
|
|
|
|
.or_insert_with(|| self.local_to_global.push(global_file_id))
|
2023-09-28 12:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn into_vec(self) -> Vec<u32> {
|
2024-08-29 05:56:21 +00:00
|
|
|
// This conversion should be optimized away to ~zero overhead.
|
|
|
|
// In any case, it's probably not hot enough to worry about.
|
|
|
|
self.local_to_global.into_iter().map(|global| global.as_u32()).collect()
|
2023-09-28 12:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 03:52:41 +00:00
|
|
|
fn span_file_name(tcx: TyCtxt<'_>, span: Span) -> Symbol {
|
|
|
|
let source_file = tcx.sess.source_map().lookup_source_file(span.lo());
|
|
|
|
let name =
|
|
|
|
source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy();
|
|
|
|
Symbol::intern(&name)
|
|
|
|
}
|
|
|
|
|
2024-10-24 11:47:45 +00:00
|
|
|
/// Generates the contents of the covmap record for this CGU, which mostly
|
|
|
|
/// consists of a header and a list of filenames. The record is then stored
|
|
|
|
/// as a global variable in the `__llvm_covmap` section.
|
|
|
|
fn generate_covmap_record<'ll>(
|
2023-09-01 13:27:45 +00:00
|
|
|
cx: &CodegenCx<'ll, '_>,
|
|
|
|
version: u32,
|
|
|
|
filenames_size: usize,
|
|
|
|
filenames_val: &'ll llvm::Value,
|
2024-10-24 11:47:45 +00:00
|
|
|
) {
|
2023-09-01 13:27:45 +00:00
|
|
|
debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
|
|
|
|
|
|
|
|
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
|
|
|
|
// as of `llvm::coverage::CovMapVersion::Version4`.)
|
|
|
|
let zero_was_n_records_val = cx.const_u32(0);
|
|
|
|
let filenames_size_val = cx.const_u32(filenames_size as u32);
|
|
|
|
let zero_was_coverage_size_val = cx.const_u32(0);
|
|
|
|
let version_val = cx.const_u32(version);
|
|
|
|
let cov_data_header_val = cx.const_struct(
|
|
|
|
&[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
|
|
|
|
/*packed=*/ false,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create the complete LLVM coverage data value to add to the LLVM IR
|
2024-10-24 11:47:45 +00:00
|
|
|
let covmap_data =
|
|
|
|
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false);
|
|
|
|
|
2024-11-01 09:32:20 +00:00
|
|
|
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(covmap_data), &llvm_cov::covmap_var_name());
|
2024-10-24 11:47:45 +00:00
|
|
|
llvm::set_initializer(llglobal, covmap_data);
|
|
|
|
llvm::set_global_constant(llglobal, true);
|
|
|
|
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
2024-11-01 09:32:20 +00:00
|
|
|
llvm::set_section(llglobal, &llvm_cov::covmap_section_name(cx.llmod));
|
2024-10-24 11:47:45 +00:00
|
|
|
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
|
2024-10-26 06:37:04 +00:00
|
|
|
// <https://llvm.org/docs/CoverageMappingFormat.html>
|
2024-10-24 11:47:45 +00:00
|
|
|
llvm::set_alignment(llglobal, Align::EIGHT);
|
|
|
|
cx.add_used_global(llglobal);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
2020-11-23 20:56:07 +00:00
|
|
|
|
2024-03-22 01:59:49 +00:00
|
|
|
/// Each CGU will normally only emit coverage metadata for the functions that it actually generates.
|
|
|
|
/// But since we don't want unused functions to disappear from coverage reports, we also scan for
|
|
|
|
/// functions that were instrumented but are not participating in codegen.
|
2020-12-01 07:58:08 +00:00
|
|
|
///
|
2023-09-24 11:06:39 +00:00
|
|
|
/// These unused functions don't need to be codegenned, but we do need to add them to the function
|
|
|
|
/// coverage map (in a single designated CGU) so that we still emit coverage mappings for them.
|
|
|
|
/// We also end up adding their symbol names to a special global array that LLVM will include in
|
|
|
|
/// its embedded coverage data.
|
2022-12-20 21:10:40 +00:00
|
|
|
fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
|
2021-12-20 21:36:56 +00:00
|
|
|
assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
|
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
|
|
|
|
2021-12-20 21:36:56 +00:00
|
|
|
let tcx = cx.tcx;
|
2024-03-22 01:59:49 +00:00
|
|
|
let usage = prepare_usage_sets(tcx);
|
|
|
|
|
|
|
|
let is_unused_fn = |def_id: LocalDefId| -> bool {
|
|
|
|
let def_id = def_id.to_def_id();
|
|
|
|
|
|
|
|
// To be eligible for "unused function" mappings, a definition must:
|
|
|
|
// - Be function-like
|
2024-03-22 02:23:04 +00:00
|
|
|
// - Not participate directly in codegen (or have lost all its coverage statements)
|
2024-03-22 01:59:49 +00:00
|
|
|
// - Not have any coverage statements inlined into codegenned functions
|
|
|
|
tcx.def_kind(def_id).is_fn_like()
|
2024-03-22 02:23:04 +00:00
|
|
|
&& (!usage.all_mono_items.contains(&def_id)
|
|
|
|
|| usage.missing_own_coverage.contains(&def_id))
|
2024-03-22 01:59:49 +00:00
|
|
|
&& !usage.used_via_inlining.contains(&def_id)
|
|
|
|
};
|
2020-12-02 07:01:26 +00:00
|
|
|
|
2024-03-22 01:59:49 +00:00
|
|
|
// Scan for unused functions that were instrumented for coverage.
|
|
|
|
for def_id in tcx.mir_keys(()).iter().copied().filter(|&def_id| is_unused_fn(def_id)) {
|
|
|
|
// Get the coverage info from MIR, skipping functions that were never instrumented.
|
|
|
|
let body = tcx.optimized_mir(def_id);
|
|
|
|
let Some(function_coverage_info) = body.function_coverage_info.as_deref() else { continue };
|
2024-03-12 01:30:33 +00:00
|
|
|
|
|
|
|
// FIXME(79651): Consider trying to filter out dummy instantiations of
|
|
|
|
// unused generic functions from library crates, because they can produce
|
|
|
|
// "unused instantiation" in coverage reports even when they are actually
|
|
|
|
// used by some downstream crate in the same binary.
|
|
|
|
|
2023-09-24 12:26:12 +00:00
|
|
|
debug!("generating unused fn: {def_id:?}");
|
2024-03-22 03:33:58 +00:00
|
|
|
add_unused_function_coverage(cx, def_id, function_coverage_info);
|
2023-09-24 12:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 01:59:49 +00:00
|
|
|
struct UsageSets<'tcx> {
|
|
|
|
all_mono_items: &'tcx DefIdSet,
|
|
|
|
used_via_inlining: FxHashSet<DefId>,
|
2024-03-22 02:23:04 +00:00
|
|
|
missing_own_coverage: FxHashSet<DefId>,
|
2024-03-22 01:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare sets of definitions that are relevant to deciding whether something
|
|
|
|
/// is an "unused function" for coverage purposes.
|
|
|
|
fn prepare_usage_sets<'tcx>(tcx: TyCtxt<'tcx>) -> UsageSets<'tcx> {
|
|
|
|
let (all_mono_items, cgus) = tcx.collect_and_partition_mono_items(());
|
|
|
|
|
|
|
|
// Obtain a MIR body for each function participating in codegen, via an
|
|
|
|
// arbitrary instance.
|
|
|
|
let mut def_ids_seen = FxHashSet::default();
|
|
|
|
let def_and_mir_for_all_mono_fns = cgus
|
|
|
|
.iter()
|
|
|
|
.flat_map(|cgu| cgu.items().keys())
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
mir::mono::MonoItem::Fn(instance) => Some(instance),
|
|
|
|
mir::mono::MonoItem::Static(_) | mir::mono::MonoItem::GlobalAsm(_) => None,
|
|
|
|
})
|
|
|
|
// We only need one arbitrary instance per definition.
|
|
|
|
.filter(move |instance| def_ids_seen.insert(instance.def_id()))
|
|
|
|
.map(|instance| {
|
|
|
|
// We don't care about the instance, just its underlying MIR.
|
|
|
|
let body = tcx.instance_mir(instance.def);
|
|
|
|
(instance.def_id(), body)
|
|
|
|
});
|
|
|
|
|
2024-09-02 05:42:38 +00:00
|
|
|
// Functions whose coverage statements were found inlined into other functions.
|
2024-03-22 01:59:49 +00:00
|
|
|
let mut used_via_inlining = FxHashSet::default();
|
2024-03-22 02:23:04 +00:00
|
|
|
// Functions that were instrumented, but had all of their coverage statements
|
|
|
|
// removed by later MIR transforms (e.g. UnreachablePropagation).
|
|
|
|
let mut missing_own_coverage = FxHashSet::default();
|
|
|
|
|
|
|
|
for (def_id, body) in def_and_mir_for_all_mono_fns {
|
|
|
|
let mut saw_own_coverage = false;
|
2024-03-22 01:59:49 +00:00
|
|
|
|
|
|
|
// Inspect every coverage statement in the function's MIR.
|
|
|
|
for stmt in body
|
|
|
|
.basic_blocks
|
|
|
|
.iter()
|
|
|
|
.flat_map(|block| &block.statements)
|
|
|
|
.filter(|stmt| matches!(stmt.kind, mir::StatementKind::Coverage(_)))
|
|
|
|
{
|
|
|
|
if let Some(inlined) = stmt.source_info.scope.inlined_instance(&body.source_scopes) {
|
|
|
|
// This coverage statement was inlined from another function.
|
|
|
|
used_via_inlining.insert(inlined.def_id());
|
2024-03-22 02:23:04 +00:00
|
|
|
} else {
|
|
|
|
// Non-inlined coverage statements belong to the enclosing function.
|
|
|
|
saw_own_coverage = true;
|
2023-09-28 05:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-22 02:23:04 +00:00
|
|
|
|
|
|
|
if !saw_own_coverage && body.function_coverage_info.is_some() {
|
|
|
|
missing_own_coverage.insert(def_id);
|
|
|
|
}
|
2023-09-28 05:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 02:23:04 +00:00
|
|
|
UsageSets { all_mono_items, used_via_inlining, missing_own_coverage }
|
2023-09-28 05:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 03:33:58 +00:00
|
|
|
fn add_unused_function_coverage<'tcx>(
|
|
|
|
cx: &CodegenCx<'_, 'tcx>,
|
2024-03-22 01:59:49 +00:00
|
|
|
def_id: LocalDefId,
|
2024-03-22 03:33:58 +00:00
|
|
|
function_coverage_info: &'tcx mir::coverage::FunctionCoverageInfo,
|
|
|
|
) {
|
|
|
|
let tcx = cx.tcx;
|
2024-03-22 01:59:49 +00:00
|
|
|
let def_id = def_id.to_def_id();
|
2024-03-22 03:33:58 +00:00
|
|
|
|
|
|
|
// Make a dummy instance that fills in all generics with placeholders.
|
|
|
|
let instance = ty::Instance::new(
|
2023-09-24 12:26:12 +00:00
|
|
|
def_id,
|
|
|
|
ty::GenericArgs::for_item(tcx, def_id, |param, _| {
|
|
|
|
if let ty::GenericParamDefKind::Lifetime = param.kind {
|
|
|
|
tcx.lifetimes.re_erased.into()
|
|
|
|
} else {
|
|
|
|
tcx.mk_param_from_def(param)
|
|
|
|
}
|
|
|
|
}),
|
2024-03-22 03:33:58 +00:00
|
|
|
);
|
2023-09-24 12:26:12 +00:00
|
|
|
|
2024-11-25 10:17:50 +00:00
|
|
|
// An unused function's mappings will all be rewritten to map to zero.
|
2024-12-08 09:49:28 +00:00
|
|
|
let function_coverage = FunctionCoverage::new_unused(function_coverage_info);
|
2024-10-24 11:23:13 +00:00
|
|
|
cx.coverage_cx().function_coverage_map.borrow_mut().insert(instance, function_coverage);
|
2020-12-01 07:58:08 +00:00
|
|
|
}
|