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

Optimize Vector128<long> multiplication for arm64 #104177

Merged
merged 8 commits into from
Jul 2, 2024
Merged
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
37 changes: 36 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21909,7 +21909,6 @@ GenTree* Compiler::gtNewSimdBinOpNode(

case GT_MUL:
{
assert(!varTypeIsLong(simdBaseType));
GenTree** scalarOp = nullptr;

if (varTypeIsArithmetic(op1))
Expand Down Expand Up @@ -21973,6 +21972,42 @@ GenTree* Compiler::gtNewSimdBinOpNode(
break;
}

case TYP_LONG:
case TYP_ULONG:
{
if (simdSize == 8)
{
// Vector64<long> vec = Vector64.CreateScalar(op1.ToScalar() * op2.ToScalar())
op1 = gtNewBitCastNode(TYP_LONG, op1);
op2 = gtNewBitCastNode(TYP_LONG, op2);
Comment on lines +21981 to +21982
Copy link
Member

Choose a reason for hiding this comment

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

Why bitcast instead of ToScalar? If this is generating better code, it seems like a pretty "core" scenario we're not handling from the ToScalar path

Copy link
Member Author

@EgorBo EgorBo Jul 2, 2024

Choose a reason for hiding this comment

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

@tannergooding because op2 can be either 8-byte TYP_SIMD8 or 8-byte scalar (TYP_LONG) so bitcast allowed me to simplify handling. In my initial version I forgot that this path is used for both MUL(vector, vector) and MUL(vector, scalar) (where scalar is broadcasted)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that makes sense, 👍

GenTreeOp* mul = gtNewOperNode(GT_MUL, TYP_LONG, op1, op2);
return gtNewSimdCreateScalarNode(TYP_SIMD8, mul, simdBaseJitType, 8);
}

// Make op1 and op2 multi-use:
GenTree* op1Dup = fgMakeMultiUse(&op1);
GenTree* op2Dup = fgMakeMultiUse(&op2);

// long left0 = op1.GetElement(0)
// long right0 = op2.GetElement(0)
GenTree* left0 = gtNewSimdToScalarNode(TYP_LONG, op1, simdBaseJitType, 16);
GenTree* right0 =
scalarOp != nullptr ? op2 : gtNewSimdToScalarNode(TYP_LONG, op2, simdBaseJitType, 16);

// long left1 = op1.GetElement(1)
// long right1 = op2.GetElement(1)
GenTree* left1 = gtNewSimdGetElementNode(TYP_LONG, op1Dup, gtNewIconNode(1), simdBaseJitType, 16);
GenTree* right1 = scalarOp != nullptr ? op2Dup
: gtNewSimdGetElementNode(TYP_LONG, op2Dup, gtNewIconNode(1),
simdBaseJitType, 16);

// Vector128<long> vec = Vector128.Create(left0 * right0, left1 * right1)
op1 = gtNewOperNode(GT_MUL, TYP_LONG, left0, right0);
op2 = gtNewOperNode(GT_MUL, TYP_LONG, left1, right1);
GenTree* vec = gtNewSimdCreateScalarUnsafeNode(type, op1, simdBaseJitType, 16);
return gtNewSimdWithElementNode(type, vec, gtNewIconNode(1), op2, simdBaseJitType, 16);
}

default:
{
unreached();
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/jit/hwintrinsicarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1769,12 +1769,6 @@ GenTree* Compiler::impSpecialIntrinsic(NamedIntrinsic intrinsic,
{
assert(sig->numArgs == 2);

if (varTypeIsLong(simdBaseType))
{
// TODO-ARM64-CQ: We should support long/ulong multiplication.
break;
}

CORINFO_ARG_LIST_HANDLE arg1 = sig->args;
CORINFO_ARG_LIST_HANDLE arg2 = info.compCompHnd->getArgNext(arg1);
var_types argType = TYP_UNKNOWN;
Expand Down
Loading