Skip to content

Commit 767b9eb

Browse files
authored
Add option to suppress emission of source locators (#5053)
1 parent c06598f commit 767b9eb

File tree

12 files changed

+114
-49
lines changed

12 files changed

+114
-49
lines changed

core/src/main/scala/chisel3/experimental/hierarchy/core/Definition.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ object Definition extends SourceInfoDoc {
120120
context.definitions,
121121
context.contextCache,
122122
context.layerMap,
123-
context.inlineTestIncluder
123+
context.inlineTestIncluder,
124+
context.suppressSourceInfo
124125
)
125126
}
126127
dynamicContext.inDefinition = true

core/src/main/scala/chisel3/internal/Builder.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ private[chisel3] class DynamicContext(
476476
val definitions: ArrayBuffer[Definition[_]],
477477
val contextCache: BuilderContextCache,
478478
val layerMap: Map[layer.Layer, layer.Layer],
479-
val inlineTestIncluder: InlineTestIncluder
479+
val inlineTestIncluder: InlineTestIncluder,
480+
val suppressSourceInfo: Boolean
480481
) {
481482
val importedDefinitionAnnos = annotationSeq.collect { case a: ImportDefinitionAnnotation[_] => a }
482483

@@ -1122,7 +1123,8 @@ private[chisel3] object Builder extends LazyLogging {
11221123
makeViewRenameMap(circuitName = components.last.name),
11231124
typeAliases,
11241125
layerAdjacencyList(layer.Layer.Root).map(foldLayers).toSeq,
1125-
optionDefs
1126+
optionDefs,
1127+
dynamicContext.suppressSourceInfo
11261128
)
11271129
(ElaboratedCircuit(circuit, dynamicContext.annotationSeq.toSeq), mod)
11281130
}

