-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Make System.Double/Single.ConvertToInteger intrinsic #122424
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
base: main
Are you sure you want to change the base?
Conversation
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.
|
Tagging subscribers to this area: @BrzVlad, @janvorli, @kg |
There was a problem hiding this 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
ConvertToIntegermethod in Double and Single classes - Reused the existing
ConvertToIntegerNativeimplementation 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 |
davidwrighton
left a comment
There was a problem hiding this 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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>.IsSupportedreturns 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.
There was a problem hiding this comment.
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?
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.