diff --git a/src/coreclr/jit/hwintrinsic.cpp b/src/coreclr/jit/hwintrinsic.cpp index e9c8f69ea63769..2b46fc8e378822 100644 --- a/src/coreclr/jit/hwintrinsic.cpp +++ b/src/coreclr/jit/hwintrinsic.cpp @@ -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; diff --git a/src/coreclr/jit/hwintrinsicarm64.cpp b/src/coreclr/jit/hwintrinsicarm64.cpp index 5d317102f27d6a..91758ae9d8ae82 100644 --- a/src/coreclr/jit/hwintrinsicarm64.cpp +++ b/src/coreclr/jit/hwintrinsicarm64.cpp @@ -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); diff --git a/src/tests/JIT/Regression_ro_2/Runtime_138387.cs b/src/tests/JIT/Regression_ro_2/Runtime_138387.cs new file mode 100644 index 00000000000000..4d9c9832d90943 --- /dev/null +++ b/src/tests/JIT/Regression_ro_2/Runtime_138387.cs @@ -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 ShiftLeft(Vector64 v, byte c) => AdvSimd.ShiftLeftLogical(v, c); + + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static Vector64 ShiftRight(Vector64 v, byte c) => AdvSimd.ShiftRightLogical(v, c); + + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static Vector64 ShiftRightArith(Vector64 v, byte c) => AdvSimd.ShiftRightArithmetic(v, c); +}