Merge pull request #123 from ucb-bar/remove-barstools-compiler

CIRCT Integration
This commit is contained in:
Jerry Zhao
2023-01-09 15:27:54 -08:00
committed by GitHub
5 changed files with 64 additions and 365 deletions

View File

@@ -0,0 +1,52 @@
package barstools.tapeout.transforms
import barstools.tapeout.transforms.stage._
import firrtl._
import firrtl.annotations._
import firrtl.ir._
import firrtl.options.{Dependency, InputAnnotationFileAnnotation, StageMain}
import firrtl.passes.memlib.ReplSeqMemAnnotation
import firrtl.stage.{FirrtlCircuitAnnotation, FirrtlStage, OutputFileAnnotation, RunFirrtlTransformAnnotation}
import firrtl.transforms.BlackBoxResourceFileNameAnno
import logger.LazyLogging
private class GenerateModelStageMain(annotations: AnnotationSeq) extends LazyLogging {
val outAnno: Option[String] = annotations.collectFirst { case OutAnnoAnnotation(s) => s }
val annoFiles: List[String] = annotations.flatMap {
case InputAnnotationFileAnnotation(f) => Some(f)
case _ => None
}.toList
// Dump firrtl and annotation files
// Use global param outAnno
protected def dumpAnnos(
annotations: AnnotationSeq
): Unit = {
outAnno.foreach { annoPath =>
val outputFile = new java.io.PrintWriter(annoPath)
outputFile.write(JsonProtocol.serialize(annotations.filter(_ match {
case _: DeletedAnnotation => false
case _: EmittedComponent => false
case _: EmittedAnnotation[_] => false
case _: FirrtlCircuitAnnotation => false
case _: OutAnnoAnnotation => false
case _ => true
})))
outputFile.close()
}
}
def executeStageMain(): Unit = {
val annos = new FirrtlStage().execute(Array.empty, annotations)
annos.collectFirst { case FirrtlCircuitAnnotation(circuit) => circuit } match {
case Some(circuit) =>
dumpAnnos(annos)
case _ =>
throw new Exception(s"executeTop failed while executing FIRRTL!\n")
}
}
}
object GenerateModelStageMain extends StageMain(new TapeoutStage())

View File

