-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chained comparison of two integers against a constant is not coalesced #102103
Open
pedrobsaila
wants to merge
21
commits into
dotnet:main
Choose a base branch
from
pedrobsaila:101347
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
43dad09
Chained comparison of two integers against a constant is not coalesced
pedrobsaila 951ce9d
format file
pedrobsaila a8dbdbf
optimize only int operands
pedrobsaila 64a79f5
try to fix segmentation fault on arm
pedrobsaila 34c6eb7
try fix errors in arm builds
pedrobsaila 4f37008
fix regression on pattern a != || b==
pedrobsaila 872a62e
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila 5591e9a
do not optimize for arm
pedrobsaila 5fbaa7d
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila 62e5261
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila 99bd794
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila c653261
rollback dev
pedrobsaila f6b4e8a
rollback test
pedrobsaila dec1c11
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila 0bfee18
fix remarks
pedrobsaila 679bfec
Merge remote-tracking branch 'upstream/main' into 101347
pedrobsaila e24840d
fix build errors 1
pedrobsaila 781c223
fix build errors 2
pedrobsaila 0d2ba7b
fix build errors 3
pedrobsaila 3e8b530
fix build errors 4
pedrobsaila 6ab5360
fix build errors 5
pedrobsaila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this check? There should be a comment why the transformation isn't profitable on these platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedrobsaila as well..
The JIT already supports creating conditional chains. It was implemented by @a74nh in #79283. It is only enabled on arm64 because only arm64 has conditional compare.
Conditional compare is going to be better than the xor pattern from the example, that's probably why this is a regression on arm64. Also, x64/x86 is getting conditional compare as part of Intel APX, so it is expected that we will enable the same logic for x86/x64 in the future. Given this and the small diffs of this PR I'm not sure the complexity is warranted.
If we want to have this transformation then it should be done by enabling
optOptimizeCompareChainCondBlock
for x86/x64. Then the backend should be taught how to translate this pattern to something more profitable on x86/x64. Currently it only knows how to do that for arm64. The transformation is done byTryLowerAndOrToCCMP
. x86/x64 should have a similar version that is translated by usingxor
instead. Most likelyoptOptimizeCompareChainCondBlock
will have to be restricted on the patterns it allows to combine on x86/x64 since there is no true conditional compare yet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I removed my comment because I misread this feedback as asking the author rather than a nudge to provide context on disabling on ARM64, though I was hoping to see much larger amount of diffs - this pattern is pretty common after all)
Indeed ccmp is emitted by both Clang and GCC: https://godbolt.org/z/srhqPrK4v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes the ARM support for branchless conditional select is what motivated the if, the xor pattern ends up being more expensive in this case.
If I do understand the code well, this would optimize comparison checks for conditional blocks but not for return ones. Do I need to delete the optimization for the latest ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok to add some handling for return blocks if the existing handling doesn't cover it. But I think you should transform it into bitwise ops (i.e.
return (x == 0x80) && (y == 0x80)
=>return (x == 0x80) & (y == 0x80)
so that the remaining handling falls out naturally fromTryLowerAndOrToCCMP
(and the x64 specific variant being introduced).