Skip to content

Commit

Permalink
Emit .fir lazily, overcomes JVM 2 GiB String limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkoenig committed Aug 23, 2021
1 parent 211161b commit 62b8c1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/scala/chisel3/internal/firrtl/Emitter.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// SPDX-License-Identifier: Apache-2.0

package chisel3.internal.firrtl
import firrtl.{ir => fir}

import scala.collection.immutable.LazyList // Needed for 2.12 alias
import firrtl.ir.Serializer

private[chisel3] object Emitter {
def emit(circuit: Circuit): String = {
val fcircuit = Converter.convertLazily(circuit)
fir.Serializer.serialize(fcircuit)
Serializer.serialize(fcircuit)
}

def emitLazily(circuit: Circuit): Iterable[String] = {
val result = LazyList(s"circuit ${circuit.name} :\n")
val modules = circuit.components.view.map(Converter.convert)
val moduleStrings = modules.flatMap { m =>
Array(Serializer.serialize(m, 1), "\n\n")
}
result ++ moduleStrings
}
}

7 changes: 5 additions & 2 deletions src/main/scala/chisel3/stage/ChiselAnnotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ case class CircuitSerializationAnnotation(circuit: Circuit, filename: String, fo

protected def suffix: Option[String] = Some(format.extension)

// TODO Use lazy Iterables so that we don't have to materialize full intermediate data structures
override def getBytes: Iterable[Byte] = format match {
case FirrtlFileFormat => OldEmitter.emit(circuit).getBytes
case FirrtlFileFormat =>
OldEmitter.emitLazily(circuit)
.map(_.getBytes)
.flatten
// TODO Use lazy Iterables so that we don't have to materialize full intermediate data structures
case ProtoBufFileFormat =>
val ostream = new java.io.ByteArrayOutputStream
val modules = circuit.components.map(m => () => chisel3.internal.firrtl.Converter.convert(m))
Expand Down

0 comments on commit 62b8c1b

Please sign in to comment.