@@ -1,124 +0,0 @@
package barstools.tapeout.transforms
import barstools.tapeout.transforms.stage._
import firrtl._
import firrtl.annotations._
import firrtl.ir._
import firrtl.options.{Dependency, InputAnnotationFileAnnotation, StageMain}
import firrtl.passes.memlib.ReplSeqMemAnnotation
import firrtl.stage.{FirrtlCircuitAnnotation, FirrtlStage, OutputFileAnnotation, RunFirrtlTransformAnnotation}
import firrtl.transforms.BlackBoxResourceFileNameAnno
import logger.LazyLogging
// Requires two phases, one to collect modules below synTop in the hierarchy
// and a second to remove those modules to generate the test harness
private class GenerateTopAndHarness(annotations: AnnotationSeq) extends LazyLogging {
val synTop: Option[String] = annotations.collectFirst { case SynTopAnnotation(s) => s }
val topFir: Option[String] = annotations.collectFirst { case TopFirAnnotation(s) => s }
val harnessFir: Option[String] = annotations.collectFirst { case HarnessFirAnnotation(s) => s }
val topAnnoOut: Option[String] = annotations.collectFirst { case TopAnnoOutAnnotation(s) => s }
val harnessAnnoOut: Option[String] = annotations.collectFirst { case HarnessAnnoOutAnnotation(s) => s }
val harnessTop: Option[String] = annotations.collectFirst { case HarnessTopAnnotation(h) => h }
val harnessConf: Option[String] = annotations.collectFirst { case HarnessConfAnnotation(h) => h }
val harnessOutput: Option[String] = annotations.collectFirst { case HarnessOutputAnnotation(h) => h }
val topDotfOut: Option[String] = annotations.collectFirst { case TopDotfOutAnnotation(h) => h }
val harnessDotfOut: Option[String] = annotations.collectFirst { case HarnessDotfOutAnnotation(h) => h }
val annoFiles: List[String] = annotations.flatMap {
case InputAnnotationFileAnnotation(f) => Some(f)
case _ => None
}.toList
lazy val rootCircuitTarget = CircuitTarget(harnessTop.get)
val topAnnos = synTop.map(st => ReParentCircuitAnnotation(rootCircuitTarget.module(st))) ++
topDotfOut.map(BlackBoxResourceFileNameAnno)
// Dump firrtl and annotation files
protected def dump(
circuit: Circuit,
annotations: AnnotationSeq,
firFile: Option[String],
annoFile: Option[String]
): Unit = {
firFile.foreach { firPath =>
val outputFile = new java.io.PrintWriter(firPath)
outputFile.write(circuit.serialize)
outputFile.close()
}
annoFile.foreach { annoPath =>
val outputFile = new java.io.PrintWriter(annoPath)
outputFile.write(JsonProtocol.serialize(annotations.filter(_ match {
case _: DeletedAnnotation => false
case _: EmittedComponent => false
case _: EmittedAnnotation[_] => false
case _: FirrtlCircuitAnnotation => false
case _ => true
})))
outputFile.close()
}
}
// Top Generation
def executeTop(): Seq[ExtModule] = {
val annos = new FirrtlStage().execute(
Array.empty,
annotations ++ Seq(
RunFirrtlTransformAnnotation(Dependency[ReParentCircuit]),
RunFirrtlTransformAnnotation(Dependency[RemoveUnusedModules])
) ++
topAnnos
)
annos.collectFirst { case FirrtlCircuitAnnotation(circuit) => circuit } match {
case Some(circuit) =>
dump(circuit, annos, topFir, topAnnoOut)
circuit.modules.collect { case e: ExtModule => e }
case _ =>
throw new Exception(s"executeTop failed while executing FIRRTL!\n")
}
}
// Top and harness generation
def executeTopAndHarness(): Unit = {
// Execute top and get list of ExtModules to avoid collisions
val topExtModules = executeTop()
// order is determined by DependencyAPIMigration
val harnessAnnos =
harnessDotfOut.map(BlackBoxResourceFileNameAnno).toSeq ++
harnessTop.map(ht => ModuleNameSuffixAnnotation(rootCircuitTarget, s"_in${ht}")) ++
synTop.map(st => ConvertToExtModAnnotation(rootCircuitTarget.module(st))) ++
Seq(
LinkExtModulesAnnotation(topExtModules),
RunFirrtlTransformAnnotation(Dependency[ConvertToExtMod]),
RunFirrtlTransformAnnotation(Dependency[RemoveUnusedModules]),
RunFirrtlTransformAnnotation(Dependency[AvoidExtModuleCollisions]),
RunFirrtlTransformAnnotation(Dependency[AddSuffixToModuleNames])
)
// 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
val generatorAnnotations = annotations
.filterNot(_.isInstanceOf[OutputFileAnnotation])
.map {
case ReplSeqMemAnnotation(i, _) => ReplSeqMemAnnotation(i, harnessConf.get)
case HarnessOutputAnnotation(s) => OutputFileAnnotation(s)
case anno => anno
} ++ harnessAnnos
val annos = new FirrtlStage().execute(Array.empty, generatorAnnotations)
annos.collectFirst { case FirrtlCircuitAnnotation(circuit) => circuit } match {
case Some(circuit) =>
dump(circuit, annos, harnessFir, harnessAnnoOut)
case _ =>
throw new Exception(s"executeTop failed while executing FIRRTL!\n")
}
}
}
object GenerateTop extends StageMain(new TapeoutStage(doHarness = false))
object GenerateTopAndHarness extends StageMain(new TapeoutStage(doHarness = true))

View File

