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
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests.experimental.hierarchy
import chiselTests.ChiselFunSpec
import chisel3._
import chisel3.stage.{ChiselCircuitAnnotation, ChiselGeneratorAnnotation, ChiselStage, DesignAnnotation}
import chisel3.experimental.hierarchy.{Definition, Instance}
import chisel3.experimental.hierarchy.ImportDefinitionAnnotation
import firrtl.AnnotationSeq
import firrtl.options.TargetDirAnnotation
import scala.io.Source
class SeparateElaborationSpec extends ChiselFunSpec with Utils {
import Examples._
/** Return a [[DesignAnnotation]] from a list of annotations. */
private def getDesignAnnotation[T <: RawModule](annos: AnnotationSeq): DesignAnnotation[T] = {
val designAnnos = annos.flatMap { a =>
a match {
case a: DesignAnnotation[T] => Some(a)
case _ => None
}
}
require(designAnnos.length == 1, s"Exactly one DesignAnnotation should exist, but found: $designAnnos.")
designAnnos.head
}
/** Elaborates [[AddOne]] and returns its [[Definition]]. */
private def getAddOneDefinition(testDir: String): Definition[AddOne] = {
val dutAnnos = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOne),
TargetDirAnnotation(testDir)
)
)
// Grab DUT definition to pass into testbench
getDesignAnnotation(dutAnnos).design.asInstanceOf[AddOne].toDefinition
}
/** Return [[Definition]]s of all modules in a circuit. */
private def allModulesToImportedDefs(annos: AnnotationSeq): Seq[ImportDefinitionAnnotation[_]] = {
annos.flatMap { a =>
a match {
case a: ChiselCircuitAnnotation =>
a.circuit.components.map { c => ImportDefinitionAnnotation(c.id.toDefinition) }
case _ => Seq.empty
}
}
}
describe("(0): Name conflicts") {
it("(0.a): should not occur between a Module and an Instance of a previously elaborated Definition.") {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutDef = getAddOneDefinition(testDir)
class Testbench(defn: Definition[AddOne]) extends Module {
val mod = Module(new AddOne)
val inst = Instance(defn)
// Tie inputs to a value so ChiselStage does not complain
mod.in := 0.U
inst.in := 0.U
dontTouch(mod.out)
}
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef)
)
)
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("module AddOne_1(")
tb_rtl should include("AddOne_1 mod (")
(tb_rtl should not).include("module AddOne(")
tb_rtl should include("AddOne inst (")
}
it(
"(0.b): should not occur between an Instance of a Definition and an Instance of a previously elaborated Definition."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutDef = getAddOneDefinition(testDir)
class Testbench(defn: Definition[AddOne]) extends Module {
val inst0 = Instance(Definition(new AddOne))
val inst1 = Instance(defn)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
dontTouch(inst0.out)
}
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef)
)
)
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("module AddOne_1(")
tb_rtl should include("AddOne_1 inst0 (")
(tb_rtl should not).include("module AddOne(")
tb_rtl should include("AddOne inst1 (")
}
}
describe("(1): Repeat Module definitions") {
it("(1.a): should not occur when elaborating multiple Instances separately from its Definition.") {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutDef = getAddOneDefinition(testDir)
class Testbench(defn: Definition[AddOne]) extends Module {
val inst0 = Instance(defn)
val inst1 = Instance(defn)
inst0.in := 0.U
inst1.in := 0.U
}
// If there is a repeat module definition, FIRRTL emission will fail
(new ChiselStage).emitFirrtl(
gen = new Testbench(dutDef),
args = Array("-td", testDir, "--full-stacktrace"),
annotations = Seq(ImportDefinitionAnnotation(dutDef))
)
}
}
describe("(2): Multiple imported Definitions of modules without submodules") {
it(
"(2.a): should work if a list of imported Definitions is passed between Stages."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(4)),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddOneParameterized].toDefinition
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(8)),
TargetDirAnnotation(s"$testDir/dutDef1"),
// pass in previously elaborated Definitions
ImportDefinitionAnnotation(dutDef0)
)
)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddOneParameterized].toDefinition
class Testbench(defn0: Definition[AddOneParameterized], defn1: Definition[AddOneParameterized]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef0),
ImportDefinitionAnnotation(dutDef1)
)
)
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddOneParameterized.v").getLines.mkString
dutDef0_rtl should include("module AddOneParameterized(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddOneParameterized_1.v").getLines.mkString
dutDef1_rtl should include("module AddOneParameterized_1(")
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("AddOneParameterized inst0 (")
tb_rtl should include("AddOneParameterized_1 inst1 (")
(tb_rtl should not).include("module AddOneParameterized(")
(tb_rtl should not).include("module AddOneParameterized_1(")
}
it(
"(2.b): should throw an exception if information is not passed between Stages."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(4)),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddOneParameterized].toDefinition
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(8)),
TargetDirAnnotation(s"$testDir/dutDef1")
)
)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddOneParameterized].toDefinition
class Testbench(defn0: Definition[AddOneParameterized], defn1: Definition[AddOneParameterized]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
// Because these elaborations have no knowledge of each other, they create
// modules of the same name
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddOneParameterized.v").getLines.mkString
dutDef0_rtl should include("module AddOneParameterized(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddOneParameterized.v").getLines.mkString
dutDef1_rtl should include("module AddOneParameterized(")
val errMsg = intercept[ChiselException] {
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef0),
ImportDefinitionAnnotation(dutDef1)
)
)
}
errMsg.getMessage should include(
"Expected distinct imported Definition names but found duplicates for: AddOneParameterized"
)
}
}
describe("(3): Multiple imported Definitions of modules with submodules") {
it(
"(3.a): should work if a list of imported Definitions for all modules is passed between Stages."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddTwoMixedModules),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddTwoMixedModules].toDefinition
val importDefinitionAnnos0 = allModulesToImportedDefs(dutAnnos0)
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddTwoMixedModules),
TargetDirAnnotation(s"$testDir/dutDef1")
) ++ importDefinitionAnnos0
)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddTwoMixedModules].toDefinition
val importDefinitionAnnos1 = allModulesToImportedDefs(dutAnnos1)
class Testbench(defn0: Definition[AddTwoMixedModules], defn1: Definition[AddTwoMixedModules]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddTwoMixedModules.v").getLines.mkString
dutDef0_rtl should include("module AddOne(")
dutDef0_rtl should include("module AddTwoMixedModules(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddTwoMixedModules_1.v").getLines.mkString
dutDef1_rtl should include("module AddOne_2(")
dutDef1_rtl should include("module AddTwoMixedModules_1(")
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir)
) ++ importDefinitionAnnos0 ++ importDefinitionAnnos1
)
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("AddTwoMixedModules inst0 (")
tb_rtl should include("AddTwoMixedModules_1 inst1 (")
(tb_rtl should not).include("module AddTwoMixedModules(")
(tb_rtl should not).include("module AddTwoMixedModules_1(")
}
}
it(
"(3.b): should throw an exception if submodules are not passed between Definition elaborations."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddTwoMixedModules),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddTwoMixedModules].toDefinition
val importDefinitionAnnos0 = allModulesToImportedDefs(dutAnnos0)
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddTwoMixedModules),
ImportDefinitionAnnotation(dutDef0),
TargetDirAnnotation(s"$testDir/dutDef1")
)
)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddTwoMixedModules].toDefinition
val importDefinitionAnnos1 = allModulesToImportedDefs(dutAnnos1)
class Testbench(defn0: Definition[AddTwoMixedModules], defn1: Definition[AddTwoMixedModules]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddTwoMixedModules.v").getLines.mkString
dutDef0_rtl should include("module AddOne(")
dutDef0_rtl should include("module AddTwoMixedModules(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddTwoMixedModules_1.v").getLines.mkString
dutDef1_rtl should include("module AddOne(")
dutDef1_rtl should include("module AddTwoMixedModules_1(")
val errMsg = intercept[ChiselException] {
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir)
) ++ importDefinitionAnnos0 ++ importDefinitionAnnos1
)
}
errMsg.getMessage should include(
"Expected distinct imported Definition names but found duplicates for: AddOne"
)
}
describe("(4): With ExtMod Names") {
it("(4.a): should pick correct ExtMod names when passed") {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutDef = getAddOneDefinition(testDir)
class Testbench(defn: Definition[AddOne]) extends Module {
val mod = Module(new AddOne)
val inst = Instance(defn)
// Tie inputs to a value so ChiselStage does not complain
mod.in := 0.U
inst.in := 0.U
dontTouch(mod.out)
}
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef, Some("CustomPrefix_AddOne_CustomSuffix"))
)
)
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("module AddOne_1(")
tb_rtl should include("AddOne_1 mod (")
(tb_rtl should not).include("module AddOne(")
tb_rtl should include("CustomPrefix_AddOne_CustomSuffix inst (")
}
}
it(
"(4.b): should work if a list of imported Definitions is passed between Stages with ExtModName."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(4)),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddOneParameterized].toDefinition
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(8)),
TargetDirAnnotation(s"$testDir/dutDef1"),
// pass in previously elaborated Definitions
ImportDefinitionAnnotation(dutDef0)
)
)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddOneParameterized].toDefinition
class Testbench(defn0: Definition[AddOneParameterized], defn1: Definition[AddOneParameterized]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef0, Some("Inst1_Prefix_AddOnePramaterized_Inst1_Suffix")),
ImportDefinitionAnnotation(dutDef1, Some("Inst2_Prefix_AddOnePrameterized_1_Inst2_Suffix"))
)
)
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddOneParameterized.v").getLines.mkString
dutDef0_rtl should include("module AddOneParameterized(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddOneParameterized_1.v").getLines.mkString
dutDef1_rtl should include("module AddOneParameterized_1(")
val tb_rtl = Source.fromFile(s"$testDir/Testbench.v").getLines.mkString
tb_rtl should include("Inst1_Prefix_AddOnePramaterized_Inst1_Suffix inst0 (")
tb_rtl should include("Inst2_Prefix_AddOnePrameterized_1_Inst2_Suffix inst1 (")
(tb_rtl should not).include("module AddOneParameterized(")
(tb_rtl should not).include("module AddOneParameterized_1(")
}
it(
"(4.c): should throw an exception if a list of imported Definitions is passed between Stages with same ExtModName."
) {
val testDir = createTestDirectory(this.getClass.getSimpleName).toString
val dutAnnos0 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(4)),
TargetDirAnnotation(s"$testDir/dutDef0")
)
)
val importDefinitionAnnos0 = allModulesToImportedDefs(dutAnnos0)
val dutDef0 = getDesignAnnotation(dutAnnos0).design.asInstanceOf[AddOneParameterized].toDefinition
val dutAnnos1 = (new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new AddOneParameterized(8)),
TargetDirAnnotation(s"$testDir/dutDef1"),
// pass in previously elaborated Definitions
ImportDefinitionAnnotation(dutDef0)
)
)
val importDefinitionAnnos1 = allModulesToImportedDefs(dutAnnos1)
val dutDef1 = getDesignAnnotation(dutAnnos1).design.asInstanceOf[AddOneParameterized].toDefinition
class Testbench(defn0: Definition[AddOneParameterized], defn1: Definition[AddOneParameterized]) extends Module {
val inst0 = Instance(defn0)
val inst1 = Instance(defn1)
// Tie inputs to a value so ChiselStage does not complain
inst0.in := 0.U
inst1.in := 0.U
}
val dutDef0_rtl = Source.fromFile(s"$testDir/dutDef0/AddOneParameterized.v").getLines.mkString
dutDef0_rtl should include("module AddOneParameterized(")
val dutDef1_rtl = Source.fromFile(s"$testDir/dutDef1/AddOneParameterized_1.v").getLines.mkString
dutDef1_rtl should include("module AddOneParameterized_1(")
val errMsg = intercept[ChiselException] {
(new ChiselStage).run(
Seq(
ChiselGeneratorAnnotation(() => new Testbench(dutDef0, dutDef1)),
TargetDirAnnotation(testDir),
ImportDefinitionAnnotation(dutDef0, Some("Inst1_Prefix_AddOnePrameterized_Inst1_Suffix")),
ImportDefinitionAnnotation(dutDef1, Some("Inst1_Prefix_AddOnePrameterized_Inst1_Suffix"))
)
)
}
errMsg.getMessage should include(
"Expected distinct overrideDef names but found duplicates for: Inst1_Prefix_AddOnePrameterized_Inst1_Suffix"
)
}
}
|