Update fgMorphSmpOp to canonicalize commutative and compare ops at the start of POST-ORDER#127661
Update fgMorphSmpOp to canonicalize commutative and compare ops at the start of POST-ORDER#127661tannergooding wants to merge 7 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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
fgMorphSmpOppost-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 theGT_EQ/GT_NEpost-order handling. - Strengthen/debug-guard morphing assumptions by asserting
fgMorphTreeis not invoked during the value-number CSE phase.
c1f0ad2 to
c4471af
Compare
…e start of POST-ORDER
c4471af to
b3541f8
Compare
|
@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. |
| std::swap(tree->AsOp()->gtOp1, tree->AsOp()->gtOp2); | ||
| tree->gtOper = GenTree::SwapRelop(tree->OperGet()); | ||
| { | ||
| fgPushConstantsRight(tree->AsOp()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| op1->SetOper(GT_AND); // Change % => & | ||
| op1op2->AsIntConCommon()->SetIconValue(modValue - 1); // Change c => c - 1 | ||
| fgUpdateConstTreeValueNumber(op1op2); | ||
| otherNode->SetOper(GT_AND); // Change % => & |
There was a problem hiding this comment.
we need to preserve VN, otherwise it makes no sense to call fgUpdateConstTreeValueNumber below
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| else if (op1->IsIntegralConst(0)) | ||
| { | ||
| cnsNode = op1; | ||
| otherNode = op2; |
There was a problem hiding this comment.
so why do we rotate constants by hands here when you introduced a helper?
There was a problem hiding this comment.
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.
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. |
No description provided.