@@ -2,7 +2,7 @@
package barstools.tapeout.transforms.stage
import barstools.tapeout.transforms.GenerateTopAndHarness
import barstools.tapeout.transforms.GenerateModelStageMain
import chisel3.stage.ChiselCli
import firrtl.AnnotationSeq
import firrtl.annotations.{Annotation, NoTargetAnnotation}
@@ -14,132 +14,15 @@ sealed trait TapeoutOption extends Unserializable {
this: Annotation =>
}
case class HarnessOutputAnnotation(harnessOutput: String) extends NoTargetAnnotation with TapeoutOption
case class OutAnnoAnnotation(outAnno: String) extends NoTargetAnnotation with TapeoutOption
object HarnessOutputAnnotation extends HasShellOptions {
object OutAnnoAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-o",
shortOption = Some("tho"),
toAnnotationSeq = (s: String) => Seq(HarnessOutputAnnotation(s)),
helpText = "use this to generate a harness at <harness-output>"
)
)
}
case class SynTopAnnotation(synTop: String) extends NoTargetAnnotation with TapeoutOption
object SynTopAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "syn-top",
shortOption = Some("tst"),
toAnnotationSeq = (s: String) => Seq(SynTopAnnotation(s)),
helpText = "use this to set synTop"
)
)
}
case class TopFirAnnotation(topFir: String) extends NoTargetAnnotation with TapeoutOption
object TopFirAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "top-fir",
shortOption = Some("tsf"),
toAnnotationSeq = (s: String) => Seq(TopFirAnnotation(s)),
helpText = "use this to set topFir"
)
)
}
case class TopAnnoOutAnnotation(topAnnoOut: String) extends NoTargetAnnotation with TapeoutOption
object TopAnnoOutAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "top-anno-out",
shortOption = Some("tsaof"),
toAnnotationSeq = (s: String) => Seq(TopAnnoOutAnnotation(s)),
helpText = "use this to set topAnnoOut"
)
)
}
case class TopDotfOutAnnotation(topDotfOut: String) extends NoTargetAnnotation with TapeoutOption
object TopDotfOutAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "top-dotf-out",
shortOption = Some("tdf"),
toAnnotationSeq = (s: String) => Seq(TopDotfOutAnnotation(s)),
helpText = "use this to set the filename for the top resource .f file"
)
)
}
case class HarnessTopAnnotation(harnessTop: String) extends NoTargetAnnotation with TapeoutOption
object HarnessTopAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-top",
shortOption = Some("tht"),
toAnnotationSeq = (s: String) => Seq(HarnessTopAnnotation(s)),
helpText = "use this to set harnessTop"
)
)
}
case class HarnessFirAnnotation(harnessFir: String) extends NoTargetAnnotation with TapeoutOption
object HarnessFirAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-fir",
shortOption = Some("thf"),
toAnnotationSeq = (s: String) => Seq(HarnessFirAnnotation(s)),
helpText = "use this to set harnessFir"
)
)
}
case class HarnessAnnoOutAnnotation(harnessAnnoOut: String) extends NoTargetAnnotation with TapeoutOption
object HarnessAnnoOutAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-anno-out",
shortOption = Some("thaof"),
toAnnotationSeq = (s: String) => Seq(HarnessAnnoOutAnnotation(s)),
helpText = "use this to set harnessAnnoOut"
)
)
}
case class HarnessDotfOutAnnotation(harnessDotfOut: String) extends NoTargetAnnotation with TapeoutOption
object HarnessDotfOutAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-dotf-out",
shortOption = Some("hdf"),
toAnnotationSeq = (s: String) => Seq(HarnessDotfOutAnnotation(s)),
helpText = "use this to set the filename for the harness resource .f file"
)
)
}
case class HarnessConfAnnotation(harnessConf: String) extends NoTargetAnnotation with TapeoutOption
object HarnessConfAnnotation extends HasShellOptions {
val options: Seq[ShellOption[_]] = Seq(
new ShellOption[String](
longOption = "harness-conf",
shortOption = Some("thconf"),
toAnnotationSeq = (s: String) => Seq(HarnessConfAnnotation(s)),
helpText = "use this to set the harness conf file location"
longOption = "out-anno-file",
shortOption = Some("oaf"),
toAnnotationSeq = (s: String) => Seq(OutAnnoAnnotation(s)),
helpText = "out-anno-file"
)
)
}
@@ -149,31 +32,17 @@ trait TapeoutCli {
parser.note("Tapeout specific options")
Seq(
HarnessOutputAnnotation,
SynTopAnnotation,
TopFirAnnotation,
TopAnnoOutAnnotation,
TopDotfOutAnnotation,
HarnessTopAnnotation,
HarnessFirAnnotation,
HarnessAnnoOutAnnotation,
HarnessDotfOutAnnotation,
HarnessConfAnnotation
OutAnnoAnnotation
).foreach(_.addOptions(parser))
}
class TapeoutStage(doHarness: Boolean) extends Stage {
class TapeoutStage() extends Stage {
override val shell: Shell = new Shell(applicationName = "tapeout") with TapeoutCli with ChiselCli with FirrtlCli
override def run(annotations: AnnotationSeq): AnnotationSeq = {
Logger.makeScope(annotations) {
val generator = new GenerateTopAndHarness(annotations)
if (doHarness) {
generator.executeTopAndHarness()
} else {
generator.executeTop()
}
val stageMain = new GenerateModelStageMain(annotations)
stageMain.executeStageMain()
}
annotations
}

View File

@@ -91,7 +91,7 @@ class GenerateSpec extends AnyFreeSpec {
val targetDir = "test_run_dir/generate_spec"
generateTestData(targetDir)
GenerateTop.main(
GenerateModelStageMain.main(
Array(
"-i",
s"$targetDir/GenerateExampleTester.fir",

View File

@@ -1,98 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
package barstools.tapeout.transforms
import chisel3.stage.ChiselStage
import firrtl.FileUtils
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import java.io.{File, PrintWriter}
class GenerateTopSpec extends AnyFreeSpec with Matchers {
"Generate top and harness" - {
"should include the following transforms" in {
val targetDir = "test_run_dir/generate_top_and_harness"
val transformListName = s"$targetDir/ExampleModuleNeesResetInvertTransforms.log"
FileUtils.makeDirectory(targetDir)
(new ChiselStage).emitChirrtl(new ExampleModuleNeedsResetInverted, Array("--target-dir", targetDir))
GenerateTopAndHarness.main(
Array(
"-i",
s"$targetDir/ExampleModuleNeedsResetInverted.fir",
"-ll",
"info",
"--log-file",
transformListName
)
)
val output = FileUtils.getText(transformListName)
output should include("barstools.tapeout.transforms.AddSuffixToModuleNames")
output should include("barstools.tapeout.transforms.ConvertToExtMod")
output should include("barstools.tapeout.transforms.RemoveUnusedModules")
output should include("barstools.tapeout.transforms.AvoidExtModuleCollisions")
}
}
"generate harness should be generated" ignore {
val targetDir = "test_run_dir/generate_top_spec"
val logOutputName = s"$targetDir/top_spec_output.log"
FileUtils.makeDirectory(targetDir)
val input = FileUtils.getLinesResource("/BlackBoxFloatTester.fir")
val printWriter = new PrintWriter(new File(s"$targetDir/BlackBoxFloatTester.fir"))
printWriter.write(input.mkString("\n"))
printWriter.close()
println(s"""Resource: ${input.mkString("\n")}""")
GenerateTopAndHarness.main(
Array(
"--target-dir",
"test_run_dir/generate_top_spec",
"-i",
s"$targetDir/BlackBoxFloatTester.fir",
"-o",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.top.v",
"-tho",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.harness.v",
"-i",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.fir",
"--syn-top",
"UnitTestSuite",
"--harness-top",
"TestHarness",
"-faf",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.anno.json",
"-tsaof",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.top.anno.json",
"-tdf",
"firrtl_black_box_resource_files.top.f",
"-tsf",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.top.fir",
"-thaof",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.harness.anno.json",
"-hdf",
"firrtl_black_box_resource_files.harness.f",
"-thf",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.harness.fir",
"--infer-rw",
"--repl-seq-mem",
"-c:TestHarness:-o:chipyard.unittest.TestHarness.IceNetUnitTestConfig.top.mems.conf",
"-thconf",
"chipyard.unittest.TestHarness.IceNetUnitTestConfig.harness.mems.conf",
"-td",
"test_run_dir/from-ci",
"-ll",
"info",
"--log-file",
logOutputName
)
)
val output = FileUtils.getText(logOutputName)
println(output)
}
}