-
Notifications
You must be signed in to change notification settings - Fork 277
/
om.py
299 lines (238 loc) · 8.56 KB
/
om.py
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
# REQUIRES: bindings_python
# RUN: %PYTHON% %s | FileCheck %s
import circt
from circt.dialects import om
from circt.ir import Context, InsertionPoint, Location, Module, IntegerAttr, IntegerType
from circt.support import var_to_attribute
from dataclasses import dataclass
from typing import Dict
with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
module = Module.parse("""
module {
om.class @node() {
%0 = om.constant #om.list<!om.string,["MyThing" : !om.string]> : !om.list<!om.string>
%1 = om.constant "Component.inst1.foo" : !om.string
om.class.field @field2, %1 : !om.string
}
om.class @comp(
%inst1_propOut_bore: !om.class.type<@node>,
%inst2_propOut_bore: !om.class.type<@node>) {
om.class.field @field2, %inst1_propOut_bore : !om.class.type<@node>
om.class.field @field3, %inst2_propOut_bore : !om.class.type<@node>
}
om.class @Client() {
%0 = om.object @node() : () -> !om.class.type<@node>
%2 = om.object @comp(%0, %0) : (!om.class.type<@node>, !om.class.type<@node>) -> !om.class.type<@comp>
om.class.field @client_omnode_0_OMIROut, %2 : !om.class.type<@comp>
om.class.field @node0_OMIROut, %0 : !om.class.type<@node>
om.class.field @node1_OMIROut, %0 : !om.class.type<@node>
}
om.class @Test(%param: !om.integer) {
%sym = om.constant #om.ref<<@Root::@x>> : !om.ref
om.class.field @field, %param : !om.integer
%c_14 = om.constant #om.integer<14> : !om.integer
%0 = om.object @Child(%c_14) : (!om.integer) -> !om.class.type<@Child>
om.class.field @child, %0 : !om.class.type<@Child>
om.class.field @reference, %sym : !om.ref
%list = om.constant #om.list<!om.string, ["X" : !om.string, "Y" : !om.string]> : !om.list<!om.string>
om.class.field @list, %list : !om.list<!om.string>
%tuple = om.tuple_create %list, %c_14: !om.list<!om.string>, !om.integer
om.class.field @tuple, %tuple : tuple<!om.list<!om.string>, !om.integer>
%c_15 = om.constant #om.integer<15> : !om.integer
%1 = om.object @Child(%c_15) : (!om.integer) -> !om.class.type<@Child>
%list_child = om.list_create %0, %1: !om.class.type<@Child>
%2 = om.object @Nest(%list_child) : (!om.list<!om.class.type<@Child>>) -> !om.class.type<@Nest>
om.class.field @nest, %2 : !om.class.type<@Nest>
%3 = om.constant #om.map<!om.integer, {a = #om.integer<42>, b = #om.integer<32>}> : !om.map<!om.string, !om.integer>
om.class.field @map, %3 : !om.map<!om.string, !om.integer>
%x = om.constant "X" : !om.string
%y = om.constant "Y" : !om.string
%entry1 = om.tuple_create %x, %c_14: !om.string, !om.integer
%entry2 = om.tuple_create %y, %c_15: !om.string, !om.integer
%map = om.map_create %entry1, %entry2: !om.string, !om.integer
om.class.field @map_create, %map : !om.map<!om.string, !om.integer>
}
om.class @Child(%0: !om.integer) {
om.class.field @foo, %0 : !om.integer
}
om.class @Nest(%0: !om.list<!om.class.type<@Child>>) {
om.class.field @list_child, %0 : !om.list<!om.class.type<@Child>>
}
hw.module @Root(in %clock: i1) {
%0 = sv.wire sym @x : !hw.inout<i1>
}
om.class @Paths(%basepath: !om.frozenbasepath) {
%0 = om.frozenbasepath_create %basepath "Foo/bar"
%1 = om.frozenpath_create reference %0 "Bar/baz:Baz>w"
om.class.field @path, %1 : !om.frozenpath
%3 = om.frozenpath_empty
om.class.field @deleted, %3 : !om.frozenpath
}
om.class @Class1(%input: !om.integer) {
%0 = om.constant #om.integer<1 : si3> : !om.integer
om.class.field @value, %0 : !om.integer
om.class.field @input, %input : !om.integer
}
om.class @Class2() {
%0 = om.constant #om.integer<2 : si3> : !om.integer
om.class.field @value, %0 : !om.integer
}
om.class @IntegerBinaryArithmeticObjectsDelayed() {
%0 = om.object @Class1(%5) : (!om.integer) -> !om.class.type<@Class1>
%1 = om.object.field %0, [@value] : (!om.class.type<@Class1>) -> !om.integer
%2 = om.object @Class2() : () -> !om.class.type<@Class2>
%3 = om.object.field %2, [@value] : (!om.class.type<@Class2>) -> !om.integer
%5 = om.integer.add %1, %3 : !om.integer
om.class.field @result, %5 : !om.integer
}
}
""")
evaluator = om.Evaluator(module)
# Test instantiate failure.
try:
obj = evaluator.instantiate("Test")
except ValueError as e:
# CHECK: actual parameter list length (0) does not match
# CHECK: actual parameters:
# CHECK: formal parameters:
# CHECK: unable to instantiate object, see previous error(s)
print(e)
# Test get field failure.
try:
obj = evaluator.instantiate("Test", 42)
obj.foo
except ValueError as e:
# CHECK: field "foo" does not exist
# CHECK: see current operation:
# CHECK: unable to get field, see previous error(s)
print(e)
# Test instantiate success.
obj = evaluator.instantiate("Test", 42)
assert isinstance(obj.type, om.ClassType)
# CHECK: Test
print(obj.type.name)
# CHECK: 42
print(obj.field)
# location of the om.class.field @field
# CHECK: loc("-":27:7)
print(obj.get_field_loc("field"))
# CHECK: 14
print(obj.child.foo)
# CHECK: loc("-":60:7)
print(obj.child.get_field_loc("foo"))
# CHECK: ('Root', 'x')
print(obj.reference)
(fst, snd) = obj.tuple
# CHECK: 14
print(snd)
# CHECK: loc("-":39:7)
print(obj.get_field_loc("tuple"))
# CHECK: loc("-":25:5)
print(obj.loc)
try:
print(obj.tuple[3])
except IndexError as e:
# CHECK: tuple index out of range
print(e)
for (name, field) in obj:
# location from om.class.field @child, %0 : !om.class.type<@Child>
# CHECK: name: child, field: <circt.dialects.om.Object object
# CHECK-SAME: loc: loc("-":30:12)
# location from om.class.field @field, %param : !om.integer
# CHECK: name: field, field: 42
# CHECK-SAME: loc: loc("-":27:7)
# location from om.class.field @reference, %sym : !om.ref
# CHECK: name: reference, field: ('Root', 'x')
# CHECK-SAME: loc: loc("-":33:7)
loc = obj.get_field_loc(name)
print(f"name: {name}, field: {field}, loc: {loc}")
# CHECK: ['X', 'Y']
print(obj.list)
for child in obj.nest.list_child:
# CHECK: 14
# CHECK-NEXT: 15
print(child.foo)
# CHECK: 2
print(len(obj.map))
# CHECK: {'a': 42, 'b': 32}
print(obj.map)
for k, v in obj.map.items():
# CHECK-NEXT: a 42
# CHECK-NEXT: b 32
print(k, v)
try:
print(obj.map_create[1])
except KeyError as e:
# CHECK-NEXT: 'key is not integer'
print(e)
try:
print(obj.map_create["INVALID"])
except KeyError as e:
# CHECK-NEXT: 'key not found'
print(e)
# CHECK-NEXT: 14
print(obj.map_create["X"])
for k, v in obj.map_create.items():
# CHECK-NEXT: X 14
# CHECK-NEXT: Y 15
print(k, v)
obj = evaluator.instantiate("Client")
object_dict: Dict[om.Object, str] = {}
for field_name, data in obj:
if isinstance(data, om.Object):
object_dict[data] = field_name
assert len(object_dict) == 2
obj = evaluator.instantiate("Test", 41)
# CHECK: 41
print(obj.field)
path = om.BasePath.get_empty(evaluator.module.context)
obj = evaluator.instantiate("Paths", path)
print(obj.path)
# CHECK: OMReferenceTarget:~Foo|Foo/bar:Bar/baz:Baz>w
print(obj.deleted)
# CHECK: OMDeleted
paths_class = [
cls for cls in module.body
if hasattr(cls, "sym_name") and cls.sym_name.value == "Paths"
][0]
base_path_type = paths_class.regions[0].blocks[0].arguments[0].type
assert isinstance(base_path_type, om.BasePathType)
paths_fields = [
op for op in paths_class.regions[0].blocks[0]
if isinstance(op, om.ClassFieldOp)
]
for paths_field in paths_fields:
assert isinstance(paths_field.value.type, om.PathType)
delayed = evaluator.instantiate("IntegerBinaryArithmeticObjectsDelayed")
# CHECK: 3
print(delayed.result)
with Context() as ctx:
circt.register_dialects(ctx)
# Signless
int_attr1 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signless(64), 42))
# CHECK: 42
print(str(int_attr1))
int_attr2 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signless(64), -42))
# CHECK: 18446744073709551574
print(str(int_attr2))
# Signed
int_attr3 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signed(64), 42))
# CHECK: 42
print(str(int_attr3))
int_attr4 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signed(64), -42))
# CHECK: -42
print(str(int_attr4))
# Unsigned
int_attr5 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_unsigned(64), 42))
# CHECK: 42
print(str(int_attr5))
int_attr6 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_unsigned(64), -42))
# CHECK: 18446744073709551574
print(str(int_attr6))