Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Integrate new transforms with firrtl.stage.Forms
Browse files Browse the repository at this point in the history
Move new transforms, recently added, into existing or new sets of
transforms (defined in firrtl.stage.Forms).

One transform is a mandatory low FIRRTL optimization:

  - firrtl.transforms.LegalizeAndReductionsTransform

Previously, this was included as a prerequisite of all Verilog
emitters (minimum, normal, and SystemVerilog).

Two transforms associated with converting and removing the new
verification statements are moved into a new set of transforms,
AssertsRemoved:

  - firrtl.transforms.formal.ConvertAsserts
  - firrtl.transforms.formal.RemoveVerificationStatements

Previously, these transforms were directly added as prerequisites to
the minimum Verilog and normal Verilog emitter, but not the
SystemVerilog emitter.

The designation of inputForm=LowForm for legacy, custom transforms is
updated to include assertion removal transforms as part of their
optionalPrerequisites. This has the effect of continuing to cause
inputForm=LowForm transforms to run as late as possible (right before
the low FIRRTL, minimum Verilog, Verilog, or SystemVeriog emitter).

Tests are updated to reflect the new order in both CustomTransformSpec
and LoweringCompilersSpec. This commit also simplifies the test used
in the CustomTransformSpec to assert that inputForm=LowForm legacy
transforms run right before the emitter. The new test looks only for a
list of (customTransform, emitter) in a sliding, size-2 window of the
flattened transform order. Previously, this was looking for a match
before and after the custom transform. The old implementation
necessitate busywork updates of the test when new transforms are added
that changed the transform running before the custom transform.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
  • Loading branch information
seldridge committed Jul 11, 2020
1 parent d470a01 commit b16ed48
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/firrtl/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ trait Transform extends TransformLike[CircuitState] with DependencyAPI[Transform
}

override def optionalPrerequisites: Seq[Dependency[Transform]] = inputForm match {
case L => Forms.LowFormOptimized
case L => Forms.LowFormOptimized ++ Forms.AssertsRemoved
case _ => Seq.empty
}

Expand Down
18 changes: 4 additions & 14 deletions src/main/scala/firrtl/Emitter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import java.io.Writer
import scala.collection.mutable
import firrtl.ir._
import firrtl.passes._
import firrtl.transforms.LegalizeAndReductionsTransform
import firrtl.annotations._
import firrtl.traversals.Foreachers._
import firrtl.PrimOps._
import firrtl.WrappedExpression._
import Utils._
import MemPortUtils.{memPortField, memType}
import firrtl.options.{Dependency, HasShellOptions, PhaseException, ShellOption, Unserializable}
import firrtl.options.{HasShellOptions, PhaseException, ShellOption, Unserializable}
import firrtl.stage.{RunFirrtlTransformAnnotation, TransformManager}
import firrtl.transforms.formal.{RemoveVerificationStatements, ConvertAsserts}
// Datastructures
import scala.collection.mutable.ArrayBuffer