core/src/main/scala/chisel3/internal/firrtl/IR.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,14 @@ private[chisel3] object ir {
600600
case class DefClass(id: Class, name: String, ports: Seq[Port], block: Block) extends Component
601601

602602
case class Circuit(
603-
name: String,
604-
components: Seq[Component],
605-
annotations: Seq[() => Seq[Annotation]],
606-
renames: RenameMap,
607-
typeAliases: Seq[DefTypeAlias],
608-
layers: Seq[Layer],
609-
options: Seq[DefOption]
603+
name: String,
604+
components: Seq[Component],
605+
annotations: Seq[() => Seq[Annotation]],
606+
renames: RenameMap,
607+
typeAliases: Seq[DefTypeAlias],
608+
layers: Seq[Layer],
609+
options: Seq[DefOption],
610+
suppressSourceInfo: Boolean
610611
) {
611612
def firrtlAnnotations: Iterable[Annotation] = annotations.flatMap(_().flatMap(_.update(renames)))
612613
}

core/src/main/scala/chisel3/internal/firrtl/Serializer.scala

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ private[chisel3] object Serializer {
5656

5757
// TODO use makeMessage to get ':' filename col separator instead of space
5858
// Can we optimize the escaping?
59-
private def serialize(info: SourceInfo)(implicit b: StringBuilder): Unit = info match {
60-
case _: NoSourceInfo => ()
61-
case sl: SourceLine =>
62-
b ++= " @["; b ++= fir.FileInfo.fromUnescaped(sl.serialize).escaped; b ++= "]"
63-
}
59+
private def serialize(info: SourceInfo)(implicit b: StringBuilder, suppressSourceInfo: Boolean): Unit =
60+
info match {
61+
case sl: SourceLine if !suppressSourceInfo =>
62+
b ++= " @["; b ++= fir.FileInfo.fromUnescaped(sl.serialize).escaped; b ++= "]"
63+
case _ => ()
64+
}
6465

6566
private def reportInternalError(msg: String): Nothing = {
6667
val link = "https://github.com/chipsalliance/chisel/issues/new"
@@ -164,7 +165,7 @@ private[chisel3] object Serializer {
164165
args: Seq[Arg],
165166
params: Seq[(String, Param)],
166167
typeAliases: Seq[String]
167-
)(implicit b: StringBuilder): Unit = {
168+
)(implicit b: StringBuilder, suppressSourceInfo: Boolean): Unit = {
168169
if (name.nonEmpty) {
169170
b ++= "node "; b ++= legalize(name.get); b ++= " = "
170171
}
@@ -192,8 +193,9 @@ private[chisel3] object Serializer {
192193

193194
/** Serialize Commands */
194195
private def serializeSimpleCommand(cmd: Command, ctx: Component, typeAliases: Seq[String])(
195-
implicit b: StringBuilder,
196-
indent: Int
196+
implicit b: StringBuilder,
197+
indent: Int,
198+
suppressSourceInfo: Boolean
197199
): Unit = cmd match {
198200
case e: DefPrim[_] =>
199201
b ++= "node "; b ++= legalize(e.name); b ++= " = ";
@@ -321,8 +323,9 @@ private[chisel3] object Serializer {
321323
}
322324

323325
private def serializeCommand(cmd: Command, ctx: Component, typeAliases: Seq[String])(
324-
implicit indent: Int
325-
): Iterator[String] =
326+
implicit indent: Int,
327+
suppressSourceInfo: Boolean
328+
): Iterator[String] = {
326329
cmd match {
327330
case When(info, pred, ifRegion, elseRegion) =>
328331
val start = {
@@ -338,13 +341,15 @@ private[chisel3] object Serializer {
338341
newLineNoIndent()
339342
Iterator(b.toString)
340343
} else {
341-
ifRegion.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1))
344+
ifRegion.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1, suppressSourceInfo))
342345
}
343346
val end = if (elseRegion.nonEmpty) {
344347
implicit val b = new StringBuilder
345348
doIndent(); b ++= "else :"
346349
newLineNoIndent()
347-
Iterator(b.toString) ++ elseRegion.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1))
350+
Iterator(b.toString) ++ elseRegion.flatMap(
351+
serializeCommand(_, ctx, typeAliases)(indent + 1, suppressSourceInfo)
352+
)
348353
} else Iterator.empty
349354
start ++ middle ++ end
350355
case LayerBlock(info, layer, region) =>
@@ -354,15 +359,15 @@ private[chisel3] object Serializer {
354359
newLineNoIndent()
355360
Iterator(b.toString)
356361
}
357-
start ++ region.iterator.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1))
362+
start ++ region.iterator.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1, suppressSourceInfo))
358363
case Placeholder(_, block) =>
359364
if (block.isEmpty) {
360365
implicit val b = new StringBuilder
361366
doIndent(); b ++= "skip"
362367
newLineNoIndent()
363368
Iterator(b.toString)
364369
} else {
365-
block.iterator.flatMap(serializeCommand(_, ctx, typeAliases))
370+
block.iterator.flatMap(serializeCommand(_, ctx, typeAliases)(indent, suppressSourceInfo))
366371
}
367372
case cmd @ DefContract(info, names, exprs) =>
368373
val start = {
@@ -382,7 +387,9 @@ private[chisel3] object Serializer {
382387
newLineNoIndent()
383388
Iterator(b.toString)
384389
}
385-
start ++ cmd.region.getAllCommands().flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1))
390+
start ++ cmd.region
391+
.getAllCommands()
392+
.flatMap(serializeCommand(_, ctx, typeAliases)(indent + 1, suppressSourceInfo))
386393
// TODO can we avoid checking 4 less common Commands every single time?
387394
case simple =>
388395
// TODO avoid Iterator boxing for every simple command
@@ -392,6 +399,7 @@ private[chisel3] object Serializer {
392399
newLineNoIndent()
393400
Iterator(b.toString)
394401
}
402+
}
395403

