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

Clean up in LowerFusedMultiplyAdd #110113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 14 additions & 34 deletions src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,49 +1330,29 @@ void Lowering::LowerHWIntrinsicCC(GenTreeHWIntrinsic* node, NamedIntrinsic newIn
void Lowering::LowerFusedMultiplyAdd(GenTreeHWIntrinsic* node)
{
assert(node->GetHWIntrinsicId() == NI_FMA_MultiplyAddScalar);
Copy link
Member

Choose a reason for hiding this comment

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

Not really for this PR; but the changes you've made here are notably a bit more supportive/general-purpose.

It would be nice to expand this helper to support the non-scalar FusedMultiplyAdd APIs as well and to support cases like -Vector128.CreateScalarUnsafe(x) (which more generally is just the -x handling needed for vector variants) not just Vector128.CreateScalarUnsafe(-x)

GenTreeHWIntrinsic* createScalarOps[3];
assert(node->GetOperandCount() == 3);

bool negatedArgs[3] = {};
for (size_t i = 1; i <= 3; i++)
{
GenTree* arg = node->Op(i);

if (!arg->OperIsHWIntrinsic() || (arg->AsHWIntrinsic()->GetHWIntrinsicId() != NI_Vector128_CreateScalarUnsafe))
if (arg->OperIsHWIntrinsic() && (arg->AsHWIntrinsic()->GetHWIntrinsicId() == NI_Vector128_CreateScalarUnsafe))
Copy link
Member

Choose a reason for hiding this comment

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

nit, we can make this a bit simpler/more readable:

Suggested change
if (arg->OperIsHWIntrinsic() && (arg->AsHWIntrinsic()->GetHWIntrinsicId() == NI_Vector128_CreateScalarUnsafe))
if (arg->OperIsHWIntrinsic(NI_Vector128_CreateScalarUnsafe))

{
return;
GenTree*& argOp = arg->AsHWIntrinsic()->Op(1);
if (argOp->OperIs(GT_NEG))
{
BlockRange().Remove(argOp);
argOp = argOp->gtGetOp1();
argOp->ClearContained();
ContainCheckHWIntrinsic(arg->AsHWIntrinsic());
negatedArgs[i - 1] = true;
}
}

createScalarOps[i - 1] = arg->AsHWIntrinsic();
}

GenTree* argX = createScalarOps[0]->Op(1);
GenTree* argY = createScalarOps[1]->Op(1);
GenTree* argZ = createScalarOps[2]->Op(1);

const bool negMul = argX->OperIs(GT_NEG) != argY->OperIs(GT_NEG);
if (argX->OperIs(GT_NEG))
{
createScalarOps[0]->Op(1) = argX->gtGetOp1();
BlockRange().Remove(argX);

createScalarOps[0]->Op(1)->ClearContained();
ContainCheckHWIntrinsic(createScalarOps[0]);
}
if (argY->OperIs(GT_NEG))
{
createScalarOps[1]->Op(1) = argY->gtGetOp1();
BlockRange().Remove(argY);

createScalarOps[1]->Op(1)->ClearContained();
ContainCheckHWIntrinsic(createScalarOps[1]);
}
if (argZ->OperIs(GT_NEG))
bool negMul = negatedArgs[0] ^ negatedArgs[1];
if (negatedArgs[2])
{
createScalarOps[2]->Op(1) = argZ->gtGetOp1();
BlockRange().Remove(argZ);

createScalarOps[2]->Op(1)->ClearContained();
ContainCheckHWIntrinsic(createScalarOps[2]);

node->ChangeHWIntrinsicId(negMul ? NI_FMA_MultiplySubtractNegatedScalar : NI_FMA_MultiplySubtractScalar);
}
else
Expand Down
Loading