Updates to experimental coverage counter injection
This is a combination of 18 commits.
Commit #2:
Additional examples and some small improvements.
Commit #3:
fixed mir-opt non-mir extensions and spanview title elements
Corrected a fairly recent assumption in runtest.rs that all MIR dump
files end in .mir. (It was appending .mir to the graphviz .dot and
spanview .html file names when generating blessed output files. That
also left outdated files in the baseline alongside the files with the
incorrect names, which I've now removed.)
Updated spanview HTML title elements to match their content, replacing a
hardcoded and incorrect name that was left in accidentally when
originally submitted.
Commit #4:
added more test examples
also improved Makefiles with support for non-zero exit status and to
force validation of tests unless a specific test overrides it with a
specific comment.
Commit #5:
Fixed rare issues after testing on real-world crate
Commit #6:
Addressed PR feedback, and removed temporary -Zexperimental-coverage
-Zinstrument-coverage once again supports the latest capabilities of
LLVM instrprof coverage instrumentation.
Also fixed a bug in spanview.
Commit #7:
Fix closure handling, add tests for closures and inner items
And cleaned up other tests for consistency, and to make it more clear
where spans start/end by breaking up lines.
Commit #8:
renamed "typical" test results "expected"
Now that the `llvm-cov show` tests are improved to normally expect
matching actuals, and to allow individual tests to override that
expectation.
Commit #9:
test coverage of inline generic struct function
Commit #10:
Addressed review feedback
* Removed unnecessary Unreachable filter.
* Replaced a match wildcard with remining variants.
* Added more comments to help clarify the role of successors() in the
CFG traversal
Commit #11:
refactoring based on feedback
* refactored `fn coverage_spans()`.
* changed the way I expand an empty coverage span to improve performance
* fixed a typo that I had accidently left in, in visit.rs
Commit #12:
Optimized use of SourceMap and SourceFile
Commit #13:
Fixed a regression, and synched with upstream
Some generated test file names changed due to some new change upstream.
Commit #14:
Stripping out crate disambiguators from demangled names
These can vary depending on the test platform.
Commit #15:
Ignore llvm-cov show diff on test with generics, expand IO error message
Tests with generics produce llvm-cov show results with demangled names
that can include an unstable "crate disambiguator" (hex value). The
value changes when run in the Rust CI Windows environment. I added a sed
filter to strip them out (in a prior commit), but sed also appears to
fail in the same environment. Until I can figure out a workaround, I'm
just going to ignore this specific test result. I added a FIXME to
follow up later, but it's not that critical.
I also saw an error with Windows GNU, but the IO error did not
specify a path for the directory or file that triggered the error. I
updated the error messages to provide more info for next, time but also
noticed some other tests with similar steps did not fail. Looks
spurious.
Commit #16:
Modify rust-demangler to strip disambiguators by default
Commit #17:
Remove std::process::exit from coverage tests
Due to Issue #77553, programs that call std::process::exit() do not
generate coverage results on Windows MSVC.
Commit #18:
fix: test file paths exceeding Windows max path len
2020-09-01 18:15:17 -05:00
|
|
|
#![allow(unused_assignments, unused_variables)]
|
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 18:32:45 -05:00
|
|
|
// compile-flags: -C opt-level=2 # fix described in rustc_middle/mir/mono.rs
|
Updates to experimental coverage counter injection
This is a combination of 18 commits.
Commit #2:
Additional examples and some small improvements.
Commit #3:
fixed mir-opt non-mir extensions and spanview title elements
Corrected a fairly recent assumption in runtest.rs that all MIR dump
files end in .mir. (It was appending .mir to the graphviz .dot and
spanview .html file names when generating blessed output files. That
also left outdated files in the baseline alongside the files with the
incorrect names, which I've now removed.)
Updated spanview HTML title elements to match their content, replacing a
hardcoded and incorrect name that was left in accidentally when
originally submitted.
Commit #4:
added more test examples
also improved Makefiles with support for non-zero exit status and to
force validation of tests unless a specific test overrides it with a
specific comment.
Commit #5:
Fixed rare issues after testing on real-world crate
Commit #6:
Addressed PR feedback, and removed temporary -Zexperimental-coverage
-Zinstrument-coverage once again supports the latest capabilities of
LLVM instrprof coverage instrumentation.
Also fixed a bug in spanview.
Commit #7:
Fix closure handling, add tests for closures and inner items
And cleaned up other tests for consistency, and to make it more clear
where spans start/end by breaking up lines.
Commit #8:
renamed "typical" test results "expected"
Now that the `llvm-cov show` tests are improved to normally expect
matching actuals, and to allow individual tests to override that
expectation.
Commit #9:
test coverage of inline generic struct function
Commit #10:
Addressed review feedback
* Removed unnecessary Unreachable filter.
* Replaced a match wildcard with remining variants.
* Added more comments to help clarify the role of successors() in the
CFG traversal
Commit #11:
refactoring based on feedback
* refactored `fn coverage_spans()`.
* changed the way I expand an empty coverage span to improve performance
* fixed a typo that I had accidently left in, in visit.rs
Commit #12:
Optimized use of SourceMap and SourceFile
Commit #13:
Fixed a regression, and synched with upstream
Some generated test file names changed due to some new change upstream.
Commit #14:
Stripping out crate disambiguators from demangled names
These can vary depending on the test platform.
Commit #15:
Ignore llvm-cov show diff on test with generics, expand IO error message
Tests with generics produce llvm-cov show results with demangled names
that can include an unstable "crate disambiguator" (hex value). The
value changes when run in the Rust CI Windows environment. I added a sed
filter to strip them out (in a prior commit), but sed also appears to
fail in the same environment. Until I can figure out a workaround, I'm
just going to ignore this specific test result. I added a FIXME to
follow up later, but it's not that critical.
I also saw an error with Windows GNU, but the IO error did not
specify a path for the directory or file that triggered the error. I
updated the error messages to provide more info for next, time but also
noticed some other tests with similar steps did not fail. Looks
spurious.
Commit #16:
Modify rust-demangler to strip disambiguators by default
Commit #17:
Remove std::process::exit from coverage tests
Due to Issue #77553, programs that call std::process::exit() do not
generate coverage results on Windows MSVC.
Commit #18:
fix: test file paths exceeding Windows max path len
2020-09-01 18:15:17 -05:00
|
|
|
fn main() {
|
|
|
|
// Initialize test constants in a way that cannot be determined at compile time, to ensure
|
|
|
|
// rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
|
|
|
|
// dependent conditions.
|
|
|
|
let is_true = std::env::args().len() == 1;
|
|
|
|
let is_false = ! is_true;
|
|
|
|
|
|
|
|
let mut some_string = Some(String::from("the string content"));
|
|
|
|
println!(
|
|
|
|
"The string or alt: {}"
|
|
|
|
,
|
|
|
|
some_string
|
|
|
|
.
|
|
|
|
unwrap_or_else
|
|
|
|
(
|
|
|
|
||
|
|
|
|
{
|
|
|
|
let mut countdown = 0;
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
"alt string 1".to_owned()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
some_string = Some(String::from("the string content"));
|
|
|
|
let
|
|
|
|
a
|
|
|
|
=
|
|
|
|
||
|
|
|
|
{
|
|
|
|
let mut countdown = 0;
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
"alt string 2".to_owned()
|
|
|
|
};
|
|
|
|
println!(
|
|
|
|
"The string or alt: {}"
|
|
|
|
,
|
|
|
|
some_string
|
|
|
|
.
|
|
|
|
unwrap_or_else
|
|
|
|
(
|
|
|
|
a
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
some_string = None;
|
|
|
|
println!(
|
|
|
|
"The string or alt: {}"
|
|
|
|
,
|
|
|
|
some_string
|
|
|
|
.
|
|
|
|
unwrap_or_else
|
|
|
|
(
|
|
|
|
||
|
|
|
|
{
|
|
|
|
let mut countdown = 0;
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
"alt string 3".to_owned()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
some_string = None;
|
|
|
|
let
|
|
|
|
a
|
|
|
|
=
|
|
|
|
||
|
|
|
|
{
|
|
|
|
let mut countdown = 0;
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
"alt string 4".to_owned()
|
|
|
|
};
|
|
|
|
println!(
|
|
|
|
"The string or alt: {}"
|
|
|
|
,
|
|
|
|
some_string
|
|
|
|
.
|
|
|
|
unwrap_or_else
|
|
|
|
(
|
|
|
|
a
|
|
|
|
)
|
|
|
|
);
|
2020-12-01 01:58:08 -06:00
|
|
|
|
|
|
|
let
|
|
|
|
quote_closure
|
|
|
|
=
|
|
|
|
|val|
|
|
|
|
{
|
|
|
|
let mut countdown = 0;
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
format!("'{}'", val)
|
|
|
|
};
|
|
|
|
println!(
|
|
|
|
"Repeated, quoted string: {:?}"
|
|
|
|
,
|
|
|
|
std::iter::repeat("repeat me")
|
|
|
|
.take(5)
|
|
|
|
.map
|
|
|
|
(
|
|
|
|
quote_closure
|
|
|
|
)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
|
|
|
|
let
|
|
|
|
_unused_closure
|
|
|
|
=
|
|
|
|
|
|
|
|
|
mut countdown
|
|
|
|
|
|
|
|
|
{
|
|
|
|
if is_false {
|
|
|
|
countdown = 10;
|
|
|
|
}
|
|
|
|
"closure should be unused".to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut countdown = 10;
|
|
|
|
let _short_unused_closure = | _unused_arg: u8 | countdown += 1;
|
|
|
|
|
Coverage instruments closure bodies in macros (not the macro body)
Fixes: #84884
This solution might be considered a compromise, but I think it is the
better choice.
The results in the `closure.rs` test correctly resolve all test cases
broken as described in #84884.
One test pattern (in both `closure_macro.rs` and
`closure_macro_async.rs`) was also affected, and removes coverage
statistics for the lines inside the closure, because the closure
includes a macro. (The coverage remains at the callsite of the macro, so
we lose some detail, but there isn't a perfect choice with macros.
Often macro implementations are split across the macro and the callsite,
and there doesn't appear to be a single "right choice" for which body
should be covered. For the current implementation, we can't do both.
The callsite is most likely to be the preferred site for coverage.
I applied this fix to all `MacroKinds`, not just `Bang`.
I'm trying to resolve an issue of lost coverage in a
`MacroKind::Attr`-based, function-scoped macro. Instead of only
searching for a body_span that is "not a function-like macro" (that is,
MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should
expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or
subsets, depending on their sub-kinds) as well, but I'm not sure that's
a good idea.
I'd like to add a test of the `Attr` macro on functions, but I need time
to figure out how to constract a good, simple example without external
crate dependencies. For the moment, all tests still work as expected (no
change), this new commit shouldn't have a negative affect, and more
importantly, I believe it will have a positive effect. I will try to
confirm this.
2021-05-04 01:21:24 -05:00
|
|
|
|
|
|
|
let short_used_covered_closure_macro = | used_arg: u8 | println!("called");
|
|
|
|
let short_used_not_covered_closure_macro = | used_arg: u8 | println!("not called");
|
|
|
|
let _short_unused_closure_macro = | _unused_arg: u8 | println!("not called");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-01 01:58:08 -06:00
|
|
|
let _short_unused_closure_block = | _unused_arg: u8 | { println!("not called") };
|
|
|
|
|
|
|
|
let _shortish_unused_closure = | _unused_arg: u8 | {
|
|
|
|
println!("not called")
|
|
|
|
};
|
|
|
|
|
|
|
|
let _as_short_unused_closure = |
|
|
|
|
_unused_arg: u8
|
|
|
|
| { println!("not called") };
|
|
|
|
|
|
|
|
let _almost_as_short_unused_closure = |
|
|
|
|
_unused_arg: u8
|
|
|
|
| { println!("not called") }
|
|
|
|
;
|
Coverage instruments closure bodies in macros (not the macro body)
Fixes: #84884
This solution might be considered a compromise, but I think it is the
better choice.
The results in the `closure.rs` test correctly resolve all test cases
broken as described in #84884.
One test pattern (in both `closure_macro.rs` and
`closure_macro_async.rs`) was also affected, and removes coverage
statistics for the lines inside the closure, because the closure
includes a macro. (The coverage remains at the callsite of the macro, so
we lose some detail, but there isn't a perfect choice with macros.
Often macro implementations are split across the macro and the callsite,
and there doesn't appear to be a single "right choice" for which body
should be covered. For the current implementation, we can't do both.
The callsite is most likely to be the preferred site for coverage.
I applied this fix to all `MacroKinds`, not just `Bang`.
I'm trying to resolve an issue of lost coverage in a
`MacroKind::Attr`-based, function-scoped macro. Instead of only
searching for a body_span that is "not a function-like macro" (that is,
MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should
expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or
subsets, depending on their sub-kinds) as well, but I'm not sure that's
a good idea.
I'd like to add a test of the `Attr` macro on functions, but I need time
to figure out how to constract a good, simple example without external
crate dependencies. For the moment, all tests still work as expected (no
change), this new commit shouldn't have a negative affect, and more
importantly, I believe it will have a positive effect. I will try to
confirm this.
2021-05-04 01:21:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let _short_unused_closure_line_break_no_block = | _unused_arg: u8 |
|
|
|
|
println!("not called")
|
|
|
|
;
|
|
|
|
|
|
|
|
let _short_unused_closure_line_break_no_block2 =
|
|
|
|
| _unused_arg: u8 |
|
|
|
|
println!(
|
|
|
|
"not called"
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
let short_used_not_covered_closure_line_break_no_block_embedded_branch =
|
|
|
|
| _unused_arg: u8 |
|
|
|
|
println!(
|
|
|
|
"not called: {}",
|
|
|
|
if is_true { "check" } else { "me" }
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
let short_used_not_covered_closure_line_break_block_embedded_branch =
|
|
|
|
| _unused_arg: u8 |
|
|
|
|
{
|
|
|
|
println!(
|
|
|
|
"not called: {}",
|
|
|
|
if is_true { "check" } else { "me" }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
let short_used_covered_closure_line_break_no_block_embedded_branch =
|
|
|
|
| _unused_arg: u8 |
|
|
|
|
println!(
|
|
|
|
"not called: {}",
|
|
|
|
if is_true { "check" } else { "me" }
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
let short_used_covered_closure_line_break_block_embedded_branch =
|
|
|
|
| _unused_arg: u8 |
|
|
|
|
{
|
|
|
|
println!(
|
|
|
|
"not called: {}",
|
|
|
|
if is_true { "check" } else { "me" }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
if is_false {
|
|
|
|
short_used_not_covered_closure_macro(0);
|
|
|
|
short_used_not_covered_closure_line_break_no_block_embedded_branch(0);
|
|
|
|
short_used_not_covered_closure_line_break_block_embedded_branch(0);
|
|
|
|
}
|
|
|
|
short_used_covered_closure_macro(0);
|
|
|
|
short_used_covered_closure_line_break_no_block_embedded_branch(0);
|
|
|
|
short_used_covered_closure_line_break_block_embedded_branch(0);
|
Updates to experimental coverage counter injection
This is a combination of 18 commits.
Commit #2:
Additional examples and some small improvements.
Commit #3:
fixed mir-opt non-mir extensions and spanview title elements
Corrected a fairly recent assumption in runtest.rs that all MIR dump
files end in .mir. (It was appending .mir to the graphviz .dot and
spanview .html file names when generating blessed output files. That
also left outdated files in the baseline alongside the files with the
incorrect names, which I've now removed.)
Updated spanview HTML title elements to match their content, replacing a
hardcoded and incorrect name that was left in accidentally when
originally submitted.
Commit #4:
added more test examples
also improved Makefiles with support for non-zero exit status and to
force validation of tests unless a specific test overrides it with a
specific comment.
Commit #5:
Fixed rare issues after testing on real-world crate
Commit #6:
Addressed PR feedback, and removed temporary -Zexperimental-coverage
-Zinstrument-coverage once again supports the latest capabilities of
LLVM instrprof coverage instrumentation.
Also fixed a bug in spanview.
Commit #7:
Fix closure handling, add tests for closures and inner items
And cleaned up other tests for consistency, and to make it more clear
where spans start/end by breaking up lines.
Commit #8:
renamed "typical" test results "expected"
Now that the `llvm-cov show` tests are improved to normally expect
matching actuals, and to allow individual tests to override that
expectation.
Commit #9:
test coverage of inline generic struct function
Commit #10:
Addressed review feedback
* Removed unnecessary Unreachable filter.
* Replaced a match wildcard with remining variants.
* Added more comments to help clarify the role of successors() in the
CFG traversal
Commit #11:
refactoring based on feedback
* refactored `fn coverage_spans()`.
* changed the way I expand an empty coverage span to improve performance
* fixed a typo that I had accidently left in, in visit.rs
Commit #12:
Optimized use of SourceMap and SourceFile
Commit #13:
Fixed a regression, and synched with upstream
Some generated test file names changed due to some new change upstream.
Commit #14:
Stripping out crate disambiguators from demangled names
These can vary depending on the test platform.
Commit #15:
Ignore llvm-cov show diff on test with generics, expand IO error message
Tests with generics produce llvm-cov show results with demangled names
that can include an unstable "crate disambiguator" (hex value). The
value changes when run in the Rust CI Windows environment. I added a sed
filter to strip them out (in a prior commit), but sed also appears to
fail in the same environment. Until I can figure out a workaround, I'm
just going to ignore this specific test result. I added a FIXME to
follow up later, but it's not that critical.
I also saw an error with Windows GNU, but the IO error did not
specify a path for the directory or file that triggered the error. I
updated the error messages to provide more info for next, time but also
noticed some other tests with similar steps did not fail. Looks
spurious.
Commit #16:
Modify rust-demangler to strip disambiguators by default
Commit #17:
Remove std::process::exit from coverage tests
Due to Issue #77553, programs that call std::process::exit() do not
generate coverage results on Windows MSVC.
Commit #18:
fix: test file paths exceeding Windows max path len
2020-09-01 18:15:17 -05:00
|
|
|
}
|