-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CodeGen: Optimize arithmetics for basic identities (#1545)
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 a - 0 => a a + (-0) => a 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 Inf/NaN) 0 - a => -a (breaks for negative zero) 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.
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 deletions.
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
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 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