Implement Interlocked for small types.
Implements Interlocked.CompareExchange/Exchange for byte/sbyte/short/ushort.
Uses an intrinsic on CoreCLR x64 and ARM64 and fallbacks for other platforms.
Fixes #64658.
Note regarding the new-api-needs-documentation label:
This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.
Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.
Issue Details
Implements Interlocked.CompareExchange/Exchange for byte/sbyte/short/ushort.
Uses an intrinsic on CoreCLR x64 and ARM64 and fallbacks for other platforms.
Fixes #64658.
| Author: | MichalPetryka |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
With this the fallbacks will be used again for the small types on RISC-V, cc @tomeksowi in case you want to add intrinsics for those too since you've recently added intrinsics for the existing overloads in #92102.
/azp run runtime-extra-platforms
Azure Pipelines successfully started running 1 pipeline(s).
/azp run runtime-extra-platforms
Azure Pipelines successfully started running 1 pipeline(s).
@MihuBot
/azp run runtime-extra-platforms
Azure Pipelines successfully started running 1 pipeline(s).
/azp run runtime-nativeaot-outerloop
Azure Pipelines successfully started running 1 pipeline(s).
@MihuBot -arm
With this the fallbacks will be used again for the small types on RISC-V, cc @tomeksowi in case you want to add intrinsics for those too since you've recently added intrinsics for the existing overloads in #92102.
Thanks for the heads-up. RISC-V atomics don't come in short-int variants so there would have to be additional masking, in that case CAS blows up to a sequence of dozen instructions or so (which clang even outlines on -Os), Interlocked.Exchange which normally is one instruction must be a full lr-sc loop for short ints.
I will put them on my todo but won't work on them anytime soon as we're focusing more on stability now. IMHO there's little gain over just calling these fallbacks.
/azp run runtime-nativeaot-outerloop
Azure Pipelines successfully started running 1 pipeline(s).
/azp run runtime-extra-platforms, runtime-nativeaot-outerloop
Azure Pipelines successfully started running 2 pipeline(s).
/azp run runtime-extra-platforms, runtime-nativeaot-outerloop
Azure Pipelines successfully started running 2 pipeline(s).
/azp run runtime-nativeaot-outerloop
(on @MichalPetryka's request)
No pipelines are associated with this pull request.
/azp run runtime-nativeaot-outerloop
Azure Pipelines successfully started running 1 pipeline(s).
Created https://github.com/dotnet/runtime/issues/93488 for mono follow-up work to actually support these operations as intrinsics in the mono JIT/AOT and interpreter
@stephentoub, assume you are good with the overall change? If so we can merge.
If so we can merge.
This needs a review on the CoreCLR JIT changes too.
cc @EgorBo, PTAL.
@stephentoub, assume you are good with the overall change?
Yes. Subsequently, we should go through use of Interlocked in the core libraries looking for opportunities to use these. There are at least a handful of places we use Interlocked with an int but could get away with it being a byte, and in some cases we may be able to shave off a few bytes of allocation.
should go through use of Interlocked in the core libraries looking for opportunities to use these
Worth noting that doing so would permanently pessimize the code performance on RISC-V.