Skip to content

Conversation

@nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Oct 27, 2025

&mut Analysis is used a lot:

  • In results visitors, even though only one visitor needs mutability.
  • In all the apply_* methods, even though only one visitor needs mutability.

I've lost track of the number of times I've thought "why are these mut again?" and had to look through the code to remind myself. It's really unexpected, and most Analysis instances are immutable, because the state values are what get mutated.

This commit introduces RefCell in one analysis and one results visitor. This then lets another existing RefCell be removed, and a ton of &mut Analysis arguments become &Analysis. And then Analysis and Results can be recombined.

r? @cjgillot

Of the many dataflow analyses, `ConstAnalysis` is the only one that requires
the analysis be mutabile when used with `ResultsVisitor`. It's needed
because of the `ecx` field -- `ecx.intern_with_temp_alloc` is called
during visiting and it takes `&mut self`.

This commit changes `ConstAnalysis` to use interior mutability for the
`ecx` field. This is a bit annoying for `ConstAnalysis`, but it will
allow more immutability in `ResultsVisitor`, as seen in the next commit.
This makes sense -- you wouldn't expect that visiting the results of an
analysis would change the analysis itself.
Put `analysis` first, to match `apply_effects_in_range`.
This will let us make `Analysis` arguments in many other places
immutable, in the next commit.
@rustbot rustbot added 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. labels Oct 27, 2025
@nnethercote nnethercote changed the title Usemut less in dataflow analysis Use mut less in dataflow analysis Oct 27, 2025
@rust-log-analyzer

This comment has been minimized.

@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 27, 2025
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 27, 2025
Copy link
Member

@hkBst hkBst left a comment

Choose a reason for hiding this comment

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

I'm not sure using more RefCell is to my taste...

View changes since this review

@rust-bors
Copy link

rust-bors bot commented Oct 27, 2025

☀️ Try build successful (CI)
Build commit: 7bd3a30 (7bd3a3001d02f884327e0ff6cd77d2ac00ac0c25, parent: 23fced0fcc5e0ec260d25f04a8b78b269e5e90f0)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7bd3a30): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

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

Max RSS (memory usage)

Results (primary -3.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.7% [-3.7%, -3.7%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.7% [-3.7%, -3.7%] 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: 475.144s -> 474.427s (-0.15%)
Artifact size: 390.52 MiB -> 390.51 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 27, 2025
The `state: A::Domain` value is the primary things that's modified when
performing an analysis. The `Analysis` impl is immutable in every case
but one (`MaybeRequiredStorage`) and it now uses interior mutability.

As well as changing many `&mut A` arguments to `&A`, this also:
- lets `CowMut` be replaced with the simpler `SimpleCow` in `cursor.rs`;
- removes the need for the `RefCell` in `Formatter`;
- removes the need for `MaybeBorrowedLocals` to impl `Clone`, because
  it's a unit type and it's now clear that its constructor can be used
  directly instead of being put into a local variable and cloned.
@nnethercote nnethercote marked this pull request as ready for review October 27, 2025 22:41
@rustbot
Copy link
Collaborator

rustbot commented Oct 27, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred to constck

cc @fee1-dead

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 27, 2025
`Results` used to contain an `Analysis`, but it was removed in rust-lang#140234.
That change made sense because the analysis was mutable but the entry
states were immutable and it was good to separate them so the mutability
of the different pieces was clear.

Now that analyses are immutable there is no need for the separation,
lots of analysis+results pairs can be combined, and the names are going
back to what they were before:
- `Results` -> `EntryStates`
- `AnalysisAndResults` -> `Results`
@rustbot
Copy link
Collaborator

rustbot commented Oct 27, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants