Skip to content

Commit

Permalink
Do polymorphism boxing for the result of regions (#655)
Browse files Browse the repository at this point in the history
Resolves #651 .
  • Loading branch information
marzipankaiser authored Oct 29, 2024
1 parent e7336aa commit 6e71bcd
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,26 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
Stmt.Var(id, transform(init), cap, transform(body))
case Stmt.Try(body, handlers) =>
Stmt.Try(transform(body), handlers map transform)
case Stmt.Region(body) => Stmt.Region(transform(body))
case Stmt.Region(body) =>
val tBody = transform(body)
// make sure the result type is a boxed one
val (expectedBodyTpe, actualReturnType, expectedReturnType) = tBody.tpe match {
case BlockType.Function(tparams, cparams, vparams, bparams, result) =>
val boxedResult = transformArg(result)
(BlockType.Function(tparams, cparams, vparams, bparams, boxedResult), boxedResult, result)
case _ => Context.abort("Body of a region cannot have interface type")
}
val doBoxResult = coercer[Block](tBody.tpe, expectedBodyTpe)
// Create coercer for eagerly unboxing the result again
val doUnboxResult = coercer(actualReturnType, expectedReturnType)
val resName = TmpValue("boxedResult")

if (doUnboxResult.isIdentity && doBoxResult.isIdentity) {
Stmt.Region(tBody)
} else {
Stmt.Val(resName, actualReturnType, Stmt.Region(doBoxResult(tBody)),
Stmt.Return(doUnboxResult(Pure.ValueVar(resName, actualReturnType))))
}
case Stmt.Hole() => Stmt.Hole()
}

Expand Down

0 comments on commit 6e71bcd

Please sign in to comment.