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
|
// SPDX-License-Identifier: Apache-2.0
package firrtlTests
import firrtl.annotations.{Annotation, CircuitName, ComponentName, ModuleName}
import firrtl.transforms.{Flatten, FlattenAnnotation, NoCircuitDedupAnnotation}
import firrtl.testutils._
/**
* Tests deep inline transformation
*/
class FlattenTests extends LowTransformSpec {
def transform = new Flatten
def flatten(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)
FlattenAnnotation(name)
}
"The modules inside Top " should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline1
| i.a <= a
| b <= i.b
| module Inline1 :
| 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
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(flatten("Top")))
}
"Two instances of the same module inside Top " should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| inst i1 of Inline1
| inst i2 of Inline1
| i1.a <= a
| node tmp = i1.b
| i2.a <= tmp
| b <= i2.b
| module Inline1 :
| 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 i1_a : UInt<32>
| wire i1_b : UInt<32>
| i1_b <= i1_a
| wire i2_a : UInt<32>
| wire i2_b : UInt<32>
| i2_b <= i2_a
| node tmp = i1_b
| b <= i2_b
| i1_a <= a
| i2_a <= tmp
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(flatten("Top")))
}
"The module instance i in Top " should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| input na : UInt<32>
| output b : UInt<32>
| output nb : UInt<32>
| inst i of Inline1
| inst ni of NotInline1
| i.a <= a
| b <= i.b
| ni.a <= na
| nb <= ni.b
| module NotInline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| i.a <= a
| b <= i.a
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| i.a <= a
| b <= i.a
| module Inline2 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| input na : UInt<32>
| output b : UInt<32>
| output nb : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| wire i_i_a : UInt<32>
| wire i_i_b : UInt<32>
| i_i_b <= i_i_a
| i_b <= i_i_a
| i_i_a <= i_a
| inst ni of NotInline1
| b <= i_b
| nb <= ni.b
| i_a <= a
| ni.a <= na
| module NotInline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| b <= i.a
| i.a <= a
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| b <= i.a
| i.a <= a
| module Inline2 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(flatten("Top.i"), NoCircuitDedupAnnotation))
}
"The module Inline1" should "be inlined" in {
val input =
"""circuit Top :
| module Top :
| input a : UInt<32>
| input na : UInt<32>
| output b : UInt<32>
| output nb : UInt<32>
| inst i of Inline1
| inst ni of NotInline1
| i.a <= a
| b <= i.b
| ni.a <= na
| nb <= ni.b
| module NotInline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| i.a <= a
| b <= i.a
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| i.a <= a
| b <= i.a
| module Inline2 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
val check =
"""circuit Top :
| module Top :
| input a : UInt<32>
| input na : UInt<32>
| output b : UInt<32>
| output nb : UInt<32>
| inst i of Inline1
| inst ni of NotInline1
| b <= i.b
| nb <= ni.b
| i.a <= a
| ni.a <= na
| module NotInline1 :
| input a : UInt<32>
| output b : UInt<32>
| inst i of Inline2
| b <= i.a
| i.a <= a
| module Inline1 :
| input a : UInt<32>
| output b : UInt<32>
| wire i_a : UInt<32>
| wire i_b : UInt<32>
| i_b <= i_a
| b <= i_a
| i_a <= a
| module Inline2 :
| input a : UInt<32>
| output b : UInt<32>
| b <= a""".stripMargin
execute(input, check, Seq(flatten("Inline1"), NoCircuitDedupAnnotation))
}
"The Flatten transform" should "do nothing if no flatten annotations are present" in {
val input =
"""|circuit Foo:
| module Foo:
| input a: UInt<1>
| output b: UInt<1>
| b <= a
|""".stripMargin
execute(input, input, Seq.empty)
}
"The Flatten transform" should "ignore extmodules" 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 i of ExternalMod
| i.a <= a
| b <= i.b
| extmodule ExternalMod :
| input a : UInt<32>
| output b : UInt<32>
| defname = ExternalMod
""".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 ExternalMod
| i_b <= i_i.b
| i_i.a <= i_a
| b <= i_b
| i_a <= a
| module Inline :
| input a : UInt<32>
| output b : UInt<32>
| inst i of ExternalMod
| b <= i.b
| i.a <= a
| extmodule ExternalMod :
| input a : UInt<32>
| output b : UInt<32>
| defname = ExternalMod
""".stripMargin
execute(input, check, Seq(flatten("Top")))
}
"The Flatten transform" should "work on modules with no instances" in {
val input = """
|circuit Top :
| module Top :
| input a : UInt<32>
| output b : UInt<32>
| b <= a
""".stripMargin
val check = input
execute(input, check, Seq(flatten("Top")))
}
}
|