1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
// SPDX-License-Identifier: Apache-2.0
package firrtl.annotations.transforms
import firrtl.Mappers._
import firrtl.analyses.InstanceKeyGraph
import firrtl.annotations.ModuleTarget
import firrtl.annotations.TargetToken.{fromDefModuleToTargetToken, Instance, OfModule}
import firrtl.annotations.analysis.DuplicationHelper
import firrtl.annotations._
import firrtl.ir._
import firrtl.{AnnotationSeq, CircuitState, DependencyAPIMigration, FirrtlInternalException, RenameMap, Transform}
import firrtl.renamemap.MutableRenameMap
import firrtl.stage.Forms
import firrtl.transforms.DedupedResult
import firrtl.transforms.DedupAnnotationsTransform
import scala.collection.mutable
/** Group of targets that should become local targets
* @param targets
*/
case class ResolvePaths(targets: Seq[CompleteTarget]) extends Annotation {
override def update(renames: RenameMap): Seq[Annotation] = {
val newTargets = targets.flatMap(t => renames.get(t).getOrElse(Seq(t)))
Seq(ResolvePaths(newTargets))
}
}
/** Holds the mapping from original module to the new, duplicated modules
* The original module target is unaffected by renaming
* @param newModules Instance target of what the original module now points to
* @param originalModule Original module
*/
case class DupedResult(newModules: Set[IsModule], originalModule: ModuleTarget) extends MultiTargetAnnotation {
override val targets: Seq[Seq[Target]] = Seq(newModules.toSeq)
override def duplicate(n: Seq[Seq[Target]]): Annotation = {
n.toList match {
case Seq(newMods) => DupedResult(newMods.collect { case x: IsModule => x }.toSet, originalModule)
case _ => DupedResult(Set.empty, originalModule)
}
}
}
case class NoSuchTargetException(message: String) extends FirrtlInternalException(message)
object EliminateTargetPaths {
@deprecated("Use version that accepts renamemap.MutableRenameMap", "FIRRTL 1.5")
def renameModules(c: Circuit, toRename: Map[String, String], renameMap: RenameMap): Circuit =
// Cast is safe because RenameMap is sealed trait, MutableRenameMap is only subclass
renameModules(c, toRename, renameMap.asInstanceOf[MutableRenameMap])
def renameModules(c: Circuit, toRename: Map[String, String], renameMap: MutableRenameMap): Circuit = {
val ct = CircuitTarget(c.main)
val cx = if (toRename.contains(c.main)) {
renameMap.record(ct, CircuitTarget(toRename(c.main)))
c.copy(main = toRename(c.main))
} else {
c
}
def onMod(m: DefModule): DefModule = {
m.map(onStmt) match {
case e: ExtModule if toRename.contains(e.name) =>
renameMap.record(ct.module(e.name), ct.module(toRename(e.name)))
e.copy(name = toRename(e.name))
case e: Module if toRename.contains(e.name) =>
renameMap.record(ct.module(e.name), ct.module(toRename(e.name)))
e.copy(name = toRename(e.name))
case o => o
}
}
def onStmt(s: Statement): Statement = s.map(onStmt) match {
case w @ DefInstance(info, name, module, _) if toRename.contains(module) => w.copy(module = toRename(module))
case other => other
}
cx.map(onMod)
}
def reorderModules(c: Circuit, toReorder: Map[String, Double]): Circuit = {
val newOrderMap = c.modules.zipWithIndex.map {
case (m, _) if toReorder.contains(m.name) => m.name -> toReorder(m.name)
case (m, i) if c.modules.size > 1 => m.name -> i.toDouble / (c.modules.size - 1)
case (m, _) => m.name -> 1.0
}.toMap
val newOrder = c.modules.sortBy { m => newOrderMap(m.name) }
c.copy(modules = newOrder)
}
}
/** For a set of non-local targets, modify the instance/module hierarchy of the circuit such that
* the paths in each non-local target can be removed
*
* In other words, if targeting a specific instance of a module, duplicate that module with a unique name
* and instantiate the new module instead.
*
* Consumes [[ResolvePaths]]
*
* E.g. for non-local target A/b:B/c:C/d, rename the following
* A/b:B/c:C/d -> C_/d
* A/b:B/c:C -> B_/c:C_
* A/b:B -> A/b:B_
* B/x -> (B/x, B_/x) // where x is any reference in B
* C/x -> (C/x, C_/x) // where x is any reference in C
*/
class EliminateTargetPaths extends Transform with DependencyAPIMigration {
import EliminateTargetPaths._
override def prerequisites = Forms.MinimalHighForm
override def optionalPrerequisites = Seq.empty
override def optionalPrerequisiteOf = Seq.empty
override def invalidates(a: Transform) = a.isInstanceOf[DedupAnnotationsTransform]
/** Replaces old ofModules with new ofModules by calling dupMap methods
* Updates oldUsedOfModules, newUsedOfModules
* @param originalModule Original name of this module
* @param newModule New name of this module
* @param s
* @return
*/
private def onStmt(dupMap: DuplicationHelper)(originalModule: String, newModule: String)(s: Statement): Statement =
s match {
case d @ DefInstance(_, name, module, _) =>
val ofModule = dupMap.getNewOfModule(originalModule, newModule, Instance(name), OfModule(module)).value
d.copy(module = ofModule)
case other => other.map(onStmt(dupMap)(originalModule, newModule))
}
/** Returns a modified circuit and [[RenameMap]] containing the associated target remapping
* @param cir
* @param targets
* @return
*/
def run(cir: Circuit, targets: Seq[IsMember], iGraph: InstanceKeyGraph): (Circuit, RenameMap, AnnotationSeq) = {
val dupMap = DuplicationHelper(cir.modules.map(_.name).toSet)
// For each target, record its path and calculate the necessary modifications to circuit
targets.foreach { t => dupMap.expandHierarchy(t) }
// Contains new list of module declarations
val duplicatedModuleList = mutable.ArrayBuffer[DefModule]()
// Foreach module, calculate the unique names of its duplicates
// Then, update the ofModules of instances that it encapsulates
val ct = CircuitTarget(cir.main)
val annos = cir.modules.map { m =>
val newNames = dupMap.getDuplicates(m.name)
newNames.foreach { newName =>
val newM = m match {
case e: ExtModule => e.copy(name = newName)
case o: Module =>
o.copy(name = newName, body = onStmt(dupMap)(m.name, newName)(o.body))
}
duplicatedModuleList += newM
}
DupedResult(newNames.map(ct.module), ct.module(m.name))
}
val finalModuleList = duplicatedModuleList
lazy val finalModuleSet = finalModuleList.map { case a: DefModule => a.name }.toSet
// Records how targets have been renamed
val renameMap = MutableRenameMap()
/* Foreach target, calculate the pathless version and only rename targets that are instantiated. Additionally, rename
* module targets
*/
def addRecord(old: IsMember, newPathless: IsMember): Unit = old match {
case x: ModuleTarget =>
renameMap.record(x, newPathless)
case x: IsComponent if x.path.isEmpty =>
renameMap.record(x, newPathless)
case x: IsComponent =>
renameMap.record(x, newPathless)
addRecord(x.stripHierarchy(1), newPathless)
}
val duplicatedParents = mutable.Set[OfModule]()
targets.foreach { t =>
val newTs = dupMap.makePathless(t)
val path = t.asPath
if (path.nonEmpty) { duplicatedParents += path(0)._2 }
newTs.toList match {
case Seq(pathless) =>
val mt = Target.referringModule(pathless)
addRecord(t, pathless)
renameMap.record(Target.referringModule(t), mt)
case _ =>
}
}
def addSelfRecord(mod: IsModule): Unit = mod match {
case m: ModuleTarget =>
case i: InstanceTarget if renameMap.underlying.contains(i) =>
case i: InstanceTarget =>
renameMap.record(i, i)
addSelfRecord(i.stripHierarchy(1))
}
val topMod = ModuleTarget(cir.main, cir.main)
duplicatedParents.foreach { parent =>
val paths = iGraph.findInstancesInHierarchy(parent.value)
val newTargets = paths.map { path =>
path.tail.foldLeft(topMod: IsModule) {
case (mod, wDefInst) =>
mod.instOf(wDefInst.name, wDefInst.module)
}
}
newTargets.foreach(addSelfRecord(_))
}
// Return modified circuit and associated renameMap
(cir.copy(modules = finalModuleList.toSeq), renameMap, annos)
}
override def execute(state: CircuitState): CircuitState = {
val moduleNames = state.circuit.modules.map(_.name).toSet
val (remainingAnnotations, targetsToEliminate, previouslyDeduped) =
state.annotations.foldLeft(
(Vector.empty[Annotation], Seq.empty[CompleteTarget], Map.empty[IsModule, (ModuleTarget, Double)])
) {
case ((remainingAnnos, targets, dedupedResult), anno) =>
anno match {
case ResolvePaths(ts) =>
(remainingAnnos, ts ++ targets, dedupedResult)
case DedupedResult(orig, dups, idx) if dups.nonEmpty =>
(remainingAnnos, targets, dedupedResult ++ dups.map(_ -> (orig, idx)).toMap)
case other =>
(remainingAnnos :+ other, targets, dedupedResult)
}
}
// Collect targets that are not local
val targets = targetsToEliminate.collect { case x: IsMember => x }
// Check validity of paths in targets
val iGraph = InstanceKeyGraph(state.circuit)
val instanceOfModules = iGraph.getChildInstances.map { case (k, v) => k -> v.map(_.toTokens) }.toMap
val targetsWithInvalidPaths = mutable.ArrayBuffer[IsMember]()
targets.foreach { t =>
val path = t match {
case _: ModuleTarget => Nil
case i: InstanceTarget => i.asPath
case r: ReferenceTarget => r.path
}
path.foldLeft(t.module) {
case (module, (inst: Instance, of: OfModule)) =>
val childrenOpt = instanceOfModules.get(module)
if (childrenOpt.isEmpty || !childrenOpt.get.contains((inst, of))) {
targetsWithInvalidPaths += t
}
of.value
}
}
if (targetsWithInvalidPaths.nonEmpty) {
val string = targetsWithInvalidPaths.mkString(",")
throw NoSuchTargetException(s"""Some targets have illegal paths that cannot be resolved/eliminated: $string""")
}
/* get rid of path prefixes of modules with only one instance so we don't rename them
* If instance targeted is in fact the only instance of a module, then it should not be renamed
* E.g. if Eliminate Target Paths on ~Top|Top/foo:Foo, but that is the only instance of Foo, then should return
* ~Top|Top/foo:Foo, not ~Top|Top/foo:Foo___Top_foo
*/
val isSingleInstMod: String => Boolean = {
val cache = mutable.Map.empty[String, Boolean]
mod => cache.getOrElseUpdate(mod, iGraph.findInstancesInHierarchy(mod).size == 1)
}
val firstRenameMap = MutableRenameMap()
val nonSingletonTargets = targets.foldRight(Seq.empty[IsMember]) {
case (t: IsComponent, acc) if t.asPath.nonEmpty =>
val origPath = t.asPath
val (singletonPrefix, rest) = origPath.partition {
case (_, OfModule(mod)) =>
isSingleInstMod(mod)
}
if (singletonPrefix.size > 0) {
val module = singletonPrefix.last._2.value
val circuit = t.circuit
val newIsModule =
if (rest.isEmpty) {
ModuleTarget(circuit, module)
} else {
val (Instance(inst), OfModule(ofMod)) = rest.last
val path = rest.dropRight(1)
InstanceTarget(circuit, module, path, inst, ofMod)
}
val newTarget = t match {
case r: ReferenceTarget => r.setPathTarget(newIsModule)
case i: InstanceTarget => newIsModule
}
firstRenameMap.record(t, Seq(newTarget))
newTarget +: acc
} else {
t +: acc
}
case (t, acc) => t +: acc
}
val (newCircuit, nextRenameMap, newAnnos) = run(state.circuit, nonSingletonTargets, iGraph)
val renameMap: MutableRenameMap =
if (firstRenameMap.hasChanges) {
// Cast is safe because RenameMap is sealed trait, MutableRenameMap is only subclass
firstRenameMap.andThen(nextRenameMap).asInstanceOf[MutableRenameMap]
} else {
// Cast is safe because RenameMap is sealed trait, MutableRenameMap is only subclass
nextRenameMap.asInstanceOf[MutableRenameMap]
}
val iGraphx = InstanceKeyGraph(newCircuit)
val newlyUnreachableModules = iGraphx.unreachableModules.toSet.diff(iGraph.unreachableModules.toSet)
val newCircuitGC = {
val modulesx = newCircuit.modules.flatMap {
case dead if newlyUnreachableModules(dead.OfModule) => None
case live =>
val m = CircuitTarget(newCircuit.main).module(live.name)
renameMap.get(m).foreach(_ => renameMap.record(m, m))
Some(live)
}
newCircuit.copy(modules = modulesx)
}
val renamedModuleMap = RenameMap()
// If previous instance target mapped to a single previously deduped module, return original name
// E.g. if previously ~Top|Top/foo:Foo was deduped to ~Top|Top/foo:Bar, then
// Eliminate target paths on ~Top|Top/foo:Bar should rename to ~Top|Top/foo:Foo, not
// ~Top|Top/foo:Bar___Top_foo
val newModuleNameMapping = previouslyDeduped.flatMap {
case (current: IsModule, (orig: ModuleTarget, idx)) =>
renameMap.get(current).collect { case Seq(ModuleTarget(_, m)) => m -> orig.name }
}
val renamedCircuit = renameModules(newCircuitGC, newModuleNameMapping, renamedModuleMap)
val reorderedCircuit = reorderModules(
renamedCircuit,
previouslyDeduped.map {
case (current: IsModule, (orig: ModuleTarget, idx)) =>
orig.name -> idx
}
)
state.copy(
circuit = reorderedCircuit,
renames = Some(renameMap.andThen(renamedModuleMap)),
annotations = remainingAnnotations ++ newAnnos
)
}
}
|