Skip to content

Conversation

@Zalathar
Copy link
Member

@Zalathar Zalathar commented Dec 3, 2025

This PR is another incremental step on the road towards proper coverage instrumentation of expansion regions.

When creating coverage mappings for each relevant span in a function body, the current implementation also needs to do a separate tree traversal for each child expansion (e.g. macro calls like println!("foo")), in order to associate a single control-flow point (BCB) with that macro call's span.

This PR changes things so that we now keep track of the “minimum” and ”maximum” BCB associated with each node in the expansion tree, which makes it much easier for the parent node to get a single BCB (min or max) for each of its child expansions.

In order to make this (relatively) easy, we first need to sort the tree nodes into depth-first order. Once that's taken care of, we can iterate over all the nodes in reverse order, knowing that each node's children will have been visited before visiting the node itself.

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

rustbot commented Dec 3, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 3, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
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

This comment has been minimized.

@rustbot

This comment has been minimized.

@Zalathar
Copy link
Member Author

Zalathar commented Jan 5, 2026

r? compiler

@rustbot rustbot assigned davidtwco and unassigned wesleywiser Jan 5, 2026
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

LGTM, r=me unless you want to make any changes. Apologies for the delay in getting to the review.

View changes since this review

nodes.sort_by_key(|_expn_id, node| node.dfs_rank);

// Verify that the depth-first search visited each node exactly once.
for (i, &ExpnNode { dfs_rank, .. }) in nodes.values().enumerate() {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to do this only when debug assertions are enabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

In theory I don't think this can ever fail, but the consequences for being wrong are potentially very bad.

If we accidentally emit malformed coverage mappings due to some unexpected arrangement of macro spans, we get bug reports about llvm-cov exiting with an unhelpful fatal error, which tends to completely break people's coverage workflows.

This makes it possible for subsequent operations to iterate over all nodes,
while assuming that every node occurs before all of its descendants.
@rustbot
Copy link
Collaborator

rustbot commented Jan 20, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Zalathar
Copy link
Member Author

@bors r=davidtwco

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 20, 2026

📌 Commit 986db13 has been approved by davidtwco

It is now in the queue for this repository.

@rust-bors rust-bors bot 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 Jan 20, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Jan 20, 2026
coverage: Sort the expansion tree to help choose a single BCB for child expansions

This PR is another incremental step on the road towards proper coverage instrumentation of expansion regions.

When creating coverage mappings for each relevant span in a function body, the current implementation also needs to do a separate tree traversal for each child expansion (e.g. macro calls like `println!("foo")`), in order to associate a single control-flow point (BCB) with that macro call's span.

This PR changes things so that we now keep track of the “minimum” and ”maximum” BCB associated with each node in the expansion tree, which makes it much easier for the parent node to get a single BCB (min or max) for each of its child expansions.

In order to make this (relatively) easy, we first need to sort the tree nodes into depth-first order. Once that's taken care of, we can iterate over all the nodes in reverse order, knowing that each node's children will have been visited before visiting the node itself.
rust-bors bot pushed a commit that referenced this pull request Jan 20, 2026
Rollup of 8 pull requests

Successful merges:

 - #149587 (coverage: Sort the expansion tree to help choose a single BCB for child expansions)
 - #150071 (Add dist step for Enzyme)
 - #150288 (Add scalar support for offload)
 - #151091 (Add new "hide deprecated items" setting in rustdoc)
 - #151255 (rustdoc: Fix ICE when deprecated note is not resolved on the correct `DefId`)
 - #151375 (Fix terminal  width dependent tests)
 - #151384 (add basic `TokenStream` api tests)
 - #151391 (rustc-dev-guide subtree update)

r? @ghost
@rust-bors rust-bors bot merged commit dd5c7eb into rust-lang:main Jan 20, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 20, 2026
rust-timer added a commit that referenced this pull request Jan 20, 2026
Rollup merge of #149587 - tree-sort, r=davidtwco

coverage: Sort the expansion tree to help choose a single BCB for child expansions

This PR is another incremental step on the road towards proper coverage instrumentation of expansion regions.

When creating coverage mappings for each relevant span in a function body, the current implementation also needs to do a separate tree traversal for each child expansion (e.g. macro calls like `println!("foo")`), in order to associate a single control-flow point (BCB) with that macro call's span.

This PR changes things so that we now keep track of the “minimum” and ”maximum” BCB associated with each node in the expansion tree, which makes it much easier for the parent node to get a single BCB (min or max) for each of its child expansions.

In order to make this (relatively) easy, we first need to sort the tree nodes into depth-first order. Once that's taken care of, we can iterate over all the nodes in reverse order, knowing that each node's children will have been visited before visiting the node itself.
@Zalathar Zalathar deleted the tree-sort branch January 20, 2026 13:10
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jan 20, 2026
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#149587 (coverage: Sort the expansion tree to help choose a single BCB for child expansions)
 - rust-lang/rust#150071 (Add dist step for Enzyme)
 - rust-lang/rust#150288 (Add scalar support for offload)
 - rust-lang/rust#151091 (Add new "hide deprecated items" setting in rustdoc)
 - rust-lang/rust#151255 (rustdoc: Fix ICE when deprecated note is not resolved on the correct `DefId`)
 - rust-lang/rust#151375 (Fix terminal  width dependent tests)
 - rust-lang/rust#151384 (add basic `TokenStream` api tests)
 - rust-lang/rust#151391 (rustc-dev-guide subtree update)

r? @ghost
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-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.

4 participants