Skip to content

Conversation

@janvorli
Copy link
Member

There are two libraries tests that fail because the implementation of the System.Double/Single.ConvertToInteger in S.P.C. doesn't match the intrinsic implementation that JIT uses and behaves differently w.r.t. small primitive types (short, sbyte).

The intrinsic reuses the ConvertToIntegerNative one, JIT also shares the implementation for some targets.

There are two libraries tests that fail because the implementation of the
System.Double/Single.ConvertToInteger in S.P.C. doesn't match the
intrinsic implementation that JIT uses and behaves differently
w.r.t. small primitive types (short, sbyte).

The intrinsic reuses the ConvertToIntegerNative one, JIT also shares
the implementation for some targets.
@janvorli janvorli added this to the 11.0.0 milestone Dec 11, 2025
@janvorli janvorli self-assigned this Dec 11, 2025
Copilot AI review requested due to automatic review settings December 11, 2025 00:11
@janvorli janvorli requested review from BrzVlad and kg as code owners December 11, 2025 00:11
@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.

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 PR makes System.Double.ConvertToInteger and System.Single.ConvertToInteger intrinsic methods in the interpreter to match the JIT behavior. The library implementation uses saturation semantics (TInteger.CreateSaturating) which differs from the platform-specific intrinsic behavior for small primitive types (short, sbyte). By making these methods intrinsic, the interpreter will use the same implementation as ConvertToIntegerNative, ensuring consistent behavior across JIT and interpreter execution.

Key changes:

  • Added intrinsic recognition for ConvertToInteger method in Double and Single classes
  • Reused the existing ConvertToIntegerNative implementation via FALLTHROUGH pattern

Reviewed changes

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

File Description
src/coreclr/interpreter/intrinsics.cpp Added method name check for "ConvertToInteger" to return NI_PRIMITIVE_ConvertToInteger for Double/Single classes
src/coreclr/interpreter/compiler.cpp Added case statement for NI_PRIMITIVE_ConvertToInteger that falls through to NI_PRIMITIVE_ConvertToIntegerNative, reusing the same implementation

Copy link
Member

@davidwrighton davidwrighton left a comment

Choose a reason for hiding this comment

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

I've reviewed this, and the JIT's implementation. This is an interesting way to get these results, but I think this should work nicely to closely match RyuJit behavior. I wonder why we don't see complaints that using ConvertToInteger results in the results from running the C# code, but shrug I guess this is what we need.

m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
return true;

case NI_PRIMITIVE_ConvertToInteger:
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, I might've misunderstood or misexplained a bit when we talked on teams.


ConvertToInteger and ConvertToIntegerNative do not have the same behavior, and this is intentional., so this fix isn't necessarily correct/valid.

The former, ConvertToInteger, does a deterministic conversion that should be correctly handling all types including byte, sbyte, and others. That is, it should always be done using saturation and so double.MinValue -> sbyte should produce -128.

The latter, ConvertToIntegerNative, does not strictly guarantee the edge case handling and may do things like simply emit cvtss2si which returns a sentinel value (0x8000_0000) instead. This may also end up doing something like double -> int -> sbyte for the conversion.


However, the former has a known issue in its intrinsic handling on RyuJIT where it may also do double -> int -> sbyte instead of directly doing double -> sbyte. This is a bug in that handling and really needs to be fixed to ensure the direct (intrinsic) and indirect (managed) implementations are aligned with eachother.

This bug stems from the historical IR handling for cast nodes and is more complex to resolve. That is, (sbyte)someDouble is conv.i1 and is effectively doing double -> int -> sbyte. And while it could be argued this is fine for the cast behavior, is not correct for ConvertToInteger.

To then answer @davidwrighton's question, users probably aren't complaining because most are doing (sbyte)someDouble, they aren't doing double.ConvertToInteger<sbyte>(someDouble) and really aren't comparing that to delegate or reflection based invocation so it all looks consistent to them.


I would expect that for the interpreter, we also need to ensure consistency between the various functions. That is, we need direct and indirect invocations of ConvertToInteger to be consistent. The same needs to be true for ConvertToIntegerNative, but the two of these do not need to be consistent with eachother.

If the interpreter can rollover to jitting at some point, then it is important that these also match the JIT behavior, so that users don't experience change in results mid compilation.

I can work on getting a fix up for the conv.i1/u1/i2/u2 issue on the JIT side.

Copy link
Member

Choose a reason for hiding this comment

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

If the interpreter can rollover to jitting at some point, then it is important that these also match the JIT behavior, so that users don't experience change in results mid compilation.

The interpreter is expected to be mixed-and-matched with R2R code in .NET 11, so this problem exists today.

The problem with ConvertToIntegerNative consistency is very similar to hardware intrinsics. The strategy that we use for hardware intrinsics is:

  • In pure interpreter config (no R2R), hardware intrinsics are disabled (<typename>.IsSupported returns false)
  • In Interpreter+R2R config, hardware instinsics are hardcoded to match platform baseline, and we make sure that all hardware intrinsics have R2R code so they are never interpreted (see Configure HardwareIntrinsic support for Apple Mobile platforms #122249)

We may want to use the same strategy for ConvertToIntegerNative and other similar JIT intrinsics.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, would it be preferrable to drop this PR a keep the two respective tests failing until we fix the JIT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants