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
|
// See LICENSE for license details.
package firrtlTests
import firrtl.RenameMap
import firrtl.FIRRTLException
import firrtl.RenameMap.{CircularRenameException, IllegalRenameException}
import firrtl.annotations._
class RenameMapSpec extends FirrtlFlatSpec {
val cir = CircuitTarget("Top")
val cir2 = CircuitTarget("Pot")
val cir3 = CircuitTarget("Cir3")
val modA = cir.module("A")
val modA2 = cir2.module("A")
val modB = cir.module("B")
val foo = modA.ref("foo")
val foo2 = modA2.ref("foo")
val bar = modA.ref("bar")
val fizz = modA.ref("fizz")
val fooB = modB.ref("foo")
val barB = modB.ref("bar")
val tmb = cir.module("Top").instOf("mid", "Middle").instOf("bot", "Bottom")
val tm2b = cir.module("Top").instOf("mid", "Middle2").instOf("bot", "Bottom")
val middle = cir.module("Middle")
val middle2 = cir.module("Middle2")
behavior of "RenameMap"
it should "return None if it does not rename something" in {
val renames = RenameMap()
renames.get(modA) should be (None)
renames.get(foo) should be (None)
}
it should "return a Seq of renamed things if it does rename something" in {
val renames = RenameMap()
renames.record(foo, bar)
renames.get(foo) should be (Some(Seq(bar)))
}
it should "allow something to be renamed to multiple things" in {
val renames = RenameMap()
renames.record(foo, bar)
renames.record(foo, fizz)
renames.get(foo) should be (Some(Seq(bar, fizz)))
}
it should "allow something to be renamed to nothing (ie. deleted)" in {
val renames = RenameMap()
renames.record(foo, Seq())
renames.get(foo) should be (Some(Seq()))
}
it should "return None if something is renamed to itself" in {
val renames = RenameMap()
renames.record(foo, foo)
renames.get(foo) should be (None)
}
it should "allow targets to change module" in {
val renames = RenameMap()
renames.record(foo, fooB)
renames.get(foo) should be (Some(Seq(fooB)))
}
it should "rename targets if their module is renamed" in {
val renames = RenameMap()
renames.record(modA, modB)
renames.get(foo) should be (Some(Seq(fooB)))
renames.get(bar) should be (Some(Seq(barB)))
}
it should "rename renamed targets if the module of the target is renamed" in {
val renames = RenameMap()
renames.record(modA, modB)
renames.record(foo, bar)
renames.get(foo) should be (Some(Seq(barB)))
}
it should "rename modules if their circuit is renamed" in {
val renames = RenameMap()
renames.record(cir, cir2)
renames.get(modA) should be (Some(Seq(modA2)))
}
it should "rename targets if their circuit is renamed" in {
val renames = RenameMap()
renames.record(cir, cir2)
renames.get(foo) should be (Some(Seq(foo2)))
}
val TopCircuit = cir
val Top = cir.module("Top")
val Top_m = Top.instOf("m", "Middle")
val Top_m_l = Top_m.instOf("l", "Leaf")
val Top_m_l_a = Top_m_l.ref("a")
val Top_m_la = Top_m.ref("l").field("a")
val Middle = cir.module("Middle")
val Middle2 = cir.module("Middle2")
val Middle_la = Middle.ref("l").field("a")
val Middle_l_a = Middle.instOf("l", "Leaf").ref("a")
it should "rename targets if modules in the path are renamed" in {
val renames = RenameMap()
renames.record(Middle, Middle2)
renames.get(Top_m) should be (Some(Seq(Top.instOf("m", "Middle2"))))
}
it should "rename targets if instance and module in the path are renamed" in {
val renames = RenameMap()
renames.record(Middle, Middle2)
renames.record(Top.ref("m"), Top.ref("m2"))
renames.get(Top_m) should be (Some(Seq(Top.instOf("m2", "Middle2"))))
}
it should "rename targets if instance in the path are renamed" in {
val renames = RenameMap()
renames.record(Top.ref("m"), Top.ref("m2"))
renames.get(Top_m) should be (Some(Seq(Top.instOf("m2", "Middle"))))
}
it should "rename targets if instance and ofmodule in the path are renamed" in {
val renames = RenameMap()
val Top_m2 = Top.instOf("m2", "Middle2")
renames.record(Top_m, Top_m2)
renames.get(Top_m) should be (Some(Seq(Top_m2)))
}
it should "properly do nothing if no remaps" in {
val renames = RenameMap()
renames.get(Top_m_l_a) should be (None)
}
it should "properly rename if leaf is inlined" in {
val renames = RenameMap()
renames.record(Middle_l_a, Middle_la)
renames.get(Top_m_l_a) should be (Some(Seq(Top_m_la)))
}
it should "properly rename if middle is inlined" in {
val renames = RenameMap()
renames.record(Top_m.ref("l"), Top.ref("m_l"))
renames.get(Top_m_l_a) should be (Some(Seq(Top.instOf("m_l", "Leaf").ref("a"))))
}
it should "properly rename if leaf and middle are inlined" in {
val renames = RenameMap()
val inlined = Top.ref("m_l_a")
renames.record(Top_m_l_a, inlined)
renames.record(Top_m_l, Nil)
renames.record(Top_m, Nil)
renames.get(Top_m_l_a) should be (Some(Seq(inlined)))
}
it should "quickly rename a target with a long path" in {
(0 until 50 by 10).foreach { endIdx =>
val renames = RenameMap()
renames.record(TopCircuit.module("Y0"), TopCircuit.module("X0"))
val deepTarget = (0 until endIdx).foldLeft(Top: IsModule) { (t, idx) =>
t.instOf("a", "A" + idx)
}.ref("ref")
val (millis, rename) = firrtl.Utils.time(renames.get(deepTarget))
println(s"${(deepTarget.tokens.size - 1) / 2} -> $millis")
//rename should be(None)
}
}
it should "rename with multiple renames" in {
val renames = RenameMap()
val Middle2 = cir.module("Middle2")
renames.record(Middle, Middle2)
renames.record(Middle.ref("l"), Middle.ref("lx"))
renames.get(Middle.ref("l")) should be (Some(Seq(Middle2.ref("lx"))))
}
it should "rename with fields" in {
val Middle_o = Middle.ref("o")
val Middle_i = Middle.ref("i")
val Middle_o_f = Middle.ref("o").field("f")
val Middle_i_f = Middle.ref("i").field("f")
val renames = RenameMap()
renames.record(Middle_o, Middle_i)
renames.get(Middle_o_f) should be (Some(Seq(Middle_i_f)))
}
it should "rename instances with same ofModule" in {
val Middle_o = Middle.ref("o")
val Middle_i = Middle.ref("i")
val renames = RenameMap()
renames.record(Middle_o, Middle_i)
renames.get(Middle.instOf("o", "O")) should be (Some(Seq(Middle.instOf("i", "O"))))
}
it should "detect circular renames" in {
case class BadRename(from: IsMember, tos: Seq[IsMember])
val badRenames =
Seq(
BadRename(foo, Seq(foo.field("bar"))),
BadRename(modA, Seq(foo))
//BadRename(cir, Seq(foo)),
//BadRename(cir, Seq(modA))
)
// Run all BadRename tests
for (BadRename(from, tos) <- badRenames) {
val fromN = from
val tosN = tos.mkString(", ")
//it should s"error if a $fromN is renamed to $tosN" in {
val renames = RenameMap()
for (to <- tos) {
a [IllegalArgumentException] shouldBe thrownBy {
renames.record(from, to)
}
}
//}
}
}
it should "be able to rename weird stuff" in {
// Renaming `from` to each of the `tos` at the same time should be ok
case class BadRename(from: CompleteTarget, tos: Seq[CompleteTarget])
val badRenames =
Seq(//BadRename(foo, Seq(cir)),
BadRename(foo, Seq(modB)),
BadRename(modA, Seq(fooB)),
//BadRename(modA, Seq(cir)),
//BadRename(cir, Seq(foo)),
//BadRename(cir, Seq(modA)),
BadRename(cir, Seq(cir2, cir3))
)
// Run all BadRename tests
for (BadRename(from, tos) <- badRenames) {
val fromN = from
val tosN = tos.mkString(", ")
//it should s"error if a $fromN is renamed to $tosN" in {
val renames = RenameMap()
for (to <- tos) {
(from, to) match {
case (f: CircuitTarget, t: CircuitTarget) => renames.record(f, t)
case (f: IsMember, t: IsMember) => renames.record(f, t)
}
}
//a [FIRRTLException] shouldBe thrownBy {
renames.get(from)
//}
//}
}
}
it should "error if a circular rename occurs" in {
val renames = RenameMap()
val top = CircuitTarget("Top")
renames.record(top.module("A"), top.module("B").instOf("c", "C"))
renames.record(top.module("B"), top.module("A").instOf("c", "C"))
a [CircularRenameException] shouldBe thrownBy {
renames.get(top.module("A"))
}
}
it should "not error if a swapping rename occurs" in {
val renames = RenameMap()
val top = CircuitTarget("Top")
renames.record(top.module("A"), top.module("B"))
renames.record(top.module("B"), top.module("A"))
renames.get(top.module("A")) should be (Some(Seq(top.module("B"))))
renames.get(top.module("B")) should be (Some(Seq(top.module("A"))))
}
it should "error if a reference is renamed to a module, and then we try to rename the reference's field" in {
val renames = RenameMap()
val top = CircuitTarget("Top")
renames.record(top.module("A").ref("ref"), top.module("B"))
renames.get(top.module("A").ref("ref")) should be(Some(Seq(top.module("B"))))
a [IllegalRenameException] shouldBe thrownBy {
renames.get(top.module("A").ref("ref").field("field"))
}
a [IllegalRenameException] shouldBe thrownBy {
renames.get(top.module("A").instOf("ref", "R"))
}
}
it should "error if we rename an instance's ofModule into a non-module" in {
val renames = RenameMap()
val top = CircuitTarget("Top")
renames.record(top.module("C"), top.module("D").ref("x"))
a [IllegalRenameException] shouldBe thrownBy {
renames.get(top.module("A").instOf("c", "C"))
}
}
it should "error if path is renamed into a non-path" ignore {
val renames = RenameMap()
val top = CircuitTarget("Top")
renames.record(top.module("E").instOf("f", "F"), top.module("E").ref("g"))
a [IllegalRenameException] shouldBe thrownBy {
println(renames.get(top.module("E").instOf("f", "F").ref("g")))
}
}
}
|