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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// See LICENSE for license details.
package firrtlTests.annotationTests
import firrtl._
import firrtl.annotations._
import firrtl.annotations.analysis.DuplicationHelper
import firrtl.annotations.transforms.NoSuchTargetException
import firrtl.transforms.DontTouchAnnotation
import firrtl.testutils.{FirrtlMatchers, FirrtlPropSpec}
class EliminateTargetPathsSpec extends FirrtlPropSpec with FirrtlMatchers {
val input =
"""circuit Top:
| module Leaf:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| node a = i
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| inst l1 of Leaf
| inst l2 of Leaf
| l1.i <= i
| l2.i <= l1.o
| o <= l2.o
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle
| inst m2 of Middle
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
""".stripMargin
val TopCircuit = CircuitTarget("Top")
val Top = TopCircuit.module("Top")
val Middle = TopCircuit.module("Middle")
val Leaf = TopCircuit.module("Leaf")
val Top_m1_l1_a = Top.instOf("m1", "Middle").instOf("l1", "Leaf").ref("a")
val Top_m2_l1_a = Top.instOf("m2", "Middle").instOf("l1", "Leaf").ref("a")
val Top_m1_l2_a = Top.instOf("m1", "Middle").instOf("l2", "Leaf").ref("a")
val Top_m2_l2_a = Top.instOf("m2", "Middle").instOf("l2", "Leaf").ref("a")
val Middle_l1_a = Middle.instOf("l1", "Leaf").ref("a")
val Middle_l2_a = Middle.instOf("l2", "Leaf").ref("a")
val Leaf_a = Leaf.ref("a")
case class DummyAnnotation(target: Target) extends SingleTargetAnnotation[Target] {
override def duplicate(n: Target): Annotation = DummyAnnotation(n)
}
class DummyTransform() extends Transform with ResolvedAnnotationPaths {
override def inputForm: CircuitForm = LowForm
override def outputForm: CircuitForm = LowForm
override val annotationClasses: Traversable[Class[_]] = Seq(classOf[DummyAnnotation])
override def execute(state: CircuitState): CircuitState = state
}
val customTransforms = Seq(new DummyTransform())
val inputState = CircuitState(parse(input), ChirrtlForm)
property("Hierarchical tokens should be expanded properly") {
val dupMap = new DuplicationHelper(inputState.circuit.modules.map(_.name).toSet)
// Only a few instance references
dupMap.expandHierarchy(Top_m1_l1_a)
dupMap.expandHierarchy(Top_m2_l1_a)
dupMap.expandHierarchy(Middle_l1_a)
dupMap.makePathless(Top_m1_l1_a).foreach {Set(TopCircuit.module("Leaf___Top_m1_l1").ref("a")) should contain (_)}
dupMap.makePathless(Top_m2_l1_a).foreach {Set(TopCircuit.module("Leaf___Top_m2_l1").ref("a")) should contain (_)}
dupMap.makePathless(Top_m1_l2_a).foreach {Set(Leaf_a) should contain (_)}
dupMap.makePathless(Top_m2_l2_a).foreach {Set(Leaf_a) should contain (_)}
dupMap.makePathless(Middle_l1_a).foreach {Set(
TopCircuit.module("Leaf___Top_m1_l1").ref("a"),
TopCircuit.module("Leaf___Top_m2_l1").ref("a"),
TopCircuit.module("Leaf___Middle_l1").ref("a")
) should contain (_) }
dupMap.makePathless(Middle_l2_a).foreach {Set(Leaf_a) should contain (_)}
dupMap.makePathless(Leaf_a).foreach {Set(
TopCircuit.module("Leaf___Top_m1_l1").ref("a"),
TopCircuit.module("Leaf___Top_m2_l1").ref("a"),
TopCircuit.module("Leaf___Middle_l1").ref("a"),
Leaf_a
) should contain (_)}
dupMap.makePathless(Top).foreach {Set(Top) should contain (_)}
dupMap.makePathless(Middle).foreach {Set(
TopCircuit.module("Middle___Top_m1"),
TopCircuit.module("Middle___Top_m2"),
Middle
) should contain (_)}
dupMap.makePathless(Leaf).foreach {Set(
TopCircuit.module("Leaf___Top_m1_l1"),
TopCircuit.module("Leaf___Top_m2_l1"),
TopCircuit.module("Leaf___Middle_l1"),
Leaf
) should contain (_) }
}
property("Hierarchical donttouch should be resolved properly") {
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DontTouchAnnotation(Top_m1_l1_a)))
val customTransforms = Seq(new LowFirrtlOptimization())
val outputState = new LowFirrtlCompiler().compile(inputState, customTransforms)
val check =
"""circuit Top :
| module Leaf___Top_m1_l1 :
| input i : UInt<1>
| output o : UInt<1>
|
| node a = i
| o <= i
|
| module Leaf :
| input i : UInt<1>
| output o : UInt<1>
|
| skip
| o <= i
|
| module Middle___Top_m1 :
| input i : UInt<1>
| output o : UInt<1>
|
| inst l1 of Leaf___Top_m1_l1
| inst l2 of Leaf
| o <= l2.o
| l1.i <= i
| l2.i <= l1.o
|
| module Middle :
| input i : UInt<1>
| output o : UInt<1>
|
| inst l1 of Leaf
| inst l2 of Leaf
| o <= l2.o
| l1.i <= i
| l2.i <= l1.o
|
| module Top :
| input i : UInt<1>
| output o : UInt<1>
|
| inst m1 of Middle___Top_m1
| inst m2 of Middle
| o <= m2.o
| m1.i <= i
| m2.i <= m1.o
|
""".stripMargin
canonicalize(outputState.circuit).serialize should be (canonicalize(parse(check)).serialize)
outputState.annotations.collect {
case x: DontTouchAnnotation => x.target
} should be (Seq(Top.circuitTarget.module("Leaf___Top_m1_l1").ref("a")))
}
property("No name conflicts between old and new modules") {
val input =
"""circuit Top:
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle
| inst m2 of Middle
| inst x of Middle___Top_m1
| x.i <= i
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
| module Middle___Top_m1:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| node a = i
""".stripMargin
val checks =
"""circuit Top :
| module Middle :
| module Top :
| module Middle___Top_m1 :
| module Middle____Top_m1 :""".stripMargin.split("\n")
val Top_m1 = Top.instOf("m1", "Middle")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DummyAnnotation(Top_m1)))
val outputState = new LowFirrtlCompiler().compile(inputState, customTransforms)
val outputLines = outputState.circuit.serialize.split("\n")
checks.foreach { line =>
outputLines should contain (line)
}
}
property("Previously unused modules should remain, but newly unused modules should be eliminated") {
val input =
"""circuit Top:
| module Leaf:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| node a = i
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle
| inst m2 of Middle
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
""".stripMargin
val checks =
"""circuit Top :
| module Leaf :
| module Top :
| module Middle___Top_m1 :
| module Middle___Top_m2 :""".stripMargin.split("\n")
val Top_m1 = Top.instOf("m1", "Middle")
val Top_m2 = Top.instOf("m2", "Middle")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DummyAnnotation(Top_m1), DummyAnnotation(Top_m2)))
val outputState = new LowFirrtlCompiler().compile(inputState, customTransforms)
val outputLines = outputState.circuit.serialize.split("\n")
checks.foreach { line =>
outputLines should contain (line)
}
checks.foreach { line =>
outputLines should not contain (" module Middle :")
}
}
property("Paths with incorrect names should error") {
val input =
"""circuit Top:
| module Leaf:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| node a = i
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle
| inst m2 of Middle
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
""".stripMargin
val e1 = the [CustomTransformException] thrownBy {
val Top_m1 = Top.instOf("m1", "MiddleX")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DummyAnnotation(Top_m1)))
new LowFirrtlCompiler().compile(inputState, customTransforms)
}
e1.cause shouldBe a [NoSuchTargetException]
val e2 = the [CustomTransformException] thrownBy {
val Top_m2 = Top.instOf("x2", "Middle")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DummyAnnotation(Top_m2)))
new LowFirrtlCompiler().compile(inputState, customTransforms)
}
e2.cause shouldBe a [NoSuchTargetException]
}
property("No name conflicts between two new modules") {
val input =
"""circuit Top:
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle_
| inst m2 of Middle
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| inst _l of Leaf
| _l.i <= i
| o <= _l.o
| module Middle_:
| input i: UInt<1>
| output o: UInt<1>
| inst l of Leaf
| l.i <= i
| node x = i
| o <= l.o
| module Leaf:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
""".stripMargin
val checks =
"""circuit Top :
| module Middle :
| module Top :
| module Leaf___Middle__l :
| module Leaf____Middle__l :""".stripMargin.split("\n")
val Middle_l1 = CircuitTarget("Top").module("Middle").instOf("_l", "Leaf")
val Middle_l2 = CircuitTarget("Top").module("Middle_").instOf("l", "Leaf")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DummyAnnotation(Middle_l1), DummyAnnotation(Middle_l2)))
val outputState = new LowFirrtlCompiler().compile(inputState, customTransforms)
val outputLines = outputState.circuit.serialize.split("\n")
checks.foreach { line =>
outputLines should contain (line)
}
}
property("Keep annotations of modules not instantiated") {
val input =
"""circuit Top:
| module Top:
| input i: UInt<1>
| output o: UInt<1>
| inst m1 of Middle
| inst m2 of Middle
| m1.i <= i
| m2.i <= m1.o
| o <= m2.o
| module Middle:
| input i: UInt<1>
| output o: UInt<1>
| inst _l of Leaf
| _l.i <= i
| o <= _l.o
| module Middle_:
| input i: UInt<1>
| output o: UInt<1>
| o <= UInt(0)
| module Leaf:
| input i: UInt<1>
| output o: UInt<1>
| o <= i
""".stripMargin
val checks =
"""circuit Top :
| module Middle_ :""".stripMargin.split("\n")
val Middle_ = CircuitTarget("Top").module("Middle_").ref("i")
val inputState = CircuitState(parse(input), ChirrtlForm, Seq(DontTouchAnnotation(Middle_)))
val outputState = new VerilogCompiler().compile(inputState, customTransforms)
val outputLines = outputState.circuit.serialize.split("\n")
checks.foreach { line =>
outputLines should contain (line)
}
}
property("It should remove ResolvePaths annotations") {
val input =
"""|circuit Foo:
| module Bar:
| skip
| module Foo:
| inst bar of Bar
|""".stripMargin
CircuitState(passes.ToWorkingIR.run(Parser.parse(input)), UnknownForm, Nil)
.resolvePaths(Seq(CircuitTarget("Foo").module("Foo").instOf("bar", "Bar")))
.annotations
.collect{ case a: firrtl.annotations.transforms.ResolvePaths => a } should be (empty)
}
property("It should rename module annotations") {
val input =
"""|circuit Foo:
| module Bar:
| node x = UInt<1>(0)
| skip
| module Foo:
| inst bar of Bar
| inst baz of Bar""".stripMargin
val Bar_x = CircuitTarget("Foo").module("Bar").ref("x")
val output = CircuitState(passes.ToWorkingIR.run(Parser.parse(input)), UnknownForm, Seq(DontTouchAnnotation(Bar_x)))
.resolvePaths(Seq(CircuitTarget("Foo").module("Foo").instOf("bar", "Bar")))
info(output.circuit.serialize)
val newBar_x = CircuitTarget("Foo").module("Bar___Foo_bar").ref("x")
output
.annotations
.filter{
case _: DeletedAnnotation => false
case _ => true
} should contain allOf (DontTouchAnnotation(newBar_x), DontTouchAnnotation(Bar_x))
}
property("It should not rename lone instances") {
val input =
"""|circuit Foo:
| module Baz:
| skip
| module Bar:
| inst baz of Baz
| skip
| module Foo:
| inst bar of Bar
|""".stripMargin
val targets = Seq(
CircuitTarget("Foo").module("Foo").instOf("bar", "Bar").instOf("baz", "Baz")
)
val output = CircuitState(passes.ToWorkingIR.run(Parser.parse(input)), UnknownForm, Nil)
.resolvePaths(targets)
info(output.circuit.serialize)
}
}
|