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
17 changes: 15 additions & 2 deletions src/coreclr/jit/hwintrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,12 +2027,25 @@ bool Compiler::CheckHWIntrinsicImmRange(NamedIntrinsic intrinsic,
#ifdef TARGET_ARM64
switch (intrinsic)
{
case NI_AdvSimd_ShiftLeftLogical:
case NI_AdvSimd_ShiftLeftLogicalScalar:
case NI_AdvSimd_ShiftRightLogical:
case NI_AdvSimd_ShiftRightLogicalScalar:
case NI_AdvSimd_ShiftRightArithmetic:
case NI_AdvSimd_ShiftRightArithmeticScalar:
if (ival > immUpperBound)
{
// Larger right shifts have the same result as shifting by the element width.
immOp->AsIntCon()->SetIconValue(immUpperBound);
return true;
}
FALLTHROUGH;

case NI_AdvSimd_ShiftLeftLogical:
case NI_AdvSimd_ShiftLeftLogicalScalar:
if (ival > immUpperBound)
{
// Left shifts by the element width require the register fallback.
immOp->AsIntCon()->SetIconValue(immUpperBound + 1);
}
*useFallback = true;
break;

Expand Down
20 changes: 19 additions & 1 deletion src/coreclr/jit/hwintrinsicarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,27 @@ GenTree* Compiler::impNonConstFallback(NamedIntrinsic intrinsic, var_types simdT
// register

GenTree* op2 = impPopStack().val;

if (!op2->IsCnsIntOrI())
{
const int elementBits = BITS_PER_BYTE * genTypeSize(simdBaseType);
GenTree* op2Dup = nullptr;

op2 =
impCloneExpr(op2, &op2Dup, CHECK_SPILL_ALL, nullptr DEBUGARG("Clone shift count for upper bound"));

GenTree* condition = gtNewOperNode(GT_GT, TYP_INT, op2, gtNewIconNode(elementBits));
GenTree* boundedCount =
gtNewQmarkNode(TYP_INT, condition, gtNewColonNode(TYP_INT, gtNewIconNode(elementBits), op2Dup));

unsigned tmp = lvaGrabTemp(true DEBUGARG("Bounded shift count"));
impStoreToTemp(tmp, boundedCount, CHECK_SPILL_ALL);
op2 = gtNewLclvNode(tmp, TYP_INT);
}

GenTree* op1 = impSIMDPopStack();

// AdvSimd.ShiftLogical does right-shifts with negative immediates, hence the negation
// Register shifts use negative counts for right shifts.
if (isRightShift)
{
op2 = gtNewOperNode(GT_NEG, genActualType(op2->TypeGet()), op2);
Expand Down
34 changes: 34 additions & 0 deletions src/tests/JIT/Regression_ro_2/Runtime_138387.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using Xunit;

public class Runtime_138387
{
[ConditionalFact(typeof(AdvSimd), nameof(AdvSimd.IsSupported))]
public static void TestEntryPoint()
{
foreach (byte c in new byte[] { 9, 128, 255 })
{
Assert.Equal((byte)0, ShiftLeft(Vector64.Create((byte)2), c).GetElement(0));
Assert.Equal((byte)0, ShiftRight(Vector64.Create((byte)2), c).GetElement(0));
Assert.Equal((sbyte)-1, ShiftRightArith(Vector64.Create((sbyte)-2), c).GetElement(0));
}

Assert.Equal((byte)0, AdvSimd.ShiftLeftLogical(Vector64.Create((byte)2), 255).GetElement(0));
Assert.Equal((byte)0, AdvSimd.ShiftRightLogical(Vector64.Create((byte)2), 255).GetElement(0));
Assert.Equal((sbyte)-1, AdvSimd.ShiftRightArithmetic(Vector64.Create((sbyte)-2), 255).GetElement(0));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static Vector64<byte> ShiftLeft(Vector64<byte> v, byte c) => AdvSimd.ShiftLeftLogical(v, c);

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static Vector64<byte> ShiftRight(Vector64<byte> v, byte c) => AdvSimd.ShiftRightLogical(v, c);

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static Vector64<sbyte> ShiftRightArith(Vector64<sbyte> v, byte c) => AdvSimd.ShiftRightArithmetic(v, c);
}
Loading