Skip to content

ARM64 - Changed GT_MADD to not emit 'msub' - #68088

Closed
TIHan wants to merge 18 commits into
dotnet:mainfrom
TIHan:arm64-madd-change
Closed

TIHan wants to merge 18 commits into
dotnet:mainfrom
TIHan:arm64-madd-change

Conversation

@TIHan

@TIHan TIHan commented Apr 15, 2022 •

Copy link
Copy Markdown
Contributor

Trying to address this issue: #67869

Description

We added a new lowering op 'GT_MSUB' for ARM64. It only emits 'msub'. However, the existing 'GT_MADD' can also emit 'msub' if only one of the multiply ops is a GT_NEG.

For consistency, 'GT_MADD' should simply emit 'madd' and we should do the transformation earlier when one of the multiply ops is 'GT_NEG'.

I'm trying out the transformation in morph or lowering to see how it goes. We should not at all regress anything as a result of this PR.

Acceptance Criteria

  • CI Passes

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

ghost commented Apr 15, 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

Trying to address this issue: #67869

Description

We added a new lowering op 'GT_MSUB' for ARM64. It only emits 'msub'. However, the existing 'GT_MADD' can also emit 'msub' if only one of the multiply ops is a GT_NEG.

For consistency, 'GT_MADD' should simply emit 'madd' and we should do the transformation earlier when one of the multiply ops is 'GT_NEG'.

I'm trying out the transformation in morph to see how it goes. We should not at all regress anything as a result of this PR.

Acceptance Criteria

  • CI Passes
Author: TIHan
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@TIHan TIHan changed the title ARM64 - Changed GT_MADD to not emit 'msub' [WIP] ARM64 - Changed GT_MADD to not emit 'msub' Apr 15, 2022
@TIHan

TIHan commented Apr 21, 2022

Copy link
Copy Markdown
Contributor Author

@kunalspathak @BruceForstall

This is ready if CI passes. From the diffs I've looked it, this is what was changed:

-            mul     w0, w21, w0
-            add     w1, w0, #5
+            mov     w1, #5
+            madd    w1, w21, w0, w1

@TIHan TIHan changed the title [WIP] ARM64 - Changed GT_MADD to not emit 'msub' ARM64 - Changed GT_MADD to not emit 'msub' Apr 21, 2022

@BruceForstall BruceForstall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few questions.

Also, looks like a lot of tests failed.

Please run jitstress when other pipelines succeed.

useMsub = !useMsub; // it's either "a * -b" or "-a * -b" which is the same as "a * b"
}
GenTree* a = tree->gtGetOp1();
GenTree* b = tree->gtGetOp2()->gtGetOp1();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is tree->gtGetOp2() expected to be GT_MUL? Is that because we don't have a GenTree node that contains all three arguments directly (should we? like some of the block ops, for example) Can you assert that?

@TIHan TIHan Apr 22, 2022 •

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.

Is tree->gtGetOp2() expected to be GT_MUL?

Yes and I believe it is because we don't have a three-argument node.

should we?

I don't think we need to.

It feels a little odd to have a tree like this:
MADD(a, MUL(b, c))

I don't think it is necessary to even have GT_MADD/GT_MSUB nodes.
Before containment, if the expr looks like this:
ADD(a, MUL(b, c)) - we really only need to mark the MUL as contained, then at codegen, we look to see if op2 is GT_MUL and contained to do the 'madd'. I did this with GT_MSUB here: #68363

I might end up combining this PR and that one because they are so similar.

{
// Find "a * b + c" or "c + a * b" in order to emit MADD/MSUB
if (node->OperIs(GT_ADD) && !node->gtOverflow() && (op1->OperIs(GT_MUL) || op2->OperIs(GT_MUL)))
// Find "a + b * c" in order to emit MADD

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't you handle the a * b + c case anymore?

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.

It does get handled if you look at the change in Lowering::LowerAdd:

                    // Transform "a * b + c" to "c + a * b"
                    else if (op1->OperIs(GT_MUL))
                    {
                        node->gtOp1 = c;
                        node->gtOp2 = mul;
                    }

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add some comments here about the purpose of the following code?

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 can do that, though this kind of code is pretty common in lowering.

Comment thread src/coreclr/jit/lower.cpp
#ifdef TARGET_ARM64
if (BlockRange().TryGetUse(node, &use))
{
if (comp->opts.OptimizationEnabled() && varTypeIsIntegral(node) && !node->gtOverflow() &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason why this optimization is done during lower instead of morph?

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.

Yes. There are a couple of reasons:

  • There are no GT_MADD/GT_MSUB HIR ops that can be used in the morph phase.
  • Re-arranging the nodes in morph, like what we are doing in lowering, change what CSEs get picked up.
    • Originally in this PR, I did the re-arrangement in morph. When I looked at the codegen, there were several places that regressed. I wish we had a morph-like phase after all CSE phases are complete instead of having to do the work in lowering.

@TIHan

TIHan commented Apr 22, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for the review @BruceForstall .

Almost all CI failures here are for x64 which is very weird; it should not be as the result of these changes.

I think I'm going to combine this with #68363 .

@TIHan

TIHan commented Apr 23, 2022

Copy link
Copy Markdown
Contributor Author

#68363 now has these changes - I'm going to combine them in there.

@TIHan TIHan closed this Apr 23, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 23, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants