Resolver: Introduce CmRef which has a speclative borrow variant for CmRefCell - #160271
Resolver: Introduce CmRef which has a speclative borrow variant for CmRefCell#160271LorrensP-2158466 wants to merge 2 commits into
CmRef which has a speclative borrow variant for CmRefCell#160271Conversation
| 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()) | ||
| } |
There was a problem hiding this comment.
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;|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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), |
There was a problem hiding this comment.
Maybe rename the variants to Tracked and Untracked as the comments say?
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| match self { | ||
| CmRef::Ref(r) => r.deref(), |
There was a problem hiding this comment.
| CmRef::Ref(r) => r.deref(), | |
| CmRef::Ref(r) => r, |
Nit: the coercion should work here.
| // 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.") |
There was a problem hiding this comment.
| 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.
| #[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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes, that is true. Do you want to keep it here or in a later pr?
There was a problem hiding this comment.
Let's wait for the perf results.
There was a problem hiding this comment.
Let's revert it for now and benchmark again.
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary 0.3%, secondary -1.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 490.333s -> 490.941s (0.12%) |
part of #158845
We now introduce
CmRefthat is returned byCmRefCell::borrow, which does a normal borrow of the underlyingTif 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 theRefCellborrow counters is not possible. Thus we just return&Tthat is "untracked".Also allows us to remove the
CmRefCellwrapper around the external module resolution table.r? @petrochenkov