diff options
| author | chick | 2020-08-14 19:47:53 -0700 |
|---|---|---|
| committer | Jack Koenig | 2020-08-14 19:47:53 -0700 |
| commit | 6fc742bfaf5ee508a34189400a1a7dbffe3f1cac (patch) | |
| tree | 2ed103ee80b0fba613c88a66af854ae9952610ce /src/main/scala/firrtl/transforms/GroupComponents.scala | |
| parent | b516293f703c4de86397862fee1897aded2ae140 (diff) | |
All of src/ formatted with scalafmt
Diffstat (limited to 'src/main/scala/firrtl/transforms/GroupComponents.scala')
| -rw-r--r-- | src/main/scala/firrtl/transforms/GroupComponents.scala | 123 |
1 files changed, 68 insertions, 55 deletions
diff --git a/src/main/scala/firrtl/transforms/GroupComponents.scala b/src/main/scala/firrtl/transforms/GroupComponents.scala index 166feba0..0db67f1e 100644 --- a/src/main/scala/firrtl/transforms/GroupComponents.scala +++ b/src/main/scala/firrtl/transforms/GroupComponents.scala @@ -10,7 +10,6 @@ import firrtl.stage.Forms import scala.collection.mutable - /** * Specifies a group of components, within a module, to pull out into their own module * Components that are only connected to a group's components will also be included @@ -21,8 +20,14 @@ import scala.collection.mutable * @param outputSuffix suggested suffix of any output ports of the new module * @param inputSuffix suggested suffix of any input ports of the new module */ -case class GroupAnnotation(components: Seq[ComponentName], newModule: String, newInstance: String, outputSuffix: Option[String] = None, inputSuffix: Option[String] = None) extends Annotation { - if(components.nonEmpty) { +case class GroupAnnotation( + components: Seq[ComponentName], + newModule: String, + newInstance: String, + outputSuffix: Option[String] = None, + inputSuffix: Option[String] = None) + extends Annotation { + if (components.nonEmpty) { require(components.forall(_.module == components.head.module), "All components must be in the same module.") require(components.forall(!_.name.contains('.')), "No components can be a subcomponent.") } @@ -35,7 +40,7 @@ case class GroupAnnotation(components: Seq[ComponentName], newModule: String, ne /* Only keeps components renamed to components */ def update(renames: RenameMap): Seq[Annotation] = { - val newComponents = components.flatMap{c => renames.get(c).getOrElse(Seq(c))}.collect { + val newComponents = components.flatMap { c => renames.get(c).getOrElse(Seq(c)) }.collect { case c: ComponentName => c } Seq(GroupAnnotation(newComponents, newModule, newInstance, outputSuffix, inputSuffix)) @@ -58,7 +63,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { } override def execute(state: CircuitState): CircuitState = { - val groups = state.annotations.collect {case g: GroupAnnotation => g} + val groups = state.annotations.collect { case g: GroupAnnotation => g } val module2group = groups.groupBy(_.currentModule) val mnamespace = Namespace(state.circuit) val newModules = state.circuit.modules.flatMap { @@ -74,13 +79,12 @@ class GroupComponents extends Transform with DependencyAPIMigration { val namespace = Namespace(m) val groupRoots = groups.map(_.components.map(_.name)) val totalSum = groupRoots.map(_.size).sum - val union = groupRoots.foldLeft(Set.empty[String]){(all, set) => all.union(set.toSet)} + val union = groupRoots.foldLeft(Set.empty[String]) { (all, set) => all.union(set.toSet) } - require(groupRoots.forall{_.forall{namespace.contains}}, "All names should be in this module") + require(groupRoots.forall { _.forall { namespace.contains } }, "All names should be in this module") require(totalSum == union.size, "No name can be in more than one group") require(groupRoots.forall(_.nonEmpty), "All groupRoots must by non-empty") - // Order of groups, according to their label. The label is the first root in the group val labelOrder = groups.collect({ case g: GroupAnnotation => g.components.head.name }) @@ -90,8 +94,8 @@ class GroupComponents extends Transform with DependencyAPIMigration { // Group roots, by label // The label "" indicates the original module, and components belonging to that group will remain // in the original module (not get moved into a new module) - val label2group: Map[String, MSet[String]] = groups.collect{ - case GroupAnnotation(set, module, instance, _, _) => set.head.name -> mutable.Set(set.map(_.name):_*) + val label2group: Map[String, MSet[String]] = groups.collect { + case GroupAnnotation(set, module, instance, _, _) => set.head.name -> mutable.Set(set.map(_.name): _*) }.toMap + ("" -> mutable.Set("")) // Name of new module containing each group, by label @@ -105,7 +109,6 @@ class GroupComponents extends Transform with DependencyAPIMigration { // Build set of components not in set val notSet = label2group.map { case (key, value) => key -> union.diff(value) } - // Get all dependencies between components val deps = getComponentConnectivity(m) @@ -114,13 +117,14 @@ class GroupComponents extends Transform with DependencyAPIMigration { // For each group (by label), add connectivity between nodes in set // Populate reachableNodes with reachability, where blacklist is their notSet - label2group.foreach { case (label, set) => - set.foreach { x => - deps.addPairWithEdge(label, x) - } - deps.reachableFrom(label, notSet(label)) foreach { node => - reachableNodes.getOrElseUpdate(node, mutable.Set.empty[String]) += label - } + label2group.foreach { + case (label, set) => + set.foreach { x => + deps.addPairWithEdge(label, x) + } + deps.reachableFrom(label, notSet(label)).foreach { node => + reachableNodes.getOrElseUpdate(node, mutable.Set.empty[String]) += label + } } // Unused nodes are not reachable from any group nor the root--add them to root group @@ -129,12 +133,13 @@ class GroupComponents extends Transform with DependencyAPIMigration { } // Add nodes who are reached by a single group, to that group - reachableNodes.foreach { case (node, membership) => - if(membership.size == 1) { - label2group(membership.head) += node - } else { - label2group("") += node - } + reachableNodes.foreach { + case (node, membership) => + if (membership.size == 1) { + label2group(membership.head) += node + } else { + label2group("") += node + } } applyGrouping(m, labelOrder, label2group, label2module, label2instance, label2annotation) @@ -150,19 +155,21 @@ class GroupComponents extends Transform with DependencyAPIMigration { * @param label2annotation annotation specifying the group, by label * @return new modules, including each group's module and the new split module */ - def applyGrouping( m: Module, - labelOrder: Seq[String], - label2group: Map[String, MSet[String]], - label2module: Map[String, String], - label2instance: Map[String, String], - label2annotation: Map[String, GroupAnnotation] - ): Seq[Module] = { + def applyGrouping( + m: Module, + labelOrder: Seq[String], + label2group: Map[String, MSet[String]], + label2module: Map[String, String], + label2instance: Map[String, String], + label2annotation: Map[String, GroupAnnotation] + ): Seq[Module] = { // Maps node to group val byNode = mutable.HashMap[String, String]() - label2group.foreach { case (group, nodes) => - nodes.foreach { node => - byNode(node) = group - } + label2group.foreach { + case (group, nodes) => + nodes.foreach { node => + byNode(node) = group + } } val groupNamespace = label2group.map { case (head, set) => head -> Namespace(set.toSeq) } @@ -180,7 +187,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { val portNames = groupPortNames(group) val suffix = d match { case Output => label2annotation(group).outputSuffix.getOrElse("") - case Input => label2annotation(group).inputSuffix.getOrElse("") + case Input => label2annotation(group).inputSuffix.getOrElse("") } val newName = groupNamespace(group).newName(source + suffix) val portName = portNames.getOrElseUpdate(source, newName) @@ -192,7 +199,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { val portName = addPort(group, exp, Output) val connectStatement = exp.tpe match { case AnalogType(_) => Attach(NoInfo, Seq(WRef(portName), exp)) - case _ => Connect(NoInfo, WRef(portName), exp) + case _ => Connect(NoInfo, WRef(portName), exp) } groupStatements(group) += connectStatement portName @@ -201,7 +208,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { // Given the sink is in a group, tidy up source references def inGroupFixExps(group: String, added: mutable.ArrayBuffer[Statement])(e: Expression): Expression = e match { case _: Literal => e - case _: DoPrim | _: Mux | _: ValidIf => e map inGroupFixExps(group, added) + case _: DoPrim | _: Mux | _: ValidIf => e.map(inGroupFixExps(group, added)) case otherExp: Expression => val wref = getWRef(otherExp) val source = wref.name @@ -238,10 +245,10 @@ class GroupComponents extends Transform with DependencyAPIMigration { // Given the sink is in the parent module, tidy up source references belonging to groups def inTopFixExps(e: Expression): Expression = e match { - case _: DoPrim | _: Mux | _: ValidIf => e map inTopFixExps + case _: DoPrim | _: Mux | _: ValidIf => e.map(inTopFixExps) case otherExp: Expression => val wref = getWRef(otherExp) - if(byNode(wref.name) != "") { + if (byNode(wref.name) != "") { // Get the name of source's group val otherGroup = byNode(wref.name) @@ -260,7 +267,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { case r: IsDeclaration if byNode(r.name) != "" => val topStmts = mutable.ArrayBuffer[Statement]() val group = byNode(r.name) - groupStatements(group) += r mapExpr inGroupFixExps(group, topStmts) + groupStatements(group) += r.mapExpr(inGroupFixExps(group, topStmts)) Block(topStmts.toSeq) case c: Connect if byNode(getWRef(c.loc).name) != "" => // Sink is in a group @@ -276,20 +283,26 @@ class GroupComponents extends Transform with DependencyAPIMigration { // TODO Attach if all are in a group? case _: IsDeclaration | _: Connect | _: Attach => // Sink is in Top - val ret = s mapExpr inTopFixExps + val ret = s.mapExpr(inTopFixExps) ret - case other => other map onStmt + case other => other.map(onStmt) } } - // Build datastructures - val newTopBody = Block(labelOrder.map(g => WDefInstance(NoInfo, label2instance(g), label2module(g), UnknownType)) ++ Seq(onStmt(m.body))) + val newTopBody = Block( + labelOrder.map(g => WDefInstance(NoInfo, label2instance(g), label2module(g), UnknownType)) ++ Seq(onStmt(m.body)) + ) val finalTopBody = Block(Utils.squashEmpty(newTopBody).asInstanceOf[Block].stmts.distinct) // For all group labels (not including the original module label), return a new Module. - val newModules = labelOrder.filter(_ != "") map { group => - Module(NoInfo, label2module(group), groupPorts(group).distinct.toSeq, Block(groupStatements(group).distinct.toSeq)) + val newModules = labelOrder.filter(_ != "").map { group => + Module( + NoInfo, + label2module(group), + groupPorts(group).distinct.toSeq, + Block(groupStatements(group).distinct.toSeq) + ) } Seq(m.copy(body = finalTopBody)) ++ newModules } @@ -298,7 +311,7 @@ class GroupComponents extends Transform with DependencyAPIMigration { case w: WRef => w case other => var w = WRef("") - other mapExpr { e => w = getWRef(e); e} + other.mapExpr { e => w = getWRef(e); e } w } @@ -317,25 +330,25 @@ class GroupComponents extends Transform with DependencyAPIMigration { bidirGraph.addPairWithEdge(sink.name, name) bidirGraph.addPairWithEdge(name, sink.name) w - case other => other map onExpr(sink) + case other => other.map(onExpr(sink)) } def onStmt(stmt: Statement): Unit = stmt match { case w: WDefInstance => case h: IsDeclaration => bidirGraph.addVertex(h.name) - h map onExpr(WRef(h.name)) + h.map(onExpr(WRef(h.name))) case Attach(_, exprs) => // Add edge between each expression - exprs.tail map onExpr(getWRef(exprs.head)) + exprs.tail.map(onExpr(getWRef(exprs.head))) case Connect(_, loc, expr) => onExpr(getWRef(loc))(expr) - case q @ Stop(_,_, clk, en) => + case q @ Stop(_, _, clk, en) => val simName = simNamespace.newTemp simulations(simName) = q - Seq(clk, en) map onExpr(WRef(simName)) + Seq(clk, en).map(onExpr(WRef(simName))) case q @ Print(_, _, args, clk, en) => val simName = simNamespace.newTemp simulations(simName) = q - (args :+ clk :+ en) map onExpr(WRef(simName)) + (args :+ clk :+ en).map(onExpr(WRef(simName))) case Block(stmts) => stmts.foreach(onStmt) case ignore @ (_: IsInvalid | EmptyStmt) => // do nothing case other => throw new Exception(s"Unexpected Statement $other") @@ -358,7 +371,7 @@ class GroupAndDedup extends GroupComponents { override def invalidates(a: Transform): Boolean = a match { case _: DedupModules => true - case _ => super.invalidates(a) + case _ => super.invalidates(a) } } |
