summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/AnnotationNoDedup.scala
blob: d93da31f7d282cca39e100ce90acfc5d89bef879 (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
// See LICENSE for license details.

package chiselTests

import chisel3._
import chisel3.experimental.{annotate, ChiselAnnotation}
import firrtl.FirrtlExecutionSuccess
import firrtl.transforms.NoDedupAnnotation
import org.scalatest.{FreeSpec, Matchers}

object doNotDedup {
  def apply(module: Module): Unit = {
    annotate(new ChiselAnnotation { def toFirrtl = NoDedupAnnotation(module.toNamed) })
  }
}

class MuchUsedModule extends Module {
  val io = IO(new Bundle {
    val in = Input(UInt(16.W))
    val out = Output(UInt(16.W))
  })
  io.out := io.in +% 1.U
}

class UsesMuchUsedModule(addAnnos: Boolean) extends Module {
  val io = IO(new Bundle {
    val in = Input(UInt(16.W))
    val out = Output(UInt(16.W))
  })

  val mod0 = Module(new MuchUsedModule)
  val mod1 = Module(new MuchUsedModule)
  val mod2 = Module(new MuchUsedModule)
  val mod3 = Module(new MuchUsedModule)

  mod0.io.in := io.in
  mod1.io.in := mod0.io.out
  mod2.io.in := mod1.io.out
  mod3.io.in := mod2.io.out
  io.out := mod3.io.out

  if(addAnnos) {
    doNotDedup(mod1)
    doNotDedup(mod3)
  }
}

class AnnotationNoDedup extends FreeSpec with Matchers {
  "Firrtl provides transform that reduces identical modules to a single instance" - {
    "Annotations can be added which will prevent this deduplication for specific modules instances" in {
      Driver.execute(Array("-X", "low", "--target-dir", "test_run_dir"), () => new UsesMuchUsedModule(addAnnos = true)) match {
        case ChiselExecutionSuccess(_, _, Some(firrtlResult: FirrtlExecutionSuccess)) =>
          val lowFirrtl = firrtlResult.emitted

          lowFirrtl should include ("module MuchUsedModule :")
          lowFirrtl should include ("module MuchUsedModule_1 :")
          lowFirrtl should include ("module MuchUsedModule_3 :")
          lowFirrtl should not include "module MuchUsedModule_2 :"
          lowFirrtl should not include "module MuchUsedModule_4 :"
        case _ =>
      }
    }
    "Turning off these annotations dedups all the occurrences" in {
      Driver.execute(Array("-X", "low", "--target-dir", "test_run_dir"), () => new UsesMuchUsedModule(addAnnos = false)) match {
        case ChiselExecutionSuccess(_, _, Some(firrtlResult: FirrtlExecutionSuccess)) =>
          val lowFirrtl = firrtlResult.emitted

          lowFirrtl should include ("module MuchUsedModule :")
          lowFirrtl should not include "module MuchUsedModule_1 :"
          lowFirrtl should not include "module MuchUsedModule_3 :"
          lowFirrtl should not include "module MuchUsedModule_2 :"
          lowFirrtl should not include "module MuchUsedModule_4 :"
        case _ =>
      }
    }
  }
}