2020-07-02 18:27:15 +00:00
|
|
|
use crate::common::CodegenCx;
|
|
|
|
use crate::coverageinfo;
|
2023-09-04 01:40:13 +00:00
|
|
|
use crate::coverageinfo::ffi::CounterMappingRegion;
|
2023-10-06 01:51:48 +00:00
|
|
|
use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector};
|
2020-07-25 04:14:28 +00:00
|
|
|
use crate::llvm;
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2023-10-03 10:40:50 +00:00
|
|
|
use itertools::Itertools as _;
|
2023-09-24 11:06:39 +00:00
|
|
|
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
|
2021-12-20 21:36:56 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
|
|
|
use rustc_hir::def::DefKind;
|
2022-12-02 15:27:25 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2023-09-03 05:52:49 +00:00
|
|
|
use rustc_index::IndexVec;
|
2022-01-21 01:23:27 +00:00
|
|
|
use rustc_middle::bug;
|
2023-09-24 12:26:12 +00:00
|
|
|
use rustc_middle::mir;
|
2020-08-15 11:42:13 +00:00
|
|
|
use rustc_middle::mir::coverage::CodeRegion;
|
2023-09-10 06:35:37 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2023-09-28 05:37:44 +00:00
|
|
|
use rustc_span::def_id::DefIdSet;
|
2023-07-24 07:27:29 +00:00
|
|
|
use rustc_span::Symbol;
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
/// Generates and exports the Coverage Map.
|
|
|
|
///
|
2023-01-09 08:31:34 +00:00
|
|
|
/// Rust Coverage Map generation supports LLVM Coverage Mapping Format version
|
|
|
|
/// 6 (zero-based encoded as 5), as defined at
|
2021-11-25 00:58:49 +00:00
|
|
|
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
|
|
|
|
/// These versions are supported by the LLVM coverage tools (`llvm-profdata` and `llvm-cov`)
|
2021-10-19 09:09:43 +00:00
|
|
|
/// bundled with Rust's fork of LLVM.
|
2020-07-02 18:27:15 +00:00
|
|
|
///
|
|
|
|
/// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
|
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
|
|
|
/// the same version. Clang's implementation of Coverage Map generation was referenced when
|
|
|
|
/// implementing this Rust version, and though the format documentation is very explicit and
|
|
|
|
/// detailed, some undocumented details in Clang's implementation (that may or may not be important)
|
|
|
|
/// were also replicated for Rust's Coverage Map.
|
2022-12-20 21:10:40 +00:00
|
|
|
pub 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
|
|
|
|
2023-01-09 08:31:34 +00:00
|
|
|
// Ensure the installed version of LLVM supports Coverage Map Version 6
|
|
|
|
// (encoded as a zero-based value: 5), which was introduced with LLVM 13.
|
2021-11-25 00:58:49 +00:00
|
|
|
let version = coverageinfo::mapping_version();
|
2023-01-09 08:31:34 +00:00
|
|
|
assert_eq!(version, 5, "The `CoverageMappingVersion` exposed by `llvm-wrapper` is out of sync");
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
let function_coverage_map = match cx.coverage_context() {
|
2020-10-23 21:58:08 +00:00
|
|
|
Some(ctx) => ctx.take_function_coverage_map(),
|
|
|
|
None => return,
|
|
|
|
};
|
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
|
|
|
|
2020-07-27 23:25:08 +00:00
|
|
|
if function_coverage_map.is_empty() {
|
2020-07-25 04:14:28 +00:00
|
|
|
// This module has no functions with coverage instrumentation
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-03 10:40:50 +00:00
|
|
|
let function_coverage_entries = function_coverage_map
|
|
|
|
.into_iter()
|
|
|
|
.map(|(instance, function_coverage)| (instance, function_coverage.into_finished()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let all_file_names =
|
|
|
|
function_coverage_entries.iter().flat_map(|(_, fn_cov)| fn_cov.all_file_names());
|
|
|
|
let global_file_table = GlobalFileTable::new(all_file_names);
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
// Encode coverage mappings and generate function records
|
2020-11-23 20:56:07 +00:00
|
|
|
let mut function_data = Vec::new();
|
2023-10-03 10:40:50 +00:00
|
|
|
for (instance, function_coverage) in function_coverage_entries {
|
2020-12-01 07:58:08 +00:00
|
|
|
debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance);
|
2023-08-14 08:01:28 +00:00
|
|
|
|
2023-07-07 12:49:45 +00:00
|
|
|
let mangled_function_name = tcx.symbol_name(instance).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
|
|
|
let source_hash = function_coverage.source_hash();
|
|
|
|
let is_used = function_coverage.is_used();
|
2020-11-23 20:56:07 +00:00
|
|
|
|
2023-09-03 06:07:50 +00:00
|
|
|
let coverage_mapping_buffer =
|
2023-10-03 10:40:50 +00:00
|
|
|
encode_mappings_for_function(&global_file_table, &function_coverage);
|
2022-01-21 01:23:27 +00:00
|
|
|
|
|
|
|
if coverage_mapping_buffer.is_empty() {
|
|
|
|
if function_coverage.is_used() {
|
|
|
|
bug!(
|
|
|
|
"A used function should have had coverage mapping data but did not: {}",
|
|
|
|
mangled_function_name
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
debug!("unused function had no coverage mapping data: {}", mangled_function_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 20:56:07 +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
|
|
|
function_data.push((mangled_function_name, source_hash, is_used, coverage_mapping_buffer));
|
2020-11-23 20:56:07 +00:00
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2020-07-25 04:14:28 +00:00
|
|
|
// Encode all filenames referenced by counters/expressions in this module
|
2023-10-03 10:40:50 +00:00
|
|
|
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2020-11-23 20:56:07 +00:00
|
|
|
let filenames_size = filenames_buffer.len();
|
2021-12-03 02:06:36 +00:00
|
|
|
let filenames_val = cx.const_bytes(&filenames_buffer);
|
2023-07-13 01:21:21 +00:00
|
|
|
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
|
2020-11-23 20:56:07 +00:00
|
|
|
|
2020-07-25 04:14:28 +00:00
|
|
|
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
|
2023-09-01 13:27:45 +00:00
|
|
|
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
|
2020-11-23 20:56:07 +00:00
|
|
|
|
2023-09-24 11:06:39 +00:00
|
|
|
let mut unused_function_names = Vec::new();
|
|
|
|
|
2023-07-24 11:15:11 +00:00
|
|
|
let covfun_section_name = coverageinfo::covfun_section_name(cx);
|
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
|
|
|
for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
|
2023-09-24 11:06:39 +00:00
|
|
|
if !is_used {
|
|
|
|
unused_function_names.push(mangled_function_name);
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:56:07 +00:00
|
|
|
save_function_record(
|
|
|
|
cx,
|
2023-07-24 11:15:11 +00:00
|
|
|
&covfun_section_name,
|
2020-11-23 20:56:07 +00:00
|
|
|
mangled_function_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
|
|
|
source_hash,
|
2020-11-23 20:56:07 +00:00
|
|
|
filenames_ref,
|
|
|
|
coverage_mapping_buffer,
|
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
|
|
|
is_used,
|
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);
|
|
|
|
|
|
|
|
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), "__llvm_coverage_names");
|
|
|
|
llvm::set_global_constant(array, true);
|
|
|
|
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
|
|
|
|
llvm::set_initializer(array, initializer);
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:56:07 +00:00
|
|
|
// Save the coverage data value to LLVM IR
|
|
|
|
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 10:40:50 +00:00
|
|
|
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
|
2023-09-01 13:27:45 +00:00
|
|
|
struct GlobalFileTable {
|
2023-10-03 10:40:50 +00:00
|
|
|
/// This "raw" table doesn't include the working dir, so a filename's
|
|
|
|
/// global ID is its index in this set **plus one**.
|
|
|
|
raw_file_table: FxIndexSet<Symbol>,
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:27:45 +00:00
|
|
|
impl GlobalFileTable {
|
2023-10-03 10:40:50 +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>>();
|
|
|
|
|
|
|
|
// 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()));
|
|
|
|
|
|
|
|
Self { raw_file_table }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn global_file_id_for_file_name(&self, file_name: Symbol) -> u32 {
|
|
|
|
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}");
|
|
|
|
});
|
|
|
|
// 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.
|
|
|
|
(raw_id + 1) as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-08-23 13:46:58 +00:00
|
|
|
use rustc_session::RemapFileNameExt;
|
2023-10-03 10:40:50 +00:00
|
|
|
let working_dir: &str = &tcx.sess.opts.working_dir.for_codegen(&tcx.sess).to_string_lossy();
|
2023-09-01 13:27:45 +00:00
|
|
|
|
|
|
|
llvm::build_byte_buffer(|buffer| {
|
|
|
|
coverageinfo::write_filenames_section_to_buffer(
|
2023-10-03 10:40:50 +00:00
|
|
|
// Insert the working dir at index 0, before the other filenames.
|
|
|
|
std::iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str)),
|
2023-09-01 13:27:45 +00:00
|
|
|
buffer,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 06:07:50 +00:00
|
|
|
/// Using the expressions and counter regions collected for a single function,
|
|
|
|
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
|
|
|
|
/// entry. The payload is returned as a vector of bytes.
|
|
|
|
///
|
|
|
|
/// Newly-encountered filenames will be added to the global file table.
|
2023-09-04 01:40:13 +00:00
|
|
|
fn encode_mappings_for_function(
|
2023-10-03 10:40:50 +00:00
|
|
|
global_file_table: &GlobalFileTable,
|
2023-09-04 01:40:13 +00:00
|
|
|
function_coverage: &FunctionCoverage<'_>,
|
2023-09-03 06:07:50 +00:00
|
|
|
) -> Vec<u8> {
|
2023-10-06 11:46:04 +00:00
|
|
|
let mut counter_regions = function_coverage.counter_regions().collect::<Vec<_>>();
|
2023-09-01 13:27:45 +00:00
|
|
|
if counter_regions.is_empty() {
|
2023-09-03 06:07:50 +00:00
|
|
|
return Vec::new();
|
2023-09-01 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 11:46:04 +00:00
|
|
|
let expressions = function_coverage.counter_expressions().collect::<Vec<_>>();
|
|
|
|
|
2023-09-03 05:52:49 +00:00
|
|
|
let mut virtual_file_mapping = IndexVec::<u32, u32>::new();
|
2023-09-03 06:22:06 +00:00
|
|
|
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
|
2023-09-03 05:52:49 +00:00
|
|
|
|
2023-09-04 02:50:51 +00:00
|
|
|
// Sort and group the list of (counter, region) mapping pairs by filename.
|
|
|
|
// (Preserve any further ordering imposed by `FunctionCoverage`.)
|
|
|
|
// Prepare file IDs for each filename, and prepare the mapping data so that
|
|
|
|
// we can pass it through FFI to LLVM.
|
|
|
|
counter_regions.sort_by_key(|(_counter, region)| region.file_name);
|
2023-09-03 05:52:49 +00:00
|
|
|
for counter_regions_for_file in
|
|
|
|
counter_regions.group_by(|(_, a), (_, b)| a.file_name == b.file_name)
|
|
|
|
{
|
2023-10-03 10:40:50 +00:00
|
|
|
// Look up the global file ID for this filename.
|
2023-09-03 05:52:49 +00:00
|
|
|
let file_name = counter_regions_for_file[0].1.file_name;
|
|
|
|
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
|
|
|
|
|
|
|
|
// Associate that global file ID with a local file ID for this function.
|
|
|
|
let local_file_id: u32 = virtual_file_mapping.push(global_file_id);
|
|
|
|
debug!(" file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'");
|
|
|
|
|
|
|
|
// For each counter/region pair in this function+file, convert it to a
|
|
|
|
// form suitable for FFI.
|
|
|
|
for &(counter, region) in counter_regions_for_file {
|
|
|
|
let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = *region;
|
|
|
|
|
|
|
|
debug!("Adding counter {counter:?} to map for {region:?}");
|
2020-07-29 06:09:16 +00:00
|
|
|
mapping_regions.push(CounterMappingRegion::code_region(
|
2020-07-25 04:14:28 +00:00
|
|
|
counter,
|
2023-09-03 05:52:49 +00:00
|
|
|
local_file_id,
|
2020-07-25 04:14:28 +00:00
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
));
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
2023-09-01 13:27:45 +00:00
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2023-09-03 06:07:50 +00:00
|
|
|
// Encode the function's coverage mappings into a buffer.
|
|
|
|
llvm::build_byte_buffer(|buffer| {
|
2020-07-02 18:27:15 +00:00
|
|
|
coverageinfo::write_mapping_to_buffer(
|
2023-09-03 05:52:49 +00:00
|
|
|
virtual_file_mapping.raw,
|
2020-07-02 18:27:15 +00:00
|
|
|
expressions,
|
|
|
|
mapping_regions,
|
2023-09-03 06:07:50 +00:00
|
|
|
buffer,
|
2020-07-02 18:27:15 +00:00
|
|
|
);
|
2023-09-03 06:07:50 +00:00
|
|
|
})
|
2023-09-01 13:27:45 +00:00
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
|
2023-09-01 13:27:45 +00:00
|
|
|
/// Construct coverage map header and the array of function records, and combine them into the
|
|
|
|
/// coverage map. Save the coverage map data into the LLVM IR as a static global using a
|
|
|
|
/// specific, well-known section and name.
|
|
|
|
fn generate_coverage_map<'ll>(
|
|
|
|
cx: &CodegenCx<'ll, '_>,
|
|
|
|
version: u32,
|
|
|
|
filenames_size: usize,
|
|
|
|
filenames_val: &'ll llvm::Value,
|
|
|
|
) -> &'ll llvm::Value {
|
|
|
|
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
|
|
|
|
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
2020-11-23 20:56:07 +00:00
|
|
|
|
|
|
|
/// Construct a function record and combine it with the function's coverage mapping data.
|
|
|
|
/// Save the function record into the LLVM IR as a static global using a
|
|
|
|
/// specific, well-known section and name.
|
|
|
|
fn save_function_record(
|
2021-12-14 18:49:49 +00:00
|
|
|
cx: &CodegenCx<'_, '_>,
|
2023-07-24 11:15:11 +00:00
|
|
|
covfun_section_name: &str,
|
2023-07-07 12:49:45 +00:00
|
|
|
mangled_function_name: &str,
|
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
|
|
|
source_hash: u64,
|
2020-11-23 20:56:07 +00:00
|
|
|
filenames_ref: u64,
|
|
|
|
coverage_mapping_buffer: Vec<u8>,
|
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
|
|
|
is_used: bool,
|
2020-11-23 20:56:07 +00:00
|
|
|
) {
|
|
|
|
// Concatenate the encoded coverage mappings
|
|
|
|
let coverage_mapping_size = coverage_mapping_buffer.len();
|
2021-12-03 02:06:36 +00:00
|
|
|
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
|
2020-11-23 20:56:07 +00:00
|
|
|
|
2023-07-07 07:07:48 +00:00
|
|
|
let func_name_hash = coverageinfo::hash_bytes(mangled_function_name.as_bytes());
|
2020-11-23 20:56:07 +00:00
|
|
|
let func_name_hash_val = cx.const_u64(func_name_hash);
|
|
|
|
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
|
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 source_hash_val = cx.const_u64(source_hash);
|
2020-11-23 20:56:07 +00:00
|
|
|
let filenames_ref_val = cx.const_u64(filenames_ref);
|
|
|
|
let func_record_val = cx.const_struct(
|
|
|
|
&[
|
|
|
|
func_name_hash_val,
|
|
|
|
coverage_mapping_size_val,
|
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
|
|
|
source_hash_val,
|
2020-11-23 20:56:07 +00:00
|
|
|
filenames_ref_val,
|
|
|
|
coverage_mapping_val,
|
|
|
|
],
|
|
|
|
/*packed=*/ true,
|
|
|
|
);
|
|
|
|
|
2023-07-24 11:15:11 +00:00
|
|
|
coverageinfo::save_func_record_to_mod(
|
|
|
|
cx,
|
|
|
|
covfun_section_name,
|
|
|
|
func_name_hash,
|
|
|
|
func_record_val,
|
|
|
|
is_used,
|
|
|
|
);
|
2020-11-23 20:56:07 +00:00
|
|
|
}
|
2020-12-01 07:58:08 +00:00
|
|
|
|
|
|
|
/// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for
|
|
|
|
/// the functions that went through codegen; such as public functions and "used" functions
|
|
|
|
/// (functions referenced by other "used" or public items). Any other functions considered unused,
|
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
|
|
|
/// or "Unreachable", were still parsed and processed through the MIR stage, but were not
|
|
|
|
/// codegenned. (Note that `-Clink-dead-code` can force some unused code to be codegenned, but
|
2021-10-21 14:04:22 +00:00
|
|
|
/// that flag is known to cause other errors, when combined with `-C instrument-coverage`; and
|
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
|
|
|
/// `-Clink-dead-code` will not generate code for unused generic functions.)
|
2020-12-01 07:58:08 +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
|
|
|
/// We can find the unused functions (including generic functions) by the set difference of all MIR
|
2023-09-28 05:37:44 +00:00
|
|
|
/// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`codegenned_and_inlined_items`).
|
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;
|
2020-12-02 07:01:26 +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 ignore_unused_generics = tcx.sess.instrument_coverage_except_unused_generics();
|
2020-12-01 07:58:08 +00:00
|
|
|
|
2022-12-02 15:27:25 +00:00
|
|
|
let eligible_def_ids: Vec<DefId> = tcx
|
2021-05-11 10:26:53 +00:00
|
|
|
.mir_keys(())
|
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
|
|
|
.iter()
|
|
|
|
.filter_map(|local_def_id| {
|
|
|
|
let def_id = local_def_id.to_def_id();
|
2021-12-21 01:15:29 +00:00
|
|
|
let kind = tcx.def_kind(def_id);
|
|
|
|
// `mir_keys` will give us `DefId`s for all kinds of things, not
|
|
|
|
// just "functions", like consts, statics, etc. Filter those out.
|
|
|
|
// If `ignore_unused_generics` was specified, filter out any
|
|
|
|
// generic functions from consideration as well.
|
|
|
|
if !matches!(
|
|
|
|
kind,
|
2023-10-19 16:06:43 +00:00
|
|
|
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Coroutine
|
2021-12-21 01:15:29 +00:00
|
|
|
) {
|
|
|
|
return None;
|
2023-01-27 05:52:44 +00:00
|
|
|
}
|
|
|
|
if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(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
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(local_def_id.to_def_id())
|
|
|
|
})
|
|
|
|
.collect();
|
2020-12-01 07:58:08 +00:00
|
|
|
|
2023-09-28 05:37:44 +00:00
|
|
|
let codegenned_def_ids = codegenned_and_inlined_items(tcx);
|
2020-12-01 07:58:08 +00:00
|
|
|
|
2023-09-24 12:26:12 +00:00
|
|
|
// For each `DefId` that should have coverage instrumentation but wasn't
|
|
|
|
// codegenned, add it to the function coverage map as an unused function.
|
|
|
|
for def_id in eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id)) {
|
2023-09-10 06:35:37 +00:00
|
|
|
// Skip any function that didn't have coverage data added to it by the
|
|
|
|
// coverage instrumentor.
|
2023-09-24 12:26:12 +00:00
|
|
|
let body = tcx.instance_mir(ty::InstanceDef::Item(def_id));
|
2023-09-10 06:35:37 +00:00
|
|
|
let Some(function_coverage_info) = body.function_coverage_info.as_deref() else {
|
2021-12-21 01:15:29 +00:00
|
|
|
continue;
|
2023-09-10 06:35:37 +00:00
|
|
|
};
|
2021-12-21 01:15:29 +00:00
|
|
|
|
2023-09-24 12:26:12 +00:00
|
|
|
debug!("generating unused fn: {def_id:?}");
|
|
|
|
let instance = declare_unused_fn(tcx, def_id);
|
|
|
|
add_unused_function_coverage(cx, instance, function_coverage_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 05:37:44 +00:00
|
|
|
/// All items participating in code generation together with (instrumented)
|
|
|
|
/// items inlined into them.
|
|
|
|
fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
|
|
|
|
let (items, cgus) = tcx.collect_and_partition_mono_items(());
|
|
|
|
let mut visited = DefIdSet::default();
|
|
|
|
let mut result = items.clone();
|
|
|
|
|
|
|
|
for cgu in cgus {
|
|
|
|
for item in cgu.items().keys() {
|
|
|
|
if let mir::mono::MonoItem::Fn(ref instance) = item {
|
|
|
|
let did = instance.def_id();
|
|
|
|
if !visited.insert(did) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let body = tcx.instance_mir(instance.def);
|
|
|
|
for block in body.basic_blocks.iter() {
|
|
|
|
for statement in &block.statements {
|
|
|
|
let mir::StatementKind::Coverage(_) = statement.kind else { continue };
|
|
|
|
let scope = statement.source_info.scope;
|
|
|
|
if let Some(inlined) = scope.inlined_instance(&body.source_scopes) {
|
|
|
|
result.insert(inlined.def_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-09-24 12:26:12 +00:00
|
|
|
fn declare_unused_fn<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::Instance<'tcx> {
|
|
|
|
ty::Instance::new(
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_unused_function_coverage<'tcx>(
|
|
|
|
cx: &CodegenCx<'_, 'tcx>,
|
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
function_coverage_info: &'tcx mir::coverage::FunctionCoverageInfo,
|
|
|
|
) {
|
|
|
|
// An unused function's mappings will automatically be rewritten to map to
|
|
|
|
// zero, because none of its counters/expressions are marked as seen.
|
2023-10-06 01:51:48 +00:00
|
|
|
let function_coverage = FunctionCoverageCollector::unused(instance, function_coverage_info);
|
2023-09-24 12:26:12 +00:00
|
|
|
|
|
|
|
if let Some(coverage_context) = cx.coverage_context() {
|
|
|
|
coverage_context.function_coverage_map.borrow_mut().insert(instance, function_coverage);
|
|
|
|
} else {
|
|
|
|
bug!("Could not get the `coverage_context`");
|
2020-12-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
}
|