-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrtlil_kept_macros_test.py
More file actions
298 lines (265 loc) · 7.85 KB
/
rtlil_kept_macros_test.py
File metadata and controls
298 lines (265 loc) · 7.85 KB
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
#!/usr/bin/env python3
"""Unit tests for rtlil_kept_macros.py."""
import os
import tempfile
import unittest
from rtlil_kept_macros import (
_base,
build_base_to_full,
collect_macros_under,
derive_kept_macros,
format_dict,
parse_rtlil,
)
def _write(rtlil_text):
"""Materialise RTLIL text into a temp file. Returns path; caller unlinks."""
f = tempfile.NamedTemporaryFile(
mode="w",
suffix=".rtlil",
delete=False,
)
f.write(rtlil_text)
f.close()
return f.name
SIMPLE_RTLIL = """\
attribute \\blackbox 1
module \\sram
end
module \\helper
cell \\sram \\u_sram
end
end
attribute \\top 1
module \\top
cell \\helper \\u_helper
end
cell \\sram \\u_top_sram
end
end
"""
KEPT_HIERARCHY_RTLIL = """\
attribute \\blackbox 1
module \\sram_a
end
attribute \\blackbox 1
module \\sram_b
end
module \\inner_kept
cell \\sram_b \\u_b
end
end
module \\helper
cell \\inner_kept \\u_inner
end
cell \\sram_a \\u_a
end
end
attribute \\top 1
module \\top
cell \\helper \\u_helper
end
end
"""
# Slang elaborates a parameterised module instance to "Base$Path.with.dots".
SLANG_SUFFIX_RTLIL = """\
attribute \\blackbox 1
module \\sram
end
module \\worker$inst.path
cell \\sram \\u_sram
end
end
attribute \\top 1
module \\top
cell \\worker$inst.path \\u_worker
end
end
"""
class TestBase(unittest.TestCase):
def test_strips_slang_suffix(self):
self.assertEqual(_base("worker$inst.path"), "worker")
def test_no_suffix_is_identity(self):
self.assertEqual(_base("worker"), "worker")
class TestParseRtlil(unittest.TestCase):
def test_detects_top_attribute(self):
path = _write(SIMPLE_RTLIL)
try:
modules, top = parse_rtlil(path)
finally:
os.unlink(path)
self.assertEqual(top, "top")
self.assertIn("top", modules)
self.assertIn("helper", modules)
self.assertIn("sram", modules)
def test_collects_cell_instances(self):
path = _write(SIMPLE_RTLIL)
try:
modules, _ = parse_rtlil(path)
finally:
os.unlink(path)
# top has helper and sram cells.
top_cells = sorted(t for t, _ in modules["top"])
self.assertEqual(top_cells, ["helper", "sram"])
# sram is a blackbox — empty body.
self.assertEqual(modules["sram"], [])
def test_skips_yosys_builtin_cells(self):
"""Yosys built-in cells like $logic_not aren't real submodules."""
rtlil = (
"attribute \\top 1\n"
"module \\top\n"
" cell $logic_not $auto$001\n"
" end\n"
"end\n"
)
path = _write(rtlil)
try:
modules, _ = parse_rtlil(path)
finally:
os.unlink(path)
# The $-prefixed cell type doesn't match the `\\…` regex, so it
# never enters the cell list.
self.assertEqual(modules["top"], [])
def test_no_top_attribute_returns_none(self):
rtlil = "module \\m\nend\n"
path = _write(rtlil)
try:
_, top = parse_rtlil(path)
finally:
os.unlink(path)
self.assertIsNone(top)
class TestBuildBaseToFull(unittest.TestCase):
def test_groups_slang_variants(self):
modules = {
"worker$a": [],
"worker$b": [],
"top": [],
}
result = build_base_to_full(modules)
self.assertEqual(sorted(result["worker"]), ["worker$a", "worker$b"])
self.assertEqual(result["top"], ["top"])
class TestCollectMacrosUnder(unittest.TestCase):
def test_finds_macros_in_descendants(self):
path = _write(SIMPLE_RTLIL)
try:
modules, _ = parse_rtlil(path)
finally:
os.unlink(path)
by_base = build_base_to_full(modules)
found = collect_macros_under(
"top",
modules,
by_base,
kept_bases=set(),
macro_bases={"sram"},
)
self.assertEqual(found, {"sram"})
def test_stops_at_kept_descendants(self):
# top -> helper -> {inner_kept -> sram_b, sram_a}. With inner_kept
# marked kept, sram_b belongs to that partition, not top's.
path = _write(KEPT_HIERARCHY_RTLIL)
try:
modules, _ = parse_rtlil(path)
finally:
os.unlink(path)
by_base = build_base_to_full(modules)
top_found = collect_macros_under(
"top",
modules,
by_base,
kept_bases={"inner_kept"},
macro_bases={"sram_a", "sram_b"},
)
self.assertEqual(top_found, {"sram_a"})
inner_found = collect_macros_under(
"inner_kept",
modules,
by_base,
kept_bases={"inner_kept"},
macro_bases={"sram_a", "sram_b"},
)
self.assertEqual(inner_found, {"sram_b"})
def test_descends_through_slang_suffixed_cells(self):
path = _write(SLANG_SUFFIX_RTLIL)
try:
modules, _ = parse_rtlil(path)
finally:
os.unlink(path)
by_base = build_base_to_full(modules)
found = collect_macros_under(
"top",
modules,
by_base,
kept_bases=set(),
macro_bases={"sram"},
)
# The walk must follow worker$inst.path even though kept_macros
# references the base name "worker".
self.assertEqual(found, {"sram"})
class TestDeriveKeptMacros(unittest.TestCase):
def test_top_residue_under_top_key(self):
# With no kept modules, every macro is in the top residue.
path = _write(SIMPLE_RTLIL)
try:
modules, top = parse_rtlil(path)
finally:
os.unlink(path)
derived = derive_kept_macros(
modules,
top,
kept_modules=[],
macros=["sram"],
)
self.assertEqual(derived, {"_top": ["sram"]})
def test_kept_partition_subtracted_from_top(self):
path = _write(KEPT_HIERARCHY_RTLIL)
try:
modules, top = parse_rtlil(path)
finally:
os.unlink(path)
derived = derive_kept_macros(
modules,
top,
kept_modules=["inner_kept"],
macros=["sram_a", "sram_b"],
)
self.assertEqual(
derived,
{"inner_kept": ["sram_b"], "_top": ["sram_a"]},
)
def test_no_entry_for_kept_module_without_macros(self):
# A kept module that instantiates no macros gets no entry.
rtlil = (
"attribute \\blackbox 1\nmodule \\sram\nend\n"
"module \\empty_kept\nend\n"
"attribute \\top 1\nmodule \\top\n"
" cell \\empty_kept \\u_e\n end\n"
" cell \\sram \\u_s\n end\n"
"end\n"
)
path = _write(rtlil)
try:
modules, top = parse_rtlil(path)
finally:
os.unlink(path)
derived = derive_kept_macros(
modules,
top,
kept_modules=["empty_kept"],
macros=["sram"],
)
self.assertNotIn("empty_kept", derived)
self.assertEqual(derived["_top"], ["sram"])
class TestFormatDict(unittest.TestCase):
def test_empty_dict(self):
self.assertEqual(format_dict({}), "kept_macros = {}")
def test_sorted_keys_and_values(self):
text = format_dict({"b": ["y", "x"], "a": ["m"]})
# The function preserves the input order of macros within each
# value (sorting happens upstream in derive_kept_macros), but
# keys must be sorted for paste-ready stability.
self.assertTrue(text.startswith("kept_macros = {"))
self.assertIn('"a":', text)
self.assertIn('"b":', text)
self.assertLess(text.index('"a":'), text.index('"b":'))
if __name__ == "__main__":
unittest.main()