Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/coreclr/interpreter/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3139,6 +3139,8 @@ bool InterpCompiler::EmitNamedIntrinsicCall(NamedIntrinsic ni, bool nonVirtualCa
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?

FALLTHROUGH;
case NI_PRIMITIVE_ConvertToIntegerNative:
{
CHECK_STACK(1);
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/interpreter/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ NamedIntrinsic GetNamedIntrinsic(COMP_HANDLE compHnd, CORINFO_METHOD_HANDLE comp
{
if (!strcmp(className, "Double") || !strcmp(className, "Single"))
{
if (!strcmp(methodName, "ConvertToIntegerNative"))
if (!strcmp(methodName, "ConvertToInteger"))
return NI_PRIMITIVE_ConvertToInteger;
else if (!strcmp(methodName, "ConvertToIntegerNative"))
return NI_PRIMITIVE_ConvertToIntegerNative;
else if (!strcmp(methodName, "MultiplyAddEstimate"))
return NI_System_Math_MultiplyAddEstimate;
Expand Down
Loading