Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Fix ShiftRightLogical constant folding #105571

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30912,9 +30912,10 @@ GenTree* Compiler::gtFoldExprHWIntrinsic(GenTreeHWIntrinsic* tree)

int64_t shiftAmount = otherNode->AsVecCon()->GetElementIntegral(TYP_LONG, 0);

if ((genTypeSize(simdBaseType) != 8) && (shiftAmount > INT_MAX))
if (static_cast<uint64_t>(shiftAmount) >=
(static_cast<uint64_t>(genTypeSize(simdBaseType)) * BITS_PER_BYTE))
{
// Ensure we don't lose track the the amount is an overshift
// Set to -1 to indicate an explicit overshift
shiftAmount = -1;
}

Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,16 @@ TBase EvaluateBinaryScalarSpecialized(genTreeOps oper, TBase arg0, TBase arg1)

case GT_ROL:
{
// Normalize the "rotate by" value
arg1 %= (sizeof(TBase) * BITS_PER_BYTE);
return EvaluateBinaryScalarSpecialized<TBase>(GT_LSH, arg0, arg1) |
EvaluateBinaryScalarRSZ<TBase>(arg0, (sizeof(TBase) * 8) - arg1);
}

case GT_ROR:
{
// Normalize the "rotate by" value
arg1 %= (sizeof(TBase) * BITS_PER_BYTE);
return EvaluateBinaryScalarRSZ<TBase>(arg0, arg1) |
EvaluateBinaryScalarSpecialized<TBase>(GT_LSH, arg0, (sizeof(TBase) * 8) - arg1);
}
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8244,14 +8244,14 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(GenTreeHWIntrinsic* tree,
// 64-bits.

uint64_t shiftAmount = GetConstantSimd16(arg1VN).u64[0];
if (shiftAmount >= (static_cast<uint64_t>(genTypeSize(baseType)) * BITS_PER_BYTE))
{
// Set to -1 to indicate an explicit overshift
shiftAmount = -1;
}

if (genTypeSize(baseType) != 8)
{
if (shiftAmount > INT_MAX)
{
// Ensure we don't lose track the the amount is an overshift
shiftAmount = -1;
}
arg1VN = VNForIntCon(static_cast<int32_t>(shiftAmount));
}
else
Expand Down
44 changes: 44 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_105558/Runtime_105558.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Generated by Fuzzlyn v2.1 on 2024-07-26 12:56:41
// Run on X64 Windows
// Seed: 13544108888657591911-vectort,vector128,vector256,x86aes,x86avx,x86avx2,x86bmi1,x86bmi1x64,x86bmi2,x86bmi2x64,x86fma,x86lzcnt,x86lzcntx64,x86pclmulqdq,x86popcnt,x86popcntx64,x86sse,x86ssex64,x86sse2,x86sse2x64,x86sse3,x86sse41,x86sse41x64,x86sse42,x86sse42x64,x86ssse3,x86x86base
// Reduced from 290.9 KiB to 0.6 KiB in 00:02:37
// Debug: Prints 1 line(s)
// Release: Prints 0 line(s)
using System;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using Xunit;

public struct S0
{
public int F0;
}

public struct S1
{
public S0 F5;
}

public class Runtime_105558
{
public static S1 s_3;

[Fact]
public static void TestEntryPoint()
{
if (!Sse2.IsSupported)
return;

var vr17 = Vector128.CreateScalar(2558356441U);
var vr18 = Vector128.Create(0, 3113514718U, 0, 0);
var vr19 = Sse2.ShiftRightLogical(vr17, vr18);
if (0 >= Sse2.ConvertToUInt32(vr19))
{
return;
}
throw new InvalidOperationException();
}
}
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>
Loading