Skip to content

Commit e8edc8f

Browse files
authored
Add ChiselStage.emitCHIRRTLFile (#4232)
Emits a file without returning the serialized object which is more memory efficient and supports > 2 GiB of serialized FIRRTL text.
1 parent 219a237 commit e8edc8f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/main/scala/circt/stage/ChiselStage.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ object ChiselStage {
9191
circuitAnno.get.emitLazily(inFileAnnos).mkString
9292
}
9393

94+
/** Elaborates a Chisel circuit and emits it to a file
95+
*
96+
* @param gen a call-by-name Chisel module
97+
* @param args additional command line arguments to pass to Chisel
98+
*/
99+
def emitCHIRRTLFile(
100+
gen: => RawModule,
101+
args: Array[String] = Array.empty
102+
): AnnotationSeq = {
103+
(new circt.stage.ChiselStage).execute(
104+
Array("--target", "chirrtl") ++ args,
105+
Seq(ChiselGeneratorAnnotation(() => gen))
106+
)
107+
}
108+
94109
/** Return a CHIRRTL circuit for a Chisel module
95110
*
96111
* @param gen a call-by-name Chisel module
@@ -186,7 +201,7 @@ object ChiselStage {
186201
gen: => RawModule,
187202
args: Array[String] = Array.empty,
188203
firtoolOpts: Array[String] = Array.empty
189-
) =
204+
): AnnotationSeq =
190205
(new circt.stage.ChiselStage).execute(
191206
Array("--target", "systemverilog") ++ args,
192207
Seq(ChiselGeneratorAnnotation(() => gen)) ++ firtoolOpts.map(FirtoolOption(_))

src/test/scala/circtTests/stage/ChiselStageSpec.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package circtTests.stage
44

5-
import chisel3.stage.ChiselGeneratorAnnotation
5+
import chisel3.stage.{ChiselGeneratorAnnotation, CircuitSerializationAnnotation}
66
import chisel3.experimental.SourceLine
77

88
import circt.stage.{ChiselStage, FirtoolOption, PreserveAggregate}
@@ -1060,6 +1060,30 @@ class ChiselStageSpec extends AnyFunSpec with Matchers with chiselTests.Utils {
10601060

10611061
}
10621062

1063+
it("should emit CHIRRTL files") {
1064+
val targetDir = new File("test_run_dir/ChiselStageSpec/emitCHIRRTLFile")
1065+
1066+
val args: Array[String] = Array(
1067+
"--target-dir",
1068+
targetDir.toString
1069+
)
1070+
1071+
// Should we be returning the CircuitSerializationAnnotation? It's consistent with emitSystemVerilogFile to do so.
1072+
ChiselStage
1073+
.emitCHIRRTLFile(
1074+
new ChiselStageSpec.Bar,
1075+
args
1076+
)
1077+
.collectFirst {
1078+
case CircuitSerializationAnnotation(_, filename, _) => filename
1079+
}
1080+
.get should be("Bar")
1081+
1082+
val expectedOutput = new File(targetDir, "Bar.fir")
1083+
expectedOutput should (exist)
1084+
info(s"'$expectedOutput' exists")
1085+
}
1086+
10631087
it("should emit FIRRTL dialect") {
10641088

10651089
ChiselStage.emitFIRRTLDialect(new ChiselStageSpec.Foo) should include(" firrtl.module")

0 commit comments

Comments
 (0)