Skip to content

[RISC-V] Sign-extend CAS comparand after reload - #137318

Merged
jakobbotsch merged 1 commit into
dotnet:mainfrom
am11:fix/riscv64-cmpxchg-sext
Nov 12, 2026
Merged

jakobbotsch merged 1 commit into
dotnet:mainfrom
am11:fix/riscv64-cmpxchg-sext

Conversation

@am11

@am11 am11 commented Nov 11, 2026

Copy link
Copy Markdown
Member

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.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 11, 2026
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 11, 2026
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
am11 force-pushed the fix/riscv64-cmpxchg-sext branch from d14461e to b2468e2 Compare November 11, 2026 01:30
@azure-pipelines

Copy link
Copy Markdown
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.

@am11 am11 added the arch-riscv Related to the RISC-V architecture label Nov 11, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@am11

am11 commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib, PTAL. Follow up changes are depending on this bugfix. It's only visible in non-Release configurations.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 genConsumeRegs and documents the reload dependency.
File Description
src/​coreclr/​jit/​codegenriscv64.cpp Defers CAS comparand sign extension until after operand reloads.

Review effort: Balanced

@jakobbotsch
jakobbotsch merged commit 64c3b9e into dotnet:main Nov 12, 2026
136 checks passed
@am11
am11 deleted the fix/riscv64-cmpxchg-sext branch November 12, 2026 14:31
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-riscv Related to the RISC-V architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants