Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
47 changes: 47 additions & 0 deletions src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,53 @@ 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;
//
GenTree* op = nullptr;
GenTree* opZero = nullptr;
if (op1->IsVectorZero())
{
op = op2;
opZero = op1;
}
else if (op2->IsVectorZero())
{
op = op1;
opZero = op2;
}

if (!varTypeIsFloating(simdBaseType) && (op != nullptr))
{
GenTree* cmp =
comp->gtNewSimdHWIntrinsicNode(simdType, op, NI_AdvSimd_Arm64_MaxAcross, CORINFO_TYPE_UBYTE, simdSize);
Copy link
Contributor

Choose a reason for hiding this comment

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

According to Arm® Cortex®-A76 Software Optimization Guide:
UMAXV, 16B has Exec latency 6 and Execution throughput 1/2
while UMAXV, 4H/4S has Exec latency 3 and Execution throughput 1

Do we want CORINFO_TYPE_USHORT/CORINFO_TYPE_UINT as a base type instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, let's change, Although, I had this in mind when I was benchmarking it and saw zero difference, but I now I see why:
image
(Apple M1, Firestorm core)

BlockRange().InsertBefore(node, cmp);
LowerNode(cmp);
BlockRange().Remove(opZero);

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);
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,88 @@
// 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));

TestReversed(Vector128.Create(0));
TestReversed(Vector128.Create(0.0f));
TestReversed(Vector128.Create(-0.0f));
TestReversed(Vector128.Create(0.0));
TestReversed(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));

TestReversed(Vector128.Create(0, 0, 0, 1, 0, 0, 0, 1));
TestReversed(Vector128.Create(0, 0, 0, -1, 0, 0, 0, -1));
TestReversed(Vector64.Create(0, 0, 0, 1, 0, 0, 0, 1));
TestReversed(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)))));

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

[MethodImpl(MethodImplOptions.NoInlining)]
public static void TestReversed<T>(Vector64<T> v) where T : unmanaged =>
AssertTrue((Vector64<T>.Zero == v) ==
(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>