Repository navigation
[RISC-V] Sign-extend CAS comparand after reload - #137318
Merged
Merged
Conversation
genCodeForCmpXchg sign-extended a 32-bit comparand into a temporary register before genConsumeRegs reloaded the comparand from its spill location. On paths with a call before the CAS, it compared against a clobbered register while the success check used the reloaded value, so a failed CAS could be treated as successful. On riscv64 hardware this breaks System.Threading.Lock in TryEnterSlow. With a checked runtime, System.Threading.Tasks.Tests, System.Runtime.Tests and baseservices/exceptions/unhandled fail with "Assertion failed. new State(this).IsLocked" and similar Lock assertions. Co-authored-by: Cursor <[email protected]>
am11
force-pushed
the
fix/riscv64-cmpxchg-sext
branch
from
November 11, 2026 01:30
d14461e to
b2468e2
Compare
|
Azure Pipelines: Successfully started running 5 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
am11
requested review from
a team,
filipnavara,
jakobbotsch,
jkotas and
tomeksowi
November 11, 2026 09:58
Member
Author
|
@dotnet/jit-contrib, PTAL. Follow up changes are depending on this bugfix. It's only visible in non-Release configurations. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the RISC-V CAS comparand sign-extension change.
No open issues 🎉
What changed in this PR
This PR fixes RISC-V compare-and-swap code generation in the JIT by sign-extending the 32-bit comparand after operand reloads, avoiding stale register values in the comparison.
Changes:
- Tracks the source comparand register separately from the sign-extension temporary.
- Moves sign extension after
genConsumeRegsand documents the reload dependency.
| File | Description |
|---|---|
src/coreclr/jit/codegenriscv64.cpp |
Defers CAS comparand sign extension until after operand reloads. |
Review effort: Balanced
jakobbotsch
approved these changes
Nov 12, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
genCodeForCmpXchg sign-extended a 32-bit comparand into a temporary
register before genConsumeRegs reloaded the comparand from its spill
location. On paths with a call before the CAS, it compared against a
clobbered register while the success check used the reloaded value, so
a failed CAS could be treated as successful.
On riscv64 hardware this breaks System.Threading.Lock in TryEnterSlow.
With a checked runtime, System.Threading.Tasks.Tests,
System.Runtime.Tests and baseservices/exceptions/unhandled fail with
"Assertion failed. new State(this).IsLocked" and similar Lock
assertions.