Skip to content
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

Allow MIR pass UnreachableProp when coverage is enabled #116938

Closed
wants to merge 3 commits into from

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Oct 19, 2023

Follow-up to this pass being disabled for coverage in #116166; fixes #116171.

This optimization pass can potentially remove all counter-increment statements from an instrumented function. Previously that would cause it to disappear from coverage reports, so the pass had to be turned off during coverage builds.

This PR introduces a more robust solution: we add an additional post-optimization MIR pass that detects when an instrumented function has lost all of its counter-increment statements, and simply adds another counter-increment statement. That allows coverage codegen to emit the function's coverage mappings as normal.

@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2023
@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@Zalathar
Copy link
Contributor Author

cc @cjgillot for awareness because you have been working on this pass, though I don't think this change directly affects your work

@Zalathar
Copy link
Contributor Author

@rustbot label +A-code-coverage +A-mir-opt

@rustbot rustbot added A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) A-mir-opt Area: MIR optimizations labels Oct 19, 2023
@Zalathar Zalathar changed the title Allow MIR pass UnreachableProp when coverage is enabled Allow MIR pass UnreachableProp when coverage is enabled Oct 19, 2023
/// instrumented, and it will disappear from coverage mappings and coverage reports.
///
/// This pass detects when that has happened, and re-inserts a dummy counter-increment
/// statement so that LLVM knows to treat the function as instrumented.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be done by codegen directly. When starting codegen for a mono-item, check if function_coverage_info is set, and add this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds reasonable. I'll have to track down the right place to add a hook, but once I do the result should be more self-contained in codegen.

@cjgillot cjgillot self-assigned this Oct 19, 2023
This test is mainly for detecting problems triggered by MIR optimizations, but
the run-coverage tests don't enable optimization by default.

(The coverage-map copy has also been updated, to keep the two tests in sync.)
…ters

If a function has been instrumented for coverage, but MIR optimizations
subsequently remove all of its counter-increment statements, then we won't emit
LLVM counter-increment intrinsics. LLVM will think the function is not
instrumented, and it will disappear from coverage mappings and coverage
reports.

This new MIR pass detects when that has happened, and re-inserts a dummy
counter-increment statement so that LLVM knows to treat the function as
instrumented.
Now that coverage has a dedicated MIR pass to detect and repair instrumented
functions that have lost all their counters, it should be OK to permit this
pass in coverage builds.
@Zalathar
Copy link
Contributor Author

Hmm, I was investigating this some more, and found that I couldn't get my tests to fail in the way they were failing before.

That means I can't be confident that my fix is working, or even necessary.

I'll put this on hold until I figure out what's going on.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 20, 2023
@Zalathar Zalathar marked this pull request as draft October 20, 2023 06:42
@bors
Copy link
Contributor

bors commented Nov 8, 2023

☔ The latest upstream changes (presumably #117484) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@Zalathar any updates on this? Thanks

@Zalathar
Copy link
Contributor Author

@Dylan-DPC This has been a low priority for me, so no updates unfortunately.

@Zalathar
Copy link
Contributor Author

I found a much nicer way to do this:

  • When looking for unused functions, we already need to scan through the body of each codegenned function, looking for coverage statements that were inlined from other functions.
  • During this scan, we can also keep track of whether we’ve seen any of the function’s own coverage statements.
  • If a function was instrumented but has no coverage statements, we can just treat it as an unused function.

@Zalathar
Copy link
Contributor Author

Closing in favour of #122860.

@Zalathar Zalathar closed this Mar 22, 2024
@Zalathar Zalathar deleted the unreachable branch March 22, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) A-mir-opt Area: MIR optimizations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make coverage robust against MIR optimizations removing all counter-increment statements
6 participants