Skip to content

Commit 28021ad

Browse files
committed
always inline jumps
1 parent 0d14901 commit 28021ad

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

effekt/shared/src/main/scala/effekt/core/optimizer/Inliner.scala

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ class Unique(usage: Map[Id, Usage]) extends InliningPolicy {
1414
ctx.blocks.get(id).exists(_.size <= ctx.maxInlineSize)
1515
}
1616

17+
class UniqueJumpSimple(usage: Map[Id, Usage]) extends InliningPolicy {
18+
override def apply(id: Id)(using ctx: Context): Boolean = {
19+
val use = usage.get(id)
20+
val block = ctx.blocks.get(id)
21+
var doInline = !usage.get(id).contains(Usage.Recursive)
22+
doInline &&= use.contains(Usage.Once) || block.collect {
23+
case effekt.core.Block.BlockLit(_, _, _, _, _: Stmt.Return) => true
24+
case effekt.core.Block.BlockLit(_, _, _, _, _: Stmt.App) => true
25+
}.isDefined
26+
doInline &&= block.exists(_.size <= ctx.maxInlineSize)
27+
doInline
28+
}
29+
}
30+
1731
case class Context(
1832
blocks: Map[Id, Block],
1933
exprs: Map[Id, Expr],
@@ -47,21 +61,17 @@ class Inliner(shouldInline: InliningPolicy) extends Tree.RewriteWithContext[Cont
4761
Stmt.Def(bparam.id, barg, body)
4862

4963
def bindBlocks(body: Stmt, blocks: List[(BlockParam, Block)]): Stmt =
50-
blocks.headOption.map { (bp, barg) =>
51-
blocks.tail.foldRight(bindBlock(body, bp, barg)) { case ((bp, barg), acc) =>
52-
bindBlock(acc, bp, barg)
53-
}
54-
}.getOrElse(body)
64+
blocks.foldRight(body) { case ((bp, barg), acc) =>
65+
bindBlock(acc, bp, barg)
66+
}
5567

5668
def bindValue(body: Stmt, vparam: ValueParam, varg: Expr): Stmt =
5769
Stmt.Let(vparam.id, vparam.tpe, varg, body)
5870

5971
def bindValues(body: Stmt, values: List[(ValueParam, Expr)]): Stmt =
60-
values.headOption.map { (vp, varg) =>
61-
values.tail.foldRight(Stmt.Let(vp.id, vp.tpe, varg, body)) { case ((vp, varg), acc) =>
62-
bindValue(acc, vp, varg)
63-
}
64-
}.getOrElse(body)
72+
values.foldRight(body) { case ((vp, varg), acc) =>
73+
bindValue(acc, vp, varg)
74+
}
6575

6676
override def stmt(using ctx: Context): PartialFunction[Stmt, Stmt] = {
6777
case app @ Stmt.App(bvar: BlockVar, targs, vargs, bargs) if shouldInline(bvar.id) =>

effekt/shared/src/main/scala/effekt/core/optimizer/Optimizer.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object Optimizer extends Phase[CoreTransformed, CoreTransformed] {
2929
}
3030

3131
if !Context.config.optimize() then return tree;
32-
32+
3333
/*
3434
def inlineSmall(usage: Map[Id, Usage]) = NewNormalizer { (id, b) =>
3535
usage.get(id).contains(Once) || (!usage.get(id).contains(Recursive) && b.size < 40)
@@ -40,10 +40,11 @@ object Optimizer extends Phase[CoreTransformed, CoreTransformed] {
4040
*/
4141
// tree = Context.timed("new-normalizer-1", source.name) { inlineSmall(Reachable(Set(mainSymbol), tree)).run(tree) }
4242
tree = Context.timed("new-normalizer-1", source.name) { NewNormalizer().run(tree) }
43-
//util.trace(util.show(tree))
44-
tree = Inliner(Unique(Reachable(Set(mainSymbol), tree))).run(tree)
43+
util.trace(util.show(tree))
44+
tree = Inliner(UniqueJump(Reachable(Set(mainSymbol), tree))).run(tree)
45+
util.trace(util.show(tree))
4546
tree = Context.timed("new-normalizer-1", source.name) { NewNormalizer().run(tree) }
46-
//util.trace(util.show(tree))
47+
util.trace(util.show(tree))
4748
tree = StaticArguments.transform(mainSymbol, tree)
4849
// println(util.show(tree))
4950
// tree = Context.timed("new-normalizer-2", source.name) { inlineSmall(Reachable(Set(mainSymbol), tree)).run(tree) }

0 commit comments

Comments
 (0)