Skip to content

Commit

Permalink
Don't inline Apply across lambdas (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek authored Dec 10, 2024
1 parent a708829 commit 4bad1ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ClangGenTest extends munit.FunSuite {
To inspect the code, change the hash, and it will print the code out
*/
testFilesCompilesToHash("test_workspace/Ackermann.bosatsu")(
"66654b8c57c8fcb5d63c48830ffebdab"
"b31036461ebf3f7474f73e6661e0812f"
)
}
}
6 changes: 4 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/TypedExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,8 @@ object TypedExpr {
def substitute[A](
ident: Bindable,
ex: TypedExpr[A],
in: TypedExpr[A]
in: TypedExpr[A],
enterLambda: Boolean = true
): Option[TypedExpr[A]] = {
// if we hit a shadow, we don't need to substitute down
// that branch
Expand All @@ -1164,7 +1165,8 @@ object TypedExpr {
case Annotation(t, tpe) =>
loop(t).map(Annotation(_, tpe))
case AnnotatedLambda(args, res, tag) =>
if (args.exists { case (n, _) => masks(n) }) None
if (!enterLambda) None
else if (args.exists { case (n, _) => masks(n) }) None
else if (args.exists { case (n, _) => shadows(n) }) Some(in)
else loop(res).map(AnnotatedLambda(args, _, tag))
case App(fn, args, tpe, tag) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,14 @@ object TypedExprNormalization {
val cnt = in1.freeVarsDup.count(_ === arg)
if (cnt > 0) {
// the arg is needed
val isSimp = Impl.isSimple(ex1, lambdaSimple = true)
val shouldInline = (!rec.isRecursive) && {
(cnt == 1) || Impl.isSimple(ex1, lambdaSimple = true)
(cnt == 1) || isSimp
}
// we don't want to inline a value that is itself a function call
// inside of lambdas
val inlined =
if (shouldInline) substitute(arg, ex1, in1) else None
if (shouldInline) substitute(arg, ex1, in1, enterLambda = isSimp) else None
inlined match {
case Some(il) =>
normalize1(namerec, il, scope, typeEnv)
Expand Down

0 comments on commit 4bad1ba

Please sign in to comment.