-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow arithmetic and certain bitwise ops on AtomicPtr #96935
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This is a new unstable API, so I guess this is appropriate. r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs These wouldn't stabilize separately from the rest of the strict provenance experiment, though, and at the moment are mostly to help test concurrent code under miri. |
I think |
@m-ou-se Isn't the same applicable to |
Not as much, because it's quite uncommon to do tricks like storing flags or counters inside a regular raw pointer. It's very common to do that with atomics though. There are many AtomicUsizes in the world that store a pointer and some flags, which could (should?) be upgraded tot AtomicPtr instead. Having |
I don't disagree, but on the other hand if we have It's a tricky API problem -- I'm open to suggestions. One option would be to only offer the bytewise versions. |
I'm not saying we should do that either. That's just as confusing. |
Maybe we should have |
9672218
to
02b2629
Compare
I've gone ahead and renamed things along these lines. |
I'm slightly worried that having an inconsistent naming scheme is worse, than having |
This problem is actually why it took so long for me to make this PR. |
FWIW this isn't exactly true. Code like our I'm not sure how much this matters, but it does make some case that this footgun would not be unprecedented (although I'm unsure how good of an argument that is in and of itself). |
I probably need to update this after #97423. I'll take a look later today or tomorrow. |
This is mainly to support migrating from AtomicUsize, for the strict provenance experiment. Fixes rust-lang#95492
02b2629
to
e65ecee
Compare
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This didn't end up needing any changes, but I rebased it, and wrote a change proposal (bit of a long one, sorry if they should be shorter -- the semantics are just a bit subtle, so I tried to cover everything): rust-lang/libs-team#60 @bors ready |
Err, hm. Wrong bot probably (oops). Either way: @rustbot label -S-waiting-on-author +S-waiting-on-review |
Btw, these operations will also need changes on the Miri side to be supported there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks terrific to me. :) Thanks. I concur with all of the design decisions justified in rust-lang/libs-team#60:
- the idea that these should exist at all, rather than doing CAS loops in the downstream code
- element-sized offsetting by itself is not sufficient
- byte-sized offsetting by itself would be annoying (but I'm sure we'll revisit this possibility when stabilizing)
- the specific naming scheme
fetch_ptr_add
andfetch_byte_add
atomptr.fetch_ptr_add(n, ord).add(n)
as the recommendation for non-wrapping semantics
The last would be good to document as an idiom in the part of the documentation that talks about fetch_ptr_add
's relationship with wrapping_add
, or maybe in the example code. r=me if you get to it before bors does, otherwise separate PR or leave to the stabilization PR.
@bors r+ |
📌 Commit e65ecee has been approved by |
…laumeGomez Rollup of 7 pull requests Successful merges: - rust-lang#96935 (Allow arithmetic and certain bitwise ops on AtomicPtr) - rust-lang#98519 (Replace some `guess_head_span` with `def_span`) - rust-lang#98911 (rustdoc: filter '_ lifetimes from ty::Generics) - rust-lang#98939 (rustdoc: Add more semantic information to impl IDs) - rust-lang#98971 (Fix typo in file descriptor docs) - rust-lang#98983 (docs: Add overview of `rustc_middle::mir::TerminatorKind`) - rust-lang#98984 (Remove erroneous doc comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
assert_eq!(atom.load(SeqCst), ptr.map_addr(|a| a | 0b1001)); | ||
|
||
assert_eq!(atom.fetch_and(MASK_PTR, SeqCst), ptr.map_addr(|a| a | 0b1001)); | ||
assert_eq!(atom.load(SeqCst), ptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test just compares the address of the pointer.
Would be good to also load from the pointer obtained via atom.load()
, to ensure that it also has the provenance to actually access memory.
rustup; ptr atomics Adds support for the operations added in rust-lang/rust#96935. I made the pointer-binops always return the provenance of the *left* argument; `@thomcc` I hope that is what you intended. I have honestly no idea if it has anything to do with what LLVM does... I also simplified our pointer comparison code while I was at it -- now that *all* comparison operators support wide pointers, we can unify those branches.
This lists #95228 as the tracking issue. Should that issue be updated to also mention this new feature ( |
I think opening a separate tracking issue would be a good idea, the "generic strict_provenance tracking issue" is already overloaded by itself. |
#[cfg(not(bootstrap))] | ||
// SAFETY: data races are prevented by atomic intrinsics. | ||
unsafe { | ||
atomic_add(self.p.get(), core::ptr::invalid_mut(val), order).cast() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's any chance that this could end up calling the intrinsic with a pointer type for the LHS and usize
for the RHS, that would be great -- it would make it very clear from the types that it's only the left input that carries provenance.
I've opened a tracking issue for this feature: #99108 |
This is mainly to support migrating from
AtomicUsize
, for the strict provenance experiment.This is a pretty dubious set of APIs, but it should be sufficient to allow code that's using
AtomicUsize
to manipulate a tagged pointer atomically. It's under a new feature gate,#![feature(strict_provenance_atomic_ptr)]
, but I'm not sure if it needs its own tracking issue. I'm happy to make one, but it's not clear that it's needed.I'm unsure if it needs changes in the various non-LLVM backends. Because we just cast things to integers anyway (and were already doing so), I doubt it.
API change proposal: rust-lang/libs-team#60
Fixes #95492