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

Ensure VN handles both forms of the xarch shift instructions for SIMD #91601

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7432,6 +7432,26 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(var_types type,
case NI_AVX512BW_ShiftLeftLogical:
#endif
{
#ifdef TARGET_XARCH
if (TypeOfVN(arg1VN) == TYP_SIMD16)
{
// The xarch shift instructions support taking the shift amount as
// a simd16, in which case they take the shift amount from the lower
// 64-bits.

uint64_t shiftAmount = GetConstantSimd16(arg1VN).u64[0];

if (genTypeSize(baseType) != 8)
{
arg1VN = VNForIntCon(static_cast<int32_t>(shiftAmount));
}
else
{
arg1VN = VNForLongCon(static_cast<int64_t>(shiftAmount));
}
}
#endif // TARGET_XARCH

return EvaluateBinarySimd(this, GT_LSH, /* scalar */ false, type, baseType, arg0VN, arg1VN);
}

Expand All @@ -7445,6 +7465,26 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(var_types type,
case NI_AVX512BW_ShiftRightArithmetic:
#endif
{
#ifdef TARGET_XARCH
if (TypeOfVN(arg1VN) == TYP_SIMD16)
{
// The xarch shift instructions support taking the shift amount as
// a simd16, in which case they take the shift amount from the lower
// 64-bits.

uint64_t shiftAmount = GetConstantSimd16(arg1VN).u64[0];

if (genTypeSize(baseType) != 8)
{
arg1VN = VNForIntCon(static_cast<int32_t>(shiftAmount));
}
else
{
arg1VN = VNForLongCon(static_cast<int64_t>(shiftAmount));
}
}
#endif // TARGET_XARCH

return EvaluateBinarySimd(this, GT_RSH, /* scalar */ false, type, baseType, arg0VN, arg1VN);
}

Expand All @@ -7457,6 +7497,26 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(var_types type,
case NI_AVX512BW_ShiftRightLogical:
#endif
{
#ifdef TARGET_XARCH
if (TypeOfVN(arg1VN) == TYP_SIMD16)
{
// The xarch shift instructions support taking the shift amount as
// a simd16, in which case they take the shift amount from the lower
// 64-bits.

uint64_t shiftAmount = GetConstantSimd16(arg1VN).u64[0];

if (genTypeSize(baseType) != 8)
{
arg1VN = VNForIntCon(static_cast<int32_t>(shiftAmount));
}
else
{
arg1VN = VNForLongCon(static_cast<int64_t>(shiftAmount));
}
}
#endif // TARGET_XARCH

return EvaluateBinarySimd(this, GT_RSZ, /* scalar */ false, type, baseType, arg0VN, arg1VN);
}

Expand Down
19 changes: 19 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_91175/Runtime_91175.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.aa

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

public class TestClass
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static Vector256<int> Method0() => Avx2.ShiftRightArithmetic(Vector256<int>.AllBitsSet, Vector128<int>.AllBitsSet);
tannergooding marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public static void TestEntryPoint()
{
_ = Method0();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is sufficient to cover the reported issue. However, it would be nice that the test checks numerical correctness as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a number of other tests covering numerical correctness of the functionality.

This is namely a regression test covering a very specific assert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had there was a numerical correctness test case covering this specific case, it would had failed on CI before this fix, right? I don't see this PR enabling any existing test. Thus, I suspect that this is sufficient numerical correctness test coverage for the scenario that this PR is fixing.

Copy link
Member Author

@tannergooding tannergooding Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have tests validating it here (which covers overshifting both via constant and non-constant): https://github.com/dotnet/runtime/blob/main/src/tests/Common/GenerateHWIntrinsicTests/GenerateHWIntrinsicTests_X86.cs#L347C1-L348

This pr is fixing an edge case around value numbering in a very particular scenario and ensuring we don't assert. We were already effectively doing the right thing codegen wise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume those tests aren't covering the VN constant folding, or we would have found this issue and #88451 as well. Can we somehow validate basic correctness of the rest of the constant folding that was added in .NET 8?

This pr is fixing an edge case around value numbering in a very particular scenario and ensuring we don't assert. We were already effectively doing the right thing codegen wise.

The release behavior here is to hit a noway assert, so I think we should backport this fix to .NET 8.

Copy link
Member

@jakobbotsch jakobbotsch Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider adding more testing separately from this so we can get going on the backport.

}
}
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