Repository navigation
Add an optional common subexpression elimination pass - #2483
Open
SahilRajputdotexe wants to merge 1 commit into
Open
SahilRajputdotexe wants to merge 1 commit into
SahilRajputdotexe wants to merge 1 commit into
Conversation
Pure expressions computed two or more times in the same statement list are evaluated once into a __cse<N> local. The pass is not part of the default optimization chain and can be enabled with -pCommonSubexpressionElimination.
Owner
|
Thanks for the contribution. Please activate this pass and report compilation time impact on files from pythran/test/cases so that we see if it makes sense to let it in - I haven't reviewed the code but I'm not opposed to having it in, but as it require me too 'absorbe' it mentally, this may require some rewrite, if you're fine with it. |
Owner
|
(stated otherwise, there is no use of having it if it's not on by default) |
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
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 adds
pythran.optimizations.CommonSubexpressionElimination, an optional pass that evaluates a repeated pure expression once into a__cse<N>local and rewrites every occurrence to read it.Pythran already has the purity (
PureExpressions) and use-def information needed to tell when two expressions are the same value, which the C++ compiler cannot recover from the generated templates.Example, with
pythran -P -pCommonSubexpressionElimination:becomes
It is conservative. Subscripts, container literals, comprehensions, lambdas, yield/await, starred, f-string parts and walrus are never hoisted, and neither are names, constants or attribute chains. Occurrences must resolve every name to the same definitions and sit in the same statement list, and a rebinding of an operand between them, or an OpenMP directive, blocks the rewrite. Short-circuit and other conditional contexts are left alone: operands of
and/orafter the first, later comparators of a chained comparison, branches of a conditional expression, assert messages and comprehension bodies. Overlapping groups hoist the larger one first, and the inner one is picked up on a later iteration of the pipeline.I did not add it to the default
optimizationslist inpythran.cfg, since I could not judge the effect on compile time and generated code across the whole suite. It is reachable through-por a customoptimizationsentry. Happy to enable it by default if you prefer.The grouping lives in a new
RepeatedExpressionsanalysis. Tests are intest_optimizations.py:check_astcomparisons, plus checks that optimized functions return or raise exactly what the originals do (for examplex and 1/x ...withx == 0). They only exercise the Python side. I ran them locally, and the full CI runs on my fork.