Skip to content

Constant Folding #29

@retronym

Description

@retronym
Member

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

Activity

DarkDimius

DarkDimius commented on Nov 6, 2015

@DarkDimius

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.

lrytz

lrytz commented on Nov 6, 2015

@lrytz
Member

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.

retronym

retronym commented on Feb 12, 2016

@retronym
MemberAuthor

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.

added this to the Backlog milestone on Oct 22, 2020
da-tubi

da-tubi commented on Jan 7, 2021

@da-tubi

@lrytz @retronym Why ConstantOptimization is removed in Scala 2.13? Would it be helpful if someone port it to Scala 2.13.x?

da-tubi

da-tubi commented on Jan 7, 2021

@da-tubi

@lrytz
I'm interested in improving the Scalac Optimizer.

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 -opt: and the essential unit tests.

lrytz

lrytz commented on Jan 7, 2021

@lrytz
Member

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.

da-tubi

da-tubi commented on Jan 8, 2021

@da-tubi

No specific project. Just for fun and performance!

da-tubi

da-tubi commented on Jan 11, 2021

@da-tubi

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 Performance). Just fixed scala/bug#11247 and I'd like to contribute more on the Scala optimizer (for Fun).

da-tubi

da-tubi commented on Jan 11, 2021

@da-tubi

I found a ConstantFolder (scala.tools.nsc.typechecker.ConstantFolder) in the typechecker.

That's why I'm asking if src/compiler/scala/tools/nsc/backend/jvm/opt is the right place (a somewhat silly question).

There are many compiler phases. If we expect that constant folding unlocks more optimization, should we implement Constant Folder in earlier phase?

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @retronym@lrytz@dwijnand@DarkDimius@da-tubi

        Issue actions

          Constant Folding · Issue #29 · scala/scala-dev