diff --git a/src/main/scala/chisel3/simulator/package.scala b/src/main/scala/chisel3/simulator/package.scala index 30787ab144d..3732d44b934 100644 --- a/src/main/scala/chisel3/simulator/package.scala +++ b/src/main/scala/chisel3/simulator/package.scala @@ -78,10 +78,11 @@ package object simulator { elaboratedModule: ElaboratedModule[T], conservativeCommandResolution: Boolean = false, verbose: Boolean = false, + traceEnabled: Boolean = false, executionScriptLimit: Option[Int] = None )(body: SimulatedModule[T] => U ): U = { - simulation.run(conservativeCommandResolution, verbose, executionScriptLimit) { controller => + simulation.run(conservativeCommandResolution, verbose, traceEnabled, executionScriptLimit) { controller => val module = new SimulatedModule(elaboratedModule, controller) AnySimulatedModule.withValue(module) { body(module) diff --git a/svsim/src/main/scala/Backend.scala b/svsim/src/main/scala/Backend.scala index f4aed71828a..10affe01e21 100644 --- a/svsim/src/main/scala/Backend.scala +++ b/svsim/src/main/scala/Backend.scala @@ -36,6 +36,10 @@ object CommonCompilationSettings { /** Optimize for compilation speed, which generally means disabling as many optimizations as possible. */ object OptimizeForCompilationSpeed extends OptimizationStyle + + /** Optimize for execution speed, which generally means enabling as many optimizations as possible. + */ + object OptimizeForSimulationSpeed extends OptimizationStyle } sealed trait AvailableParallelism diff --git a/svsim/src/main/scala/Simulation.scala b/svsim/src/main/scala/Simulation.scala index cffb043287b..7286bd998e6 100644 --- a/svsim/src/main/scala/Simulation.scala +++ b/svsim/src/main/scala/Simulation.scala @@ -17,6 +17,7 @@ final class Simulation private[svsim] ( def run[T]( conservativeCommandResolution: Boolean = false, verbose: Boolean = false, + traceEnabled: Boolean = false, executionScriptLimit: Option[Int] = None )(body: Simulation.Controller => T ): T = { @@ -40,6 +41,10 @@ final class Simulation private[svsim] ( processBuilder.environment().put(pair._1, pair._2) } val process = processBuilder.start() + sys.addShutdownHook { + if (process.isAlive()) + process.destroyForcibly() + } val controller = new Simulation.Controller( new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), new BufferedReader(new InputStreamReader(process.getInputStream())), @@ -47,6 +52,9 @@ final class Simulation private[svsim] ( conservativeCommandResolution = conservativeCommandResolution, logMessagesAndCommands = verbose ) + if (traceEnabled) { + controller.setTraceEnabled(true) + } val bodyOutcome = Try { val result = body(controller) // Exceptions thrown from commands still in the queue when `body` returns should supercede returning `result` diff --git a/svsim/src/main/scala/Workspace.scala b/svsim/src/main/scala/Workspace.scala index f0f3344038c..0c6f5c72c69 100644 --- a/svsim/src/main/scala/Workspace.scala +++ b/svsim/src/main/scala/Workspace.scala @@ -278,10 +278,18 @@ final class Workspace( .flatMap(_.listFiles()) .map { file => workingDirectory.toPath().relativize(file.toPath()).toString() } + val traceFileStem = (backendSpecificSettings match { + case s: verilator.Backend.CompilationSettings => + s.traceStyle.collectFirst { + case verilator.Backend.CompilationSettings.TraceStyle.Vcd(_, filename: String) if filename.nonEmpty => + filename.stripSuffix(".vcd") + } + case _ => None + }).getOrElse(s"$workingDirectoryPath/trace") val simulationEnvironment = Seq( "SVSIM_SIMULATION_LOG" -> s"$workingDirectoryPath/simulation-log.txt", // The simulation driver appends the appropriate extension to the file path - "SVSIM_SIMULATION_TRACE" -> s"$workingDirectoryPath/trace" + "SVSIM_SIMULATION_TRACE" -> traceFileStem ) ++ parameters.simulationInvocation.environment // Emit Makefile for debugging (will be emitted even if compile fails) diff --git a/svsim/src/main/scala/vcs/Backend.scala b/svsim/src/main/scala/vcs/Backend.scala index 2cfc34ff0c1..43163c6b848 100644 --- a/svsim/src/main/scala/vcs/Backend.scala +++ b/svsim/src/main/scala/vcs/Backend.scala @@ -162,6 +162,7 @@ final class Backend( commonSettings.optimizationStyle match { case OptimizationStyle.Default => Seq() case OptimizationStyle.OptimizeForCompilationSpeed => Seq("-O0") + case OptimizationStyle.OptimizeForSimulationSpeed=> Seq("-O3") }, additionalHeaderPaths.map { path => s"-I${path}" }, diff --git a/svsim/src/main/scala/verilator/Backend.scala b/svsim/src/main/scala/verilator/Backend.scala index 8e2dc18419e..add25234482 100644 --- a/svsim/src/main/scala/verilator/Backend.scala +++ b/svsim/src/main/scala/verilator/Backend.scala @@ -10,7 +10,7 @@ object Backend { object CompilationSettings { sealed trait TraceStyle object TraceStyle { - case class Vcd(traceUnderscore: Boolean = false) extends TraceStyle + case class Vcd(traceUnderscore: Boolean = false, filename: String = "") extends TraceStyle } } @@ -55,6 +55,7 @@ final class Backend( "--cc", // "Create C++ output" "--exe", // "Link to create executable" "--build", // "Build model executable/library after Verilation" + "-j", "0", // Parallelism for --build-jobs/--verilate-jobs, when 0 uses all available cores "-o", s"../$outputBinaryName", // "Name of final executable" "--top-module", topModuleName, // "Name of top-level input module" "--Mdir", "verilated-sources", // "Name of output object directory" @@ -76,7 +77,7 @@ final class Backend( }, backendSpecificSettings.traceStyle match { - case Some(TraceStyle.Vcd(traceUnderscore)) => + case Some(TraceStyle.Vcd(traceUnderscore, _)) => if (traceUnderscore) { Seq("--trace", "--trace-underscore") } else { @@ -97,6 +98,8 @@ final class Backend( commonSettings.optimizationStyle match { case OptimizationStyle.Default => Seq() case OptimizationStyle.OptimizeForCompilationSpeed => Seq("-O1") + case OptimizationStyle.OptimizeForSimulationSpeed => + Seq("-O3", "--x-assign", "fast", "--x-initial", "fast") }, Seq[(String, Option[String])]( @@ -118,6 +121,8 @@ final class Backend( commonSettings.optimizationStyle match { case OptimizationStyle.Default => Seq() case OptimizationStyle.OptimizeForCompilationSpeed => Seq("-O1") + case OptimizationStyle.OptimizeForSimulationSpeed => + Seq("-O3", "-march=native", "-mtune=native") }, Seq("-std=c++14"),