Skip to content

Commit

Permalink
Fix constant folding for shift SIMD operators (dotnet#105752)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Jul 31, 2024
1 parent 7f2b222 commit 87b1d84
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30912,10 +30912,7 @@ GenTree* Compiler::gtFoldExprHWIntrinsic(GenTreeHWIntrinsic* tree)
{
if (otherNode->TypeIs(TYP_SIMD16))
{
if ((ni != NI_AVX2_ShiftLeftLogicalVariable) && (ni != NI_AVX2_ShiftRightArithmeticVariable) &&
(ni != NI_AVX512F_VL_ShiftRightArithmeticVariable) &&
(ni != NI_AVX10v1_ShiftRightArithmeticVariable) &&
(ni != NI_AVX2_ShiftRightLogicalVariable))
if (!HWIntrinsicInfo::IsVariableShift(ni))
{
// The xarch shift instructions support taking the shift amount as
// a simd16, in which case they take the shift amount from the lower
Expand Down
26 changes: 26 additions & 0 deletions src/coreclr/jit/hwintrinsic.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,32 @@ struct HWIntrinsicInfo
}
}

static bool IsVariableShift(NamedIntrinsic id)
{
#ifdef TARGET_XARCH
switch (id)
{
case NI_AVX2_ShiftRightArithmeticVariable:
case NI_AVX512F_ShiftRightArithmeticVariable:
case NI_AVX512F_VL_ShiftRightArithmeticVariable:
case NI_AVX512BW_ShiftRightArithmeticVariable:
case NI_AVX512BW_VL_ShiftRightArithmeticVariable:
case NI_AVX10v1_ShiftRightArithmeticVariable:
case NI_AVX2_ShiftRightLogicalVariable:
case NI_AVX512F_ShiftRightLogicalVariable:
case NI_AVX512BW_ShiftRightLogicalVariable:
case NI_AVX512BW_VL_ShiftRightLogicalVariable:
case NI_AVX10v1_ShiftRightLogicalVariable:
case NI_AVX2_ShiftLeftLogicalVariable:
case NI_AVX512BW_VL_ShiftLeftLogicalVariable:
return true;
default:
return false;
}
#endif // TARGET_XARCH
return false;
}

static bool HasImmediateOperand(NamedIntrinsic id)
{
#if defined(TARGET_ARM64)
Expand Down
4 changes: 1 addition & 3 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8235,9 +8235,7 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(GenTreeHWIntrinsic* tree,
{
if (TypeOfVN(arg1VN) == TYP_SIMD16)
{
if ((ni != NI_AVX2_ShiftLeftLogicalVariable) && (ni != NI_AVX2_ShiftRightArithmeticVariable) &&
(ni != NI_AVX512F_VL_ShiftRightArithmeticVariable) &&
(ni != NI_AVX10v1_ShiftRightArithmeticVariable) && (ni != NI_AVX2_ShiftRightLogicalVariable))
if (!HWIntrinsicInfo::IsVariableShift(ni))
{
// The xarch shift instructions support taking the shift amount as
// a simd16, in which case they take the shift amount from the lower
Expand Down
53 changes: 53 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_105742/Runtime_105742.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Run on Arm64 Linux
// Seed: 10489942769529190437-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
// Reduced from 58.9 KiB to 0.4 KiB in 00:00:28
// Debug: Outputs [9223372036854775808, 9223372036854775808]
// Release: Outputs [0, 0]

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

public class Runtime_105742
{
[Fact]
public static void TestEntyPoint()
{
if (!Avx512BW.VL.IsSupported)
{
return;
}

if (ShiftLeft().ToString() != "<2, 1, 1, 1, 1, 1, 1, 1>")
{
throw new Exception("ShiftLeft");
}
if (ShiftRight().ToString() != "<0, 32767, -1, -1, -1, -1, -1, -1>")
{
throw new Exception("ShiftRight");
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Vector128<short> ShiftLeft()
{
var vr4 = Vector128.Create<short>(1);
var vr5 = (ushort)1;
var vr6 = Vector128.CreateScalar(vr5);
var vr7 = Avx512BW.VL.ShiftLeftLogicalVariable(vr4, vr6);
return vr7;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Vector128<short> ShiftRight()
{
var vr2 = Vector128.Create<short>(-1);
var vr3 = Vector128.Create(65534, 1, 0, 0, 0, 0, 0, 0);
return Avx512BW.VL.ShiftRightLogicalVariable(vr2, vr3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit 87b1d84

Please sign in to comment.