-
Notifications
You must be signed in to change notification settings - Fork 242
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
[compiler] Add If
and MakeStruct
Simplifications
#14231
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad5a34f
[compiler] Add `If` and `MakeStruct` Simplifications
ehigham c63580e
failures
ehigham f1b2bc6
cock
ehigham 1dcff40
Merge remote-tracking branch 'upstream/main' into ehigham/simplify-ma…
ehigham 159263c
fix ssubsetstruct rename
ehigham af893e4
scalafix
ehigham 65bf923
Merge remote-tracking branch 'upstream/main' into ehigham/simplify-ma…
ehigham 2dd1fa3
Generalise `SSubsetStruct` to views on `SBaseStruct`s
ehigham d6729e0
revert whitespace change
ehigham e8f1a19
revert me being dumb
ehigham 576770e
Merge remote-tracking branch 'upstream/main' into ehigham/simplify-ma…
ehigham c3437cd
bad merge
ehigham a6ed108
prelim changes suggested by @patrick-schultz
ehigham 49912a5
debug my idiocy
ehigham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
hail/src/main/scala/is/hail/types/physical/stypes/concrete/SStructView.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package is.hail.types.physical.stypes.concrete | ||
|
||
import is.hail.annotations.Region | ||
import is.hail.asm4s.{Settable, TypeInfo, Value} | ||
import is.hail.expr.ir.{EmitCodeBuilder, IEmitCode} | ||
import is.hail.types.physical.{PCanonicalStruct, PType} | ||
import is.hail.types.physical.stypes.{EmitType, SType, SValue} | ||
import is.hail.types.physical.stypes.interfaces.{SBaseStruct, SBaseStructSettable, SBaseStructValue} | ||
import is.hail.types.virtual.{TBaseStruct, TStruct, Type} | ||
|
||
object SStructView { | ||
def subset(fieldnames: IndexedSeq[String], struct: SBaseStruct): SStructView = | ||
struct match { | ||
case s: SStructView => | ||
val pfields = s.parent.virtualType.fields | ||
new SStructView( | ||
s.parent, | ||
fieldnames.map(f => pfields(s.newToOldFieldMapping(s.fieldIdx(f))).name), | ||
s.rename.typeAfterSelectNames(fieldnames), | ||
) | ||
|
||
case s => | ||
val restrict = s.virtualType.asInstanceOf[TStruct].typeAfterSelectNames(fieldnames) | ||
new SStructView(s, fieldnames, restrict) | ||
} | ||
} | ||
|
||
// A 'view' on `SBaseStruct`s, ie one that presents an upcast and/or renamed facade on another | ||
final class SStructView( | ||
private val parent: SBaseStruct, | ||
private val restrict: IndexedSeq[String], | ||
private val rename: TStruct, | ||
) extends SBaseStruct { | ||
|
||
assert( | ||
parent.virtualType.asInstanceOf[TStruct].typeAfterSelectNames(restrict) canCastTo rename, | ||
s"""Renamed type is not isomorphic to subsetted type | ||
| parent: '${parent.virtualType._toPretty}' | ||
| restrict: '${restrict.mkString("[", ",", "]")}' | ||
| rename: '${rename._toPretty}' | ||
|""".stripMargin, | ||
) | ||
|
||
override def size: Int = | ||
restrict.length | ||
|
||
lazy val newToOldFieldMapping: Map[Int, Int] = | ||
restrict.view.zipWithIndex.map { case (f, i) => i -> parent.fieldIdx(f) }.toMap | ||
|
||
override lazy val fieldTypes: IndexedSeq[SType] = | ||
Array.tabulate(size) { i => | ||
parent | ||
.fieldTypes(newToOldFieldMapping(i)) | ||
.castRename(rename.fields(i).typ) | ||
} | ||
|
||
override lazy val fieldEmitTypes: IndexedSeq[EmitType] = | ||
Array.tabulate(size) { i => | ||
parent | ||
.fieldEmitTypes(newToOldFieldMapping(i)) | ||
.copy(st = fieldTypes(i)) | ||
} | ||
|
||
override def virtualType: TBaseStruct = | ||
rename | ||
|
||
override def fieldIdx(fieldName: String): Int = | ||
rename.fieldIdx(fieldName) | ||
|
||
override def castRename(t: Type): SType = | ||
new SStructView(parent, restrict, rename = t.asInstanceOf[TStruct]) | ||
|
||
override def _coerceOrCopy( | ||
cb: EmitCodeBuilder, | ||
region: Value[Region], | ||
value: SValue, | ||
deepCopy: Boolean, | ||
): SValue = { | ||
if (deepCopy) | ||
throw new NotImplementedError("Deep copy on struct view") | ||
|
||
value.st match { | ||
case s: SStructView if this == s && !deepCopy => | ||
value | ||
} | ||
} | ||
|
||
override def settableTupleTypes(): IndexedSeq[TypeInfo[_]] = | ||
parent.settableTupleTypes() | ||
|
||
override def fromSettables(settables: IndexedSeq[Settable[_]]): SStructViewSettable = | ||
new SStructViewSettable( | ||
this, | ||
parent.fromSettables(settables).asInstanceOf[SBaseStructSettable], | ||
) | ||
|
||
override def fromValues(values: IndexedSeq[Value[_]]): SStructViewValue = | ||
new SStructViewValue(this, parent.fromValues(values).asInstanceOf[SBaseStructValue]) | ||
|
||
override def copiedType: SType = | ||
if (virtualType.size < 64) | ||
SStackStruct(virtualType, fieldEmitTypes.map(_.copiedType)) | ||
else { | ||
val ct = SBaseStructPointer(storageType().asInstanceOf[PCanonicalStruct]) | ||
assert(ct.virtualType == virtualType, s"ct=$ct, this=$this") | ||
ct | ||
} | ||
|
||
def storageType(): PType = { | ||
val pt = PCanonicalStruct( | ||
required = false, | ||
args = rename.fieldNames.zip(fieldEmitTypes.map(_.copiedType.storageType)): _*, | ||
) | ||
assert(pt.virtualType == virtualType, s"pt=$pt, this=$this") | ||
pt | ||
} | ||
|
||
// aspirational implementation | ||
// def storageType(): PType = StoredSTypePType(this, false) | ||
|
||
override def containsPointers: Boolean = | ||
parent.containsPointers | ||
|
||
override def equals(obj: Any): Boolean = | ||
obj match { | ||
case s: SStructView => | ||
rename == s.rename && | ||
newToOldFieldMapping == s.newToOldFieldMapping && | ||
parent == s.parent // todo test isIsomorphicTo | ||
case _ => | ||
false | ||
} | ||
} | ||
|
||
class SStructViewValue(val st: SStructView, val prev: SBaseStructValue) extends SBaseStructValue { | ||
|
||
override lazy val valueTuple: IndexedSeq[Value[_]] = | ||
prev.valueTuple | ||
|
||
override def subset(fieldNames: String*): SBaseStructValue = | ||
new SStructViewValue(SStructView.subset(fieldNames.toIndexedSeq, st), prev) | ||
|
||
override def loadField(cb: EmitCodeBuilder, fieldIdx: Int): IEmitCode = | ||
prev | ||
.loadField(cb, st.newToOldFieldMapping(fieldIdx)) | ||
.map(cb)(_.castRename(st.virtualType.fields(fieldIdx).typ)) | ||
|
||
override def isFieldMissing(cb: EmitCodeBuilder, fieldIdx: Int): Value[Boolean] = | ||
prev.isFieldMissing(cb, st.newToOldFieldMapping(fieldIdx)) | ||
} | ||
|
||
final class SStructViewSettable(st: SStructView, prev: SBaseStructSettable) | ||
extends SStructViewValue(st, prev) with SBaseStructSettable { | ||
override def subset(fieldNames: String*): SBaseStructValue = | ||
new SStructViewSettable(SStructView.subset(fieldNames.toIndexedSeq, st), prev) | ||
|
||
override def settableTuple(): IndexedSeq[Settable[_]] = | ||
prev.settableTuple() | ||
|
||
override def store(cb: EmitCodeBuilder, pv: SValue): Unit = | ||
prev.store(cb, pv.asInstanceOf[SStructViewValue].prev) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks like this assumes that the fields of
restrict
have the same virtual type as the corresponding fields inparent
. I'm in favor of not makingSStructView
support deep subsetting for now to keep things simple, but in that case, having an entirerestrict
type is redundant and, I think, confusing. It would be sufficient to just use a list of parent fields to subset to (but then still the fullrename
struct type).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.
OK