Skip to content

coverage: Rearrange the code for embedding per-function coverage metadata #134163

New issue

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

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

Already on GitHub? # to your account

Merged
merged 5 commits into from
Dec 11, 2024

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Dec 11, 2024

This is a series of refactorings to the code that prepares and embeds per-function coverage metadata records (“covfun records”) in the __llvm_covfun linker section of the final binary. The llvm-cov tool reads this metadata from the binary when preparing a coverage report.

Beyond general cleanup, a big motivation behind these changes is to pave the way for re-landing an updated version of #133418.


There should be no change in compiler output, as demonstrated by the absence of (meaningful) changes to coverage tests.

The first patch is just moving code around, so I suggest looking at the other patches to see the actual changes.


try-job: x86_64-gnu
try-job: x86_64-msvc
try-job: aarch64-apple

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

rustbot commented Dec 11, 2024

r? @SparrowLii

rustbot has assigned @SparrowLii.
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 Dec 11, 2024
@rust-log-analyzer

This comment has been minimized.

@Zalathar Zalathar force-pushed the covfun branch 2 times, most recently from e0ad6ad to ec7eb7e Compare December 11, 2024 08:04
@Zalathar
Copy link
Contributor Author

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 11, 2024
coverage: Rearrange the code for embedding per-function coverage metadata

This is a series of refactorings to the code that prepares and embeds per-function coverage metadata records (“covfun records”) in the `__llvm_covfun` linker section of the final binary. The `llvm-cov` tool reads this metadata from the binary when preparing a coverage report.

Beyond general cleanup, a big motivation behind these changes is to pave the way for re-landing an updated version of rust-lang#133418.

---

There should be no change in compiler output, as demonstrated by the absence of (meaningful) changes to coverage tests.

---

try-job: x86_64-gnu
try-job: x86_64-msvc
try-job: aarch64-apple
@bors
Copy link
Collaborator

bors commented Dec 11, 2024

⌛ Trying commit ec7eb7e with merge 4bb99ca...

Copy link
Member

@SparrowLii SparrowLii left a comment

Choose a reason for hiding this comment

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

Thanks!
I think this PR doesn't change the functionality. Just making some comments for the code.

@bors
Copy link
Collaborator

bors commented Dec 11, 2024

☀️ Try build successful - checks-actions
Build commit: 4bb99ca (4bb99ca1934dc99af504ea1693260dc0e4452999)

@Zalathar
Copy link
Contributor Author

Zalathar commented Dec 11, 2024

Added some more comments in response to feedback (diff).

This defers the call to `llvm_cov::write_function_mappings_to_buffer` until
just before its enclosing global variable is created.
@Zalathar
Copy link
Contributor Author

I had another look at that test, and changing it to use CHECK-DAG was easier than I expected, so I just fixed it.

@jieyouxu
Copy link
Member

jieyouxu commented Dec 11, 2024

No further concerns from me, feel free to r=Sparrow and me if @SparrowLii has no further concerns.

@SparrowLii
Copy link
Member

LGTM

@Zalathar
Copy link
Contributor Author

Thanks for the reviews.

@bors r=SparrowLii,jieyouxu

@bors
Copy link
Collaborator

bors commented Dec 11, 2024

📌 Commit 3f3a9bf has been approved by SparrowLii,jieyouxu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 11, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#132975 (De-duplicate and improve definition of core::ffi::c_char)
 - rust-lang#133598 (Change `GetManyMutError` to match T-libs-api decision)
 - rust-lang#134148 (add comments in check_expr_field)
 - rust-lang#134163 (coverage: Rearrange the code for embedding per-function coverage metadata)
 - rust-lang#134165 (wasm(32|64): update alignment string)
 - rust-lang#134170 (Subtree update of `rust-analyzer`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 13c13ee into rust-lang:master Dec 11, 2024
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 11, 2024
Rollup merge of rust-lang#134163 - Zalathar:covfun, r=SparrowLii,jieyouxu

coverage: Rearrange the code for embedding per-function coverage metadata

This is a series of refactorings to the code that prepares and embeds per-function coverage metadata records (“covfun records”) in the `__llvm_covfun` linker section of the final binary. The `llvm-cov` tool reads this metadata from the binary when preparing a coverage report.

Beyond general cleanup, a big motivation behind these changes is to pave the way for re-landing an updated version of rust-lang#133418.

---

There should be no change in compiler output, as demonstrated by the absence of (meaningful) changes to coverage tests.

