[RISC-V] Use fine-grained fence variants for memory barriers#126566
Merged
jakobbotsch merged 2 commits intodotnet:mainfrom Apr 17, 2026
Merged
[RISC-V] Use fine-grained fence variants for memory barriers#126566jakobbotsch merged 2 commits intodotnet:mainfrom
jakobbotsch merged 2 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves RISC-V64 JIT codegen for memory barriers by honoring BarrierKind and emitting a more specific fence variant instead of always using the heaviest fence rw,rw.
Changes:
- Extends RISC-V64
insBarrierimmediates to represent multiplefencepredecessor/successor masks. - Updates
CodeGen::instGen_MemoryBarrier(RISC-V64) to select thefenceimmediate based onBarrierKind. - Cleans up outdated comments that claimed only full barriers were supported.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/jit/instr.h | Adds additional RISC-V64 insBarrier values intended to represent different fence masks. |
| src/coreclr/jit/codegenriscv64.cpp | Uses BarrierKind to pick an insBarrier immediate when emitting INS_fence. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
The JIT always emitted fence rw,rw (full barrier) regardless of the requested BarrierKind. RISC-V's fence instruction supports finer predecessor/successor bits, so use them: - BARRIER_FULL -> fence rw,rw (0x33) - unchanged - BARRIER_LOAD_ONLY -> fence r,rw (0x23) - acquire semantics - BARRIER_STORE_ONLY -> fence rw,w (0x31) - release semantics Same idea as ARM64, which uses DMB ISHLD for load-only and DMB ISH for full. Volatile reads and post-CpBlk barriers were paying for a full fence when a lighter one is sufficient under RVWMO.
9414867 to
398c025
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refines RISC-V64 JIT memory barrier emission so instGen_MemoryBarrier(BarrierKind) uses the appropriate fence predecessor/successor mask instead of always emitting the heaviest fence rw,rw, aligning barrier strength with .NET volatile semantics.
Changes:
- Add RISC-V-specific
insBarrierencodings for full, load-only (acquire), and store-only (release) fences. - Update
CodeGen::instGen_MemoryBarrieron RISC-V64 to select the correct fence variant based onBarrierKind. - Remove an outdated TODO that stated only full barriers were supported on RISC-V64.
Show a summary per file
| File | Description |
|---|---|
src/coreclr/jit/instr.h |
Defines RISC-V64 fence immediate values for full/load-only/store-only barrier variants. |
src/coreclr/jit/codegenriscv64.cpp |
Maps BarrierKind to the corresponding RISC-V64 fence variant during codegen. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
This was referenced Apr 14, 2026
Open
jakobbotsch
approved these changes
Apr 17, 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.
instGen_MemoryBarrieron RISC-V ignored thebarrierKindparameter and always emittedfence rw,rw. The RISC-V fence instruction has per-bit control over predecessor and successor ordering (read, write, or both), so there's no reason to always use the heaviest option.This maps
BarrierKindto the matching fence variant:BARRIER_FULLfence rw,rwfence rw,rw(unchanged)BARRIER_LOAD_ONLYfence rw,rwfence r,rw(acquire)BARRIER_STORE_ONLYfence rw,rwfence rw,w(release)ARM64 does the same thing with
DMB ISHLDfor load-only andDMB ISHfor full. The load/store variants use acquire/release semantics rather than just load-load or store-store ordering, since .NET's volatile semantics require it.The two call sites that benefit today are
GT_MEMORYBARRIERwithGTF_MEMORYBARRIER_LOAD/GTF_MEMORYBARRIER_STOREflags, and the post-volatile-CpBlk load barrier.Validated the encoding on QEMU (
fence rw,rw=0x0330000f,fence r,rw=0x0230000f,fence rw,w=0x0310000f).