Repository navigation
rust: fix compare_exchange_weak never succeeding on mpc85xx #30258
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
Merged
BKPepe
merged 1 commit into
openwrt:openwrt-24.10
from
BKPepe:rust-powerpc-weak-cmpxchg-24.10
Aug 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
lang/rust/patches/0002-AtomicExpand-keep-the-release-fence-out-of-the-reserv.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
| From: Josef Schlehofer <pepe.schlehofer@gmail.com> | ||
| Date: Sat, 9 Aug 2026 09:15:00 +0000 | ||
| Subject: [PATCH] AtomicExpand: keep the release fence out of the reservation | ||
|
|
||
| AtomicExpandPass sinks the leading fence of a cmpxchg between the | ||
| load-linked and the store-conditional. On e500v2, the core in mpc85xx, a | ||
| sync there clears the reservation and the store-conditional fails. The | ||
| strong form recovers, because its retry re-reserves after the fence; the | ||
| weak form has no retry and so can never succeed. | ||
|
|
||
| fetch_update in core is a compare_exchange_weak retry loop, so on this | ||
| subtarget it spins forever and anything built on it hangs. A tokio reactor | ||
| livelocks on the first I/O event it receives, burning CPU and issuing no | ||
| syscalls. | ||
|
|
||
| Add a TargetLowering hook, defaulting to false, and hoist the fence for a | ||
| weak cmpxchg only where the target says a fence can clear its reservation. | ||
| PowerPC says so. For every other target the condition is unchanged, so | ||
| their fence placement and codegen are preserved. | ||
|
|
||
| Verified against LLVM built for PowerPC, ARM, AArch64, RISCV, Mips and | ||
| Sparc: the same 75 pre-existing test failures with the change and without | ||
| it, identical lists. | ||
|
|
||
| Only the three source files are carried here. The upstream change also adds | ||
| tests, which OpenWrt does not build. | ||
|
|
||
| Upstream-Status: Submitted [https://github.com/llvm/llvm-project/pull/214867] | ||
|
|
||
| Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com> | ||
| --- | ||
| --- a/src/llvm-project/llvm/include/llvm/CodeGen/TargetLowering.h | ||
| +++ b/src/llvm-project/llvm/include/llvm/CodeGen/TargetLowering.h | ||
| @@ -2220,6 +2220,13 @@ | ||
| return AtomicOrdering::Monotonic; | ||
| } | ||
|
|
||
| + /// Whether a fence placed between the load-linked and the store-conditional | ||
| + /// can clear the reservation on this target. When it can, AtomicExpandPass | ||
| + /// must not sink the leading fence of a weak cmpxchg into the reservation | ||
| + /// window: the store-conditional would fail, and a weak cmpxchg has no retry | ||
| + /// to re-reserve and recover with. Defaults to false. | ||
| + virtual bool fenceClearsLoadLinkedReservation() const { return false; } | ||
| + | ||
| /// Whether AtomicExpandPass should automatically insert a trailing fence | ||
| /// without reducing the ordering for this atomic. Defaults to false. | ||
| virtual bool | ||
| --- a/src/llvm-project/llvm/lib/CodeGen/AtomicExpandPass.cpp | ||
| +++ b/src/llvm-project/llvm/lib/CodeGen/AtomicExpandPass.cpp | ||
| @@ -1370,7 +1370,17 @@ bool AtomicExpandImpl::expandAtomicCmpXc | ||
|
|
||
| // There's no overhead for sinking the release barrier in a weak cmpxchg, so | ||
| // do it even on minsize. | ||
| - bool UseUnconditionalReleaseBarrier = F->hasMinSize() && !CI->isWeak(); | ||
| + // | ||
| + // Except where a fence can clear the reservation. Sinking puts the fence | ||
| + // between the load-linked and the store-conditional, and there the | ||
| + // store-conditional fails; the strong form recovers because its retry | ||
| + // re-reserves afterwards, but a weak cmpxchg has no retry and so could never | ||
| + // succeed. Hoisting is valid because the fence is only needed to provide the | ||
| + // release ordering of a successful store, and executing it on an attempt | ||
| + // that fails before the store has no additional ordering effect. | ||
| + bool UseUnconditionalReleaseBarrier = | ||
| + (F->hasMinSize() && !CI->isWeak()) || | ||
| + (CI->isWeak() && TLI->fenceClearsLoadLinkedReservation()); | ||
|
|
||
| // Given: cmpxchg some_op iN* %addr, iN %desired, iN %new success_ord fail_ord | ||
| // | ||
| --- a/src/llvm-project/llvm/lib/Target/PowerPC/PPCISelLowering.h | ||
| +++ b/src/llvm-project/llvm/lib/Target/PowerPC/PPCISelLowering.h | ||
| @@ -338,6 +338,13 @@ namespace llvm { | ||
| return true; | ||
| } | ||
|
|
||
| + /// The Power ISA lets an implementation clear a reservation for reasons of | ||
| + /// its own, and e500v2 does so for a sync between the lwarx and the | ||
| + /// stwcx., which leaves a weak cmpxchg unable to ever succeed. Gating on | ||
| + /// isE500() would miss generic powerpc builds, where nothing enables it; | ||
| + /// the cost elsewhere is one fence on the comparison-failed path. | ||
| + bool fenceClearsLoadLinkedReservation() const override { return true; } | ||
| + | ||
| Value *emitLoadLinked(IRBuilderBase &Builder, Type *ValueTy, Value *Addr, | ||
| AtomicOrdering Ord) const override; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: this hunk diverges from the master version by a bit more than the PR description accounts for. The description says only that "the comment it anchors on was reworded in LLVM after 1.94.0", but the leading context differs too — the same hunk in #30206`` anchors after a hook returning
false, whereas here it anchors after one returning `AtomicOrdering::Monotonic`. So the new hook is being inserted at a different position in the class, not just ahead of a differently-worded comment. The hunk header has also lost the `public:` section marker that the master version carries.None of that is a functional problem — declaration order inside the class is irrelevant, and the surrounding atomics hooks are public in both versions — and you state the tree builds, so it clearly applies. It just reads like the anchor was adjusted by hand rather than the patch regenerated (
make package/rust/refreshagainst 1.94.0), which is worth confirming since this hunk is the one thing that will not match master once #30206 lands.I verified the rest of the patch file is byte-identical to the one in #30206; this is the only differing hunk.
Generated by Claude Code