Skip to content

Conversation

@davidwrighton
Copy link
Member

Add the missing MustExpand intrinsics that are not related to vector or hardware intrinsics

  • 8 and 16 bit forms of Interlocked.Exchange
  • Interlocked.ExchangeAdd
  • Interlocked.MemoryBarrier
  • Volatile.WriteBarrier
  • Double.MultiplyAddEstimate and Single.MultiplyAddEstimate
  • Double.ConvertToIntegerNative and Single.ConvertToIntegerNative
  • Math.ReciprocalEstimate and MathF.ReciprocalEstimate
  • Math.ReciprocalSqrtEstimate and MathF.ReciprocalSqrtEstimate
    And finally as a bonus a related non must-expand intrinsic
  • Math.Sqrt and MathF.Sqrt

Detect MustExpand correctly. (virtual functions cannot be MustExpand)

Also in debug builds, print unconditionally when a MustExpand intrinsic is not expanded, and run the NO_WAY code. (In practice on platforms with a JIT this will fall back to running the JIT to provide the implementation)

Collectively, this unblocks use of InterpMode=3 for some tests. Previously some of these tests would stack overflow in InterpMode=3.

We may want to revisit some of these implementations (notably MultiplyAddEstimate, ConvertToIntegerNative, ReciprocalEstimate, and ReciprocalSqrtEstimate since their behavior will not necessarily match that of the JIT, but for now this will provide generally functional implementations of all of this.)

Copilot AI review requested due to automatic review settings September 12, 2025 22:02
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 12, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds missing "MustExpand" intrinsics to the .NET CoreCLR interpreter to support non-vector and non-hardware intrinsics that are essential for correct program execution. The changes expand intrinsic support for atomic operations, math functions, and memory barriers.

Key changes include:

  • Added 8-bit and 16-bit Interlocked operations (Exchange, CompareExchange) with complex alignment handling
  • Implemented Interlocked.ExchangeAdd and memory barrier operations
  • Added math intrinsics like Sqrt, MultiplyAddEstimate, ReciprocalEstimate, and ConvertToIntegerNative
  • Fixed MustExpand detection to exclude virtual function calls and improved debug output

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/coreclr/vm/interpexec.cpp Implements execution logic for new 8/16-bit atomic operations and math sqrt functions
src/coreclr/interpreter/intrinsics.cpp Maps method names to intrinsic identifiers for Sqrt and ExchangeAdd
src/coreclr/interpreter/inc/intops.def Defines new interpreter opcodes for the additional intrinsic operations
src/coreclr/interpreter/compiler.h Updates function signature and adds helper for floating-point conversions
src/coreclr/interpreter/compiler.cpp Contains the main intrinsic compilation logic and MustExpand detection fixes
Comments suppressed due to low confidence (2)

src/coreclr/interpreter/compiler.cpp:1

  • The alignment check shift & 1 is incorrect for 16-bit operations. Since shift = ((size_t)dst & 3) * 8, this check verifies if the shift is odd (8 or 24 bits), but 16-bit values should be 2-byte aligned. The correct check should be ((size_t)dst & 1) != 0 to verify the address is 2-byte aligned.
// Licensed to the .NET Foundation under one or more agreements.

src/coreclr/interpreter/compiler.cpp:1

  • The alignment check shift & 1 is incorrect for 16-bit operations. Since shift = ((size_t)dst & 3) * 8, this check verifies if the shift is odd (8 or 24 bits), but 16-bit values should be 2-byte aligned. The correct check should be ((size_t)dst & 1) != 0 to verify the address is 2-byte aligned.
// Licensed to the .NET Foundation under one or more agreements.

@dotnet-policy-service
Copy link
Contributor

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

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@kg
Copy link
Member

kg commented Sep 16, 2025

After #119775 this will need to use something other than NO_WAY to get JIT fallback (we want to return CORJIT_SKIPPED)

@davidwrighton davidwrighton merged commit aa77683 into dotnet:main Sep 17, 2025
96 of 98 checks passed
xtqqczze pushed a commit to xtqqczze/dotnet-runtime that referenced this pull request Sep 20, 2025
…dotnet#119670)

Add the missing MustExpand intrinsics that are not related to vector or hardware intrinsics

- 8 and 16 bit forms of Interlocked.Exchange
- Interlocked.ExchangeAdd
- Interlocked.MemoryBarrier
- Volatile.WriteBarrier
- Double.MultiplyAddEstimate and Single.MultiplyAddEstimate
- Double.ConvertToIntegerNative and Single.ConvertToIntegerNative
- Math.ReciprocalEstimate and MathF.ReciprocalEstimate
- Math.ReciprocalSqrtEstimate and MathF.ReciprocalSqrtEstimate
And finally as a bonus a related non must-expand intrinsic
- Math.Sqrt and MathF.Sqrt

Detect MustExpand correctly. (virtual functions cannot be MustExpand)

Also in debug builds, print unconditionally when a MustExpand intrinsic is not expanded, and run the NO_WAY code. (In practice on platforms with a JIT this will fall back to running the JIT to provide the implementation)

Collectively, this unblocks use of InterpMode=3 for some tests. Previously some of these tests would stack overflow in InterpMode=3.

We may want to revisit some of these implementations (notably MultiplyAddEstimate, ConvertToIntegerNative, ReciprocalEstimate, and ReciprocalSqrtEstimate since their behavior will not necessarily match that of the JIT, but for now this will provide generally functional implementations of all of this.)
@github-actions github-actions bot locked and limited conversation to collaborators Oct 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-Interpreter-coreclr needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants