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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
// SPDX-License-Identifier: Apache-2.0
package firrtlTests
import firrtl._
import firrtl.annotations._
import firrtl.passes.{InlineAnnotation, InlineInstances, ResolveKinds}
import firrtl.transforms.NoCircuitDedupAnnotation
import firrtl.testutils._
import firrtl.testutils.FirrtlCheckers._
import firrtl.stage.TransformManager
import firrtl.options.Dependency
/**
* Tests inline instances transformation
*/
class InlineInstancesTests extends LowTransformSpec {
def transform = new InlineInstances
def inline(mod: String): Annotation = {
val parts = mod.split('.')
val modName = ModuleName(parts.head, CircuitName("Top")) // If this fails, bad input
val name = if (parts.size == 1) modName else ComponentName(parts.tail.mkString("."), modName)
InlineAnnotation(name)
}
// Set this to debug, this will apply to all tests
// Logger.setLevel(this.getClass, Debug)
"The module Inline" should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.a <= a
| b <= i.b
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| i_b <= i_a
| b <= i_b
| i_a <= a""".stripMargin
execute(input, check, Seq(inline("Inline")))
}
"The all instances of Simple" should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i0 of Simple
| inst i1 of Simple
| i0.a <= a
| i1.a <= i0.b
| b <= i1.b
| module Simple :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i0_a : UInt<32>
| wire i0_b : UInt<32>
| i0_b <= i0_a
| wire i1_a : UInt<32>
| wire i1_b : UInt<32>
| i1_b <= i1_a
| b <= i1_b
| i0_a <= a
| i1_a <= i0_b""".stripMargin
execute(input, check, Seq(inline("Simple")))
}
"Only one instance of Simple" should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i0 of Simple
| inst i1 of Simple
| i0.a <= a
| i1.a <= i0.b
| b <= i1.b
| module Simple :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i0_a : UInt<32>
| wire i0_b : UInt<32>
| i0_b <= i0_a
| inst i1 of Simple
| b <= i1.b
| i0_a <= a
| i1.a <= i0_b
| module Simple :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(inline("Top.i0")))
}
"All instances of A" should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i0 of A
| inst i1 of B
| i0.a <= a
| i1.a <= i0.b
| b <= i1.b
| module A :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
| module B :
| input a : UInt<32>
| output b : UInt<32>
| inst i of A
| i.a <= a
| b <= i.b""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i0_a : UInt<32>
| wire i0_b : UInt<32>
| i0_b <= i0_a
| inst i1 of B
| b <= i1.b
| i0_a <= a
| i1.a <= i0_b
| module B :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| i_b <= i_a
| b <= i_b
| i_a <= a""".stripMargin
execute(input, check, Seq(inline("A")))
}
"Non-inlined instances" should "still prepend prefix" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of A
| i.a <= a
| b <= i.b
| module A :
| input a : UInt<32>
| output b : UInt<32>
| inst i of B
| i.a <= a
| b <= i.b
| module B :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| inst i_i of B
| i_b <= i_i.b
| i_i.a <= i_a
| b <= i_b
| i_a <= a
| module B :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(inline("A")))
}
"A module with nested inlines" should "still prepend prefixes" in {
val input =
"""|circuit Top:
| module Top:
| inst foo of Foo
| module Foo:
| inst bar of Bar
| inst baz of Bar
| node foo = UInt<1>("h0")
| module Bar:
| node bar = UInt<1>("h0")
|""".stripMargin
val check =
"""|circuit Top:
| module Top:
| node foo_bar_bar = UInt<1>("h0")
| inst foo_baz of Bar
| node foo_foo = UInt<1>("h0")
| module Bar:
| node bar = UInt<1>("h0")
|""".stripMargin
execute(input, check, Seq(inline("Foo"), inline("Foo.bar")))
}
"An inlined module" should "NOT be prefix unique" in {
val input =
"""|circuit Top:
| module Top:
| inst a of A
| node a_foo = UInt<1>("h0")
| node a__bar = UInt<1>("h0")
| module A:
| node bar = UInt<1>("h0")
|""".stripMargin
val check =
"""|circuit Top:
| module Top:
| node a_bar = UInt<1>("h0")
| node a_foo = UInt<1>("h0")
| node a__bar = UInt<1>("h0")
|""".stripMargin
execute(input, check, Seq(inline("A")))
}
/* This test is mutually exclusive with the above */
ignore should "be prefix unique" in {
val input =
"""|circuit Top:
| module Top:
| inst a of A
| node a_foo = UInt<1>("h0")
| node a__bar = UInt<1>("h0")
| module A:
| node bar = UInt<1>("h0")
|""".stripMargin
val check =
"""|circuit Top:
| module Top:
| node a___bar = UInt<1>("h0")
| node a_foo = UInt<1>("h0")
| node a__bar = UInt<1>("h0")
|""".stripMargin
execute(input, check, Seq(inline("A")))
}
it should "uniquify sanely" in {
val input =
"""|circuit Top:
| module Top:
| inst foo of Foo
| node foo_ = UInt<1>("h0")
| node foo__bar = UInt<1>("h0")
| module Foo:
| inst bar of Bar
| inst baz of Bar
| node foo = UInt<1>("h0")
| module Bar:
| node bar = UInt<1>("h0")
|""".stripMargin
val check =
"""|circuit Top:
| module Top:
| node foo__bar_bar = UInt<1>("h0")
| inst foo__baz of Bar
| node foo__foo = UInt<1>("h0")
| node foo_ = UInt<1>("h0")
| node foo__bar = UInt<1>("h0")
| module Bar:
| node bar = UInt<1>("h0")
|""".stripMargin
execute(input, check, Seq(inline("Foo"), inline("Foo.bar")))
}
// ---- Errors ----
// 1) ext module
"External module" should "not be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of A
| i.a <= a
| b <= i.b
| extmodule A :
| input a : UInt<32>
| output b : UInt<32>""".stripMargin
failingexecute(input, Seq(inline("A")))
}
// 2) ext instance
"External instance" should "not be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of A
| i.a <= a
| b <= i.b
| extmodule A :
| input a : UInt<32>
| output b : UInt<32>""".stripMargin
failingexecute(input, Seq(inline("A")))
}
// 3) no module
"Inlined module" should "exist" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
failingexecute(input, Seq(inline("A")))
}
// 4) no inst
"Inlined instance" should "exist" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
failingexecute(input, Seq(inline("A")))
}
"Jack's Bug" should "not fail" in {
val input = """circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.a <= a
| b <= i.b
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| inst child of InlineChild
| child.a <= a
| b <= child.b
| module InlineChild :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check = """circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| inst i_child of InlineChild
| i_b <= i_child.b
| i_child.a <= i_a
| b <= i_b
| i_a <= a
| module InlineChild :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(inline("Inline")))
}
case class DummyAnno(targets: CompleteTarget*) extends Annotation {
override def update(renames: RenameMap): Seq[Annotation] = {
Seq(DummyAnno(targets.flatMap { t =>
renames.get(t).getOrElse(Seq(t))
}: _*))
}
}
"annotations" should "be renamed" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.a <= a
| b <= i.b
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| inst foo of NestedInline
| inst bar of NestedNoInline
| foo.a <= a
| bar.a <= foo.b
| b <= bar.b
| module NestedInline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
| module NestedNoInline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
|""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| wire i_foo_a : UInt<32>
| wire i_foo_b : UInt<32>
| i_foo_b <= i_foo_a
| inst i_bar of NestedNoInline
| i_b <= i_bar.b
| i_foo_a <= i_a
| i_bar.a <= i_foo_b
| b <= i_b
| i_a <= a
| module NestedNoInline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
|""".stripMargin
val top = CircuitTarget("Top").module("Top")
val inlined = top.instOf("i", "Inline")
val nestedInlined = top.instOf("i", "Inline").instOf("foo", "NestedInline")
val nestedNotInlined = top.instOf("i", "Inline").instOf("bar", "NestedNoInline")
executeWithAnnos(
input,
check,
Seq(
inline("Inline"),
inline("NestedInline"),
NoCircuitDedupAnnotation,
DummyAnno(inlined.ref("a")),
DummyAnno(inlined.ref("b")),
DummyAnno(nestedInlined.ref("a")),
DummyAnno(nestedInlined.ref("b")),
DummyAnno(nestedNotInlined.ref("a")),
DummyAnno(nestedNotInlined.ref("b"))
),
Seq(
DummyAnno(top.ref("i_a")),
DummyAnno(top.ref("i_b")),
DummyAnno(top.ref("i_foo_a")),
DummyAnno(top.ref("i_foo_b")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("a")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("b"))
)
)
}
"inlining named statements" should "work" in {
val input =
"""circuit Top :
| module Top :
| input clock : Clock
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.clock <= clock
| i.a <= a
| b <= i.b
| module Inline :
| input clock : Clock
| input a : UInt<32>
| output b : UInt<32>
| b <= a
| assert(clock, UInt(1), eq(a,b), "a == b") : assert1
| assert(clock, UInt(1), not(eq(a,b)), "a != b")
| stop(clock, UInt(0), 0)
|""".stripMargin
val check =
"""circuit Top :
| module Top :
| input clock : Clock
| input a : UInt<32>
| output b : UInt<32>
| wire i_clock : Clock
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| i_b <= i_a
| assert(i_clock, UInt(1), eq(i_a, i_b), "a == b") : i_assert1
| assert(i_clock, UInt(1), not(eq(i_a, i_b)), "a != b")
| stop(i_clock, UInt(0), 0)
| b <= i_b
| i_clock <= clock
| i_a <= a
|""".stripMargin
val top = CircuitTarget("Top").module("Top")
val inlined = top.instOf("i", "Inline")
executeWithAnnos(
input,
check,
Seq(
inline("Inline"),
NoCircuitDedupAnnotation,
DummyAnno(inlined.ref("assert1"))
),
Seq(
DummyAnno(top.ref("i_assert1"))
)
)
}
"inlining both grandparent and grandchild" should "should work" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.a <= a
| b <= i.b
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| inst foo of NestedInline
| inst bar of NestedNoInline
| foo.a <= a
| bar.a <= foo.b
| b <= bar.b
| module NestedInline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
| module NestedNoInline :
| input a : UInt<32>
| output b : UInt<32>
| inst foo of NestedInline
| foo.a <= a
| b <= foo.b
|""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| wire i_foo_a : UInt<32>
| wire i_foo_b : UInt<32>
| i_foo_b <= i_foo_a
| inst i_bar of NestedNoInline
| i_b <= i_bar.b
| i_foo_a <= i_a
| i_bar.a <= i_foo_b
| b <= i_b
| i_a <= a
| module NestedNoInline :
| input a : UInt<32>
| output b : UInt<32>
| wire foo_a : UInt<32>
| wire foo_b : UInt<32>
| foo_b <= foo_a
| b <= foo_b
| foo_a <= a
|""".stripMargin
val top = CircuitTarget("Top").module("Top")
val inlined = top.instOf("i", "Inline")
val nestedInlined = inlined.instOf("foo", "NestedInline")
val nestedNotInlined = inlined.instOf("bar", "NestedNoInline")
val innerNestedInlined = nestedNotInlined.instOf("foo", "NestedInline")
val inlineModuleTarget = top.copy(module = "Inline")
val nestedInlineModuleTarget = top.copy(module = "NestedInline")
executeWithAnnos(
input,
check,
Seq(
inline("Inline"),
inline("NestedInline"),
DummyAnno(inlined.ref("a")),
DummyAnno(inlined.ref("b")),
DummyAnno(nestedInlined.ref("a")),
DummyAnno(nestedInlined.ref("b")),
DummyAnno(nestedNotInlined.ref("a")),
DummyAnno(nestedNotInlined.ref("b")),
DummyAnno(innerNestedInlined.ref("a")),
DummyAnno(innerNestedInlined.ref("b")),
DummyAnno(inlineModuleTarget.instOf("bar", "NestedNoInline")),
DummyAnno(inlineModuleTarget.ref("a"), inlineModuleTarget.ref("b")),
DummyAnno(nestedInlineModuleTarget.ref("a"))
),
Seq(
DummyAnno(top.ref("i_a")),
DummyAnno(top.ref("i_b")),
DummyAnno(top.ref("i_foo_a")),
DummyAnno(top.ref("i_foo_b")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("a")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("b")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("foo_a")),
DummyAnno(top.instOf("i_bar", "NestedNoInline").ref("foo_b")),
DummyAnno(top.instOf("i_bar", "NestedNoInline")),
DummyAnno(top.ref("i_a"), top.ref("i_b")),
DummyAnno(top.ref("i_foo_a"), top.copy(module = "NestedNoInline").ref("foo_a"))
)
)
}
"InlineInstances" should "properly invalidate ResolveKinds" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline
| i.a <= a
| b <= i.b
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val state = CircuitState(parse(input), ChirrtlForm, Seq(inline("Inline")))
val manager = new TransformManager(Seq(Dependency[InlineInstances], Dependency(ResolveKinds)))
val result = manager.execute(state)
result shouldNot containTree { case WRef("i_a", _, PortKind, _) => true }
result should containTree { case WRef("i_a", _, WireKind, _) => true }
}
}
// Execution driven tests for inlining modules
// TODO(izraelevitz) fix this test
//class InlineInstancesIntegrationSpec extends FirrtlPropSpec {
// // Shorthand for creating annotations to inline modules
// def inlineModules(names: Seq[String]): Seq[CircuitAnnotation] =
// Seq(StickyCircuitAnnotation(InlineCAKind, names.map(n => ModuleName(n) -> TagAnnotation).toMap))
//
// case class Test(name: String, dir: String, ann: Seq[CircuitAnnotation])
//
// val runTests = Seq(
// Test("GCDTester", "/integration", inlineModules(Seq("DecoupledGCD")))
// )
//
// runTests foreach { test =>
// property(s"${test.name} should execute correctly with inlining") {
// println(s"Got annotations ${test.ann}")
// runFirrtlTest(test.name, test.dir, test.ann)
// }
// }
//}
|