JIT: relop simplification during redundant branch opts#127181
JIT: relop simplification during redundant branch opts#127181AndyAyersMS wants to merge 2 commits intodotnet:mainfrom
Conversation
Suppose we have a dominating branch A (with predicate pA) that shares a successor with a dominated branch B (with predicate pB). can optimize away the comparison done in A. Here we extend that optimization to handle some cases where pB does not imply pA, by forming (AND pB pA) (in VN space) and seeing if we can simplify it to a relop over the same operands as pB, or to a constant. If so, we can remove the comparison in done A but now also must modify the comparison done in B. For example ```if ((x >= 100) && (x <= 100)) S;``` can be simplified to ```if (x == 100) S;``` and ```if ((x >= 100) || (x <= 100)) S;``` can be simplified to ```S;``` As part of this, teach VN how to simplify various combinations of AND/OR/NOT involving relops (there are many cases). Incorporates some of the changes from dotnet#83859, but does not try and handle "ignorable" side effects. Fixes dotnet#98227.
|
@EgorBo PTAL Modest number of diffs locally. |
There was a problem hiding this comment.
Pull request overview
This PR extends redundant-branch optimization in the CoreCLR JIT by using value-number (VN) boolean simplification to eliminate a dominating comparison even when the dominated predicate doesn’t directly imply it, by simplifying (pB AND pA) into a single relop (or constant) and updating the dominated comparison accordingly.
Changes:
- Add VN simplifications for
NOTover boolean forms (double-negation, DeMorgan transforms, and relop negation). - Add VN
AND/ORsimplification tables to combine related relops with identical operands into a tighter relop or a constant. - Extend
optRedundantDominatingBranchto attempt VN-based simplification ofblockPathVN AND domPathVNand (when successful) rewrite the dominated relop.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/coreclr/jit/valuenum.cpp | Adds VN boolean/relop simplifications (NOT handling and AND/OR relop-combination tables). |
| src/coreclr/jit/redundantbranchopts.cpp | Tries VN simplification when direct implication fails; rewrites dominated compare when simplification yields a new relop. |
| if (andApp.m_args[0] == pathApp.m_args[0] && andApp.m_args[1] == pathApp.m_args[1]) | ||
| { | ||
| newRelop = (genTreeOps)andApp.m_func; | ||
| } |
There was a problem hiding this comment.
andApp.m_func is a VNFunc and may be one of the unsigned relop VNFuncs (e.g., VNF_LT_UN, VNF_GE_UN), which are not valid genTreeOps values. Casting it to genTreeOps and later calling OpName/SetOper can misbehave. Map unsigned VNFuncs to the corresponding GT_LT/GT_GE/... and update the compare node's unsigned flag instead of casting directly.
| ValueNum andVN = vnStore->VNForFunc(TYP_INT, VNF_AND, blockPathVN, domPathVN); | ||
| VNFuncApp andApp; | ||
| VNFuncApp pathApp; | ||
| if (vnStore->IsVNRelop(andVN, &andApp) && vnStore->GetVNFunc(blockPathVN, &pathApp)) | ||
| { |
There was a problem hiding this comment.
The simplification path only handles the case where andVN simplifies to a relop. VNForFunc(AND, ...) can also simplify to a constant 0/1; if that happens, IsVNRelop(andVN, ...) will be false and you miss the optimization. Consider explicitly handling the constant case (e.g., by bashing/removing the dominated compare when the path condition becomes always-true/always-false).
| // NOT(relop(x,y)) ==> Reverse(relop)(x,y) | ||
| // | ||
| else if (VNFuncIsComparison(funcApp.m_func)) | ||
| { | ||
| *resultVN = GetRelatedRelop(arg0VN, VN_RELATION_KIND::VRK_Reverse); | ||
| } |
There was a problem hiding this comment.
This rewrite treats GT_NOT(relop) as logical negation of the comparison, but VN constant folding for GT_NOT is bitwise complement (~) and comparisons fold to 0/1. With 0/1 semantics, ~(x < y) is not equivalent to (x >= y), so this can make VNs unsound. Either represent logical NOT explicitly (e.g., XOR-with-1 / EQ-with-0) or gate this rewrite to a representation where true is all-bits-set and ensure folding matches it.
| // NOT(relop(x,y)) ==> Reverse(relop)(x,y) | |
| // | |
| else if (VNFuncIsComparison(funcApp.m_func)) | |
| { | |
| *resultVN = GetRelatedRelop(arg0VN, VN_RELATION_KIND::VRK_Reverse); | |
| } |
| VNFuncApp arg1FN; | ||
| if (GetVNFunc(arg1VN, &arg1FN) && VNFuncIsComparison(arg1FN.m_func)) | ||
| { | ||
| if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1])) |
There was a problem hiding this comment.
This relop-combination simplification for GT_OR currently triggers for floating-point comparisons too, but several table identities are not valid with NaN/unordered semantics (e.g., LT | GE is not always true for floats). Consider skipping this optimization when either relop operand is floating-point (similar to GetRelatedRelop, which returns NoVN for float compares).
| if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1])) | |
| if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1]) && | |
| !varTypeIsFloating(TypeOfVN(arg0FN.m_args[0])) && !varTypeIsFloating(TypeOfVN(arg0FN.m_args[1]))) |
| VNFuncApp arg0FN; | ||
| if (GetVNFunc(arg0VN, &arg0FN) && VNFuncIsComparison(arg0FN.m_func)) | ||
| { | ||
| VNFuncApp arg1FN; | ||
| if (GetVNFunc(arg1VN, &arg1FN) && VNFuncIsComparison(arg1FN.m_func)) |
There was a problem hiding this comment.
Same issue for the GT_AND relop-combination: for floating-point comparisons, NaN/unordered semantics make several AND identities invalid. Add a non-floating check before consulting s_relatedRelopTable_AND.
| } | ||
| else if (andApp.m_args[0] == pathApp.m_args[1] && andApp.m_args[1] == pathApp.m_args[0]) | ||
| { | ||
| vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap); |
There was a problem hiding this comment.
In the swapped-operands case, the return value of GetRelatedRelop(andVN, VRK_Swap) is ignored. GetRelatedRelop returns a new VN and does not mutate andVN, so andApp will still reflect the original (unswapped) relop. Assign the returned VN back to a local (e.g., andVN = ...) before re-querying andApp.
| vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap); | |
| andVN = vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap); |
| rii.domCmpNormVN = blockPathVN; | ||
|
|
||
| optRelopImpliesRelop(&rii); | ||
| bool canOptimize = rii.canInfer && rii.canInferFromTrue && !rii.reverseSense; |
There was a problem hiding this comment.
optRelopImpliesRelop appears to exclude floating point values, so canOptimize will be false for FP operations, right? So copilot's stuff about FP is wrong?
There was a problem hiding this comment.
Not quite, because we now do this new thing if that check fails, and that was not sufficiently cautious around FP compares.
|
I went over this and don't feel confident enough in my understanding of VN to give it a green check. A lot of copilot's concerns match mine. |
|
Looks like there are some failures to sort through. |
Suppose we have a dominating branch A (with predicate pA) that shares a successor with a dominated branch B (with predicate pB).
can optimize away the comparison done in A.
Here we extend that optimization to handle some cases where pB does not imply pA, by forming (AND pB pA) (in VN space) and seeing if we can simplify it to a relop over the same operands as pB, or to a constant.
If so, we can remove the comparison in done A but now also must modify the comparison done in B.
For example
if ((x >= 100) && (x <= 100)) S;can be simplified to
if (x == 100) S;and
if ((x >= 100) || (x <= 100)) S;can be simplified to
S;As part of this, teach VN how to simplify various combinations of AND/OR/NOT involving relops (there are many cases).
Incorporates some of the changes from #83859, but does not try and handle "ignorable" side effects.
Fixes #98227.