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

Streamline MIR dataflow cursors #118230

Merged
merged 9 commits into from
Dec 5, 2023

Conversation

nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Nov 24, 2023

rustc_mir_dataflow has two kinds of results (Results and ResultsCloned) and three kinds of results cursor (ResultsCursor, ResultsClonedCursor, ResultsRefCursor). I found this quite confusing.

This PR removes ResultsCloned, ResultsClonedCursor, and ResultsRefCursor, leaving just Results and ResultsCursor. This makes the relevant code shorter and easier to read, and there is no performance penalty.

r? @cjgillot

@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 Nov 24, 2023
@rustbot
Copy link
Collaborator

rustbot commented Nov 24, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@nnethercote nnethercote marked this pull request as draft November 24, 2023 05:49
@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

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

bors commented Nov 24, 2023

⌛ Trying commit 0e0ffbd with merge d449aa5...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 24, 2023
…ors, r=<try>

Streamline MIR dataflow cursors

r? `@ghost`
@bors
Copy link
Contributor

bors commented Nov 24, 2023

☀️ Try build successful - checks-actions
Build commit: d449aa5 (d449aa59d57a9d5e76aa76b08a77ab46c8a0d77c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d449aa5): comparison URL.

Overall result: no relevant changes - no action needed

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 may lead to changes in compiler perf.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.6% [-2.6%, -2.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.6% [-2.6%, -2.6%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 675.697s -> 677.516s (0.27%)
Artifact size: 313.74 MiB -> 313.73 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 24, 2023
@nnethercote
Copy link
Contributor Author

No change on perf, as expected. That's good news, it means the very small increase in cloning has no meaningful perf effect.

And insert some whitespace.
It's only implemented for analyses that implement `Copy`, which means
it's basically a complicated synonym for `Copy`. So this commit removes
it and uses `Copy` directly. (That direct use will be removed in a later
commit.)
It's currently used because `requires_storage_results` is used in two
locations: once with a cursor, and once later on without a cursor. The
non-consuming `as_results_cursor` is used for the first location.

But we can instead use the consuming `into_results_cursor` and then use
`into_results` to extract the `Results` from the finished-with cursor
for use at the second location.
The new code is a little clunky, but I couldn't see how to make it
better.
It's no longer used.
By just cloning the entire `Results` in the one place where
`ResultsClonedCursor` was used. This is extra allocations but the
performance effect is negligible.
They both now only ever contain a `Results<'tcx, A>`.

This means `AnalysisResults` can be removed, as can many
`borrow`/`borrow_mut` calls. Also `Results` no longer needs a
`PhantomData` because `'tcx` is now named by `entry_sets`.
@nnethercote
Copy link
Contributor Author

cc @Jarcho

@Jarcho
Copy link
Contributor

Jarcho commented Nov 27, 2023

Looked back at what this was before I changed it. The current mess of different cursor types was just a way to avoid cloning in all the cases it did before.

It might be possible to simplify things by only borrowing the entry sets and cloning the analysis.

@nnethercote
Copy link
Contributor Author

It might be possible to simplify things by only borrowing the entry sets and cloning the analysis.

That's exactly what ResultsClonedCursor did, which required the assistance of CloneAnalysis and AnalysisResults. But the cost of cloning the entry sets is negligible, so it's not worth all the extra complexity to avoid cloning.

@Jarcho
Copy link
Contributor

Jarcho commented Nov 27, 2023

CloneAnalysis could just be Clone. There would have been (very small) perf hits on some of the stuff I was working on at time, but it's really not needed. If I were to do things now I wouldn't have added it at all.

AnalysisResults wouldn't be needed if the entry sets and analysis were stored directly as separate fields. I think that caused some other problems somewhere else, but I don't remember everything I tried at the time.

Since this doesn't have a big effect, I agree with the assessment that it doesn't really matter.

@nnethercote
Copy link
Contributor Author

CloneAnalysis could just be Clone. There would have been (very small) perf hits on some of the stuff I was working on at time, but it's really not needed. If I were to do things now I wouldn't have added it at all.

The PR removes CloneAnalysis, exactly by just using Clone in a very small number of places.

AnalysisResults wouldn't be needed if the entry sets and analysis were stored directly as separate fields. I think that caused some other problems somewhere else, but I don't remember everything I tried at the time.

The PR removes AnalysisResults as well.

@cjgillot
Copy link
Contributor

cjgillot commented Dec 3, 2023

LGTM. Why is this still marked as draft?

@nnethercote nnethercote marked this pull request as ready for review December 3, 2023 22:29
@nnethercote
Copy link
Contributor Author

Sorry, I forgot to remove the draft marker :)

@nnethercote
Copy link
Contributor Author

@cjgillot: is your "LGTM" equivalent to an r+ ?

@cjgillot
Copy link
Contributor

cjgillot commented Dec 4, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Dec 4, 2023

📌 Commit e966c89 has been approved by cjgillot

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 4, 2023
@bors
Copy link
Contributor

bors commented Dec 5, 2023

⌛ Testing commit e966c89 with merge 317d14a...

@bors
Copy link
Contributor

bors commented Dec 5, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 317d14a to master...

1 similar comment
@bors
Copy link
Contributor

bors commented Dec 5, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 317d14a to master...

@bors bors added merged-by-bors This PR was explicitly merged by bors. labels Dec 5, 2023
@bors bors merged commit 317d14a into rust-lang:master Dec 5, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 5, 2023
@nnethercote nnethercote deleted the streamline-dataflow-cursors branch December 5, 2023 04:55
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (317d14a): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.7% [2.7%, 2.7%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.8% [1.8%, 1.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 673.516s -> 673.262s (-0.04%)
Artifact size: 314.15 MiB -> 314.18 MiB (0.01%)

nnethercote added a commit to nnethercote/rust that referenced this pull request Oct 23, 2024
`ResultsCursor` currently owns its `Results`. But sometimes the
`Results` is needed again afterwards. So there is
`ResultsCursor::into_results` for extracting the `Results`, which leads
to some awkwardness.

This commit adds `ResultsHandle`, a `Cow`-like type that can either
borrow or own a a `Results`. `ResultsCursor` now uses it. This is good
because some `ResultsCursor`s really want to own their `Results`, while
others just want to borrow it.

We end with with a few more lines of code, but get some nice cleanups.
- `ResultsCursor::into_results` is removed.
- `Formatter::into_results` is removed.
- `write_graphviz_results` now just borrows a `Results`, instead of the
  awkward "take ownership of a `Results` and then return it unchanged"
  pattern.
- `MaybeRequiresStorage` can borrow the `MaybeBorrowedLocals` results,
  avoiding two `clone` calls: one in `check_for_move` and one in
  `locals_live_across_suspend_points`.
- `Results` no longer needs to derive `Clone`.

This reinstates the cursor flexibility that was lost in rust-lang#118230 -- which
removed the old `ResultsRefCursor` and `ResultsCloneCursor` types -- but
in a *much* simpler way. Hooray!
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 23, 2024
…aflow-cleanups, r=<try>

Still more `rustc_mir_dataflow` cleanups

A sequel to rust-lang#118203, rust-lang#118230, rust-lang#118638, and rust-lang#131481.

I'm pleased with this PR. It cleans up a few things that have been bugging me for a while. It took quite some effort to find these improvements.

r? `@cjgillot`
nnethercote added a commit to nnethercote/rust that referenced this pull request Oct 24, 2024
`ResultsCursor` currently owns its `Results`. But sometimes the
`Results` is needed again afterwards. So there is
`ResultsCursor::into_results` for extracting the `Results`, which leads
to some awkwardness.

This commit adds `ResultsHandle`, a `Cow`-like type that can either
borrow or own a a `Results`. `ResultsCursor` now uses it. This is good
because some `ResultsCursor`s really want to own their `Results`, while
others just want to borrow it.

We end with with a few more lines of code, but get some nice cleanups.
- `ResultsCursor::into_results` is removed.
- `Formatter::into_results` is removed.
- `write_graphviz_results` now just borrows a `Results`, instead of the
  awkward "take ownership of a `Results` and then return it unchanged"
  pattern.
- `MaybeRequiresStorage` can borrow the `MaybeBorrowedLocals` results,
  avoiding two `clone` calls: one in `check_for_move` and one in
  `locals_live_across_suspend_points`.
- `Results` no longer needs to derive `Clone`.

This reinstates the cursor flexibility that was lost in rust-lang#118230 -- which
removed the old `ResultsRefCursor` and `ResultsCloneCursor` types -- but
in a *much* simpler way. Hooray!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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