Expand Down Expand Up @@ -181,10 +179,7 @@ class VerilogEmitter extends SeqTransform with Emitter {
def inputForm = LowForm
def outputForm = LowForm

override def prerequisites =
Dependency(ConvertAsserts) +:
Dependency[RemoveVerificationStatements] +:
Dependency[LegalizeAndReductionsTransform] +:
override def prerequisites = firrtl.stage.Forms.AssertsRemoved ++
firrtl.stage.Forms.LowFormOptimized

override def optionalPrerequisiteOf = Seq.empty
Expand Down Expand Up @@ -1240,10 +1235,7 @@ class VerilogEmitter extends SeqTransform with Emitter {

class MinimumVerilogEmitter extends VerilogEmitter with Emitter {

override def prerequisites =
Dependency(ConvertAsserts) +:
Dependency[RemoveVerificationStatements] +:
Dependency[LegalizeAndReductionsTransform] +:
override def prerequisites = firrtl.stage.Forms.AssertsRemoved ++
firrtl.stage.Forms.LowFormMinimumOptimized

override def transforms = new TransformManager(firrtl.stage.Forms.VerilogMinimumOptimized, prerequisites)
Expand All @@ -1254,9 +1246,7 @@ class MinimumVerilogEmitter extends VerilogEmitter with Emitter {
class SystemVerilogEmitter extends VerilogEmitter {
override val outputSuffix: String = ".sv"

override def prerequisites =
Dependency[LegalizeAndReductionsTransform] +:
firrtl.stage.Forms.LowFormOptimized
override def prerequisites = firrtl.stage.Forms.LowFormOptimized

override def addFormalStatement(formals: mutable.Map[Expression, ArrayBuffer[Seq[Any]]],
clk: Expression, en: Expression,
Expand Down
7 changes: 6 additions & 1 deletion src/main/scala/firrtl/stage/Forms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ object Forms {
Seq( Dependency(passes.RemoveValidIf),
Dependency(passes.PadWidths),
Dependency(passes.memlib.VerilogMemDelays),
Dependency(passes.SplitExpressions) )
Dependency(passes.SplitExpressions),
Dependency[firrtl.transforms.LegalizeAndReductionsTransform] )

val LowFormOptimized: Seq[TransformDependency] = LowFormMinimumOptimized ++
Seq( Dependency[firrtl.transforms.ConstantPropagation],
Expand All @@ -95,6 +96,10 @@ object Forms {

val VerilogOptimized: Seq[TransformDependency] = LowFormOptimized ++ VerilogMinimumOptimized

val AssertsRemoved: Seq[TransformDependency] =
Seq( Dependency(firrtl.transforms.formal.ConvertAsserts),
Dependency[firrtl.transforms.formal.RemoveVerificationStatements] )

val BackendEmitters =
Seq( Dependency[VerilogEmitter],
Dependency[MinimumVerilogEmitter],
Expand Down
44 changes: 13 additions & 31 deletions src/test/scala/firrtlTests/CustomTransformSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,22 @@ class CustomTransformSpec extends FirrtlFlatSpec {

they should "run right before the emitter* when inputForm=LowForm" in {

val custom = Dependency[IdentityLowForm]

def testOrder(after: Seq[Dependency[Transform]], before: Seq[Dependency[Transform]]): Unit = {
val expectedSlice: Seq[Dependency[Transform]] = before ++: custom +: after

info(expectedSlice.map(_.getSimpleName).mkString(" -> ") + " ok!")

val compiler = new firrtl.stage.transforms.Compiler(custom +: after)
info("Transform Order: \n" + compiler.prettyPrint(" "))


compiler
Seq(
Dependency[LowFirrtlEmitter],
Dependency[MinimumVerilogEmitter],
Dependency[VerilogEmitter],
Dependency[SystemVerilogEmitter]
).foreach { emitter =>
val custom = Dependency[IdentityLowForm]
val tm = new firrtl.stage.transforms.Compiler(custom :: emitter :: Nil)
info(s"when using ${emitter.getObject.name}")
tm
.flattenedTransformOrder
.map(Dependency.fromTransform(_))
.containsSlice(expectedSlice) should be (true)
.map(Dependency.fromTransform)
.sliding(2)
.toList should contain (Seq(custom, emitter))
}

val Seq(low, lowMinOpt, lowOpt) =
Seq(Forms.LowForm, Forms.LowFormMinimumOptimized, Forms.LowFormOptimized)
.map(target => new firrtl.stage.transforms.Compiler(target))
.map(_.flattenedTransformOrder.map(Dependency.fromTransform(_)))

Seq( (Seq(Dependency[LowFirrtlEmitter]), Seq(low.last) ),
(Seq(Dependency[LegalizeAndReductionsTransform],
Dependency(ConvertAsserts),
Dependency[RemoveVerificationStatements],
Dependency[MinimumVerilogEmitter]), Seq(lowMinOpt.last)),
(Seq(Dependency[LegalizeAndReductionsTransform],
Dependency(ConvertAsserts),
Dependency[RemoveVerificationStatements],
Dependency[VerilogEmitter]), Seq(lowOpt.last) ),
(Seq(Dependency[LegalizeAndReductionsTransform],
Dependency[SystemVerilogEmitter]), Seq(lowOpt.last) )
).foreach((testOrder _).tupled)
}

they should "work if placed inside an object" in {
Expand Down
6 changes: 4 additions & 2 deletions src/test/scala/firrtlTests/LoweringCompilersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class LoweringCompilersSpec extends FlatSpec with Matchers {
val tm = new TransformManager(Forms.LowFormMinimumOptimized, Forms.LowForm)
val patches = Seq(
Add(4, Seq(Dependency(firrtl.passes.ResolveFlows))),
Add(5, Seq(Dependency(firrtl.passes.ResolveKinds)))
Add(6, Seq(Dependency[firrtl.transforms.LegalizeAndReductionsTransform],
Dependency(firrtl.passes.ResolveKinds)))
)
compare(legacyTransforms(new MinimumLowFirrtlOptimization), tm, patches)
}
Expand All @@ -204,7 +205,8 @@ class LoweringCompilersSpec extends FlatSpec with Matchers {
val patches = Seq(
Add(6, Seq(Dependency(firrtl.passes.ResolveFlows))),
Add(7, Seq(Dependency(firrtl.passes.Legalize))),
Add(8, Seq(Dependency(firrtl.passes.ResolveKinds)))
Add(8, Seq(Dependency[firrtl.transforms.LegalizeAndReductionsTransform],
Dependency(firrtl.passes.ResolveKinds)))
)
compare(legacyTransforms(new LowFirrtlOptimization), tm, patches)
}
Expand Down

0 comments on commit b16ed48

Please sign in to comment.