Skip to content

arm64: Additions with GetActiveElementCount() should use incp when given the same predicate #114628

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions src/coreclr/jit/codegenarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,17 @@ void CodeGen::genCodeForBinary(GenTreeOp* tree)
emit->emitIns_Add_Add_Tls_Reloc(attr, targetReg, op1->GetRegNum(), op2->AsIntCon()->IconValue());
return;
}
else if (oper == GT_ADD && op1->isContained() && op1->OperIsHWIntrinsic(NI_Sve_GetActiveElementCount))
{
const GenTreeHWIntrinsic* elementCountNode = op1->AsHWIntrinsic();
GenTree* elementCountChildNode = elementCountNode->Op(1);

regNumber reg1 = op2->GetRegNum();
regNumber reg2 = elementCountChildNode->GetRegNum();

emit->emitInsSve_R_R(INS_sve_incp, EA_8BYTE, reg1, reg2, INS_OPTS_SCALABLE_B);
return;
}

instruction ins = genGetInsForOper(tree->OperGet(), targetType);

Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20494,7 +20494,8 @@ bool GenTree::isContainableHWIntrinsic() const
}
}
#elif defined(TARGET_ARM64)
return (AsHWIntrinsic()->GetHWIntrinsicId() == NI_Sve_ConditionalSelect);
return (AsHWIntrinsic()->GetHWIntrinsicId() == NI_Sve_ConditionalSelect ||
AsHWIntrinsic()->GetHWIntrinsicId() == NI_Sve_GetActiveElementCount);
#else
return false;
#endif // TARGET_XARCH
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7299,6 +7299,11 @@ GenTree* Lowering::LowerAdd(GenTreeOp* node)
{
return next;
}

if (TryContainingGetActiveElementCount(node))
{
return next;
}
}
#endif // TARGET_ARM64

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Lowering final : public Phase
bool TryLowerAddSubToMulLongOp(GenTreeOp* op, GenTree** next);
bool TryLowerNegToMulLongOp(GenTreeOp* op, GenTree** next);
bool TryContainingCselOp(GenTreeHWIntrinsic* parentNode, GenTreeHWIntrinsic* childNode);
bool TryContainingGetActiveElementCount(GenTreeOp* node);
#endif
#ifdef TARGET_RISCV64
bool TryLowerShiftAddToShxadd(GenTreeOp* tree, GenTree** next);
Expand Down
25 changes: 25 additions & 0 deletions src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3738,6 +3738,31 @@ bool Lowering::TryContainingCselOp(GenTreeHWIntrinsic* parentNode, GenTreeHWIntr
return canContain;
}

bool Lowering::TryContainingGetActiveElementCount(GenTreeOp* node)
{
assert(node->OperIs(GT_ADD));

GenTree* op1 = node->gtGetOp1();
GenTree* op2 = node->gtGetOp2();
if (!op1->OperIsHWIntrinsic(NI_Sve_GetActiveElementCount))
{
return false;
}

assert(op1->canBeContained());

const GenTreeHWIntrinsic* elementCountNode = op1->AsHWIntrinsic();
GenTree* mask1 = elementCountNode->Op(1)->AsHWIntrinsic()->Op(2);
GenTree* mask2 = elementCountNode->Op(2)->AsHWIntrinsic()->Op(2);
if (mask1->GetVN(VNK_Liberal) != mask2->GetVN(VNK_Liberal))
Copy link
Member

Choose a reason for hiding this comment

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

So here we are using value numbering to contain the node, which seems wrong. This should ideally be happening in VN/CSE. Did you check why that is not happening?

{
return false;
}

MakeSrcContained(node, op1);
return true;
}

#endif // TARGET_ARM64

//------------------------------------------------------------------------
Expand Down
85 changes: 85 additions & 0 deletions src/tests/JIT/opt/SVE/active_element_count.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using Xunit;

public class TestGetActiveElementCount
{
[Fact]
public static int TestEntryPoint()
{
if (Sve.IsSupported)
{
bool fail = false;

Vector<byte> v0 = Vector.Create<byte>(1);
Vector<byte> v1 = Vector.Create<byte>(4);
if (DifferentVector(v0, v1, 0) != 16)
{
fail = true;
}

if (SameVector(v0, 0) != 16)
{
fail = true;
}

Vector<double> vDouble0 = Vector.Create<double>(1);
Vector<double> vDouble1 = Vector.Create<double>(4);
if (DifferentVectorDouble(vDouble0, vDouble1, 0) != 2)
{
fail = true;
}

if (SameVectorDouble(vDouble0, 0) != 2)
{
fail = true;
}

if (fail)
{
return 101;
}
}
return 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ulong DifferentVector(Vector<byte> v0, Vector<byte> v1, ulong total)
{
//ARM64-FULL-LINE: cntp {{x[0-9]+}}, {{p[0-9]+}}, {{p[0-9]+}}.b
//ARM64-FULL-LINE: add {{x[0-9]+}}, {{x[0-9]+}}, {{x[0-9]+}}
total += Sve.GetActiveElementCount(v0, v1);
return total;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ulong SameVector(Vector<byte> v0, ulong total)
{
//ARM64-FULL-LINE: incp {{x[0-9]+}}, {{p[0-9]+}}.b
total += Sve.GetActiveElementCount(v0, v0);
return total;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ulong DifferentVectorDouble(Vector<double> v0, Vector<double> v1, ulong total)
{
//ARM64-FULL-LINE: cntp {{x[0-9]+}}, {{p[0-9]+}}, {{p[0-9]+}}.b
//ARM64-FULL-LINE: add {{x[0-9]+}}, {{x[0-9]+}}, {{x[0-9]+}}
total += Sve.GetActiveElementCount(v0, v1);
return total;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ulong SameVectorDouble(Vector<double> v0, ulong total)
{
//ARM64-FULL-LINE: incp {{x[0-9]+}}, {{p[0-9]+}}.b
total += Sve.GetActiveElementCount(v0, v0);
return total;
}
}
10 changes: 10 additions & 0 deletions src/tests/JIT/opt/SVE/active_element_count.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<Optimize>True</Optimize>
<NoWarn>$(NoWarn),SYSLIB5003</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading