Savepoint

This commit is contained in:
InversionSpaces 2024-01-04 15:02:09 +00:00
parent a73996d8de
commit 09a8fe4b62
5 changed files with 47 additions and 47 deletions

View File

@ -3,6 +3,7 @@ package aqua.model.inline
import aqua.model.*
import aqua.model.inline.Inline.MergeMode.*
import aqua.model.inline.raw.*
import aqua.model.inline.state.Exports.Export
import aqua.model.inline.state.{Counter, Exports, Mangler}
import aqua.raw.ops.*
import aqua.raw.value.*
@ -26,24 +27,24 @@ object RawValueInliner extends Logging {
private[inline] def unfold[S: Mangler: Exports](
raw: ValueRaw,
propertiesAllowed: Boolean = true
): State[S, (Exports.Export, Inline)] = for {
): State[S, (Export, Inline)] = for {
optimized <- StateT.liftF(Optimization.optimize(raw))
_ <- StateT.liftF(Eval.later(logger.trace("OPTIMIZIED " + optimized)))
result <- optimized match {
case LiteralRaw(value, t) =>
val model = LiteralModel(value, t)
val exp = Export.Value(model)
State.pure(exp -> Inline.empty)
case VarRaw(name, t) =>
for {
maybeExport <- Exports[S].get(name)
model = VarModel(name, t)
exp = maybeExport.getOrElse(
Exports.Export.Value(model)
Export.Value(model)
)
} yield exp -> Inline.empty
case LiteralRaw(value, t) =>
val model = LiteralModel(value, t)
val exp = Exports.Export.Value(model)
State.pure(exp -> Inline.empty)
case alr: ApplyPropertyRaw =>
ApplyPropertiesRawInliner(alr, propertiesAllowed)

View File

@ -28,7 +28,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
def flatLiteralWithProperties[S: Mangler: Exports](
literal: LiteralModel,
inl: Inline,
properties: Chain[PropertyModel]
properties: Chain[PropertyModel] = Chain.empty
): State[S, (VarModel, Inline)] = {
for {
apName <- Mangler[S].findAndForbidName("literal_ap")
@ -213,15 +213,13 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
}
private def unfoldProperties[S: Mangler: Exports](
prevInline: Inline,
vm: VarModel,
properties: Chain[PropertyRaw],
propertiesAllowed: Boolean
): State[S, (VarModel, Inline)] = {
exp: Export,
properties: Chain[PropertyRaw]
): State[S, (Export, Inline)] = {
optimizeProperties(properties).flatMap { case (optimizedProps, optimizationInline) =>
optimizedProps
.foldLeft[State[S, (VarModel, Inline)]](
State.pure((vm, prevInline.mergeWith(optimizationInline, SeqMode)))
State.pure((vm, Inline.empty))
) { case (state, property) =>
state.flatMap {
case (vm @ Ability(_, at), leftInline) =>
@ -275,19 +273,27 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
* apply possible optimizations
*/
sizeInlined <- unfold(idx.increment)
(sizeVM, sizeInline) = sizeInlined
(sizeExp, sizeInline) = sizeInlined
sizeVM <- sizeExp match {
case Export.Value(vm) =>
State.pure(vm)
case _ =>
internalError(s"Unexpected: size ($sizeExp) is not a value")
}
/**
* Inline idx which is `size - 1`
* TODO: Do not generate it if
* it is not needed, e.g. in `join`
*/
idxInlined <- sizeVM match {
idxInlined <- vm match {
/**
* Micro optimization: if idx is a literal
* do not generate inline.
*/
case LiteralModel.Integer(i, t) =>
(LiteralModel((i - 1).toString, t), Inline.empty).pure[State[S, *]]
State.pure(
(LiteralModel((i - 1).toString, t), Inline.empty)
)
case _ =>
Mangler[S].findAndForbidName(s"${streamName}_idx").map { idxName =>
val idxVar = VarModel(idxName, sizeVM.`type`)
@ -328,8 +334,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
private def unfoldRawWithProperties[S: Mangler: Exports](
raw: ValueRaw,
properties: Chain[PropertyRaw],
propertiesAllowed: Boolean
properties: Chain[PropertyRaw]
): State[S, (Export, Inline)] =
(raw, properties.uncons) match {
case (
@ -343,30 +348,24 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
(gateVM, gateInline) = gateInlined
propsInlined <- unfoldProperties(
gateInline,
gateVM,
otherProperties,
propertiesAllowed
Export.Value(gateVM),
otherProperties
)
} yield propsInlined
case _ =>
internalError(
s"Unfolded stream ($vr) cannot be a literal"
s"Unexpected: stream ($vr) is not a variable"
)
}
case (_, _) =>
unfold(raw).flatMap {
case (vm: VarModel, prevInline) =>
unfoldProperties(prevInline, vm, properties, propertiesAllowed)
// To coerce types
.map(identity)
case (l: LiteralModel, inl) =>
flatLiteralWithProperties(l, inl, Chain.empty).flatMap { (varModel, prevInline) =>
unfoldProperties(prevInline, varModel, properties, propertiesAllowed).map {
case (v, i) =>
v -> i
}
case (Export.Value(l: LiteralModel), inl) =>
flatLiteralWithProperties(l, inl).flatMap { (vm, prevInline) =>
unfoldProperties(prevInline, Export.Value(vm), properties)
}
case (exp, prevInline) =>
unfoldProperties(prevInline, exp, properties)
}
}
@ -384,10 +383,9 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
} yield flatten -> Inline.tree(FlattenModel(varModel, flatten.name).leaf)
override def apply[S: Mangler: Exports](
apr: ApplyPropertyRaw,
propertiesAllowed: Boolean
apr: ApplyPropertyRaw
): State[S, (Export, Inline)] = {
val (raw, properties) = apr.unwind
unfoldRawWithProperties(raw, properties, propertiesAllowed)
unfoldRawWithProperties(raw, properties)
}
}

View File

@ -14,7 +14,7 @@ import scribe.Logging
object CallArrowRawInliner extends RawInliner[CallArrowRaw] with Logging {
private[inline] def unfold[S: Mangler: Exports: Arrows](
private[inline] def unfold[S: Mangler: Exports](
value: CallArrowRaw,
exportTo: List[Call.Export]
): State[S, (List[ValueModel], Inline)] = {

View File

@ -2,15 +2,16 @@ package aqua.model.inline.raw
import aqua.model.ValueModel
import aqua.model.inline.Inline
import aqua.model.inline.state.{Arrows, Exports, Mangler}
import aqua.model.inline.state.Exports.Export
import aqua.model.inline.state.{Exports, Mangler}
import aqua.raw.value.ValueRaw
import cats.data.State
trait RawInliner[T <: ValueRaw] {
def apply[S: Mangler: Exports: Arrows](
raw: T,
propertiesAllowed: Boolean = true
): State[S, (ValueModel, Inline)]
def apply[S: Mangler: Exports](
raw: T
): State[S, (Export, Inline)]
}

View File

@ -3,17 +3,17 @@ package aqua.model.inline.raw
import aqua.errors.Errors.internalError
import aqua.model.*
import aqua.model.inline.Inline
import aqua.model.inline.state.{Arrows, Exports, Mangler}
import aqua.model.inline.RawValueInliner.unfold
import aqua.model.inline.state.{Arrows, Exports, Mangler}
import aqua.types.{ArrayType, CanonStreamType, ScalarType, StreamType}
import cats.data.State
import cats.data.Chain
import cats.data.State
import cats.instances.stream
import cats.syntax.applicative.*
import cats.syntax.monoid.*
import cats.syntax.option.*
import cats.syntax.applicative.*
import scribe.Logging
import cats.instances.stream
object StreamGateInliner extends Logging {
@ -88,7 +88,7 @@ object StreamGateInliner extends Logging {
)
}
def apply[S: Mangler: Exports: Arrows](
def apply[S: Mangler: Exports](
streamName: String,
streamType: StreamType,
sizeModel: ValueModel