Auto merge of #83307 - richkadel:cov-unused-functions-1.1, r=tmandry
coverage bug fixes and optimization support Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to address multiple, somewhat related issues. Fixed a significant flaw in prior coverage solution: Every counter generated a new counter variable, but there should have only been one counter variable per function. This appears to have bloated .profraw files significantly. (For a small program, it increased the size by about 40%. I have not tested large programs, but there is anecdotal evidence that profraw files were way too large. This is a good fix, regardless, but hopefully it also addresses related issues. Fixes: #82144 Invalid LLVM coverage data produced when compiled with -C opt-level=1 Existing tests now work up to at least `opt-level=3`. This required a detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR when compiled with coverage, and a lot of trial and error with codegen adjustments. The biggest hurdle was figuring out how to continue to support coverage results for unused functions and generics. Rust's coverage results have three advantages over Clang's coverage results: 1. Rust's coverage map does not include any overlapping code regions, making coverage counting unambiguous. 2. Rust generates coverage results (showing zero counts) for all unused functions, including generics. (Clang does not generate coverage for uninstantiated template functions.) 3. Rust's unused functions produce minimal stubbed functions in LLVM IR, sufficient for including in the coverage results; while Clang must generate the complete LLVM IR for each unused function, even though it will never be called. This PR removes the previous hack of attempting to inject coverage into some other existing function instance, and generates dedicated instances for each unused function. This change, and a few other adjustments (similar to what is required for `-C link-dead-code`, but with lower impact), makes it possible to support LLVM optimizations. Fixes: #79651 Coverage report: "Unexecuted instantiation:..." for a generic function from multiple crates Fixed by removing the aforementioned hack. Some "Unexecuted instantiation" notices are unavoidable, as explained in the `used_crate.rs` test, but `-Zinstrument-coverage` has new options to back off support for either unused generics, or all unused functions, which avoids the notice, at the cost of less coverage of unused functions. Fixes: #82875 Invalid LLVM coverage data produced with crate brotli_decompressor Fixed by disabling the LLVM function attribute that forces inlining, if `-Z instrument-coverage` is enabled. This attribute is applied to Rust functions with `#[inline(always)], and in some cases, the forced inlining breaks coverage instrumentation and reports. FYI: `@wesleywiser` r? `@tmandry`
This commit is contained in:
commit
dbc37a97dc
62 changed files with 3065 additions and 342 deletions
|
@ -184,6 +184,37 @@ pub enum MirSpanview {
|
|||
Block,
|
||||
}
|
||||
|
||||
/// The different settings that the `-Z instrument-coverage` flag can have.
|
||||
///
|
||||
/// Coverage instrumentation now supports combining `-Z instrument-coverage`
|
||||
/// with compiler and linker optimization (enabled with `-O` or `-C opt-level=1`
|
||||
/// and higher). Nevertheless, there are many variables, depending on options
|
||||
/// selected, code structure, and enabled attributes. If errors are encountered,
|
||||
/// either while compiling or when generating `llvm-cov show` reports, consider
|
||||
/// lowering the optimization level, including or excluding `-C link-dead-code`,
|
||||
/// or using `-Z instrument-coverage=except-unused-functions` or `-Z
|
||||
/// instrument-coverage=except-unused-generics`.
|
||||
///
|
||||
/// Note that `ExceptUnusedFunctions` means: When `mapgen.rs` generates the
|
||||
/// coverage map, it will not attempt to generate synthetic functions for unused
|
||||
/// (and not code-generated) functions (whether they are generic or not). As a
|
||||
/// result, non-codegenned functions will not be included in the coverage map,
|
||||
/// and will not appear, as covered or uncovered, in coverage reports.
|
||||
///
|
||||
/// `ExceptUnusedGenerics` will add synthetic functions to the coverage map,
|
||||
/// unless the function has type parameters.
|
||||
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
|
||||
pub enum InstrumentCoverage {
|
||||
/// Default `-Z instrument-coverage` or `-Z instrument-coverage=statement`
|
||||
All,
|
||||
/// `-Z instrument-coverage=except-unused-generics`
|
||||
ExceptUnusedGenerics,
|
||||
/// `-Z instrument-coverage=except-unused-functions`
|
||||
ExceptUnusedFunctions,
|
||||
/// `-Z instrument-coverage=off` (or `no`, etc.)
|
||||
Off,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Hash)]
|
||||
pub enum LinkerPluginLto {
|
||||
LinkerPlugin(PathBuf),
|
||||
|
@ -1911,7 +1942,9 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
|
|||
);
|
||||
}
|
||||
|
||||
if debugging_opts.instrument_coverage {
|
||||
if debugging_opts.instrument_coverage.is_some()
|
||||
&& debugging_opts.instrument_coverage != Some(InstrumentCoverage::Off)
|
||||
{
|
||||
if cg.profile_generate.enabled() || cg.profile_use.is_some() {
|
||||
early_error(
|
||||
error_format,
|
||||
|
@ -2298,9 +2331,9 @@ impl PpMode {
|
|||
/// how the hash should be calculated when adding a new command-line argument.
|
||||
crate mod dep_tracking {
|
||||
use super::{
|
||||
CFGuard, CrateType, DebugInfo, ErrorOutputType, LinkerPluginLto, LtoCli, OptLevel,
|
||||
OutputTypes, Passes, SanitizerSet, SourceFileHashAlgorithm, SwitchWithOptPath,
|
||||
SymbolManglingVersion, TrimmedDefPaths,
|
||||
CFGuard, CrateType, DebugInfo, ErrorOutputType, InstrumentCoverage, LinkerPluginLto,
|
||||
LtoCli, OptLevel, OutputTypes, Passes, SanitizerSet, SourceFileHashAlgorithm,
|
||||
SwitchWithOptPath, SymbolManglingVersion, TrimmedDefPaths,
|
||||
};
|
||||
use crate::lint;
|
||||
use crate::options::WasiExecModel;
|
||||
|
@ -2364,6 +2397,7 @@ crate mod dep_tracking {
|
|||
impl_dep_tracking_hash_via_hash!(Option<WasiExecModel>);
|
||||
impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
|
||||
impl_dep_tracking_hash_via_hash!(Option<RelroLevel>);
|
||||
impl_dep_tracking_hash_via_hash!(Option<InstrumentCoverage>);
|
||||
impl_dep_tracking_hash_via_hash!(Option<lint::Level>);
|
||||
impl_dep_tracking_hash_via_hash!(Option<PathBuf>);
|
||||
impl_dep_tracking_hash_via_hash!(CrateType);
|
||||
|
|
|
@ -262,6 +262,7 @@ macro_rules! options {
|
|||
pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavor::one_of();
|
||||
pub const parse_optimization_fuel: &str = "crate=integer";
|
||||
pub const parse_mir_spanview: &str = "`statement` (default), `terminator`, or `block`";
|
||||
pub const parse_instrument_coverage: &str = "`all` (default), `except-unused-generics`, `except-unused-functions`, or `off`";
|
||||
pub const parse_unpretty: &str = "`string` or `string=string`";
|
||||
pub const parse_treat_err_as_bug: &str = "either no value or a number bigger than 0";
|
||||
pub const parse_lto: &str =
|
||||
|
@ -592,6 +593,41 @@ macro_rules! options {
|
|||
true
|
||||
}
|
||||
|
||||
fn parse_instrument_coverage(slot: &mut Option<InstrumentCoverage>, v: Option<&str>) -> bool {
|
||||
if v.is_some() {
|
||||
let mut bool_arg = None;
|
||||
if parse_opt_bool(&mut bool_arg, v) {
|
||||
*slot = if bool_arg.unwrap() {
|
||||
Some(InstrumentCoverage::All)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
let v = match v {
|
||||
None => {
|
||||
*slot = Some(InstrumentCoverage::All);
|
||||
return true;
|
||||
}
|
||||
Some(v) => v,
|
||||
};
|
||||
|
||||
*slot = Some(match v {
|
||||
"all" => InstrumentCoverage::All,
|
||||
"except-unused-generics" | "except_unused_generics" => {
|
||||
InstrumentCoverage::ExceptUnusedGenerics
|
||||
}
|
||||
"except-unused-functions" | "except_unused_functions" => {
|
||||
InstrumentCoverage::ExceptUnusedFunctions
|
||||
}
|
||||
"off" | "no" | "n" | "false" | "0" => InstrumentCoverage::Off,
|
||||
_ => return false,
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
fn parse_treat_err_as_bug(slot: &mut Option<NonZeroUsize>, v: Option<&str>) -> bool {
|
||||
match v {
|
||||
Some(s) => { *slot = s.parse().ok(); slot.is_some() }
|
||||
|
@ -967,12 +1003,14 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
|
|||
"control whether `#[inline]` functions are in all CGUs"),
|
||||
input_stats: bool = (false, parse_bool, [UNTRACKED],
|
||||
"gather statistics about the input (default: no)"),
|
||||
instrument_coverage: bool = (false, parse_bool, [TRACKED],
|
||||
instrument_coverage: Option<InstrumentCoverage> = (None, parse_instrument_coverage, [TRACKED],
|
||||
"instrument the generated code to support LLVM source-based code coverage \
|
||||
reports (note, the compiler build config must include `profiler = true`, \
|
||||
and is mutually exclusive with `-C profile-generate`/`-C profile-use`); \
|
||||
implies `-Z symbol-mangling-version=v0`; disables/overrides some Rust \
|
||||
optimizations (default: no)"),
|
||||
optimizations. Optional values are: `=all` (default coverage), \
|
||||
`=except-unused-generics`, `=except-unused-functions`, or `=off` \
|
||||
(default: instrument-coverage=off)"),
|
||||
instrument_mcount: bool = (false, parse_bool, [TRACKED],
|
||||
"insert function instrument code for mcount-based tracing (default: no)"),
|
||||
keep_hygiene_data: bool = (false, parse_bool, [UNTRACKED],
|
||||
|
|
|
@ -1150,6 +1150,21 @@ impl Session {
|
|||
self.opts.cg.link_dead_code.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn instrument_coverage(&self) -> bool {
|
||||
self.opts.debugging_opts.instrument_coverage.unwrap_or(config::InstrumentCoverage::Off)
|
||||
!= config::InstrumentCoverage::Off
|
||||
}
|
||||
|
||||
pub fn instrument_coverage_except_unused_generics(&self) -> bool {
|
||||
self.opts.debugging_opts.instrument_coverage.unwrap_or(config::InstrumentCoverage::Off)
|
||||
== config::InstrumentCoverage::ExceptUnusedGenerics
|
||||
}
|
||||
|
||||
pub fn instrument_coverage_except_unused_functions(&self) -> bool {
|
||||
self.opts.debugging_opts.instrument_coverage.unwrap_or(config::InstrumentCoverage::Off)
|
||||
== config::InstrumentCoverage::ExceptUnusedFunctions
|
||||
}
|
||||
|
||||
pub fn mark_attr_known(&self, attr: &Attribute) {
|
||||
self.known_attrs.lock().mark(attr)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue