-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Only generate a ptrtoint in AtomicPtr codegen when absolutely necessary #122220
Conversation
This comment has been minimized.
This comment has been minimized.
37cfd25
to
794d604
Compare
This comment has been minimized.
This comment has been minimized.
Seems to work? |
794d604
to
aa6cfb2
Compare
I do not know for sure if GCC has the same limitations. If not, I can just open an issue on the rustc_codegen_gcc repo and link this PR so I could make the change myself if needed. |
I'm not sure. |
order: rustc_codegen_ssa::common::AtomicOrdering, | ||
) -> &'ll Value { | ||
// The only RMW operation that LLVM supports on pointers is compare-exchange. |
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.
should we report this somewhere upstream? Or at least track it with a FIXME or issue?
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.
Which operations does Rust use on pointers? To the most part these just don't make sense, at least not in a straightforward way (even something like add
would run up against the provenance question).
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.
I agree that compare-exchange is kind of the odd-one-out here (in that it makes sense and the other RMW ops do not).
Up to you where the check for it should live 🤷 I decided to sink the branch because otherwise we have an odd interface where we accept an enum but you better only pass one variant.
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.
The check is fine -- my comment was to answer @oli-obk's question on whether this needs to be reported upstream, and I think the answer is "no".
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (e61dcc7): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 672.147s -> 673.298s (0.17%) |
if self.val_ty(src) == self.type_ptr() | ||
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg | ||
{ | ||
src = self.ptrtoint(src, self.type_isize()); |
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 a ptrtoint for the argument, doesn't there also have to be an inttoptr for the result?
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.
Hunh. It sure seems like an inttoptr should be required, but the previous implementation didn't have one either, right? I think I'm going to add the conversion back to pointer type anyway with a codegen test.
Add the missing inttoptr when we ptrtoint in ptr atomics Ralf noticed this here: rust-lang#122220 (comment) Our previous codegen forgot to add the cast back to integer type. The code compiles anyway, because of course all locals are in-memory to start with, so previous codegen would do the integer atomic, store the integer to a local, then load a pointer from that local. Which is definitely _not_ what we wanted: That's an integer-to-pointer transmute, so all pointers returned by these `AtomicPtr` methods didn't have provenance. Yikes. Here's the IR for `AtomicPtr::fetch_byte_add` on 1.76: https://godbolt.org/z/8qTEjeraY ```llvm define noundef ptr `@atomicptr_fetch_byte_add(ptr` noundef nonnull align 8 %a, i64 noundef %v) unnamed_addr #0 !dbg !7 { start: %0 = alloca ptr, align 8, !dbg !12 %val = inttoptr i64 %v to ptr, !dbg !12 call void `@llvm.lifetime.start.p0(i64` 8, ptr %0), !dbg !28 %1 = ptrtoint ptr %val to i64, !dbg !28 %2 = atomicrmw add ptr %a, i64 %1 monotonic, align 8, !dbg !28 store i64 %2, ptr %0, align 8, !dbg !28 %self = load ptr, ptr %0, align 8, !dbg !28 call void `@llvm.lifetime.end.p0(i64` 8, ptr %0), !dbg !28 ret ptr %self, !dbg !33 } ``` r? `@RalfJung` cc `@nikic`
Add the missing inttoptr when we ptrtoint in ptr atomics Ralf noticed this here: rust-lang/rust#122220 (comment) Our previous codegen forgot to add the cast back to integer type. The code compiles anyway, because of course all locals are in-memory to start with, so previous codegen would do the integer atomic, store the integer to a local, then load a pointer from that local. Which is definitely _not_ what we wanted: That's an integer-to-pointer transmute, so all pointers returned by these `AtomicPtr` methods didn't have provenance. Yikes. Here's the IR for `AtomicPtr::fetch_byte_add` on 1.76: https://godbolt.org/z/8qTEjeraY ```llvm define noundef ptr `@atomicptr_fetch_byte_add(ptr` noundef nonnull align 8 %a, i64 noundef %v) unnamed_addr #0 !dbg !7 { start: %0 = alloca ptr, align 8, !dbg !12 %val = inttoptr i64 %v to ptr, !dbg !12 call void `@llvm.lifetime.start.p0(i64` 8, ptr %0), !dbg !28 %1 = ptrtoint ptr %val to i64, !dbg !28 %2 = atomicrmw add ptr %a, i64 %1 monotonic, align 8, !dbg !28 store i64 %2, ptr %0, align 8, !dbg !28 %self = load ptr, ptr %0, align 8, !dbg !28 call void `@llvm.lifetime.end.p0(i64` 8, ptr %0), !dbg !28 ret ptr %self, !dbg !33 } ``` r? `@RalfJung` cc `@nikic`
Add the missing inttoptr when we ptrtoint in ptr atomics Ralf noticed this here: rust-lang/rust#122220 (comment) Our previous codegen forgot to add the cast back to integer type. The code compiles anyway, because of course all locals are in-memory to start with, so previous codegen would do the integer atomic, store the integer to a local, then load a pointer from that local. Which is definitely _not_ what we wanted: That's an integer-to-pointer transmute, so all pointers returned by these `AtomicPtr` methods didn't have provenance. Yikes. Here's the IR for `AtomicPtr::fetch_byte_add` on 1.76: https://godbolt.org/z/8qTEjeraY ```llvm define noundef ptr `@atomicptr_fetch_byte_add(ptr` noundef nonnull align 8 %a, i64 noundef %v) unnamed_addr #0 !dbg !7 { start: %0 = alloca ptr, align 8, !dbg !12 %val = inttoptr i64 %v to ptr, !dbg !12 call void `@llvm.lifetime.start.p0(i64` 8, ptr %0), !dbg !28 %1 = ptrtoint ptr %val to i64, !dbg !28 %2 = atomicrmw add ptr %a, i64 %1 monotonic, align 8, !dbg !28 store i64 %2, ptr %0, align 8, !dbg !28 %self = load ptr, ptr %0, align 8, !dbg !28 call void `@llvm.lifetime.end.p0(i64` 8, ptr %0), !dbg !28 ret ptr %self, !dbg !33 } ``` r? `@RalfJung` cc `@nikic`
Add the missing inttoptr when we ptrtoint in ptr atomics Ralf noticed this here: rust-lang/rust#122220 (comment) Our previous codegen forgot to add the cast back to integer type. The code compiles anyway, because of course all locals are in-memory to start with, so previous codegen would do the integer atomic, store the integer to a local, then load a pointer from that local. Which is definitely _not_ what we wanted: That's an integer-to-pointer transmute, so all pointers returned by these `AtomicPtr` methods didn't have provenance. Yikes. Here's the IR for `AtomicPtr::fetch_byte_add` on 1.76: https://godbolt.org/z/8qTEjeraY ```llvm define noundef ptr `@atomicptr_fetch_byte_add(ptr` noundef nonnull align 8 %a, i64 noundef %v) unnamed_addr #0 !dbg !7 { start: %0 = alloca ptr, align 8, !dbg !12 %val = inttoptr i64 %v to ptr, !dbg !12 call void `@llvm.lifetime.start.p0(i64` 8, ptr %0), !dbg !28 %1 = ptrtoint ptr %val to i64, !dbg !28 %2 = atomicrmw add ptr %a, i64 %1 monotonic, align 8, !dbg !28 store i64 %2, ptr %0, align 8, !dbg !28 %self = load ptr, ptr %0, align 8, !dbg !28 call void `@llvm.lifetime.end.p0(i64` 8, ptr %0), !dbg !28 ret ptr %self, !dbg !33 } ``` r? `@RalfJung` cc `@nikic`
This special case was added in this PR: #77611 in response to this error message:
But when I tried searching for more information about that intrinsic I found this: llvm/llvm-project#55983 which is a report of someone hitting this same error and a fix was landed in LLVM, 2 years after the above Rust PR.