From ec6fbb7e0cca3a688ea633c20dcb81edbe6342ee Mon Sep 17 00:00:00 2001 From: chick Date: Mon, 30 Oct 2017 12:56:24 -0700 Subject: [PATCH] This creates a toolchain that unites the machinery for Issue #114 See InstrumentingSpec:"run with with bits reduced" for example of use New executeWithBitReduction acts like ordinary dsptools.Driver.execute Adds in the following - Run with interpreter bit instrumentation - Analyzes report, creating annotations to reduce size - Runs transform to reduce bits in low Firrtl - Re-runs the tests re-instrumenting Produces files - .signal-bitsizes.csv - .signal-bitsizes-2.csv - .bit-reduced.fir --- src/main/scala/dsptools/Driver.scala | 119 +++++++++++++++++- .../scala/examples/InstrumentingSpec.scala | 11 +- 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/src/main/scala/dsptools/Driver.scala b/src/main/scala/dsptools/Driver.scala index c8c0e29a..fd5af7c9 100644 --- a/src/main/scala/dsptools/Driver.scala +++ b/src/main/scala/dsptools/Driver.scala @@ -2,9 +2,12 @@ package dsptools +import java.io.PrintWriter + import chisel3._ import chisel3.iotesters._ -import firrtl.{HasFirrtlOptions, ExecutionOptionsManager} +import dsptools.numbers.resizer.{BitReducer, ChangeWidthTransform} +import firrtl._ import numbers.DspRealFactory import firrtl_interpreter._ @@ -21,9 +24,9 @@ object Driver { val om = optionsManager match { case d: DspTesterOptionsManager => Some(d) case _ => None - } + } - optionsManagerVar.withValue(om) { + optionsManagerVar.withValue(om) { optionsManager.interpreterOptions = optionsManager.interpreterOptions.copy( blackBoxFactories = optionsManager.interpreterOptions.blackBoxFactories :+ new DspRealFactory) iotesters.Driver.execute(dutGenerator, optionsManager)(testerGen) @@ -46,6 +49,116 @@ object Driver { } } + //scalastyle:off method.length + def executeWithBitReduction[T <: Module](dutGenerator: () => T, + optionsManager: TesterOptionsManager)(testerGen: T => PeekPokeTester[T]): Boolean = { + + val om = optionsManager match { + case d: DspTesterOptionsManager => Some(d) + case _ => None + } + + optionsManagerVar.withValue(om) { + optionsManager.interpreterOptions = optionsManager.interpreterOptions.copy( + blackBoxFactories = optionsManager.interpreterOptions.blackBoxFactories :+ new DspRealFactory) + + val requestedName = optionsManager.interpreterOptions.monitorReportFileName + + optionsManager.interpreterOptions = optionsManager.interpreterOptions.copy( + monitorBitUsage = true, + monitorReportFileName = "signal-bitsizes.csv", + prettyPrintReport = false + ) + val passed = iotesters.Driver.execute(dutGenerator, optionsManager)(testerGen) + + val reportFileName = optionsManager.interpreterOptions.getMonitorReportFile(optionsManager) + + if(requestedName.nonEmpty) { + println(s"Warning: ignoring monitorReportFileName=$requestedName, using: $reportFileName") + } + + val data = io.Source.fromFile(reportFileName).getLines().toList.drop(1) + + val im = new BitReducer(data) + im.run() + val report = im.getReportString + println(report) + + val annotationMap = im.getAnnotationMap + annotationMap.annotations.foreach { anno => + println(anno.serialize) + } + + val firrtlFilename = optionsManager.firrtlOptions.getTargetFile(optionsManager) + val firrtlString = io.Source.fromFile(firrtlFilename).getLines().mkString("\n") + + println("Firrtl:\n" + firrtlString) + + + val circuitState = firrtl.CircuitState(Parser.parse(firrtlString), LowForm, Some(annotationMap)) + + val transform = new ChangeWidthTransform + + val newCircuitState = transform.execute(circuitState) + + val newFirrtlString = newCircuitState.circuit.serialize + + println("Bit-reduced Firrtl\n" + newFirrtlString) + + val newFirrtlFileName = { + optionsManager.firrtlOptions.getTargetFile(optionsManager).replaceFirst(""".lo.fir$""", ".bit-reduced.fir") + } + val writer = new PrintWriter(newFirrtlFileName) + writer.write(newFirrtlString) + writer.close() + + optionsManager.interpreterOptions = optionsManager.interpreterOptions.copy( + monitorBitUsage = true, + monitorReportFileName = "signal-bitsizes-2.csv", + prettyPrintReport = false + ) + val passed2 = iotesters.Driver.execute( + dutGenerator, + optionsManager, + Some(newFirrtlString) + )(testerGen) + + val reportFileName2 = optionsManager.interpreterOptions.getMonitorReportFile(optionsManager) + + val data2 = io.Source.fromFile(reportFileName2).getLines().toList.drop(1) + + val im2 = new BitReducer(data2) + im2.run() + val report2 = im2.getReportString + println(report2) + + passed & passed2 + } + } + + def executeWithBitReduction[T <: Module]( + dutGenerator: () => T, + args: Array[String] = Array.empty + ) + ( + testerGen: T => PeekPokeTester[T] + ): Boolean = { + + val optionsManager = new DspTesterOptionsManager { + interpreterOptions = interpreterOptions.copy( + blackBoxFactories = interpreterOptions.blackBoxFactories :+ new DspRealFactory) + } + + if (optionsManager.parse(args)) { + executeWithBitReduction(dutGenerator, optionsManager)(testerGen) + } + else { + optionsManager.parser.showUsageAsError() + false + } + } + + def executeFirrtlRepl[T <: Module](dutGenerator: () => T, optionsManager: ReplOptionsManager = new ReplOptionsManager): Boolean = { diff --git a/src/test/scala/examples/InstrumentingSpec.scala b/src/test/scala/examples/InstrumentingSpec.scala index ac3f970d..4c34ab72 100644 --- a/src/test/scala/examples/InstrumentingSpec.scala +++ b/src/test/scala/examples/InstrumentingSpec.scala @@ -41,14 +41,23 @@ class InstrumentingAdderTester[T<:Data:Ring](c: InstrumentingAdder[T]) extends D class InstrumentingAdderSpec extends FlatSpec with Matchers { + def getFixed: FixedPoint = FixedPoint(100.W, 16.BP) + behavior of "parameterized adder circuit on blackbox real" it should "run while being instrumented" in { - def getFixed: FixedPoint = FixedPoint(32.W, 16.BP) dsptools.Driver.execute(() => new InstrumentingAdder(getFixed _), Array("-fimbu", "-fimof", "signals.csv", "-fimhb", "16")) { c => new InstrumentingAdderTester(c) } should be (true) } + + it should "run twice with bits reduced in second run based on analysis of first run" in { + dsptools.Driver.executeWithBitReduction(() => new InstrumentingAdder(getFixed _), + Array("-fimbu", "-fimof", "signals.csv", "-fimhb", "16")) { c => +// Array.empty[String]) { c => + new InstrumentingAdderTester(c) + } should be (true) + } }