Coordinate Top and Harness generation (#63)

* Coordinate Top and Harness generation

* Update to use .f filename override annotations

* Move top generation to def to help GC
This commit is contained in:
Albert Magyar
2019-07-30 22:42:05 -07:00
committed by GitHub
parent e3c822709b
commit 26096e07f6

View File

@@ -8,6 +8,8 @@ import firrtl.passes.Pass
import java.io.File import java.io.File
import firrtl.annotations.AnnotationYamlProtocol._ import firrtl.annotations.AnnotationYamlProtocol._
import firrtl.passes.memlib.ReplSeqMemAnnotation
import firrtl.transforms.BlackBoxResourceFileNameAnno
import net.jcazevedo.moultingyaml._ import net.jcazevedo.moultingyaml._
import com.typesafe.scalalogging.LazyLogging import com.typesafe.scalalogging.LazyLogging
@@ -60,6 +62,17 @@ trait HasTapeoutOptions { self: ExecutionOptionsManager with HasFirrtlOptions =>
"use this to set topAnnoOut" "use this to set topAnnoOut"
} }
parser.opt[String]("top-dotf-out")
.abbr("tdf")
.valueName("<top-dotf-out>")
.foreach { x =>
tapeoutOptions = tapeoutOptions.copy(
topDotfOut = Some(x)
)
}.text {
"use this to set the filename for the top resource .f file"
}
parser.opt[String]("harness-top") parser.opt[String]("harness-top")
.abbr("tht") .abbr("tht")
.valueName("<harness-top>") .valueName("<harness-top>")
@@ -93,6 +106,28 @@ trait HasTapeoutOptions { self: ExecutionOptionsManager with HasFirrtlOptions =>
"use this to set harnessAnnoOut" "use this to set harnessAnnoOut"
} }
parser.opt[String]("harness-dotf-out")
.abbr("hdf")
.valueName("<harness-dotf-out>")
.foreach { x =>
tapeoutOptions = tapeoutOptions.copy(
harnessDotfOut = Some(x)
)
}.text {
"use this to set the filename for the harness resource .f file"
}
parser.opt[String]("harness-conf")
.abbr("thconf")
.valueName ("<harness-conf-file>")
.foreach { x =>
tapeoutOptions = tapeoutOptions.copy(
harnessConf = Some(x)
)
}.text {
"use this to set the harness conf file location"
}
} }
case class TapeoutOptions( case class TapeoutOptions(
@@ -100,9 +135,12 @@ case class TapeoutOptions(
synTop: Option[String] = None, synTop: Option[String] = None,
topFir: Option[String] = None, topFir: Option[String] = None,
topAnnoOut: Option[String] = None, topAnnoOut: Option[String] = None,
topDotfOut: Option[String] = None,
harnessTop: Option[String] = None, harnessTop: Option[String] = None,
harnessFir: Option[String] = None, harnessFir: Option[String] = None,
harnessAnnoOut: Option[String] = None harnessAnnoOut: Option[String] = None,
harnessDotfOut: Option[String] = None,
harnessConf: Option[String] = None
) extends LazyLogging ) extends LazyLogging
// Requires two phases, one to collect modules below synTop in the hierarchy // Requires two phases, one to collect modules below synTop in the hierarchy
@@ -123,79 +161,88 @@ sealed trait GenerateTopAndHarnessApp extends LazyLogging { this: App =>
// FIRRTL options // FIRRTL options
lazy val annoFiles = firrtlOptions.annotationFileNames lazy val annoFiles = firrtlOptions.annotationFileNames
private def topTransforms: Seq[Transform] = { lazy val topTransforms: Seq[Transform] = {
Seq( Seq(
new ReParentCircuit(synTop.get), new ReParentCircuit(synTop.get),
new RemoveUnusedModules new RemoveUnusedModules
) )
} }
lazy val topOptions = firrtlOptions.copy(
customTransforms = firrtlOptions.customTransforms ++ topTransforms,
annotations = firrtlOptions.annotations ++ tapeoutOptions.topDotfOut.map(BlackBoxResourceFileNameAnno(_))
)
private def harnessTransforms: Seq[Transform] = { class AvoidExtModuleCollisions(mustLink: Seq[ExtModule]) extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(state: CircuitState): CircuitState = {
state.copy(circuit = state.circuit.copy(modules = state.circuit.modules ++ mustLink))
}
}
private def harnessTransforms(topExtModules: Seq[ExtModule]): Seq[Transform] = {
// XXX this is a hack, we really should be checking the masters to see if they are ExtModules // XXX this is a hack, we really should be checking the masters to see if they are ExtModules
val externals = Set(harnessTop.get, synTop.get, "SimSerial", "SimDTM") val externals = Set(harnessTop.get, synTop.get, "SimSerial", "SimDTM")
Seq( Seq(
new ConvertToExtMod((m) => m.name == synTop.get), new ConvertToExtMod((m) => m.name == synTop.get),
new RemoveUnusedModules, new RemoveUnusedModules,
new AvoidExtModuleCollisions(topExtModules),
new RenameModulesAndInstances((old) => if (externals contains old) old else (old + "_in" + harnessTop.get)) new RenameModulesAndInstances((old) => if (externals contains old) old else (old + "_in" + harnessTop.get))
) )
} }
// Top Generation // Dump firrtl and annotation files
protected def executeTop: Unit = { protected def dump(res: FirrtlExecutionSuccess, firFile: Option[String], annoFile: Option[String]): Unit = {
firFile.foreach { firPath =>
optionsManager.firrtlOptions = optionsManager.firrtlOptions.copy( val outputFile = new java.io.PrintWriter(firPath)
customTransforms = firrtlOptions.customTransforms ++ topTransforms outputFile.write(res.circuitState.circuit.serialize)
) outputFile.close()
}
val result = firrtl.Driver.execute(optionsManager) annoFile.foreach { annoPath =>
val outputFile = new java.io.PrintWriter(annoPath)
result match { outputFile.write(JsonProtocol.serialize(res.circuitState.annotations.filter(_ match {
case x: FirrtlExecutionSuccess => case ea: EmittedAnnotation[_] => false
tapeoutOptions.topFir.foreach { firFile => case fca: FirrtlCircuitAnnotation => false
val outputFile = new java.io.PrintWriter(firFile) case _ => true
outputFile.write(x.circuitState.circuit.serialize) })))
outputFile.close() outputFile.close()
}
tapeoutOptions.topAnnoOut.foreach { annoFile =>
val outputFile = new java.io.PrintWriter(annoFile)
outputFile.write(JsonProtocol.serialize(x.circuitState.annotations.filter(_ match {
case ea: EmittedAnnotation[_] => false
case fca: FirrtlCircuitAnnotation => false
case _ => true
})))
outputFile.close()
}
case _ =>
} }
} }
// Harness Generation // Top Generation
protected def executeHarness: Unit = { protected def executeTop(): Seq[ExtModule] = {
optionsManager.firrtlOptions = topOptions
optionsManager.firrtlOptions = optionsManager.firrtlOptions.copy(
customTransforms = firrtlOptions.customTransforms ++ harnessTransforms
)
val result = firrtl.Driver.execute(optionsManager) val result = firrtl.Driver.execute(optionsManager)
result match { result match {
case x: FirrtlExecutionSuccess => case x: FirrtlExecutionSuccess =>
tapeoutOptions.harnessFir.foreach { firFile => dump(x, tapeoutOptions.topFir, tapeoutOptions.topAnnoOut)
val outputFile = new java.io.PrintWriter(firFile) x.circuitState.circuit.modules.collect{ case e: ExtModule => e }
outputFile.write(x.circuitState.circuit.serialize) case _ =>
outputFile.close() throw new Exception("executeTop failed on illegal FIRRTL input!")
} }
tapeoutOptions.harnessAnnoOut.foreach { annoFile => }
val outputFile = new java.io.PrintWriter(annoFile)
outputFile.write(JsonProtocol.serialize(x.circuitState.annotations.filter(_ match { // Top and harness generation
case ea: EmittedAnnotation[_] => false protected def executeTopAndHarness(): Unit = {
case fca: FirrtlCircuitAnnotation => false // Execute top and get list of ExtModules to avoid collisions
case _ => true val topExtModules = executeTop()
})))
outputFile.close() // For harness run, change some firrtlOptions (below) for harness phase
} // customTransforms: setup harness transforms, add AvoidExtModuleCollisions
// outputFileNameOverride: change to harnessOutput
// conf file must change to harnessConf by mapping annotations
optionsManager.firrtlOptions = firrtlOptions.copy(
customTransforms = firrtlOptions.customTransforms ++ harnessTransforms(topExtModules),
outputFileNameOverride = tapeoutOptions.harnessOutput.get,
annotations = firrtlOptions.annotations.map({
case ReplSeqMemAnnotation(i, o) => ReplSeqMemAnnotation(i, tapeoutOptions.harnessConf.get)
case a => a
}) ++ tapeoutOptions.harnessDotfOut.map(BlackBoxResourceFileNameAnno(_))
)
val harnessResult = firrtl.Driver.execute(optionsManager)
harnessResult match {
case x: FirrtlExecutionSuccess => dump(x, tapeoutOptions.harnessFir, tapeoutOptions.harnessAnnoOut)
case _ => case _ =>
} }
} }
@@ -203,10 +250,9 @@ sealed trait GenerateTopAndHarnessApp extends LazyLogging { this: App =>
object GenerateTop extends App with GenerateTopAndHarnessApp { object GenerateTop extends App with GenerateTopAndHarnessApp {
// Only need a single phase to generate the top module // Only need a single phase to generate the top module
executeTop executeTop()
} }
object GenerateHarness extends App with GenerateTopAndHarnessApp { object GenerateTopAndHarness extends App with GenerateTopAndHarnessApp {
// Do minimal work for the first phase to generate test harness executeTopAndHarness()
executeHarness
} }