-
Notifications
You must be signed in to change notification settings - Fork 394
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
CodeGen: Optimize arithmetics for basic identities #1545
Merged
Merged
Conversation
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
This change folds: a * 1 => a a / 1 => a a * -1 => -a a / -1 => -a a * 2 => a + a a / 2^k => a * 2^-k Note that the following folds are all invalid: a + 0 => a (breaks for negative zero) a - (-0) => a (breaks for negative zero) a - a => 0 (breaks for NaN) a - 0 could be folded into a but that doesn't happen in benchmarks. Various cases of UNM_NUM could be optimized (eg (-a) * (-b) = a * b), but that doesn't happen in benchmarks either.
Check various specials that we can't optimize and division by large power of two.
vegorov-rbx
approved these changes
Nov 26, 2024
This can sometimes be helpful after all.
After testing this on math_map equivalent, I've went ahead and added a-0/a+(-0) folding after all. With it, this code:
gets compiled into this:
... which is almost good, short of an extra dead store in the first basic block that should have been elided; explicit type annotations on |
zeux
changed the title
CodeGen: Optimize arithmetics for basic multiplicative identities
CodeGen: Optimize arithmetics for basic identities
Nov 27, 2024
0-a can't be simplified with -a as 0 becomes -0 under negation.
vegorov-rbx
approved these changes
Nov 27, 2024
Thank you. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This change folds:
Note that the following folds are all invalid:
Various cases of UNM_NUM could be optimized (eg (-a) * (-b) = a * b),
but that doesn't happen in benchmarks.
While it would be possible to also fold inverse multiplications (k * v),
these do not happen in benchmarks and rarely happen in bytecode due
to type based optimizations. Maybe this can be improved with some sort of
IR canonicalization in the future if necessary.
I've considered moving some of these, like division strength reduction,
to IR translation (as this is where POW is lowered presently) but it didn't
seem better one way or the other.
This change improves performance on some benchmarks, e.g. trig and voxelgen,
and should be a strict uplift as it never generates more instructions or longer
latency chains. On Apple M2, without division->multiplication optimization, both
benchmarks see 0.1-0.2% uplift. Division optimization makes trig 3% faster; I expect
the gains on X64 will be more muted, but on Apple this seems to allow loop iterations
to overlap better by removing the division bottleneck.