Skip to content

Commit

Permalink
This creates a toolchain that unites the machinery for Issue #114
Browse files Browse the repository at this point in the history
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

- <dut-name>.signal-bitsizes.csv
- <dut-name>.signal-bitsizes-2.csv
- <dut-name>.bit-reduced.fir
  • Loading branch information
chick committed Oct 30, 2017
1 parent f862ae1 commit ec6fbb7
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
119 changes: 116 additions & 3 deletions src/main/scala/dsptools/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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)
Expand All @@ -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 = {

Expand Down
11 changes: 10 additions & 1 deletion src/test/scala/examples/InstrumentingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ec6fbb7

Please sign in to comment.