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
|
// SPDX-License-Identifier: Apache-2.0
package firrtlTests
import firrtl.ir.Circuit
import firrtl._
import firrtl.passes.Pass
import firrtl.ir._
import firrtl.stage.{FirrtlSourceAnnotation, FirrtlStage, RunFirrtlTransformAnnotation}
import firrtl.options.Dependency
import firrtl.transforms.{IdentityTransform, LegalizeAndReductionsTransform}
import firrtl.testutils._
import firrtl.transforms.formal.ConvertAsserts
import scala.reflect.runtime
object CustomTransformSpec {
class ReplaceExtModuleTransform extends SeqTransform with FirrtlMatchers {
// Simple module
val delayModuleString = """
|circuit Delay :
| module Delay :
| input clock : Clock
| input reset : UInt<1>
| input a : UInt<32>
| input en : UInt<1>
| output b : UInt<32>
|
| reg r : UInt<32>, clock
| r <= r
| when en :
| r <= a
| b <= r
|""".stripMargin
val delayModuleCircuit = parse(delayModuleString)
val delayModule = delayModuleCircuit.modules.find(_.name == delayModuleCircuit.main).get
class ReplaceExtModule extends Pass {
def run(c: Circuit): Circuit = c.copy(
modules = c.modules.map {
case ExtModule(_, "Delay", _, _, _) => delayModule
case other => other
}
)
}
def transforms = Seq(new ReplaceExtModule)
def inputForm = LowForm
def outputForm = HighForm
}
val input = """
|circuit test :
| module test :
| output out : UInt
| out <= UInt(123)""".stripMargin
val errorString = "My Custom Transform failed!"
class ErroringTransform extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(state: CircuitState): CircuitState = {
require(false, errorString)
state
}
}
object MutableState {
var count: Int = 0
}
class FirstTransform extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(state: CircuitState): CircuitState = {
require(MutableState.count == 0, s"Count was ${MutableState.count}, expected 0")
MutableState.count = 1
state
}
}
class SecondTransform extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(state: CircuitState): CircuitState = {
require(MutableState.count == 1, s"Count was ${MutableState.count}, expected 1")
MutableState.count = 2
state
}
}
class ThirdTransform extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(state: CircuitState): CircuitState = {
require(MutableState.count == 2, s"Count was ${MutableState.count}, expected 2")
MutableState.count = 3
state
}
}
class IdentityLowForm extends IdentityTransform(LowForm) {
override val name = ">>>>> IdentityLowForm <<<<<"
}
object Foo {
class A extends Transform {
def inputForm = HighForm
def outputForm = HighForm
def execute(s: CircuitState) = {
assert(name.endsWith("A"))
s
}
}
}
}
class CustomTransformSpec extends FirrtlFlatSpec {
import CustomTransformSpec._
behavior.of("Custom Transforms")
they should "be able to introduce high firrtl" in {
runFirrtlTest("CustomTransform", "/features", customTransforms = List(new ReplaceExtModuleTransform))
}
they should "not cause \"Internal Errors\"" in {
val optionsManager = new ExecutionOptionsManager("test") with HasFirrtlOptions {
firrtlOptions = FirrtlExecutionOptions(firrtlSource = Some(input), customTransforms = List(new ErroringTransform))
}
(the[java.lang.IllegalArgumentException] thrownBy {
Driver.execute(optionsManager)
}).getMessage should include(errorString)
}
they should "preserve the input order" in {
runFirrtlTest(
"CustomTransform",
"/features",
customTransforms = List(
new FirstTransform,
new SecondTransform,
new ThirdTransform,
new ReplaceExtModuleTransform
)
)
}
they should "run right before the emitter* when inputForm=LowForm" in {
Seq(
Dependency[LowFirrtlEmitter],
Dependency[MinimumVerilogEmitter],
Dependency[VerilogEmitter],
Dependency[SystemVerilogEmitter]
).foreach { emitter =>
val custom = Dependency[IdentityLowForm]
val tm = new firrtl.stage.transforms.Compiler(custom :: emitter :: Nil)
info(s"when using ${emitter.getObject.name}")
tm.flattenedTransformOrder
.map(Dependency.fromTransform)
.sliding(2)
.toList should contain(Seq(custom, emitter))
}
}
they should "work if placed inside an object" in {
val input =
"""|circuit Foo:
| module Foo:
| node a = UInt<1>(0)
|""".stripMargin
val annotations = Seq(
RunFirrtlTransformAnnotation(new Foo.A),
FirrtlSourceAnnotation(input)
)
(new FirrtlStage).execute(Array.empty, annotations)
}
}
|