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
|
// See LICENSE for license details.
package firrtl.annotations.transforms
import firrtl.Mappers._
import firrtl.analyses.InstanceGraph
import firrtl.annotations.ModuleTarget
import firrtl.annotations.TargetToken.{Instance, OfModule, fromDefModuleToTargetToken}
import firrtl.annotations.analysis.DuplicationHelper
import firrtl.annotations._
import firrtl.ir._
import firrtl.{CircuitState, DependencyAPIMigration, FirrtlInternalException, RenameMap, Transform, WDefInstance}
import firrtl.options.PreservesAll
import firrtl.stage.Forms
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))
}
}
case class NoSuchTargetException(message: String) extends FirrtlInternalException(message)
/** 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 with PreservesAll[Transform] {
override def prerequisites = Forms.MinimalHighForm
override def optionalPrerequisites = Seq.empty
override def optionalPrerequisiteOf = Seq.empty
/** 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 d@WDefInstance(_, 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]): (Circuit, RenameMap) = {
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
cir.modules.foreach { m =>
dupMap.getDuplicates(m.name).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
}
}
val finalModuleList = duplicatedModuleList
lazy val finalModuleSet = finalModuleList.map{ case a: DefModule => a.name }.toSet
// Records how targets have been renamed
val renameMap = RenameMap()
/* Foreach target, calculate the pathless version and only rename targets that are instantiated. Additionally, rename
* module targets
*/
targets.foreach { t =>
val newTsx = dupMap.makePathless(t)
val newTs = newTsx
if(newTs.nonEmpty) {
renameMap.record(t, newTs)
val m = Target.referringModule(t)
val duplicatedModules = newTs.map(Target.referringModule)
val oldModule: Option[ModuleTarget] = m match {
case a: ModuleTarget if finalModuleSet(a.module) => Some(a)
case _ => None
}
renameMap.record(m, (duplicatedModules).distinct)
}
}
// Return modified circuit and associated renameMap
(cir.copy(modules = finalModuleList), renameMap)
}
override def execute(state: CircuitState): CircuitState = {
val (annotations, annotationsx) = state.annotations.partition{
case a: ResolvePaths => true
case _ => false
}
// Collect targets that are not local
val targets = annotations.map(_.asInstanceOf[ResolvePaths]).flatMap(_.targets.collect { case x: IsMember => x })
// Check validity of paths in targets
val iGraph = new InstanceGraph(state.circuit)
val instanceOfModules = iGraph.getChildrenInstanceOfModule
val targetsWithInvalidPaths = mutable.ArrayBuffer[IsMember]()
targets.foreach { t =>
val path = t match {
case m: 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
val isSingleInstMod: String => Boolean = {
val cache = mutable.Map.empty[String, Boolean]
mod => cache.getOrElseUpdate(mod, iGraph.findInstancesInHierarchy(mod).size == 1)
}
val firstRenameMap = RenameMap()
val nonSingletonTargets = targets.foldLeft(Seq.empty[IsMember]) {
case (acc, t: IsComponent) 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 (acc, t) => t +: acc
}
val (newCircuit, nextRenameMap) = run(state.circuit, nonSingletonTargets)
val renameMap =
if (firstRenameMap.hasChanges) {
firstRenameMap andThen nextRenameMap
} else {
nextRenameMap
}
val iGraphx = new InstanceGraph(newCircuit)
val newlyUnreachableModules = iGraphx.unreachableModules diff iGraph.unreachableModules
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)
}
state.copy(circuit = newCircuitGC, renames = Some(renameMap), annotations = annotationsx)
}
}
|