The first patch is just moving code around, so I suggest looking at the other patches to see the actual changes.

---

try-job: x86_64-gnu
try-job: x86_64-msvc
try-job: aarch64-apple
@Zalathar Zalathar deleted the covfun branch December 11, 2024 23:12
Comment on lines +92 to +99
// If there are no covfun records for this CGU, don't generate a covmap record.
// Emitting a covmap record without any covfun records causes `llvm-cov` to
// fail when generating coverage reports, and if there are no covfun records
// then the covmap record isn't useful anyway.
// This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
if covfun_records.is_empty() {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, it only occurred to me post-merge as I was looking at #134208. Did we ever have a regression test for this case (I imagine it can be hard to come with up with one)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be possible, but I would have to do some non-trivial detective work, because currently we don't have any examples smaller than “the coverage tests for my entire workspace, in CI”.

compiler-errors added a commit to compiler-errors/rust that referenced this pull request Dec 13, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 13, 2024
coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
Zalathar added a commit to Zalathar/rust that referenced this pull request Dec 13, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 13, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 13, 2024
coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 13, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 13, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
Zalathar added a commit to Zalathar/rust that referenced this pull request Dec 14, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 14, 2024
…-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 14, 2024
Rollup merge of rust-lang#134208 - Zalathar:covmap-covfun, r=compiler-errors

coverage: Tidy up creation of covmap and covfun records

This is a small follow-up to rust-lang#134163 that mostly just inlines and renames some variables, and adds a few comments.

It also slightly defers the creation of the LLVM value that holds the filename table, to just before the value is needed.

---

try-job: x86_64-mingw-2
try-job: dist-x86_64-linux
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 19, 2024
coverage: Store coverage source regions as `Span` until codegen (take 2)

This is an attempt to re-land rust-lang#133418:

> Historically, coverage spans were converted into line/column coordinates during the MIR instrumentation pass.

> This PR moves that conversion step into codegen, so that coverage spans spend most of their time stored as Span instead.

> In addition to being conceptually nicer, this also reduces the size of coverage mappings in MIR, because Span is smaller than 4x u32.

That PR was reverted by rust-lang#133608, because in some circumstances not covered by our test suite we were emitting coverage metadata that was causing `llvm-cov` to exit with an error (rust-lang#133606).

---

The implementation here is *mostly* the same, but adapted for subsequent changes in the relevant code (e.g. rust-lang#134163).

I believe that the changes in rust-lang#134163 should be sufficient to prevent the problem that required the original PR to be reverted. But I haven't been able to reproduce the original breakage in a regression test, and the `llvm-cov` error message is extremely unhelpful, so I can't completely rule out the possibility of this breaking again.

r? jieyouxu (reviewer of the original PR)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 20, 2024
Rollup merge of rust-lang#134497 - Zalathar:spans, r=jieyouxu

coverage: Store coverage source regions as `Span` until codegen (take 2)

This is an attempt to re-land rust-lang#133418:

> Historically, coverage spans were converted into line/column coordinates during the MIR instrumentation pass.

> This PR moves that conversion step into codegen, so that coverage spans spend most of their time stored as Span instead.

> In addition to being conceptually nicer, this also reduces the size of coverage mappings in MIR, because Span is smaller than 4x u32.

That PR was reverted by rust-lang#133608, because in some circumstances not covered by our test suite we were emitting coverage metadata that was causing `llvm-cov` to exit with an error (rust-lang#133606).

---

The implementation here is *mostly* the same, but adapted for subsequent changes in the relevant code (e.g. rust-lang#134163).

I believe that the changes in rust-lang#134163 should be sufficient to prevent the problem that required the original PR to be reverted. But I haven't been able to reproduce the original breakage in a regression test, and the `llvm-cov` error message is extremely unhelpful, so I can't completely rule out the possibility of this breaking again.

r? jieyouxu (reviewer of the original PR)
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Mar 11, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#132975 (De-duplicate and improve definition of core::ffi::c_char)
 - rust-lang#133598 (Change `GetManyMutError` to match T-libs-api decision)
 - rust-lang#134148 (add comments in check_expr_field)
 - rust-lang#134163 (coverage: Rearrange the code for embedding per-function coverage metadata)
 - rust-lang#134165 (wasm(32|64): update alignment string)
 - rust-lang#134170 (Subtree update of `rust-analyzer`)

r? `@ghost`
`@rustbot` modify labels: rollup
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants