Skip to content

atomicrmw on pointers: move integer-pointer cast hacks into backend #144192

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jul 19, 2025

Conceptually, we want to have atomic operations on pointers of the form fn atomic_add(ptr: *mut T, offset: usize, ...). However, LLVM does not directly support such operations (llvm/llvm-project#120837), so we have to cast the offset to a pointer somewhere.

This PR moves that hack into the LLVM backend, so that the standard library, intrinsic, and Miri all work with the conceptual operation we actually want. Hopefully, one day LLVM will gain a way to represent these operations without integer-pointer casts, and then the hack will disappear entirely.

Cc @nikic -- this is the best we can do right now, right?
Fixes #134617

@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

// FIXME: LLVM does not support an atomic "add integer to pointer" operation, so
// we need to instead add two pointers and hope that works out. See
// <https://github.com/llvm/llvm-project/issues/120837>.
bx.inttoptr(val, bx.backend_type(bx.layout_of(ty_mem)))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is now doing something pretty weird where this code casts the integer to pointer and then bx.atomic_rmw is going to cast it back to integer.

LLVM already expects the arguments to atomicrmw here to be a pointer and an integer. Now that the Rust intrinsic no longer artificially forces both arguments to be pointers, we should remove both this cast and the argument cast in atomic_rmw.

The only cast you need to keep is the cast of the return value of atomicrmw.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is now doing something pretty weird where this code casts the integer to pointer and then bx.atomic_rmw is going to cast it back to integer.

Ah, fair.

LLVM already expects the arguments to atomicrmw here to be a pointer and an integer.

Ah right, it expects a pointer-to-integer and an integer (when it really should be ptr-to-ptr and integer, for RMWs on pointers).

@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jul 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@RalfJung
Copy link
Member Author

RalfJung commented Jul 21, 2025

@antoyo, @GuillaumeGomez I took a wild guess with those GCC changes, please take a look.

@bjorn3 this seems to somehow break cranelift, which is surprising since I thought usize and raw pointers were the same type there. Any idea what is happening?

@RalfJung RalfJung force-pushed the atomicrmw-ptr branch 2 times, most recently from 3494e9c to a5a87d3 Compare July 21, 2025 06:52
@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@RalfJung
Copy link
Member Author

I took a guess at what I have to do in cranelift, I hope it makes sense.

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the atomicrmw-ptr branch 2 times, most recently from 577b7f8 to c8c38b3 Compare July 21, 2025 09:18
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the atomicrmw-ptr branch 2 times, most recently from 1eb3567 to bd0cb3c Compare July 21, 2025 11:48
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Jul 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2025

This PR modifies run-make tests.

cc @jieyouxu

@@ -35,51 +35,62 @@ impl<T: ?Sized> Copy for *mut T {}
impl ConstParamTy for AtomicOrdering {}

#[cfg(target_has_atomic = "8")]
#[unsafe(no_mangle)] // let's make sure we actually generate a symbol to check
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this test might actually have been ineffective ever since we gained that heuristic that automatically marks small functions as #[inline].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs 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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

atomic RMW intrinsics: avoid unnecessary ptr/int conversions
6 participants