-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JIT: Transform arithmetic using distributive property #126852
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
Open
BoyBaykiller
wants to merge
11
commits into
dotnet:main
Choose a base branch
from
BoyBaykiller:transform-using-distributive-property
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.
+84
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
97985db
* initial impl
BoyBaykiller 2c5e2eb
* call from fgMorphSmpOp
BoyBaykiller 538dd18
* apply the opt to the code itself
BoyBaykiller 5f2952d
* call morph in optimizeBool to catch more cases after control flow s…
BoyBaykiller 93d2457
* call fgOptimizeDistributiveArithemtic directly instead of fgMorphBl…
BoyBaykiller 83c4e4d
* add AND over XOR
BoyBaykiller e2379fa
* fix build error on linux
BoyBaykiller 445e20d
* fix typo
BoyBaykiller 2abb78c
* move to gtFoldExpr
BoyBaykiller b89a3c4
* replace GenTree::Compare with lclCheck (gets us 0.84x of the diffs)
BoyBaykiller 472e994
* allow for any OperIsAnyLocal()
BoyBaykiller 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Would it be better to computer CNF and DNF expression minimization then compare their weights here in the future: #127533 (comment)? I was experimenting with it in gentree but this seems like a better place instead. 🙂
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.
Is that some general framework for doing arithmetic/logic simplifications?
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.
These are Boolean normalization/minimization forms from Boolean algebra, similar to techniques used in logic/circuit simplification. For side-effect-free expressions, we could theoretically derive CNF/DNF/ANF candidates, eliminate redundant minterms via prime implicant and don’t-care analysis, then choose the lowest-cost form using some weight metric. The idea would be to pick whichever representation is less “chatty” / cheaper for the JIT.
https://www.cs.cornell.edu/courses/cs3410/2024fa/assignments/circuits/instructions.html
Here’s a better sample program that selects between CNF, DNF, ANF, and the original input form. I also added a threshold for Espresso heuristic backoff to simulate how this could behave with JIT time budget. (ANF is treated separately since it has its own implementation https://en.wikipedia.org/wiki/Algebraic_normal_form)
Program.cs.txt
output:
From the sample output can see that any of the representations, including the original input tree, can "win" for a given expression depending on the minimization/cost metric.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thats cool. Not sure how practical it would be in a real compiler compared but definitely interesting.
I guess make a PR : )
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.
Yup, I think if diffs support it (significant enough), we may be able to enable it for NativeAOT/R2R at least. I will try to land a PoC PR. :)
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.
You don't really need to do anything fancy here even. You can reuse the
vpternlogtables from the hwintrinsics info to allow simplifying any sequence of binary operations involving no more than 3 operands.