Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0] [mono][mini] Interlocked.CompareExchange and Interlocked.Exchange intrinsics for small types and enums #106897

Merged
merged 28 commits into from
Aug 26, 2024

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Aug 23, 2024

Backport of #106660 to release/9.0

/cc @lambdageek

Customer Impact

  • Customer reported
  • Found internally

After the BCL started to use the new Interlocked.Exchange<T>(...) and Interlocked.CompareExchange<T>(...) APIs with bool and enum types for certain internal lock-free data structures, Mono experienced a severe performance regression across multiple form factors because we did not implement intrinsics for types backed by 8-bit and 16-bit integers. As a result the slower IL fallbacks were used. See #105335

Regression

  • Yes
  • No

This is a performance regression, not a correctness regression.

Testing

We have test coverage for these cases in the JIT and libraries testsuires.

Risk

Medium.

Additionally on WebAssembly we now always emit the atomic instructions in the jiterpreter. It is possible customers may deploy apps on older browser that do not implement those instructions. (Although all modern browsers support the opcodes even on non-shared memory). As a mitigation the jiterpreter includes a configuration flag to turn off JITing for traces that require the new opcodes.

It is possible that there are correctness criteria that are missed by our testsuites and we may JIT or incorrect code on some architectures. It is unlikely that we will AOT incorrect code as changes to LLVM codegen were minor. Possible customer workarounds in case of a JIT bug would be to produce AOT profiles that force AOT for the affected methods.

Fixes #105335
Fixes #93488

lambdageek and others added 26 commits August 23, 2024 20:11
also add mono_atomic_cas_u8 utility function
maybe we can't actually do a 1 or 2 byte move from RAX ?
fixup u16 win32 atomic
for the 16-bit addressing prefix
…t fail to allocate a table index and generate a warning. This shouldn't happen in prod anyway

Implement cmpxchg atomics natively in jiterpreter

Remove unnecessary jiterp cas helpers

Do cmpxchg result fixups as needed

Add runtime option for jiterpreter atomics
Implement atomic exchanges in the jiterpreter
@lambdageek
Copy link
Member

Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

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

approved. please get a code review. once ready we can merge

@jeffschwMSFT jeffschwMSFT added the Servicing-approved Approved for servicing release label Aug 23, 2024
@lambdageek lambdageek self-assigned this Aug 23, 2024
Copy link
Member

@kg kg left a comment

Choose a reason for hiding this comment

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

Interp/jiterp look right.

Copy link
Member

@jkurdek jkurdek left a comment

Choose a reason for hiding this comment

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

The arm and llvm changes look good.

@@ -1216,12 +1216,42 @@ typedef union {

#define amd64_prefix_size(inst,p,size) do { x86_prefix((inst), p); } while (0)
#define amd64_rdtsc_size(inst,size) do { amd64_codegen_pre(inst); amd64_emit_rex ((inst),(size),0,0,0); x86_rdtsc(inst); amd64_codegen_post(inst); } while (0)
// FIXME: wrong for size == 1 or 2
Copy link
Member

Choose a reason for hiding this comment

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

those limitations do not concern current usage scenarios?

Copy link
Member

Choose a reason for hiding this comment

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

Correct. These macros are not currently used for size 1 or 2

@lambdageek
Copy link
Member

lambdageek commented Aug 26, 2024

PR is green, reviewed and signed off. Ready to merge /cc @carlossanlop

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-meta-mono Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants