Skip to content

Commit

Permalink
Even more descriptive LLVM codegen (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinborner authored Aug 6, 2024
2 parents 7de9eff + e9d0b91 commit 9f0d46a
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 91 deletions.
2 changes: 2 additions & 0 deletions effekt/js/src/main/scala/effekt/EffektConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ trait EffektConfig {
def maxInlineSize() = 50L

def timed() = false

def debug() = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ object DirectStyleMutableState extends Phase[CoreTransformed, CoreTransformed] {
case Stmt.Val(y, ytpe, Put(x, tpe, v), s) => Let(y, ytpe, Put(x, tpe, rewrite(v)), rewrite(s))

case Get(x, tpe) =>
val id = Id("tmp")
val id = Id("tmp_get")
val binding = Get(x, tpe)
Let(id, binding.tpe, binding, Stmt.Return(Pure.ValueVar(id, tpe.result)))

case Put(x, tpe, v) =>
val id = Id("tmp")
val id = Id("tmp_put")
val binding = Put(x, tpe, v)
Let(id, binding.tpe, binding, Stmt.Return(Pure.ValueVar(id, Type.TUnit)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ object PatternMatchingCompiler {
// used to make up new scrutinees
val varsFor = mutable.Map.empty[Id, List[ValueVar]]
def fieldVarsFor(constructor: Id, fieldTypes: List[ValueType]): List[ValueVar] =
varsFor.getOrElseUpdate(constructor, fieldTypes.map { tpe => ValueVar(TmpValue(), tpe) })
varsFor.getOrElseUpdate(constructor, fieldTypes.map { tpe => ValueVar(TmpValue("y"), tpe) })

normalized.foreach {
case Clause(Split(Pattern.Tag(constructor, patternsAndTypes), restPatterns, restConds), label, args) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
if (coerce.isIdentity) {
List(Definition.Let(id, transform(tpe), transform(binding)))
} else {
val orig = TmpValue()
val orig = TmpValue("coe")
val origTpe = binding.tpe
List(
Definition.Let(orig, origTpe, transform(binding)),
Expand Down Expand Up @@ -248,7 +248,7 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
if (coerce.isIdentity) {
Stmt.Val(id, transform(tpe), transform(binding), transform(body))
} else {
val orig = TmpValue()
val orig = TmpValue("coe")
Stmt.Val(orig, binding.tpe, transform(binding),
Let(id, transform(binding.tpe), coerce(Pure.ValueVar(orig, binding.tpe)),
transform(body)))
Expand Down Expand Up @@ -508,9 +508,9 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
override def to = totpe

override def apply(block: B): B = {
val vparams: List[Param.ValueParam] = vcoercers.map { c => Param.ValueParam(TmpValue(), transform(c.from)) }
val bparams: List[Param.BlockParam] = bcoercers.map { c => val id = TmpBlock(); Param.BlockParam(id, transform(c.from), Set(id)) }
val result = TmpValue()
val vparams: List[Param.ValueParam] = vcoercers.map { c => Param.ValueParam(TmpValue("coe"), transform(c.from)) }
val bparams: List[Param.BlockParam] = bcoercers.map { c => val id = TmpBlock("coe"); Param.BlockParam(id, transform(c.from), Set(id)) }
val result = TmpValue("coe")
val inner = TmpBlock()
val vargs = (vcoercers zip vparams).map { case (c, p) => c(Pure.ValueVar(p.id, p.tpe)) }
val bargs = (bcoercers zip bparams).map { case (c, p) => c(Block.BlockVar(p.id, p.tpe, Set.empty)) }
Expand All @@ -525,15 +525,15 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
}

override def callDirect(block: B, vargs: List[Pure], bargs: List[Block])(using PContext): Expr = {
val result = TmpValue()
val result = TmpValue("coe")
Run(Let(result, rcoercer.from, DirectApp(block, targs map transformArg,
(vcoercers zip vargs).map {case (c,v) => c(v)},
(bcoercers zip bargs).map {case (c,b) => c(b)}),
Return(rcoercer(Pure.ValueVar(result, rcoercer.from)))))
}

override def call(block: B, vargs: List[Pure], bargs: List[Block])(using PContext): Stmt = {
val result = TmpValue()
val result = TmpValue("coe")
Stmt.Val(result, rcoercer.from, Stmt.App(block, targs map transformArg,
(vcoercers zip vargs).map { case (c, v) => c(v) },
(bcoercers zip bargs).map { case (c, b) => c(b) }),
Expand Down
16 changes: 8 additions & 8 deletions effekt/shared/src/main/scala/effekt/core/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
// if this block argument expects to be called using PureApp or DirectApp, make sure it is
// by wrapping it in a BlockLit
val targs = tparams.map(core.ValueType.Var.apply)
val vparams: List[Param.ValueParam] = vparamtps.map { t => Param.ValueParam(TmpValue(), transform(t))}
val vparams: List[Param.ValueParam] = vparamtps.map { t => Param.ValueParam(TmpValue("valueParam"), transform(t))}
val vargs = vparams.map { case Param.ValueParam(id, tpe) => Pure.ValueVar(id, tpe) }

// [[ f ]] = { (x) => f(x) }
Expand All @@ -321,11 +321,11 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
// [[ f ]] = { (x){g} => let r = f(x){g}; return r }
def etaExpandDirect(f: ExternFunction): BlockLit = {
assert(effects.isEmpty)
val bparams: List[Param.BlockParam] = bparamtps.map { t => val id = TmpBlock(); Param.BlockParam(id, transform(t), Set(id)) }
val bparams: List[Param.BlockParam] = bparamtps.map { t => val id = TmpBlock("etaParam"); Param.BlockParam(id, transform(t), Set(id)) }
val bargs = bparams.map {
case Param.BlockParam(id, tpe, capt) => Block.BlockVar(id, tpe, capt)
}
val result = TmpValue()
val result = TmpValue("etaBinding")
val resultBinding = DirectApp(BlockVar(f), targs, vargs, bargs)
BlockLit(tparams, bparams.map(_.id), vparams, bparams,
core.Let(result, resultBinding.tpe, resultBinding,
Expand Down Expand Up @@ -411,15 +411,15 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
// def loop$13() = if ([[cond]]) { [[ body ]]; loop$13() } else { return () }
// loop$13()
case source.While(guards, body, default) =>
val loopName = TmpBlock()
val loopName = TmpBlock("whileLoop")
val loopType = core.BlockType.Function(Nil, Nil, Nil, Nil, core.Type.TUnit)

// TODO double check: probably we are forgetting the capture of the guards!
val loopCapt = transform(Context.inferredCapture(body))
val loopCall = Stmt.App(core.BlockVar(loopName, loopType, loopCapt), Nil, Nil, Nil)

val transformedBody = transform(body)
val thenBranch = Stmt.Val(TmpValue(), transformedBody.tpe, transformedBody, loopCall)
val thenBranch = Stmt.Val(TmpValue("whileThen"), transformedBody.tpe, transformedBody, loopCall)
val elseBranch = default.map(transform).getOrElse(Return(Literal((), core.Type.TUnit)))

val loopBody = guards match {
Expand Down Expand Up @@ -611,7 +611,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {

// create joinpoint
val params = patterns.flatMap { case (sc, p) => boundInPattern(p) } ++ guards.flatMap(boundInGuard)
val joinpoint = Context.bind(TmpBlock(), BlockLit(Nil, Nil, params, Nil, body))
val joinpoint = Context.bind(TmpBlock("k"), BlockLit(Nil, Nil, params, Nil, body))

def transformPattern(p: source.MatchPattern): Pattern = p match {
case source.AnyPattern(id) =>
Expand Down Expand Up @@ -809,7 +809,7 @@ trait TransformerOps extends ContextOps { Context: Context =>
private[core] def bind(s: Stmt): ValueVar = {

// create a fresh symbol and assign the type
val x = TmpValue()
val x = TmpValue("r")

val binding = Binding.Val(x, s.tpe, s)
bindings += binding
Expand All @@ -821,7 +821,7 @@ trait TransformerOps extends ContextOps { Context: Context =>
case x: ValueVar => x
case e =>
// create a fresh symbol and assign the type
val x = TmpValue()
val x = TmpValue("r")

val binding = Binding.Let(x, e.tpe, e)
bindings += binding
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/core/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ object normal {
case x: Block.BlockVar => bvars = bvars :+ x
// introduce a binding
case block =>
val id = symbols.TmpBlock()
val id = symbols.TmpBlock("blockBinding")
bindings = bindings :+ Definition.Def(id, block)
bvars = bvars :+ Block.BlockVar(id, block.tpe, block.capt)
ids += id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ ${indentedLines(instructions.map(show).mkString("\n"))}
case ExtractValue(result, aggregate, index) =>
s"${localName(result)} = extractvalue ${show(aggregate)}, $index"

case Comment(msg) =>
case Comment(msg) if C.config.debug() =>
val sanitized = msg.map((c: Char) => if (' ' <= c && c != '\\' && c <= '~') c else '?').mkString
s"; $sanitized"
s"\n; $sanitized"

case Comment(msg) => ""
}

def show(terminator: Terminator): LLVMString = terminator match {
Expand Down
Loading

0 comments on commit 9f0d46a

Please sign in to comment.