Skip to content

Remove reachable coverage without counters #99711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn remove_dead_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
/// with `0` executions.
///
/// If there are no live `Counter` `Coverage` statements remaining, we remove
/// dead `Coverage` statements along with the dead blocks. Since at least one
/// `Coverage` statements along with the dead blocks. Since at least one
/// counter per function is required by LLVM (and necessary, to add the
/// `function_hash` to the counter's call to the LLVM intrinsic
/// `instrprof.increment()`).
Expand All @@ -342,6 +342,16 @@ fn save_unreachable_coverage(
}
}

for block in &mut basic_blocks.raw[..first_dead_block] {
for statement in &mut block.statements {
let StatementKind::Coverage(_) = &statement.kind else { continue };
let instance = statement.source_info.scope.inlined_instance(source_scopes);
if !live.contains(&instance) {
statement.make_nop();
}
}
}

if live.is_empty() {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
1| |// Regression test for issue #98833.
2| |// compile-flags: -Zinline-mir
2| |// compile-flags: -Zinline-mir -Cdebug-assertions=off
3| |
4| 1|fn main() {
5| 1| println!("{}", live::<false>());
6| 1|}
7| |
8| |#[inline]
9| 1|fn live<const B: bool>() -> u32 {
10| 1| if B {
11| 0| dead()
12| | } else {
13| 1| 0
14| | }
15| 1|}
16| |
17| |#[inline]
18| 0|fn dead() -> u32 {
19| 0| 42
20| 0|}
6| 1|
7| 1| let f = |x: bool| {
8| | debug_assert!(
9| | x
10| | );
11| 1| };
12| 1| f(false);
13| 1|}
14| |
15| |#[inline]
16| 1|fn live<const B: bool>() -> u32 {
17| 1| if B {
18| 0| dead()
19| | } else {
20| 1| 0
21| | }
22| 1|}
23| |
24| |#[inline]
25| 0|fn dead() -> u32 {
26| 0| 42
27| 0|}

9 changes: 8 additions & 1 deletion src/test/run-make-fulldeps/coverage/inline-dead.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// Regression test for issue #98833.
// compile-flags: -Zinline-mir
// compile-flags: -Zinline-mir -Cdebug-assertions=off

fn main() {
println!("{}", live::<false>());

let f = |x: bool| {
debug_assert!(
x
);
};
f(false);
}

#[inline]
Expand Down