Skip to content

ARM64 - Consolidate 'msub' and 'madd' logic - #68363

Merged
TIHan merged 36 commits into
dotnet:mainfrom
TIHan:arm64-msub-change
May 12, 2022
Merged

ARM64 - Consolidate 'msub' and 'madd' logic#68363
TIHan merged 36 commits into
dotnet:mainfrom
TIHan:arm64-msub-change

Conversation

@TIHan

@TIHan TIHan commented Apr 22, 2022

Copy link
Copy Markdown
Contributor

This PR combines this one: #68363

As we move forward with implementing more combined operations for ARM64, it is important that we try to make things as simple as we can.

I added GT_MSUB, a lowering-only op for ARM64, that only emits msub. However, I think we do not need to introduce a new op to achieve this. We only need to check if the second operand is a multiply op that is contained.

@ghost ghost assigned TIHan Apr 22, 2022
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 22, 2022
@ghost

ghost commented Apr 22, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

As we move forward with implementing more combined operations for ARM64, it is important that we try to make things as simple as we can.

I added GT_MSUB, a lowering-only op for ARM64, that only emits msub. However, I think we do not need to introduce a new op to achieve this. We only need to check if the second operand is a multiply op that is contained.

Author: TIHan
Assignees: TIHan
Labels:

area-CodeGen-coreclr

Milestone: -
Comment thread src/coreclr/jit/codegen.h Outdated
@@ -1379,7 +1379,6 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#if defined(TARGET_ARM64)
void genCodeForJumpCompare(GenTreeOp* tree);
void genCodeForMadd(GenTreeOp* tree);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should madd be handled as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe I should combine this with #68088 .

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change makes sense to me and will ensure we don't need to add n new node kinds to eventually handle everything in #68028

It just becomes a case of adding the containment check in lowering and then switching the instruction in codegen, which becomes a much smaller and easier change to get in.

@TIHan TIHan changed the title ARM64 - Removed GT_MSUB in favor of a simple containment check. Apr 23, 2022
Comment thread src/coreclr/jit/lowerarmarch.cpp Outdated
if (node->OperIs(GT_ADD) && !node->gtOverflow() && (op1->OperIs(GT_MUL) || op2->OperIs(GT_MUL)))
// Find "a + b * c" or "a - b * c".
// Then mark "b * c" op as contained in order to emit 'madd' or 'msub' respectively.
if (node->OperIs(GT_ADD, GT_SUB) && !(node->gtFlags & GTF_SET_FLAGS) && !node->gtOverflow() &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I should make some helper functions to handle this; there are a lot of conditions here...

Comment thread src/coreclr/jit/lower.cpp Outdated
return next;
}
#ifdef TARGET_ARM64
if (BlockRange().TryGetUse(node, &use))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you need the use for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought I had an understanding of this, but I think I actually don't.

I removed this piece from the change.

Comment thread src/coreclr/jit/lower.cpp Outdated
Comment on lines +5482 to +5506
if (a->OperIs(GT_NEG) && !(a->gtFlags & GTF_SET_FLAGS) && !b->OperIs(GT_NEG) && !a->isContained() &&
!a->gtGetOp1()->isContained())
{
mul->AsOp()->gtOp1 = a->gtGetOp1();
BlockRange().Remove(a);
node->gtOp1 = c;
node->gtOp2 = mul;
node->ChangeOper(GT_SUB);
}
// Transform "a * -b + c" to "c - a * b"
else if (b->OperIs(GT_NEG) && !(b->gtFlags & GTF_SET_FLAGS) && !a->OperIs(GT_NEG) &&
!b->isContained() && !b->gtGetOp1()->isContained())
{
mul->AsOp()->gtOp2 = b->gtGetOp1();
BlockRange().Remove(b);
node->gtOp1 = c;
node->gtOp2 = mul;
node->ChangeOper(GT_SUB);
}
// Transform "a * b + c" to "c + a * b"
else if (op1->OperIs(GT_MUL))
{
node->gtOp1 = c;
node->gtOp2 = mul;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The reordering that is happening here makes me a little uneasy. Since locals are used at their user, it is now technically possible that this reorders the read of a local with a write of the same local.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If anything the problem will happen during containment, so this code at least should be ok. And the containment checks are preexisting, so I suppose it is fine in practice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I know what you mean, I felt the same way. I spoke to @tannergooding about it.

As far as I know, the re-ordering of the tree itself doesn't change the linear execution unless containment happens - correct me if I'm wrong here... So I think it should be ok.

@TIHan

TIHan commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

@kunalspathak I think this should do it. I ran an asmdiff locally and it showed no changes.

Comment thread src/coreclr/jit/lowerarmarch.cpp
Comment thread src/coreclr/jit/lowerarmarch.cpp Outdated
@TIHan

TIHan commented May 11, 2022

Copy link
Copy Markdown
Contributor Author

@dotnet/jit-contrib This PR is done. There should be no diffs with this change.

@TIHan
TIHan merged commit 2cc7fea into dotnet:main May 12, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

4 participants