Skip to content

Commit b2d559e

Browse files
committed
Change the ReciprocalEstimate and ReciprocalSqrtEstimate APIs to be mustExpand on RyuJIT
1 parent 95a0265 commit b2d559e

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

src/coreclr/jit/importercalls.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,15 +3125,7 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
31253125
// To be fixed in https://github.com/dotnet/runtime/pull/77465
31263126
const bool tier0opts = !opts.compDbgCode && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT);
31273127

3128-
if (tier0opts)
3129-
{
3130-
// The *Estimate APIs are allowed to differ in behavior across hardware
3131-
// so ensure we treat them as "betterToExpand" to get deterministic behavior
3132-
3133-
betterToExpand |= (ni == NI_System_Math_ReciprocalEstimate);
3134-
betterToExpand |= (ni == NI_System_Math_ReciprocalSqrtEstimate);
3135-
}
3136-
else if (!mustExpand)
3128+
if (!mustExpand)
31373129
{
31383130
switch (ni)
31393131
{
@@ -7471,6 +7463,8 @@ bool Compiler::IsTargetIntrinsic(NamedIntrinsic intrinsicName)
74717463
{
74727464
case NI_System_Math_Abs:
74737465
case NI_System_Math_Sqrt:
7466+
case NI_System_Math_ReciprocalEstimate:
7467+
case NI_System_Math_ReciprocalSqrtEstimate:
74747468
return true;
74757469

74767470
default:
@@ -8847,7 +8841,7 @@ GenTree* Compiler::impEstimateIntrinsic(CORINFO_METHOD_HANDLE method,
88478841
simdSize = 16;
88488842
}
88498843

8850-
GenTree* op1 = impPopStack().val;
8844+
GenTree* op1 = impImplicitR4orR8Cast(impPopStack().val, callType);
88518845

88528846
op1 = gtNewSimdCreateScalarUnsafeNode(simdType, op1, callJitType, simdSize);
88538847
op1 = gtNewSimdHWIntrinsicNode(simdType, op1, intrinsicId, callJitType, simdSize);
@@ -8856,10 +8850,27 @@ GenTree* Compiler::impEstimateIntrinsic(CORINFO_METHOD_HANDLE method,
88568850
}
88578851
#endif // FEATURE_HW_INTRINSICS
88588852

8859-
// TODO-CQ: Returning this as an intrinsic blocks inlining and is undesirable
8860-
// return impMathIntrinsic(method, sig, callType, intrinsicName, tailCall);
8853+
switch (intrinsicName)
8854+
{
8855+
case NI_System_Math_ReciprocalEstimate:
8856+
case NI_System_Math_ReciprocalSqrtEstimate:
8857+
{
8858+
GenTree* op1 = impImplicitR4orR8Cast(impPopStack().val, callType);
88618859

8862-
return nullptr;
8860+
if (intrinsicName == NI_System_Math_ReciprocalSqrtEstimate)
8861+
{
8862+
op1 = new (this, GT_INTRINSIC) GenTreeIntrinsic(genActualType(callType), op1, NI_System_Math_Sqrt,
8863+
nullptr);
8864+
}
8865+
8866+
return gtNewOperNode(GT_DIV, genActualType(callType), gtNewDconNode(1.0, callType), op1);
8867+
}
8868+
8869+
default:
8870+
{
8871+
unreached();
8872+
}
8873+
}
88638874
}
88648875

88658876
GenTree* Compiler::impMathIntrinsic(CORINFO_METHOD_HANDLE method,
@@ -8958,8 +8969,8 @@ GenTree* Compiler::impMinMaxIntrinsic(CORINFO_METHOD_HANDLE method,
89588969
GenTreeDblCon* cnsNode = nullptr;
89598970
GenTree* otherNode = nullptr;
89608971

8961-
GenTree* op2 = impStackTop().val;
8962-
GenTree* op1 = impStackTop(1).val;
8972+
GenTree* op2 = impImplicitR4orR8Cast(impStackTop().val, callType);
8973+
GenTree* op1 = impImplicitR4orR8Cast(impStackTop(1).val, callType);
89638974

89648975
if (op2->IsCnsFltOrDbl())
89658976
{
@@ -9227,7 +9238,7 @@ GenTree* Compiler::impMinMaxIntrinsic(CORINFO_METHOD_HANDLE method,
92279238
retNode->AsHWIntrinsic()->Op(2) = op1;
92289239
}
92299240

9230-
return gtNewSimdToScalarNode(callType, retNode, callJitType, 16);
9241+
return gtNewSimdToScalarNode(genActualType(callType), retNode, callJitType, 16);
92319242
}
92329243
}
92339244
#endif // FEATURE_HW_INTRINSICS && TARGET_XARCH
@@ -9392,7 +9403,7 @@ GenTree* Compiler::impMinMaxIntrinsic(CORINFO_METHOD_HANDLE method,
93929403
callJitType, 16);
93939404
}
93949405

9395-
return gtNewSimdToScalarNode(callType, tmp, callJitType, 16);
9406+
return gtNewSimdToScalarNode(genActualType(callType), tmp, callJitType, 16);
93969407
}
93979408
#endif // FEATURE_HW_INTRINSICS && TARGET_XARCH
93989409

src/libraries/System.Private.CoreLib/src/System/Math.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,10 +1196,13 @@ public static double MinMagnitude(double x, double y)
11961196
/// <para>On hardware without specialized support, this may just return <c>1.0 / d</c>.</para>
11971197
/// </remarks>
11981198
[Intrinsic]
1199-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12001199
public static double ReciprocalEstimate(double d)
12011200
{
1201+
#if MONO
12021202
return 1.0 / d;
1203+
#else
1204+
return ReciprocalEstimate(d);
1205+
#endif
12031206
}
12041207

12051208
/// <summary>Returns an estimate of the reciprocal square root of a specified number.</summary>
@@ -1210,10 +1213,13 @@ public static double ReciprocalEstimate(double d)
12101213
/// <para>On hardware without specialized support, this may just return <c>1.0 / Sqrt(d)</c>.</para>
12111214
/// </remarks>
12121215
[Intrinsic]
1213-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12141216
public static double ReciprocalSqrtEstimate(double d)
12151217
{
1218+
#if MONO
12161219
return 1.0 / Sqrt(d);
1220+
#else
1221+
return ReciprocalSqrtEstimate(d);
1222+
#endif
12171223
}
12181224

12191225
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/libraries/System.Private.CoreLib/src/System/MathF.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,13 @@ public static float MinMagnitude(float x, float y)
314314
/// <para>On hardware without specialized support, this may just return <c>1.0 / x</c>.</para>
315315
/// </remarks>
316316
[Intrinsic]
317-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
318317
public static float ReciprocalEstimate(float x)
319318
{
319+
#if MONO
320320
return 1.0f / x;
321+
#else
322+
return ReciprocalEstimate(x);
323+
#endif
321324
}
322325

323326
/// <summary>Returns an estimate of the reciprocal square root of a specified number.</summary>
@@ -329,10 +332,13 @@ public static float ReciprocalEstimate(float x)
329332
/// <para>On hardware without specialized support, this may just return <c>1.0 / Sqrt(x)</c>.</para>
330333
/// </remarks>
331334
[Intrinsic]
332-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
333335
public static float ReciprocalSqrtEstimate(float x)
334336
{
337+
#if MONO
335338
return 1.0f / Sqrt(x);
339+
#else
340+
return ReciprocalSqrtEstimate(x);
341+
#endif
336342
}
337343

338344
[Intrinsic]

0 commit comments

Comments
 (0)