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

coverage: Remove some complex heuristics from counter creation #131398

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Oct 8, 2024

When the coverage instrumentor creates coverage counters for various parts of the control-flow graph, it sometimes needs to choose which edges should get a physical counter, and which should get a counter expression computed from other counters.

The current code uses a heuristic based on trying to identify “loop backedges”, which relies on a complex custom graph traversal to identify relevant loop header nodes. That complexity is getting in the way of some deeper changes that I'm trying to make to counter creation, and removing it doesn't seem to make the resulting coverage mappings much worse. In some cases it even makes them better!

This PR therefore removes the edge-expression heuristic and its associated traversal. Now we always choose an arbitrary edge to be given an expression, and we iterate over the graph nodes in ascending ID order.


I've also included a change to the coverage-dump tool to make it print out the highest counter ID that it sees when printing coverage mappings, as this makes it easier to see whether the number of physical counters has changed.

@Zalathar Zalathar added the A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) label Oct 8, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 8, 2024

r? @matthewjasper

rustbot has assigned @matthewjasper.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 8, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 8, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@Zalathar
Copy link
Contributor Author

Zalathar commented Oct 8, 2024

A few tests have changes in their coverage reports, but that seems to be a result of them experiencing panics. Coverage instrumentation assumes that panics don't occur, so when a panic does occur, the counts aren't guaranteed to be meaningful, and can be disturbed by implementation details of counter creation.

When making changes that have a large impact on coverage counter creation, this
makes it easier to see whether the number of physical counters has changed.

(The highest counter ID seen in coverage maps is not necessarily the same as
the number of physical counters actually used by the instrumented code, but
it's the best approximation we can get from looking only at the coverage maps,
and it should be reasonably accurate in most cases.)
This heuristic makes sense on paper, but currently isn't worth the extra
complexity, especially when trying to refactor counter creation in other
directions.
The logic behind this traversal order sounds compelling, but in practice it
doesn't seem to produce enough benefit to justify its complexity.
@Zalathar
Copy link
Contributor Author

On reflection, I want to give this some more thought before merging it.

@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 10, 2024
Zalathar added a commit to Zalathar/rust that referenced this pull request Oct 11, 2024
coverage: Include the highest counter ID seen in `.cov-map` dumps

When making changes that have a large impact on coverage counter creation, this makes it easier to see whether the number of physical counters has changed.

(The highest counter ID seen in coverage maps is not necessarily the same as the number of physical counters actually used by the instrumented code, but it's the best approximation we can get from looking only at the coverage maps, and it should be reasonably accurate in most cases.)

Extracted from rust-lang#131398, since I'm still considering whether to make those changes as-is, whereas this PR is useful and good on its own.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 11, 2024
coverage: Include the highest counter ID seen in `.cov-map` dumps

When making changes that have a large impact on coverage counter creation, this makes it easier to see whether the number of physical counters has changed.

(The highest counter ID seen in coverage maps is not necessarily the same as the number of physical counters actually used by the instrumented code, but it's the best approximation we can get from looking only at the coverage maps, and it should be reasonably accurate in most cases.)

Extracted from rust-lang#131398, since I'm still considering whether to make those changes as-is, whereas this PR is useful and good on its own.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 11, 2024
Rollup merge of rust-lang#131476 - Zalathar:highest-counter, r=jieyouxu

coverage: Include the highest counter ID seen in `.cov-map` dumps

When making changes that have a large impact on coverage counter creation, this makes it easier to see whether the number of physical counters has changed.

(The highest counter ID seen in coverage maps is not necessarily the same as the number of physical counters actually used by the instrumented code, but it's the best approximation we can get from looking only at the coverage maps, and it should be reasonably accurate in most cases.)

Extracted from rust-lang#131398, since I'm still considering whether to make those changes as-is, whereas this PR is useful and good on its own.
@Swatinem
Copy link
Contributor

Coverage instrumentation assumes that panics don't occur, so when a panic does occur, the counts aren't guaranteed to be meaningful, and can be disturbed by implementation details of counter creation.

This is a very interesting observation. I wonder if it would make sense to introduce another -Z option to enable instrumenting panicing code/branches?

I think there might be value in providing the maximum amount of detail.
Even though most of the users would probably prefer to not instrument code paths that are "unreachable" for some definition or other.

@Zalathar
Copy link
Contributor Author

Eventually I would love to have better support for accurate coverage in the presence of panics.

But there's still a lot to be cleaned up in the current implementation before that becomes feasible, unfortunately.

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) 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.

4 participants