Skip to content

Update fgMorphSmpOp to canonicalize commutative and compare ops at the start of POST-ORDER#127661

Open
tannergooding wants to merge 7 commits intodotnet:mainfrom
tannergooding:fgMorphSmpOp
Open

Update fgMorphSmpOp to canonicalize commutative and compare ops at the start of POST-ORDER#127661
tannergooding wants to merge 7 commits intodotnet:mainfrom
tannergooding:fgMorphSmpOp

Conversation

@tannergooding
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings May 1, 2026 20:09
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 1, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts CoreCLR JIT morphing to canonicalize operand ordering for commutative operators and comparisons at the start of fgMorphSmpOp post-order processing, improving consistency and enabling post-order optimizations that rely on constants being placed predictably.

Changes:

  • Canonicalize commutative and comparison trees in fgMorphSmpOp post-order by swapping when the left operand is a constant (and swapping relops as needed).
  • Move the ((a % pow2) ==/!= 0) => (a & (pow2-1) ==/!= 0) pattern optimization from pre-order into the GT_EQ/GT_NE post-order handling.
  • Strengthen/debug-guard morphing assumptions by asserting fgMorphTree is not invoked during the value-number CSE phase.

Copilot AI review requested due to automatic review settings May 1, 2026 20:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/jit/morph.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Copilot AI review requested due to automatic review settings May 1, 2026 23:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI review requested due to automatic review settings May 2, 2026 01:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/jit/morph.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Copilot AI review requested due to automatic review settings May 2, 2026 04:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/jit/morph.cpp
@tannergooding tannergooding marked this pull request as ready for review May 2, 2026 05:59
@tannergooding
Copy link
Copy Markdown
Member Author

@EgorBo, this should be ready now. There's a few regressions where folding looks to stop happening. It needs to be investigated, but I don't think we should block on it. There's likely some other places that aren't looking for or handling the right edges.

Comment thread src/coreclr/jit/morph.cpp
std::swap(tree->AsOp()->gtOp1, tree->AsOp()->gtOp2);
tree->gtOper = GenTree::SwapRelop(tree->OperGet());
{
fgPushConstantsRight(tree->AsOp());
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.

I do not understand why we have to call it here and above + more code above vs what I suggested in https://github.com/dotnet/runtime/compare/main...EgorBo:runtime-1:swap-cns?expand=1 - much smaller change with better diffs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because it had a significant throughput hit, nearly 0.20% in minopts

It is simply not efficient to have every single simple op check if they are a compare or commutative node.

Comment thread src/coreclr/jit/morph.cpp
op1->SetOper(GT_AND); // Change % => &
op1op2->AsIntConCommon()->SetIconValue(modValue - 1); // Change c => c - 1
fgUpdateConstTreeValueNumber(op1op2);
otherNode->SetOper(GT_AND); // Change % => &
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.

we need to preserve VN, otherwise it makes no sense to call fgUpdateConstTreeValueNumber below

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What isn't being preserved for VN here? This is doing the exact same thing as before just handling either op1 or op2 being the constant

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 isn't being preserved for VN here? This is doing the exact same thing as before just handling either op1 or op2 being the constant

SetOper erases it by default, PRESERVE_VN should be used

Comment thread src/coreclr/jit/morph.cpp
else if (op1->IsIntegralConst(0))
{
cnsNode = op1;
otherNode = op2;
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.

so why do we rotate constants by hands here when you introduced a helper?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because this isn't actually swapping the constants. It's keeping them as is and simply tracking which is constant.

They are swapped further down in post order, which avoids the extra expense and repeated work.

@EgorBo
Copy link
Copy Markdown
Member

EgorBo commented May 2, 2026

There's a few regressions

any idea why osx-arm64 seems to be a pure regression?

@tannergooding
Copy link
Copy Markdown
Member Author

any idea why osx-arm64 seems to be a pure regression?

It has more opts being done, in part because it has a higher ISA baseline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

3 participants