runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Implement Interlocked for small types.

Open MichalPetryka opened this issue 2 years ago • 47 comments

Implements Interlocked.CompareExchange/Exchange for byte/sbyte/short/ushort.

Uses an intrinsic on CoreCLR x64 and ARM64 and fallbacks for other platforms.

Fixes #64658.

MichalPetryka avatar Oct 03 '23 23:10 MichalPetryka

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:

area-System.Threading, new-api-needs-documentation

Milestone: -

ghost avatar Oct 03 '23 23:10 ghost

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.

MichalPetryka avatar Oct 06 '23 18:10 MichalPetryka

/azp run runtime-extra-platforms

lambdageek avatar Oct 06 '23 18:10 lambdageek

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 06 '23 18:10 azure-pipelines[bot]

/azp run runtime-extra-platforms

lambdageek avatar Oct 06 '23 19:10 lambdageek

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 06 '23 19:10 azure-pipelines[bot]

@MihuBot

MichalPetryka avatar Oct 08 '23 01:10 MichalPetryka

/azp run runtime-extra-platforms

EgorBo avatar Oct 08 '23 15:10 EgorBo

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 08 '23 15:10 azure-pipelines[bot]

/azp run runtime-nativeaot-outerloop

EgorBo avatar Oct 08 '23 15:10 EgorBo

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 08 '23 15:10 azure-pipelines[bot]

@MihuBot -arm

MichalPetryka avatar Oct 09 '23 03:10 MichalPetryka

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.

tomeksowi avatar Oct 09 '23 07:10 tomeksowi

/azp run runtime-nativeaot-outerloop

EgorBo avatar Oct 09 '23 12:10 EgorBo

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 09 '23 12:10 azure-pipelines[bot]

/azp run runtime-extra-platforms, runtime-nativeaot-outerloop

lambdageek avatar Oct 10 '23 16:10 lambdageek

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Oct 10 '23 16:10 azure-pipelines[bot]

/azp run runtime-extra-platforms, runtime-nativeaot-outerloop

lambdageek avatar Oct 10 '23 16:10 lambdageek

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Oct 10 '23 16:10 azure-pipelines[bot]

/azp run runtime-nativeaot-outerloop

(on @MichalPetryka's request)

EgorBo avatar Oct 11 '23 20:10 EgorBo

No pipelines are associated with this pull request.

azure-pipelines[bot] avatar Oct 11 '23 20:10 azure-pipelines[bot]

/azp run runtime-nativeaot-outerloop

EgorBo avatar Oct 11 '23 20:10 EgorBo

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Oct 11 '23 20:10 azure-pipelines[bot]

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

lambdageek avatar Oct 13 '23 18:10 lambdageek

@stephentoub, assume you are good with the overall change? If so we can merge.

mangod9 avatar Nov 27 '23 16:11 mangod9

If so we can merge.

This needs a review on the CoreCLR JIT changes too.

MichalPetryka avatar Nov 27 '23 17:11 MichalPetryka

cc @EgorBo, PTAL.

JulieLeeMSFT avatar Nov 27 '23 21:11 JulieLeeMSFT

@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.

stephentoub avatar Nov 28 '23 02:11 stephentoub

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.

MichalPetryka avatar Nov 28 '23 02:11 MichalPetryka