From 032dd3945de872205411e55588f67ace5d4257c1 Mon Sep 17 00:00:00 2001 From: Rachel Jarvi Date: Tue, 1 Dec 2026 15:22:16 -0800 Subject: [PATCH 1/2] fix 138387 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: b53d3c92-88dc-48a6-a7d9-3e7d6d43a34c --- src/coreclr/jit/hwintrinsic.cpp | 17 ++++++++-- src/coreclr/jit/hwintrinsicarm64.cpp | 20 ++++++++++- .../JIT/Regression_ro_2/Runtime_138387.cs | 34 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 src/tests/JIT/Regression_ro_2/Runtime_138387.cs 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..97b1f25e730a28 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); +} From a41031a306fa31de6aba47188480bf47fa6f50bb Mon Sep 17 00:00:00 2001 From: Rachel Jarvi Date: Tue, 1 Dec 2026 16:14:25 -0800 Subject: [PATCH 2/2] fix format --- src/coreclr/jit/hwintrinsicarm64.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/hwintrinsicarm64.cpp b/src/coreclr/jit/hwintrinsicarm64.cpp index 97b1f25e730a28..91758ae9d8ae82 100644 --- a/src/coreclr/jit/hwintrinsicarm64.cpp +++ b/src/coreclr/jit/hwintrinsicarm64.cpp @@ -635,8 +635,8 @@ GenTree* Compiler::impNonConstFallback(NamedIntrinsic intrinsic, var_types simdT 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")); + 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 =