Skip to content

flare lms-mod #121

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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 224 additions & 111 deletions src/common/ArrayOps.scala

Large diffs are not rendered by default.

159 changes: 106 additions & 53 deletions src/common/BooleanOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,153 @@ package common

import java.io.PrintWriter
import scala.reflect.SourceContext
import scala.lms.internal._

trait LiftBoolean {
this: Base =>

implicit def boolToBoolRep(b: Boolean) = unit(b)
}

trait BooleanOps extends Variables {
trait BooleanOps extends Variables with Expressions {
def infix_unary_!(x: Rep[Boolean])(implicit pos: SourceContext) = boolean_negate(x)
def infix_&&(lhs: Rep[Boolean], rhs: =>Rep[Boolean])(implicit pos: SourceContext) = boolean_and(lhs,rhs)
def infix_||(lhs: Rep[Boolean], rhs: =>Rep[Boolean])(implicit pos: SourceContext) = boolean_or(lhs,rhs)

// TODO: short-circuit by default
def infix_&&(lhs: Rep[Boolean], rhs: => Rep[Boolean])(implicit pos: SourceContext) = boolean_and(lhs,rhs)
def infix_&&(lhs: Boolean, rhs: => Rep[Boolean])(implicit pos: SourceContext): Exp[Boolean] = {
if (lhs == true) rhs.asInstanceOf[Exp[Boolean]]
else Const(false)
}
def infix_||(lhs: Rep[Boolean], rhs: => Rep[Boolean])(implicit pos: SourceContext) = boolean_or(lhs,rhs)
def infix_||(lhs: Boolean, rhs: => Rep[Boolean])(implicit pos: SourceContext): Exp[Boolean] = {
if (lhs == true) Const(true)
else rhs.asInstanceOf[Exp[Boolean]]
}

def boolean_negate(lhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean]
def boolean_and(lhs: Rep[Boolean], rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean]
def boolean_or(lhs: Rep[Boolean], rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean]
def boolean_and(lhs: Rep[Boolean], rhs: => Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean]
def boolean_or(lhs: Rep[Boolean], rhs: => Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean]
}

trait BooleanOpsExp extends BooleanOps with EffectExp {
trait BooleanOpsExp extends BooleanOps with BaseExp with EffectExp {
case class BooleanNegate(lhs: Exp[Boolean]) extends Def[Boolean]
case class BooleanAnd(lhs: Exp[Boolean], rhs: Exp[Boolean]) extends Def[Boolean]
case class BooleanOr(lhs: Exp[Boolean], rhs: Exp[Boolean]) extends Def[Boolean]
case class BooleanAnd(lhs: Exp[Boolean], rhs: Block[Boolean]) extends Def[Boolean] {
val c = fresh[Boolean] // used in c code generation
}
case class BooleanOr(lhs: Exp[Boolean], rhs: Block[Boolean]) extends Def[Boolean] {
val c = fresh[Boolean] // used in c code generation
}

def boolean_negate(lhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = BooleanNegate(lhs)
def boolean_and(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = BooleanAnd(lhs,rhs)
def boolean_or(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = BooleanOr(lhs,rhs)
def boolean_and(lhs: Exp[Boolean], frhs: => Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = {
lhs match {
case x@Const(false) => x
case x@Const(true) => frhs
case _ => {
val rhs = reifyEffects(frhs)
BooleanAnd(lhs,rhs)
}
}
}
def boolean_or(lhs: Exp[Boolean], frhs: => Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = {
lhs match {
case x@Const(true) => x
case x@Const(false) => frhs
case _ => {
val rhs = reifyEffects(frhs)
BooleanOr(lhs,rhs)
}
}
}

override def mirror[A:Manifest](e: Def[A], f: Transformer)(implicit pos: SourceContext): Exp[A] = (e match {
case BooleanNegate(x) => boolean_negate(f(x))
case BooleanAnd(x,y) => boolean_and(f(x),f(y))
case BooleanOr(x,y) => boolean_or(f(x),f(y))

case Reflect(BooleanNegate(x), u, es) => reflectMirrored(Reflect(BooleanNegate(f(x)), mapOver(f,u), f(es)))(mtype(manifest[A]), pos)
case Reflect(BooleanAnd(x,y), u, es) => reflectMirrored(Reflect(BooleanAnd(f(x),f(y)), mapOver(f,u), f(es)))(mtype(manifest[A]), pos)
case Reflect(BooleanOr(x,y), u, es) => reflectMirrored(Reflect(BooleanOr(f(x),f(y)), mapOver(f,u), f(es)))(mtype(manifest[A]), pos)
case BooleanAnd(x,y) => toAtom(BooleanAnd(f(x),f(y)))
case BooleanOr(x,y) => toAtom(BooleanOr(f(x),f(y)))
case _ => super.mirror(e, f)
}).asInstanceOf[Exp[A]] // why??
}

override def syms(e: Any): List[Sym[Any]] = e match {
case BooleanAnd(lhs,rhs) => syms(lhs):::syms(rhs)
case BooleanOr(lhs,rhs) => syms(lhs):::syms(rhs)
case _ => super.syms(e)
}

override def boundSyms(e: Any): List[Sym[Any]] = e match {
case BooleanAnd(lhs,rhs) => effectSyms(lhs) ::: effectSyms(rhs)
case BooleanOr(lhs,rhs) => effectSyms(lhs) ::: effectSyms(rhs)
case _ => super.boundSyms(e)
}

/**
* @author Alen Stojanov ([email protected])
*/
trait BooleanOpsExpOpt extends BooleanOpsExp {
override def symsFreq(e: Any): List[(Sym[Any], Double)] = e match {
case BooleanAnd(a, x) => freqHot(a):::freqCold(x)
case BooleanOr(a, x) => freqHot(a):::freqCold(x)
case _ => super.symsFreq(e)
}


}

trait BooleanOpsExpOpt extends BooleanOpsExp {
override def boolean_negate(lhs: Exp[Boolean])(implicit pos: SourceContext) = lhs match {
case Def(BooleanNegate(x)) => x
case Const(a) => Const(!a)
case _ => super.boolean_negate(lhs)
}

override def boolean_and(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = {
(lhs, rhs) match {
case (Const(false), _) => Const(false)
case (_, Const(false)) => Const(false)
case (Const(true), x) => x
case (x, Const(true)) => x
case _ => super.boolean_and(lhs, rhs)
}
}

override def boolean_or(lhs: Exp[Boolean], rhs: Exp[Boolean])(implicit pos: SourceContext) : Exp[Boolean] = {
(lhs, rhs) match {
case (Const(false), x) => x
case (x, Const(false)) => x
case (Const(true), _) => Const(true)
case (_, Const(true)) => Const(true)
case _ => super.boolean_or(lhs, rhs)
}
}
}

trait ScalaGenBooleanOps extends ScalaGenBase {
trait ScalaGenBooleanOps extends ScalaGenBase with GenericNestedCodegen {
val IR: BooleanOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BooleanNegate(b) => emitValDef(sym, src"!$b")
case BooleanAnd(lhs,rhs) => emitValDef(sym, src"$lhs && $rhs")
case BooleanOr(lhs,rhs) => emitValDef(sym, src"$lhs || $rhs")
case BooleanAnd(lhs,rhs) =>
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
gen"""if ($lhs == true) {
|${nestedBlock(rhs)}
|$rhs
|} else false"""
}
emitValDef(sym, strWriter.toString)
case BooleanOr(lhs,rhs) =>
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
gen"""if ($lhs == false) {
|${nestedBlock(rhs)}
|$rhs
|} else true"""
}
emitValDef(sym, strWriter.toString)
case _ => super.emitNode(sym,rhs)
}
}

trait CLikeGenBooleanOps extends CLikeGenBase {
trait CLikeGenBooleanOps extends CLikeGenBase with GenericNestedCodegen {
val IR: BooleanOpsExp
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BooleanNegate(b) => emitValDef(sym, src"!$b")
case BooleanAnd(lhs,rhs) => emitValDef(sym, src"$lhs && $rhs")
case BooleanOr(lhs,rhs) => emitValDef(sym, src"$lhs || $rhs")
case _ => super.emitNode(sym,rhs)
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case BooleanNegate(b) => emitValDef(sym, src"!$b")
case b@BooleanAnd(lhs,rhs) => {
emitValDef(b.c, quote(lhs))
stream.println("if (" + quote(lhs) + ") {")
emitBlock(rhs)
stream.println(quote(b.c) + " = " + quote(getBlockResult(rhs)) + ";")
stream.println("}")
emitValDef(sym, quote(b.c))
}
case b@BooleanOr(lhs,rhs) => {
emitValDef(b.c, quote(lhs))
stream.println("if (" + quote(lhs) + " == false) {")
emitBlock(rhs)
stream.println(quote(b.c) + " = " + quote(getBlockResult(rhs)) + ";")
stream.println("}")
emitValDef(sym, quote(b.c))
}
case _ => super.emitNode(sym,rhs)
}
}
}

Expand Down
Loading