-
Notifications
You must be signed in to change notification settings - Fork 244
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
IRFunctionRegistry changes #3202
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,38 +7,60 @@ import is.hail.utils._ | |
import is.hail.asm4s.coerce | ||
|
||
import scala.collection.mutable | ||
import scala.reflect.ClassTag | ||
|
||
object IRFunctionRegistry { | ||
|
||
val registry: mutable.Map[String, Seq[(Seq[Type], Seq[IR] => IR)]] = mutable.Map().withDefaultValue(Seq.empty) | ||
val irRegistry: mutable.Map[String, Seq[(Seq[Type], Seq[IR] => IR)]] = mutable.Map().withDefaultValue(Seq.empty) | ||
|
||
val codeRegistry: mutable.Map[String, Seq[(Seq[Type], IRFunction)]] = mutable.Map().withDefaultValue(Seq.empty) | ||
|
||
def addIRFunction(f: IRFunction) { | ||
val l = registry(f.name) | ||
registry.put(f.name, | ||
l :+ (f.argTypes, { args: Seq[IR] => | ||
ApplyFunction(f, args) | ||
})) | ||
val l = codeRegistry(f.name) | ||
codeRegistry.put(f.name, | ||
l :+ (f.argTypes, f)) | ||
} | ||
|
||
def addIR(name: String, types: Seq[Type], f: Seq[IR] => IR) { | ||
val l = registry(name) | ||
registry.put(name, l :+ ((types, f))) | ||
val l = irRegistry(name) | ||
irRegistry.put(name, l :+ ((types, f))) | ||
} | ||
|
||
def lookupFunction(name: String, args: Seq[Type]): Option[IRFunction] = { | ||
val validF = codeRegistry(name).flatMap { case (ts, f) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use any fail all fail here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the things (except the return) are Options? I don't see how it would work here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh, got it. nevermind. |
||
if (ts.length == args.length) { | ||
ts.foreach(_.clear()) | ||
if ((ts, args).zipped.forall(_.unify(_))) | ||
Some(f) | ||
else | ||
None | ||
} else | ||
None | ||
} | ||
|
||
validF match { | ||
case Seq() => None | ||
case Seq(x) => Some(x) | ||
case _ => fatal(s"Multiple IRFunctions found that satisfy $name$args.") | ||
} | ||
} | ||
|
||
def lookupFunction(name: String, args: Seq[Type]): Option[Seq[IR] => IR] = { | ||
def lookupConversion(name: String, args: Seq[Type]): Option[Seq[IR] => IR] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would the code look simpler if we had one trait IRFun {
def args: Seq[Type]
def ret: Type
def emit(
emitter: IR => (Code[Unit], Code[Boolean], Code[_]),
mb: MethodBuilder,
args: IR*
): (Code[Unit], Code[Boolean], Code[_])
}
class IRMacro(
val args: Seq[Type],
val ret: Type,
val impl: Array[IR] => IR
) extends IRFun {
def emit(
emitter: IR => (Code[Unit], Code[Boolean], Code[_]),
mb: MethodBuilder,
args: IR*
): (Code[Unit], Code[Boolean], Code[_]) = emitter(impl(args.toArray))
}
class JVMFun(
val args: Seq[Type],
retType: Type,
impl: (MethodBuilder, Array[Code[_]]) => Code[_]
) extends IRFun {
def emit(
emitter: IR => (Code[Unit], Code[Boolean], Code[_]),
mb: MethodBuilder,
args: IR*
): (Code[Unit], Code[Boolean], Code[_]) = {
val (setup, ms, vs) = args.map(emitter).unzip
(Code(setup:_*), ms.fold(_ || _), impl(mb, vs))
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe let JVMFun also control missingness? class MissingAwareJVMFun(
val args: Seq[Type],
retType: Type,
impl: (MethodBuilder, Array[Code[_]]) => (Code[Unit], Code[Boolean], Code[_])
) extends IRFun {
def emit(
emitter: IR => (Code[Unit], Code[Boolean], Code[_]),
mb: MethodBuilder,
args: IR*
): (Code[Unit], Code[Boolean], Code[_]) = {
val (setups, ms, vs) = args.map(emitter).unzip
val (setup, m, v) = impl(mb, vs)
(Code(setups ++ setup:_*), (ms :+ m).fold(_ || _), v)
}
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really want to entangle them because of two resasons:
|
||
assert(args.forall(_ != null)) | ||
val validMethods = registry(name).flatMap { case (ts, f) => | ||
val validIR = irRegistry(name).flatMap { case (ts, f) => | ||
if (ts.length == args.length) { | ||
ts.foreach(_.clear()) | ||
if ((ts, args).zipped.forall(_.unify(_))) | ||
Some(f) | ||
else | ||
None | ||
} else { | ||
} else | ||
None | ||
} | ||
} | ||
|
||
val validMethods = validIR ++ lookupFunction(name, args).map { f => | ||
{ args: Seq[IR] => Apply(name, args, f) } | ||
} | ||
|
||
validMethods match { | ||
case Seq() => None | ||
case Seq(x) => Some(x) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@catoverdrive Sorry I'm late to the game here. We should use
mutable.MultiMap
when we haveMap[K, Set[V]]
. Unless there's a compelling case forSeq
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That also makes
addIRFunction
andaddIR
just calls toirRegistry.addBinding
andcodeRegistry.addBinding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
womp, I didn't know this was a thing. I'll change.