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: Faster vector == Vector128.Zero on arm64 #65632

Merged
merged 11 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
36 changes: 36 additions & 0 deletions src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,42 @@ void Lowering::LowerHWIntrinsicCmpOp(GenTreeHWIntrinsic* node, genTreeOps cmpOp)
GenTree* op1 = node->Op(1);
GenTree* op2 = node->Op(2);

// Optimize comparison against Vector64/128<>.Zero via UMAX:
//
// bool eq = v == Vector128<integer>.Zero
//
// to:
//
// bool eq = AdvSimd.Arm64.MaxAcross(v.AsByte()).ToScalar() == 0;
//
// Assume morph made get_Zero rhs if it was lhs
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
NamedIntrinsic op2ni = op2->OperIsHWIntrinsic() ? op2->AsHWIntrinsic()->GetHWIntrinsicId() : NI_Illegal;
if (!varTypeIsFloating(simdBaseType) && ((op2ni == NI_Vector64_get_Zero) || (op2ni == NI_Vector128_get_Zero)))
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
GenTree* cmp =
comp->gtNewSimdHWIntrinsicNode(simdType, op1, NI_AdvSimd_Arm64_MaxAcross, CORINFO_TYPE_UBYTE, simdSize);
BlockRange().InsertBefore(node, cmp);
LowerNode(cmp);
BlockRange().Remove(op2);

GenTree* val = comp->gtNewSimdHWIntrinsicNode(TYP_INT, cmp, NI_Vector128_ToScalar, CORINFO_TYPE_UINT, simdSize);
BlockRange().InsertAfter(cmp, val);
LowerNode(val);

GenTree* cmpZroCns = comp->gtNewIconNode(0, TYP_INT);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
BlockRange().InsertAfter(val, cmpZroCns);

node->ChangeOper(cmpOp);
node->gtType = TYP_INT;
node->AsOp()->gtOp1 = val;
node->AsOp()->gtOp2 = cmpZroCns;
LowerNodeCC(node, (cmpOp == GT_EQ) ? GenCondition::EQ : GenCondition::NE);
node->gtType = TYP_VOID;
node->ClearUnusedValue();
LowerNode(node);
return;
}

NamedIntrinsic cmpIntrinsic;

switch (simdBaseType)
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/mini/simd-intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@ emit_sri_vector (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsi
}
case SN_Create: {
MonoType *etype = get_vector_t_elem_type (fsig->ret);
if (!MONO_TYPE_IS_INTRINSICS_VECTOR_PRIMITIVE (etype))
return NULL;
if (fsig->param_count == 1 && mono_metadata_type_equal (fsig->params [0], etype))
return emit_simd_ins (cfg, klass, type_to_expand_op (etype), args [0]->dreg, -1);
else if (is_create_from_half_vectors_overload (fsig))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

public class CompareVectorWithZero
{
public static int Main()
{
Test(Vector128.Create(0));
Test(Vector128.Create(0.0f));
Test(Vector128.Create(-0.0f));
Test(Vector128.Create(0.0));
Test(Vector128.Create(-0.0));
Test(Vector128.Create(-10));
Test(Vector128.Create(10));
Test(Vector128.Create((sbyte)-10));
Test(Vector128.Create((ushort)10));
Test(Vector64.Create(0));
Test(Vector64.Create(0.0f));
Test(Vector64.Create(-0.0f));
Test(Vector64.Create(0.0));
Test(Vector64.Create(-0.0));
Test(Vector64.Create(-10));
Test(Vector64.Create(10));
Test(Vector64.Create((sbyte)-10));
Test(Vector64.Create((ushort)10));
Test(Vector128.Create(0, 0, 0, 0, 0, 0, 0, 1));
Test(Vector128.Create(0, 0, 0, 0, 0, 0, 0, -1));
Test(Vector64.Create(0, 0, 0, 0, 0, 0, 0, 1));
Test(Vector64.Create(0, 0, 0, 0, 0, 0, 0, -1));

Test(Vector128.Create(0, 0, 0, 0, 0, 0, 1, 0));
Test(Vector128.Create(0, 0, 0, 0, 0, 0, 1, 0));
Test(Vector64.Create(0, 0, 0, 0, 0, 0, -1, 0));
Test(Vector64.Create(0, 0, 0, 0, 0, 0, -1, 0));

Test(Vector128.Create(0, 0, 0, 1, 0, 0, 0, 1));
Test(Vector128.Create(0, 0, 0, -1, 0, 0, 0, -1));
Test(Vector64.Create(0, 0, 0, 1, 0, 0, 0, 1));
Test(Vector64.Create(0, 0, 0, -1, 0, 0, 0, -1));
return 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static T ToVar<T>(T t) => t;

[MethodImpl(MethodImplOptions.NoInlining)]
public static void AssertTrue(bool expr)
{
if (!expr)
throw new InvalidOperationException();
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Test<T>(Vector128<T> v) where T : unmanaged =>
AssertTrue((v == Vector128<T>.Zero) ==
(v == Vector128.Create(ToVar(default(T)))));

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Test<T>(Vector64<T> v) where T : unmanaged =>
AssertTrue((v == Vector64<T>.Zero) ==
(v == Vector64.Create(ToVar(default(T)))));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>