396404
/** Serialize Chisel IR Block into FIRRTL Statements
397405
*
@@ -403,7 +411,8 @@ private[chisel3] object Serializer {
403411
* @return Iterator[String] of the equivalent FIRRTL text
404412
*/
405413
private def serialize(block: Block, ctx: Component, typeAliases: Seq[String])(
406-
implicit indent: Int
414+
implicit indent: Int,
415+
suppressSourceInfo: Boolean
407416
): Iterator[String] = {
408417
val commands = block.getCommands()
409418
val secretCommands = block.getSecretCommands()
@@ -414,7 +423,7 @@ private[chisel3] object Serializer {
414423
return Iterator(b.toString)
415424
} else {
416425
Iterator.empty[String] ++ (commands.iterator ++ secretCommands).flatMap(c =>
417-
serializeCommand(c, ctx, typeAliases)
426+
serializeCommand(c, ctx, typeAliases)(indent, suppressSourceInfo)
418427
)
419428
}
420429
}
@@ -547,7 +556,7 @@ private[chisel3] object Serializer {
547556
port: Port,
548557
typeAliases: Seq[String],
549558
topDir: SpecifiedDirection = SpecifiedDirection.Unspecified
550-
)(implicit b: StringBuilder, indent: Int): Unit = {
559+
)(implicit b: StringBuilder, indent: Int, suppressSourceInfo: Boolean): Unit = {
551560
val resolvedDir = SpecifiedDirection.fromParent(topDir, firrtlUserDirOf(port.id))
552561
val dir = resolvedDir match {
553562
case SpecifiedDirection.Unspecified | SpecifiedDirection.Output => "output"
@@ -565,7 +574,10 @@ private[chisel3] object Serializer {
565574
}
566575

567576
// TODO what is typeAliases for? Should it be a Set?
568-
private def serialize(component: Component, typeAliases: Seq[String])(implicit indent: Int): Iterator[String] =
577+
private def serialize(component: Component, typeAliases: Seq[String])(
578+
implicit indent: Int,
579+
suppressSourceInfo: Boolean
580+
): Iterator[String] = {
569581
component match {
570582
case ctx @ DefModule(id, name, public, layers, ports, block) =>
571583
val start = {
@@ -581,7 +593,7 @@ private[chisel3] object Serializer {
581593
newLineNoIndent() // newline for body, serialize(body) will indent
582594
b.toString
583595
}
584-
Iterator(start) ++ serialize(block, ctx, typeAliases)(indent + 1)
596+
Iterator(start) ++ serialize(block, ctx, typeAliases)(indent + 1, suppressSourceInfo)
585597

586598
case ctx @ DefBlackBox(id, name, ports, topDir, params, knownLayers) =>
587599
implicit val b = new StringBuilder
@@ -616,7 +628,7 @@ private[chisel3] object Serializer {
616628
newLineNoIndent() // newline for body, serialize(body) will indent
617629
b.toString
618630
}
619-
Iterator(start) ++ serialize(block, ctx, typeAliases)(indent + 1)
631+
Iterator(start) ++ serialize(block, ctx, typeAliases)(indent + 1, suppressSourceInfo)
620632

621633
case ctx @ DefFormalTest(name, module, params, sourceInfo) =>
622634
implicit val b = new StringBuilder
@@ -627,8 +639,9 @@ private[chisel3] object Serializer {
627639
}
628640
Iterator(b.toString)
629641
}
642+
}
630643

631-
private def serialize(layer: Layer)(implicit b: StringBuilder, indent: Int): Unit = {
644+
private def serialize(layer: Layer)(implicit b: StringBuilder, indent: Int, suppressSourceInfo: Boolean): Unit = {
632645
newLineAndIndent()
633646
b ++= "layer "
634647
b ++= layer.name
@@ -647,13 +660,13 @@ private[chisel3] object Serializer {
647660
}
648661
b ++= " :"
649662
serialize(layer.sourceInfo)
650-
layer.children.foreach(serialize(_)(b, indent + 1))
663+
layer.children.foreach(serialize(_)(b, indent + 1, suppressSourceInfo))
651664
}
652665

653-
private def serialize(layers: Seq[Layer])(implicit indent: Int): Iterator[String] = {
666+
private def serialize(layers: Seq[Layer])(implicit indent: Int, suppressSourceInfo: Boolean): Iterator[String] = {
654667
if (layers.nonEmpty) {
655668
implicit val b = new StringBuilder
656-
layers.foreach(serialize)
669+
layers.foreach(serialize(_)(b, indent, suppressSourceInfo))
657670
newLineNoIndent()
658671
Iterator(b.toString)
659672
} else Iterator.empty
@@ -667,7 +680,8 @@ private[chisel3] object Serializer {
667680

668681
// TODO make Annotation serialization lazy
669682
private def serialize(circuit: Circuit, annotations: Seq[Annotation]): Iterator[String] = {
670-
implicit val indent: Int = 0
683+
implicit val indent: Int = 0
684+
implicit val suppressSourceInfo: Boolean = circuit.suppressSourceInfo
671685
val prelude = {
672686
implicit val b = new StringBuilder
673687
b ++= s"FIRRTL version $version\n"
@@ -698,7 +712,7 @@ private[chisel3] object Serializer {
698712
b += NewLine
699713
Iterator(b.toString)
700714
} else Iterator.empty
701-
val layers = serialize(circuit.layers)(indent + 1)
715+
val layers = serialize(circuit.layers)(indent + 1, suppressSourceInfo)
702716
// TODO what is typeAliases for? Should it be a Set?
703717
val typeAliasesSeq: Seq[String] = circuit.typeAliases.map(_.name)
704718
prelude ++
@@ -707,7 +721,7 @@ private[chisel3] object Serializer {
707721
layers ++
708722
circuit.components.iterator.zipWithIndex.flatMap { case (m, i) =>
709723
val newline = Iterator(if (i == 0) s"$NewLine" else s"${NewLine}${NewLine}")
710-
newline ++ serialize(m, typeAliasesSeq)(indent + 1)
724+
newline ++ serialize(m, typeAliasesSeq)(indent + 1, suppressSourceInfo)
711725
} ++
712726
Iterator(s"$NewLine")
713727
}

release.mill

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import mill.scalalib._
77
import mill.scalalib.scalafmt._
88
import mill.scalalib.publish._
99
import mill.util.Jvm.createJar
10-
import com.github.lolgab.mill.mima.Mima
10+
import com.github.lolgab.mill.mima._
1111

1212
//import de.tobiasroeser.mill.vcs.version.VcsVersion
1313

@@ -48,6 +48,12 @@ trait Unipublish extends ScalaModule with ChiselPublishModule with Mima {
4848
os.read.lines(BuildCtx.workspaceRoot / "etc" / "previous-versions.txt")
4949
}
5050

51+
override def mimaBinaryIssueFilters = super.mimaBinaryIssueFilters() ++ Seq(
52+
// chisel3.internal.firrtl.ir is package private
53+
ProblemFilter.exclude[DirectMissingMethodProblem]("chisel3.internal.firrtl.ir*"),
54+
ProblemFilter.exclude[MissingTypesProblem]("chisel3.internal.firrtl.ir*")
55+
)
56+
5157
/** Publish both this project and the plugin (for the default Scala version) */
5258
override def publishLocal(
5359
localMvnRepo: String = null,

src/main/scala/chisel3/stage/ChiselAnnotations.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,26 @@ case object IncludeInlineTestsWithName extends HasShellOptions {
612612
)
613613
)
614614
}
615+
616+
/** Suppress emission of source info in FIRRTL output.
617+
*
618+
* Use as CLI option `--no-source-info`.
619+
*
620+
* When this option is enabled, source info (e.g., @[MyFile.scala 42:10]) will not be
621+
* emitted in the generated FIRRTL output. This can be useful for reducing output size or
622+
* for generating more stable output.
623+
*/
624+
case object SuppressSourceInfoAnnotation
625+
extends NoTargetAnnotation
626+
with ChiselOption
627+
with HasShellOptions
628+
with Unserializable {
629+
630+
val options = Seq(
631+
new ShellOption[Unit](
632+
longOption = "no-source-info",
633+
toAnnotationSeq = _ => Seq(SuppressSourceInfoAnnotation),
634+
helpText = "Suppress emission of source info in FIRRTL output"
635+
)
636+
)
637+
}

src/main/scala/chisel3/stage/ChiselOptions.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ChiselOptions private[stage] (
1919
val includeUtilMetadata: Boolean = false,
2020
val useSRAMBlackbox: Boolean = false,
2121
val elaboratedCircuit: Option[ElaboratedCircuit] = None,
22-
val inlineTestIncluder: InlineTestIncluder = InlineTestIncluder.none
22+
val inlineTestIncluder: InlineTestIncluder = InlineTestIncluder.none,
23+
val suppressSourceInfo: Boolean = false
2324
) {
2425

2526
private[stage] def copy(
@@ -33,7 +34,8 @@ class ChiselOptions private[stage] (
3334
includeUtilMetadata: Boolean = includeUtilMetadata,
3435
useSRAMBlackbox: Boolean = useSRAMBlackbox,
3536
elaboratedCircuit: Option[ElaboratedCircuit] = elaboratedCircuit,
36-
inlineTestIncluder: InlineTestIncluder = inlineTestIncluder
37+
inlineTestIncluder: InlineTestIncluder = inlineTestIncluder,
38+
suppressSourceInfo: Boolean = suppressSourceInfo
3739
): ChiselOptions = {
3840

3941
new ChiselOptions(
@@ -47,7 +49,8 @@ class ChiselOptions private[stage] (
4749
includeUtilMetadata = includeUtilMetadata,
4850
useSRAMBlackbox = useSRAMBlackbox,
4951
elaboratedCircuit = elaboratedCircuit,
50-
inlineTestIncluder = inlineTestIncluder
52+
inlineTestIncluder = inlineTestIncluder,
53+
suppressSourceInfo = suppressSourceInfo
5154
)
5255

5356
}

src/main/scala/chisel3/stage/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ package object stage {
3838
c.copy(inlineTestIncluder = c.inlineTestIncluder.includeModule(glob))
3939
case IncludeInlineTestsWithNameAnnotation(glob) =>
4040
c.copy(inlineTestIncluder = c.inlineTestIncluder.includeTest(glob))
41+
case SuppressSourceInfoAnnotation => c.copy(suppressSourceInfo = true)
4142
}
4243
}
4344

src/main/scala/chisel3/stage/phases/Elaborate.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class Elaborate extends Phase {
5454
ArrayBuffer[Definition[_]](),
5555
BuilderContextCache.empty,
5656
chiselOptions.layerMap,
57-
chiselOptions.inlineTestIncluder
57+
chiselOptions.inlineTestIncluder,
58+
chiselOptions.suppressSourceInfo
5859
)
5960
val (elaboratedCircuit, dut) = {
6061
Builder.build(Module(gen()), context)

src/main/scala/circt/stage/Shell.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import chisel3.stage.{
1313
PrintFullStackTraceAnnotation,
1414
RemapLayer,
1515
SourceRootAnnotation,
16+
SuppressSourceInfoAnnotation,
1617
ThrowOnFirstErrorAnnotation,
1718
UseLegacyWidthBehavior,
1819
UseSRAMBlackbox,
@@ -54,7 +55,8 @@ trait CLI extends BareShell { this: BareShell =>
5455
IncludeUtilMetadata,
5556
UseSRAMBlackbox,
5657
IncludeInlineTestsForModule,
57-
IncludeInlineTestsWithName
58+
IncludeInlineTestsWithName,
59+
SuppressSourceInfoAnnotation
5860
).foreach(_.addOptions(parser))
5961

6062
parser.note("CIRCT (MLIR FIRRTL Compiler) options")

0 commit comments

Comments
 (0)