Skip to content

Add eta reduction miniphase #14210

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

Merged
merged 4 commits into from
Jan 11, 2022
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 4, 2022

Fixes #10889. Alternative to #13750, from which the test is taken.

Fixes scala#10889. Alternative to scala#13750, from which the test is taken.
@som-snytt
Copy link
Contributor

That was a quickie.

Shorter test with arbitrary constants

import scala.annotation.tailrec
import scala.util.chaining.given

object Test {
  class Ctx
  type Op[A] = Ctx ?=> A

  var min = Int.MaxValue
  var max = 0
  def stk = new Throwable().getStackTrace.length

  @tailrec def f[A](n: Int)(op: Op[A]): A =
    val depth = stk
    min = min.min(depth)
    max = max.max(depth)
    given Ctx = Ctx()
    if (n > 0) f(n-1)(op)
    else op

  def g(ctx: Ctx) = stk

  def main(args: Array[String]) =
    val extra = 3
    f(10)(Ctx ?=> g(summon[Ctx])).tap(res => assert(res <= max + extra, s"min $min, max $max, ran g at $res"))
}

@@ -106,6 +106,7 @@ class Compiler {
List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types
new PureStats, // Remove pure stats from blocks
new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations
new EtaReduce, // Reduce eta expansions of pure paths to the underlying function reference
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any fundamental reason this is after erasure but the existing BetaReduce phase is before erasure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment of betaReduce mentions blackbox macros, which would be eliminated after erasure. That might have something to do with it, but I did not dig deeper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EtaReduce is after erasure since a lot of context function wizardry happens before or at erasure so we want to make sure this is all done before we apply the optimization.

@anatoliykmetyuk anatoliykmetyuk merged commit 53ab457 into scala:master Jan 11, 2022
@anatoliykmetyuk anatoliykmetyuk deleted the fix-10889 branch January 11, 2022 15:06
@elfprince13
Copy link

This is great, thanks!

@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rewrite rules mean implicit function variables cannot be safely passed as arguments
6 participants