-
Notifications
You must be signed in to change notification settings - Fork 14
Constant Folding #29
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
Comments
Also look at the 2.11 optimizer: https://github.com/scala/scala/blob/2.11.x/src/compiler/scala/tools/nsc/backend/opt/ConstantOptimization.scala |
Is the goal of constant folding is to reduce bytecode size? If not, is there some smart constant optimization planned that is not performed by JVM? See also https://bugs.openjdk.java.net/browse/JDK-8058164 and related tickets. |
I'm not actually planning to work on this in the near future. As you say, it would only make sense if there was something that we can do considerably better than the JVM. |
one existing test (currently in pending): https://github.com/scala/scala/tree/93fa559f62637fc3a49d7b961a1d07cf843b0e6b/test/pending/jvm/constant-optimization |
I think the motivation of this one would be to enable further optimizations, e.g. to prove that some branches are dead, which might in turn enable box/unbox elim etc. |
@lrytz If I write a ConstantOptimizer, is it correct to create a PR under https://github.com/scala/scala/tree/2.13.x/src/compiler/scala/tools/nsc/backend/jvm/opt ? And also provide a option under |
Hi @da-tubi, nice to see you interested! This is the right place for contributions, yes. See https://github.com/scala/scala/tree/2.13.x/test/junit/scala/tools/nsc/backend/jvm/opt for tests. But first I'd like to learn why you are interested in contributing a constant folder. What is your motivation, do you have a specific project where you know it would help? In my view, implemention constant folding "for its own sake" is not worth it, as the JVM is doing that already. It would only be worth the effort if constant folding unlocks more optimizations that the Scala optimzier can perform. Or maybe if we can treat certain Scala constructs as "constants" that are not actually constants at the bytecode level, but we know they can be treated as such given Scala's semantics? I don't have anything particular in mind though. |
No specific project. Just for fun and performance! |
I'm trying the tuning my Spark ETL code using the scala compiler flags. For CPU bound tasks, it works as expected. That's the motivation (for |
I found a ConstantFolder ( That's why I'm asking if There are many compiler phases. If we expect that constant folding unlocks more optimization, should we implement Constant Folder in earlier phase? |
|
From Wikipedia: https://en.wikipedia.org/wiki/Constant_folding
We need to enable constant propagation in the ConstantFolder. |
There is a constant folder in the type checker indeed, it's implemented at the type level. This constant folder is mainly useful for literal types ( For executable code, programmers never write code that directly benefits from constant folding (stuff like The reason constant folding is still a useful optimization is inlinling. This is true for almost all local optimizations, they typically optimize patterns that programmers never write directly, but that show up after inlining. One way to think about inilining is specialization: the code of the inlined function is copied and then specialized to the arguments that were previously passed in. If an argument is constant, the constant folder can do something, if it's a lambda, closure elimination can do something, etc. Many optimizations can enable other optimizations to be applied, so the various optimizations in the backend are executed in a loop until nothing can be done anymore. That's why the constant folder in the type checker is not helpful for optimizing the executable code. (In case you didn't come across it, this blog post might be interesting to you: https://www.lightbend.com/blog/scala-inliner-optimizer) |
For the BinaryConstantFoldVal example: The following code is generated using the optimizer option
And the following pattern should be optimized in
|
Implement constant folding. Miguel's impl: https://github.com/lrytz/scala/blob/opt/rebaseWip/src/compiler/scala/tools/nsc/backend/bcode/ConstantFolder.java
Existing partest tests:
jvm/constant-optimization
,t7006
The text was updated successfully, but these errors were encountered: