Skip to content

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

Open
retronym opened this issue Aug 5, 2015 · 15 comments
Open

Constant Folding #29

retronym opened this issue Aug 5, 2015 · 15 comments

Comments

@retronym
Copy link
Member

retronym commented Aug 5, 2015

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

@lrytz
Copy link
Member

lrytz commented Nov 6, 2015

@DarkDimius
Copy link

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
Copy link
Member

lrytz commented Nov 6, 2015

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.

@lrytz
Copy link
Member

lrytz commented Nov 9, 2015

@lrytz lrytz added the prio:low label Jan 19, 2016
@retronym
Copy link
Member Author

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.

@dwijnand dwijnand added this to the Backlog milestone Oct 22, 2020
@da-tubi
Copy link

da-tubi commented Jan 7, 2021

@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
Copy link

da-tubi commented Jan 7, 2021

@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
Copy link
Member

lrytz commented Jan 7, 2021

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
Copy link

da-tubi commented Jan 8, 2021

No specific project. Just for fun and performance!

@da-tubi
Copy link

da-tubi commented Jan 11, 2021

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
Copy link

da-tubi commented Jan 11, 2021

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?

@da-tubi
Copy link

da-tubi commented Jan 11, 2021

From Wikipedia: https://en.wikipedia.org/wiki/Constant_folding

Constant propagation is the process of substituting the values of known constants in expressions at compile time.

We need to enable constant propagation in the ConstantFolder.

@lrytz
Copy link
Member

lrytz commented Jan 11, 2021

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 (scala> val a: 1 = 1; val b: 2 = 2; val c: 3 = a + b) and also for handling arguments to ConstantAnnotations.

For executable code, programmers never write code that directly benefits from constant folding (stuff like val x = 1 + 10; if (x == 11) { ... } / if (true) { ... } / true || x == true), so the constant folder in the type checker typically doesn't change anything in the code that it compiles. One exception is maybe code that originates from a code generator.

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)

@da-tubi
Copy link

da-tubi commented Jan 12, 2021

For the BinaryConstantFoldVal example:

The following code is generated using the optimizer option -opt:l:method

Compiled from "BinaryConstantFoldVal.scala"
public final class BinaryConstantFoldVal$ {
  public static final BinaryConstantFoldVal$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class BinaryConstantFoldVal$
       3: dup
       4: invokespecial #22                 // Method "<init>":()V
       7: putstatic     #24                 // Field MODULE$:LBinaryConstantFoldVal$;
      10: return

  public void main(java.lang.String[]);
    Code:
       0: bipush        11
       2: bipush        11
       4: if_icmpne     16
       7: getstatic     #32                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      10: ldc           #34                 // String Yes
      12: invokevirtual #38                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      15: return
      16: getstatic     #32                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      19: ldc           #40                 // String Deadcode
      21: invokevirtual #38                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      24: getstatic     #32                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      27: getstatic     #45                 // Field scala/runtime/RichInt$.MODULE$:Lscala/runtime/RichInt$;
      30: getstatic     #32                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      33: iconst_1
      34: invokevirtual #49                 // Method scala/Predef$.intWrapper:(I)I
      37: ldc           #50                 // int 100000
      39: invokevirtual #54                 // Method scala/runtime/RichInt$.to$extension:(II)Lscala/collection/immutable/Range$Inclusive;
      42: getstatic     #57                 // Field scala/math/Numeric$IntIsIntegral$.MODULE$:Lscala/math/Numeric$IntIsIntegral$;
      45: invokevirtual #61                 // Method scala/collection/immutable/Range$Inclusive.sum:(Lscala/math/Numeric;)I
      48: invokestatic  #67                 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
      51: invokevirtual #38                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      54: return
}

And the following pattern should be optimized in simplifyJumps

       0: bipush        11
       2: bipush        11
       4: if_icmpne     16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants