Skip to content

Resolver: Introduce CmRef which has a speclative borrow variant for CmRefCell - #160271

Open
LorrensP-2158466 wants to merge 2 commits into
rust-lang:mainfrom
LorrensP-2158466:res-cm-refcell
Open

Resolver: Introduce CmRef which has a speclative borrow variant for CmRefCell#160271
LorrensP-2158466 wants to merge 2 commits into
rust-lang:mainfrom
LorrensP-2158466:res-cm-refcell

Conversation

@LorrensP-2158466

@LorrensP-2158466 LorrensP-2158466 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

part of #158845

We now introduce CmRef that is returned by CmRefCell::borrow, which does a normal borrow of the underlying T if we are in speculative resolution. We do not alter state during speculative resolution and because it will be run in parallel in the future, updating the RefCell borrow counters is not possible. Thus we just return &T that is "untracked".

Also allows us to remove the CmRefCell wrapper around the external module resolution table.

r? @petrochenkov

@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 Jul 31, 2026
Comment on lines -2947 to +2995
pub(crate) fn borrow(&self) -> Ref<'_, T> {
self.0.borrow()
pub(crate) fn borrow<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> CmRef<'_, T> {
if r.assert_speculative {
// SAFETY: We are in `speculative_resolution`, so we cannot mutate the `CmRefCell`,
// not even the borrow counters. However, because of the way we use
// `assert_speculative`, we know no borrow to the underlying value is alive. Thus
// making this "unguarded_borrow" safe.
//
// Note however, that it is unsafe to borrow this `CmRefCell` and then immediately
// flip the `assert_speculative` flag, as that will cause aliased borrows. No
// idea how to mitigate this...
let unguarded_borrow = unsafe {
self.0.try_borrow_unguarded().expect("`CmRefCell` already mutably borrowed.")
};
CmRef::Normal(unguarded_borrow)
} else {
CmRef::Ref(self.0.borrow())
}

@LorrensP-2158466 LorrensP-2158466 Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because of the way we do speculative resolution, this is "safe". However it is perfectly fine to do the following:

let mut resolver = Resolver {
    assert_speculative: true,
    data: CmRefCell::new(10),
};

// we have a spec resolver and take a speculative borrow:
let spec_bor = resolver.data.borrow(&resolver);

// we then switch to non-speculative:
resolver.assert_speculative = false;

// and then we take a mutable borrow on `data`:
let mut mut_bor = resolver.data.borrow_mut(&resolver);

// Miri will report an error here:
// Undefined Behavior: trying to retag from <tag> for SharedReadOnly permission at allocxxxxx[0x8],
// but that tag does not exist in the borrow stack for this location
// Basically telling us that `spec_bor` is still live
*mut_bor = 20;

View changes since the review

@petrochenkov

Copy link
Copy Markdown
Contributor

@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 Jul 31, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 31, 2026
Resolver: Introduce `CmRef` which has a speclative borrow variant for `CmRefCell`
/// A tracked borrow of a [`CmRefCell`]
Ref(Ref<'b, T>),
/// An untracked or normal reference (not dynamically borrow-checked by `RefCell`)
Normal(&'b T),

@petrochenkov petrochenkov Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe rename the variants to Tracked and Untracked as the comments say?

View changes since the review


fn deref(&self) -> &Self::Target {
match self {
CmRef::Ref(r) => r.deref(),

@petrochenkov petrochenkov Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
CmRef::Ref(r) => r.deref(),
CmRef::Ref(r) => r,

Nit: the coercion should work here.

View changes since the review

// flip the `assert_speculative` flag, as that will cause aliased borrows. No
// idea how to mitigate this...
let unguarded_borrow = unsafe {
self.0.try_borrow_unguarded().expect("`CmRefCell` already mutably borrowed.")

@petrochenkov petrochenkov Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.0.try_borrow_unguarded().expect("`CmRefCell` already mutably borrowed.")
self.0.try_borrow_unguarded().unwrap()

expect on Results usually duplicates the message that Result::Err already reports on unwrap.

View changes since the review

#[track_caller]
pub(crate) fn borrow(&self) -> Ref<'_, T> {
self.0.borrow()
pub(crate) fn borrow<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> CmRef<'_, T> {

@petrochenkov petrochenkov Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Technically this change is unnecessary for removing CmRefCell from Resolutions (at least until parallelism is enabled).
We could just always return CmRef::Ref for Resolutions::Local.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is true. Do you want to keep it here or in a later pr?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's wait for the perf results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's revert it for now and benchmark again.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2026
@rust-bors

rust-bors Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: caf2dd1 (caf2dd11317e37ec790b3746057fae8a36779c87)
Base parent: 922325b (922325bb13bfea5b41454318563f2a65e83c2336)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (caf2dd1): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.2% [0.2%, 0.3%] 34
Regressions ❌
(secondary)
0.4% [0.1%, 0.8%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.1%] 3
All ❌✅ (primary) 0.2% [0.2%, 0.3%] 34

Max RSS (memory usage)

Results (primary 1.5%, secondary 1.7%)

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

mean range count
Regressions ❌
(primary)
3.4% [3.4%, 3.4%] 1
Regressions ❌
(secondary)
2.8% [0.8%, 6.2%] 4
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
-0.5% [-0.6%, -0.4%] 2
All ❌✅ (primary) 1.5% [-0.4%, 3.4%] 2

Cycles

Results (primary 0.3%, secondary -1.2%)

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

mean range count
Regressions ❌
(primary)
1.2% [0.4%, 2.7%] 7
Regressions ❌
(secondary)
1.7% [0.5%, 3.5%] 15
Improvements ✅
(primary)
-1.0% [-2.2%, -0.5%] 5
Improvements ✅
(secondary)
-5.1% [-10.8%, -0.6%] 11
All ❌✅ (primary) 0.3% [-2.2%, 2.7%] 12

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 490.333s -> 490.941s (0.12%)
Artifact size: 392.58 MiB -> 390.55 MiB (-0.52%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants