Skip to content

Encode spans relative to the enclosing item#84373

Merged
bors merged 12 commits intorust-lang:masterfrom
cjgillot:resolve-span
Sep 12, 2021
Merged

Encode spans relative to the enclosing item#84373
bors merged 12 commits intorust-lang:masterfrom
cjgillot:resolve-span

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Apr 20, 2021

The aim of this PR is to avoid recomputing queries when code is moved without modification.

MCP at rust-lang/compiler-team#443

This is achieved by :

  1. storing the HIR owner LocalDefId information inside the span;
  2. encoding and decoding spans relative to the enclosing item in the incremental on-disk cache;
  3. marking a dependency to the source_span(LocalDefId) query when we translate a span from the short (Span) representation to its explicit (SpanData) representation.

Since all client code uses Span, step 3 ensures that all manipulations
of span byte positions actually create the dependency edge between
the caller and the source_span(LocalDefId).
This query return the actual absolute span of the parent item.
As a consequence, any source code motion that changes the absolute byte position of a node will either:

  • modify the distance to the parent's beginning, so change the relative span's hash;
  • dirty source_span, and trigger the incremental recomputation of all code that
    depends on the span's absolute byte position.

With this scheme, I believe the dependency tracking to be accurate.

For the moment, the spans are marked during lowering.
I'd rather do this during def-collection,
but the AST MutVisitor is not practical enough just yet.
The only difference is that we attach macro-expanded spans
to their expansion point instead of the macro itself.

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

I don't like the idea of keeping the ID in SpanData and would like to see a separate type for this, like LoweredSpan.
I see why it was faster to prototype this with a single Span type though.

@jyn514 jyn514 added A-incr-comp Area: Incremental compilation T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-compiletime Issue: Problems and improvements with respect to compile times. labels Apr 20, 2021
@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 24, 2021
@bors
Copy link
Collaborator

bors commented Apr 24, 2021

⌛ Trying commit d861775a3095b6bc7b23e4dc2acfb30a82f104c2 with merge 53c860c88de8437287861ee8c26eaedbe95dd7fb...

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

I don't like the idea of keeping the ID in SpanData and would like to see a separate type for this, like LoweredSpan.
I see why it was faster to prototype this with a single Span type though.

On the one hand, I agree with you, putting the ID in the SpanData is a bad hack, and invites silent invalidations.
On the other hand, I would like to assign the stable spans in the AST, to make macro-expanded spans relative to the enclosing macro.

I shall think a bit more about it. Creating a hir::Span and putting it everywhere is probably the most robust option.

@bors
Copy link
Collaborator

bors commented Apr 24, 2021

☀️ Try build successful - checks-actions
Build commit: 53c860c88de8437287861ee8c26eaedbe95dd7fb (53c860c88de8437287861ee8c26eaedbe95dd7fb)

@rust-timer
Copy link
Collaborator

Queued 53c860c88de8437287861ee8c26eaedbe95dd7fb with parent 2b68027, future comparison URL.

@rust-log-analyzer

This comment has been minimized.

@rust-timer

This comment has been minimized.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 24, 2021
@cjgillot
Copy link
Contributor Author

perf report : bad (I mean don't look, its really bad).
I think I've been too heavy handed in querifying source_map, which pessimizes MIR and CGU reuse.

@cjgillot
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Collaborator

bors commented Sep 2, 2021

⌛ Trying commit f9e65cc5e495f6943d3c02cc4c4f2c30809379f2 with merge 4bbad094c33b7feb41e0f016c658bccffd0793bb...

@bors
Copy link
Collaborator

bors commented Sep 2, 2021

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

@rust-timer
Copy link
Collaborator

Queued 4bbad094c33b7feb41e0f016c658bccffd0793bb with parent fcce644, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (4bbad094c33b7feb41e0f016c658bccffd0793bb): comparison url.

Summary: This change led to large relevant regressions 😿 in compiler performance.

  • Large regression in instruction counts (up to 2.2% on incr-unchanged builds of tuple-stress)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf +perf-regression

@petrochenkov
Copy link
Contributor

petrochenkov commented Sep 3, 2021

I don't have any more comments about the code, I'd just like to identify the regression sources.
If it's simply branches on parent and larger size of SpanData, then we cannot do anything and have to live with it until incremental_relative_spans compensates for the overhead from keeping span parents (or until the parents are removed if it doesn't compensate enough).
r? @michaelwoerister

@michaelwoerister
Copy link
Member

Well, the regressions are not great but not catastrophic either. The compiler team decided to go ahead with this change via MCP rust-lang/compiler-team#443 in order to enable further experimentation in this area. So I would say we merge the PR now. The feature is behind a flag and we can still revert it if it turns out to be a dead end.

@cjgillot, you can r=michaelwoerister,petrochenkov after fixing the conflicts.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Sep 8, 2021

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

We force the relative span's parent to be absolute. This avoids having to
handle long dependency chains.
Now that we encode spans relative to the items, the item's own span is
never actually hashed as part of the HIR.
In consequence, we explicitly include it in the crate hash to avoid
missing cross-crate invalidations.
We only do this operation when incremental compilation is enabled. This
avoids pessimizing the span handling for non-incremental compilation.
@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

@bors r=michaelwoerister,petrochenkov

About the perf regression: this PR implements an opt-in feature to reduce span-related recomputations for incr-comp. Measurements in #84762 show up to -25% instruction reduction when enabled. When disabled, the perf hit appears to be a consistent 1-2% instruction hit.

@bors
Copy link
Collaborator

bors commented Sep 11, 2021

📌 Commit 7842b80 has been approved by michaelwoerister,petrochenkov

@bors
Copy link
Collaborator

bors commented Sep 11, 2021

⌛ Testing commit 7842b80 with merge 547d937...

@bors
Copy link
Collaborator

bors commented Sep 12, 2021

☀️ Test successful - checks-actions
Approved by: michaelwoerister,petrochenkov
Pushing 547d937 to master...

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (547d937): comparison url.

Summary: This change led to large relevant regressions 😿 in compiler performance.

  • Large regression in instruction counts (up to 2.1% on incr-unchanged builds of tuple-stress)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression

@Mark-Simulacrum
Copy link
Member

About the perf regression: this PR implements an opt-in feature to reduce span-related recomputations for incr-comp. Measurements in #84762 show up to -25% instruction reduction when enabled. When disabled, the perf hit appears to be a consistent 1-2% instruction hit.

So seems justified and action on this PR not expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-incr-comp Area: Incremental compilation I-compiletime Issue: Problems and improvements with respect to compile times. merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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. WG-incr-comp Working group: Incremental compilation

Projects

None yet

Development

Successfully merging this pull request may close these issues.