aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/InlineBooleanExpressionsSpec.scala
blob: a1f272c9b50e3cf0cd3262fe6f2bc30d9747d8cc (plain)
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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests

import firrtl._
import firrtl.ir.{Circuit, Connect, FileInfo, MultiInfo, Statement}
import firrtl.annotations.{Annotation, ReferenceTarget}
import firrtl.options.Dependency
import firrtl.passes._
import firrtl.transforms._
import firrtl.testutils._
import firrtl.stage.{PrettyNoExprInlining, TransformManager}

class InlineBooleanExpressionsSpec extends FirrtlFlatSpec {
  val transform = new InlineBooleanExpressions
  val transforms: Seq[Transform] = new TransformManager(
    transform.prerequisites
  ).flattenedTransformOrder :+ transform

  protected def exec(input: Circuit, annos: Seq[Annotation] = Nil) = {
    transforms
      .foldLeft(CircuitState(input, UnknownForm, AnnotationSeq(annos))) { (c: CircuitState, t: Transform) =>
        t.runTransform(c)
      }
      .circuit
      .serialize
  }

  it should "inline mux operands" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output out : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |    node _t = head(x1, 1)
        |    node _f = head(x2, 1)
        |    node _c = lt(x1, x2)
        |    node _y = mux(_c, _t, _f)
        |    out <= _y""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output out : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |    node _t = head(x1, 1)
        |    node _f = head(x2, 1)
        |    node _c = lt(x1, x2)
        |    node _y = mux(lt(x1, x2), head(x1, 1), head(x2, 1))
        |    out <= mux(lt(x1, x2), head(x1, 1), head(x2, 1))""".stripMargin
    val result = exec(parse(input))
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "not inline dontTouched signals" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output out : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |    node _t = head(x1, 1)
        |    node _f = head(x2, 1)
        |    node _c = lt(x1, x2)
        |    node _y = mux(_c, _t, _f)
        |    out <= _y""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output out : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |    node _t = head(x1, 1)
        |    node _f = head(x2, 1)
        |    node _c = lt(x1, x2)
        |    node _y = mux(lt(x1, x2), _t, _f)
        |    out <= mux(lt(x1, x2), _t, _f)""".stripMargin
    val result = exec(
      parse(input),
      Seq(
        DontTouchAnnotation(ReferenceTarget("Top", "Top", Seq.empty, "_t", Seq.empty)),
        DontTouchAnnotation(ReferenceTarget("Top", "Top", Seq.empty, "_f", Seq.empty))
      )
    )
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "only inline expressions with the same file and line number" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output outA1 : UInt<1>
        |    output outA2 : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |
        |    node _t = head(x1, 1) @[A 1:1]
        |    node _f = head(x2, 1) @[A 1:2]
        |    node _y = mux(lt(x1, x2), _t, _f) @[A 1:3]
        |    outA1 <= _y @[A 1:3]
        |
        |    outA2 <= _y @[A 2:3]
        |
        |    outB <= _y @[B]""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output outA1 : UInt<1>
        |    output outA2 : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<1>(0)
        |    node x2 = UInt<1>(1)
        |
        |    node _t = head(x1, 1) @[A 1:1]
        |    node _f = head(x2, 1) @[A 1:2]
        |    node _y = mux(lt(x1, x2), head(x1, 1), head(x2, 1)) @[A 1:3]
        |    outA1 <= mux(lt(x1, x2), head(x1, 1), head(x2, 1)) @[A 1:3]
        |
        |    outA2 <= _y @[A 2:3]
        |
        |    outB <= _y @[B]""".stripMargin
    val result = exec(parse(input))
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "inline if subexpression info is a subset of parent info" in {
    val input =
      parse("""circuit test :
              |  module test :
              |    input in_1 : UInt<1>
              |    input in_2 : UInt<1>
              |    input in_3 : UInt<1>
              |    output out : UInt<1>
              |    node _c = in_1 @[A 1:1]
              |    node _t = in_2 @[A 1:1]
              |    node _f = in_3 @[A 1:1]
              |    out <= mux(_c, _t, _f)""".stripMargin).mapModule { m =>
        // workaround to insert MultiInfo
        def onStmt(stmt: Statement): Statement = stmt match {
          case c: Connect =>
            c.mapInfo { _ =>
              MultiInfo(
                Seq(
                  FileInfo("A 1:1"),
                  FileInfo("A 2:2"),
                  FileInfo("A 3:3")
                )
              )
            }
          case other => other.mapStmt(onStmt)
        }
        m.mapStmt(onStmt)
      }
    val check =
      """circuit test :
        |  module test :
        |    input in_1 : UInt<1>
        |    input in_2 : UInt<1>
        |    input in_3 : UInt<1>
        |    output out : UInt<1>
        |    node _c = in_1 @[A 1:1]
        |    node _t = in_2 @[A 1:1]
        |    node _f = in_3 @[A 1:1]
        |    out <= mux(in_1, in_2, in_3) @[A 1:1 2:2 3:3]""".stripMargin
    val result = exec(input)
    (result) should be(parse(check).serialize)
  }

  it should "inline mux condition and dshl/dhslr shamt args" in {
    val input =
      """circuit inline_mux_dshl_dshlr_args :
        |  module inline_mux_dshl_dshlr_args :
        |    input in_1 : UInt<3>
        |    input in_2 : UInt<3>
        |    input in_3 : UInt<3>
        |    output out_1 : UInt<3>
        |    output out_2 : UInt<3>
        |    output out_3 : UInt<4>
        |    node _c = head(in_1, 1)
        |    node _t = in_2
        |    node _f = in_3
        |    out_1 <= mux(_c, _t, _f)
        |    out_2 <= dshr(in_1, _c)
        |    out_3 <= dshl(in_1, _c)""".stripMargin
    val check =
      """circuit inline_mux_dshl_dshlr_args :
        |  module inline_mux_dshl_dshlr_args :
        |    input in_1 : UInt<3>
        |    input in_2 : UInt<3>
        |    input in_3 : UInt<3>
        |    output out_1 : UInt<3>
        |    output out_2 : UInt<3>
        |    output out_3 : UInt<4>
        |    node _c = head(in_1, 1)
        |    node _t = in_2
        |    node _f = in_3
        |    out_1 <= mux(head(in_1, 1), _t, _f)
        |    out_2 <= dshr(in_1, head(in_1, 1))
        |    out_3 <= dshl(in_1, head(in_1, 1))""".stripMargin
    val result = exec(parse(input))
    (result) should be(parse(check).serialize)
  }

  it should "inline boolean DoPrims" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output outA : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<3>(0)
        |    node x2 = UInt<3>(1)
        |
        |    node _a = lt(x1, x2)
        |    node _b = eq(_a, x2)
        |    node _c = and(_b, x2)
        |    outA <= _c
        |
        |    node _d = head(_c, 1)
        |    node _e = andr(_d)
        |    node _f = lt(_e, x2)
        |    outB <= _f""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output outA : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<3>(0)
        |    node x2 = UInt<3>(1)
        |
        |    node _a = lt(x1, x2)
        |    node _b = eq(lt(x1, x2), x2)
        |    node _c = and(eq(lt(x1, x2), x2), x2)
        |    outA <= and(eq(lt(x1, x2), x2), x2)
        |
        |    node _d = head(_c, 1)
        |    node _e = andr(head(_c, 1))
        |    node _f = lt(andr(head(_c, 1)), x2)
        |
        |    outB <= lt(andr(head(_c, 1)), x2)""".stripMargin
    val result = exec(parse(input))
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "inline more boolean DoPrims" in {
    val input =
      """circuit Top :
        |  module Top :
        |    output outA : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<3>(0)
        |    node x2 = UInt<3>(1)
        |
        |    node _a = lt(x1, x2)
        |    node _b = leq(_a, x2)
        |    node _c = gt(_b, x2)
        |    node _d = geq(_c, x2)
        |    outA <= _d
        |
        |    node _e = lt(x1, x2)
        |    node _f = leq(x1, _e)
        |    node _g = gt(x1, _f)
        |    node _h = geq(x1, _g)
        |    outB <= _h""".stripMargin
    val check =
      """circuit Top :
        |  module Top :
        |    output outA : UInt<1>
        |    output outB : UInt<1>
        |    node x1 = UInt<3>(0)
        |    node x2 = UInt<3>(1)
        |
        |    node _a = lt(x1, x2)
        |    node _b = leq(lt(x1, x2), x2)
        |    node _c = gt(leq(lt(x1, x2), x2), x2)
        |    node _d = geq(gt(leq(lt(x1, x2), x2), x2), x2)
        |    outA <= geq(gt(leq(lt(x1, x2), x2), x2), x2)
        |
        |    node _e = lt(x1, x2)
        |    node _f = leq(x1, lt(x1, x2))
        |    node _g = gt(x1, leq(x1, lt(x1, x2)))
        |    node _h = geq(x1, gt(x1, leq(x1, lt(x1, x2))))
        |
        |    outB <= geq(x1, gt(x1, leq(x1, lt(x1, x2))))""".stripMargin
    val result = exec(parse(input))
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "limit the number of inlines" in {
    val input =
      s"""circuit Top :
         |  module Top :
         |    input c_0: UInt<1>
         |    input c_1: UInt<1>
         |    input c_2: UInt<1>
         |    input c_3: UInt<1>
         |    input c_4: UInt<1>
         |    input c_5: UInt<1>
         |    input c_6: UInt<1>
         |    output out : UInt<1>
         |
         |    node _1 = or(c_0, c_1)
         |    node _2 = or(_1, c_2)
         |    node _3 = or(_2, c_3)
         |    node _4 = or(_3, c_4)
         |    node _5 = or(_4, c_5)
         |    node _6 = or(_5, c_6)
         |
         |    out <= _6""".stripMargin
    val check =
      s"""circuit Top :
         |  module Top :
         |    input c_0: UInt<1>
         |    input c_1: UInt<1>
         |    input c_2: UInt<1>
         |    input c_3: UInt<1>
         |    input c_4: UInt<1>
         |    input c_5: UInt<1>
         |    input c_6: UInt<1>
         |    output out : UInt<1>
         |
         |    node _1 = or(c_0, c_1)
         |    node _2 = or(or(c_0, c_1), c_2)
         |    node _3 = or(or(or(c_0, c_1), c_2), c_3)
         |    node _4 = or(_3, c_4)
         |    node _5 = or(or(_3, c_4), c_5)
         |    node _6 = or(or(or(_3, c_4), c_5), c_6)
         |
         |    out <= or(or(or(_3, c_4), c_5), c_6)""".stripMargin
    val result = exec(parse(input), Seq(InlineBooleanExpressionsMax(3)))
    (result) should be(parse(check).serialize)
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "be equivalent" in {
    val input =
      """circuit InlineBooleanExpressionsEquivalenceTest :
        |  module InlineBooleanExpressionsEquivalenceTest :
        |    input in : UInt<1>[6]
        |    output out : UInt<1>
        |
        |    node _a = or(in[0], in[1])
        |    node _b = and(in[2], _a)
        |    node _c = eq(in[3], _b)
        |    node _d = lt(in[4], _c)
        |    node _e = eq(in[5], _d)
        |    node _f = head(_e, 1)
        |    out <= _f""".stripMargin
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "emit parentheses in the correct places" in {
    // should fail if any of these sub-expressions does not have parentheses
    val input =
      """
        |circuit TestParentheses :
        |  module TestParentheses :
        |    input in : UInt<1>[3]
        |    output out : UInt<1>[13]
        |
        |    out[0] <= mul(and(in[0], in[1]), in[2])
        |    out[1] <= div(and(in[0], in[1]), in[2])
        |    out[2] <= rem(and(in[0], in[1]), in[2])
        |    out[3] <= add(and(in[0], in[1]), in[2])
        |    out[4] <= sub(and(in[0], in[1]), in[2])
        |    out[5] <= dshl(in[0], and(in[1], in[2]))
        |    out[6] <= dshr(in[0], and(in[1], in[2]))
        |    out[7] <= lt(and(in[0], in[1]), in[2])
        |    out[8] <= gt(in[0], or(in[1], in[2]))
        |    out[9] <= eq(in[0], or(in[1], in[2]))
        |    out[10] <= neq(in[0], or(in[1], in[2]))
        |    out[11] <= and(in[0], xor(in[1], in[2]))
        |    out[12] <= xor(in[0], or(in[1], in[2]))
    """.stripMargin
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should "avoid inlining when it would create context-sensitivity bugs" in {
    val input =
      """circuit AddNot:
        |  module AddNot:
        |    input a: UInt<1>
        |    input b: UInt<1>
        |    output o: UInt<2>
        |    o <= add(a, not(b))""".stripMargin
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  // https://github.com/chipsalliance/firrtl/issues/2035
  // This is interesting because other ways of trying to express this get split out by
  // SplitExpressions and don't get inlined again
  // If we were to inline more expressions (ie. not just boolean ones) the issue this represents
  // would come up more often
  it should "handle cvt nested inside of a dshl" in {
    val input =
      """circuit DshlCvt:
        |  module DshlCvt:
        |    input a: UInt<4>
        |    input b: SInt<1>
        |    output o: UInt
        |    o <= dshl(a, asUInt(cvt(b)))""".stripMargin
    firrtlEquivalenceTest(input, Seq(new InlineBooleanExpressions))
  }

  it should s"respect --${PrettyNoExprInlining.longOption}" in {
    val input =
      """circuit Top :
        |  module Top :
        |    input a : UInt<1>
        |    input b : UInt<1>
        |    input c : UInt<1>
        |    output out : UInt<1>
        |
        |    node _T_1 = and(a, b)
        |    out <= and(_T_1, c)""".stripMargin
    val result = exec(parse(input), PrettyNoExprInlining :: Nil)
    (result) should be(parse(input).serialize)
  }
}