/
attributes.py
429 lines (354 loc) · 10.3 KB
/
attributes.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
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
426
427
428
429
# RUN: %PYTHON %s | FileCheck %s
import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
return f
# CHECK-LABEL: TEST: testParsePrint
@run
def testParsePrint():
with Context() as ctx:
t = Attribute.parse('"hello"')
assert t.context is ctx
ctx = None
gc.collect()
# CHECK: "hello"
print(str(t))
# CHECK: Attribute("hello")
print(repr(t))
# CHECK-LABEL: TEST: testParseError
# TODO: Hook the diagnostic manager to capture a more meaningful error
# message.
@run
def testParseError():
with Context():
try:
t = Attribute.parse("BAD_ATTR_DOES_NOT_EXIST")
except ValueError as e:
# CHECK: Unable to parse attribute: 'BAD_ATTR_DOES_NOT_EXIST'
print("testParseError:", e)
else:
print("Exception not produced")
# CHECK-LABEL: TEST: testAttrEq
@run
def testAttrEq():
with Context():
a1 = Attribute.parse('"attr1"')
a2 = Attribute.parse('"attr2"')
a3 = Attribute.parse('"attr1"')
# CHECK: a1 == a1: True
print("a1 == a1:", a1 == a1)
# CHECK: a1 == a2: False
print("a1 == a2:", a1 == a2)
# CHECK: a1 == a3: True
print("a1 == a3:", a1 == a3)
# CHECK: a1 == None: False
print("a1 == None:", a1 == None)
# CHECK-LABEL: TEST: testAttrHash
@run
def testAttrHash():
with Context():
a1 = Attribute.parse('"attr1"')
a2 = Attribute.parse('"attr2"')
a3 = Attribute.parse('"attr1"')
# CHECK: hash(a1) == hash(a3): True
print("hash(a1) == hash(a3):", a1.__hash__() == a3.__hash__())
s = set()
s.add(a1)
s.add(a2)
s.add(a3)
# CHECK: len(s): 2
print("len(s): ", len(s))
# CHECK-LABEL: TEST: testAttrCast
@run
def testAttrCast():
with Context():
a1 = Attribute.parse('"attr1"')
a2 = Attribute(a1)
# CHECK: a1 == a2: True
print("a1 == a2:", a1 == a2)
# CHECK-LABEL: TEST: testAttrIsInstance
@run
def testAttrIsInstance():
with Context():
a1 = Attribute.parse("42")
a2 = Attribute.parse("[42]")
assert IntegerAttr.isinstance(a1)
assert not IntegerAttr.isinstance(a2)
assert not ArrayAttr.isinstance(a1)
assert ArrayAttr.isinstance(a2)
# CHECK-LABEL: TEST: testAttrEqDoesNotRaise
@run
def testAttrEqDoesNotRaise():
with Context():
a1 = Attribute.parse('"attr1"')
not_an_attr = "foo"
# CHECK: False
print(a1 == not_an_attr)
# CHECK: False
print(a1 == None)
# CHECK: True
print(a1 != None)
# CHECK-LABEL: TEST: testAttrCapsule
@run
def testAttrCapsule():
with Context() as ctx:
a1 = Attribute.parse('"attr1"')
# CHECK: mlir.ir.Attribute._CAPIPtr
attr_capsule = a1._CAPIPtr
print(attr_capsule)
a2 = Attribute._CAPICreate(attr_capsule)
assert a2 == a1
assert a2.context is ctx
# CHECK-LABEL: TEST: testStandardAttrCasts
@run
def testStandardAttrCasts():
with Context():
a1 = Attribute.parse('"attr1"')
astr = StringAttr(a1)
aself = StringAttr(astr)
# CHECK: Attribute("attr1")
print(repr(astr))
try:
tillegal = StringAttr(Attribute.parse("1.0"))
except ValueError as e:
# CHECK: ValueError: Cannot cast attribute to StringAttr (from Attribute(1.000000e+00 : f64))
print("ValueError:", e)
else:
print("Exception not produced")
# CHECK-LABEL: TEST: testAffineMapAttr
@run
def testAffineMapAttr():
with Context() as ctx:
d0 = AffineDimExpr.get(0)
d1 = AffineDimExpr.get(1)
c2 = AffineConstantExpr.get(2)
map0 = AffineMap.get(2, 3, [])
# CHECK: affine_map<(d0, d1)[s0, s1, s2] -> ()>
attr_built = AffineMapAttr.get(map0)
print(str(attr_built))
attr_parsed = Attribute.parse(str(attr_built))
assert attr_built == attr_parsed
# CHECK-LABEL: TEST: testFloatAttr
@run
def testFloatAttr():
with Context(), Location.unknown():
fattr = FloatAttr(Attribute.parse("42.0 : f32"))
# CHECK: fattr value: 42.0
print("fattr value:", fattr.value)
# Test factory methods.
# CHECK: default_get: 4.200000e+01 : f32
print("default_get:", FloatAttr.get(
F32Type.get(), 42.0))
# CHECK: f32_get: 4.200000e+01 : f32
print("f32_get:", FloatAttr.get_f32(42.0))
# CHECK: f64_get: 4.200000e+01 : f64
print("f64_get:", FloatAttr.get_f64(42.0))
try:
fattr_invalid = FloatAttr.get(
IntegerType.get_signless(32), 42)
except ValueError as e:
# CHECK: invalid 'Type(i32)' and expected floating point type.
print(e)
else:
print("Exception not produced")
# CHECK-LABEL: TEST: testIntegerAttr
@run
def testIntegerAttr():
with Context() as ctx:
iattr = IntegerAttr(Attribute.parse("42"))
# CHECK: iattr value: 42
print("iattr value:", iattr.value)
# CHECK: iattr type: i64
print("iattr type:", iattr.type)
# Test factory methods.
# CHECK: default_get: 42 : i32
print("default_get:", IntegerAttr.get(
IntegerType.get_signless(32), 42))
# CHECK-LABEL: TEST: testBoolAttr
@run
def testBoolAttr():
with Context() as ctx:
battr = BoolAttr(Attribute.parse("true"))
# CHECK: iattr value: True
print("iattr value:", battr.value)
# Test factory methods.
# CHECK: default_get: true
print("default_get:", BoolAttr.get(True))
# CHECK-LABEL: TEST: testFlatSymbolRefAttr
@run
def testFlatSymbolRefAttr():
with Context() as ctx:
sattr = FlatSymbolRefAttr(Attribute.parse('@symbol'))
# CHECK: symattr value: symbol
print("symattr value:", sattr.value)
# Test factory methods.
# CHECK: default_get: @foobar
print("default_get:", FlatSymbolRefAttr.get("foobar"))
# CHECK-LABEL: TEST: testStringAttr
@run
def testStringAttr():
with Context() as ctx:
sattr = StringAttr(Attribute.parse('"stringattr"'))
# CHECK: sattr value: stringattr
print("sattr value:", sattr.value)
# Test factory methods.
# CHECK: default_get: "foobar"
print("default_get:", StringAttr.get("foobar"))
# CHECK: typed_get: "12345" : i32
print("typed_get:", StringAttr.get_typed(
IntegerType.get_signless(32), "12345"))
# CHECK-LABEL: TEST: testNamedAttr
@run
def testNamedAttr():
with Context():
a = Attribute.parse('"stringattr"')
named = a.get_named("foobar") # Note: under the small object threshold
# CHECK: attr: "stringattr"
print("attr:", named.attr)
# CHECK: name: foobar
print("name:", named.name)
# CHECK: named: NamedAttribute(foobar="stringattr")
print("named:", named)
# CHECK-LABEL: TEST: testDenseIntAttr
@run
def testDenseIntAttr():
with Context():
raw = Attribute.parse("dense<[[0,1,2],[3,4,5]]> : vector<2x3xi32>")
# CHECK: attr: dense<[{{\[}}0, 1, 2], [3, 4, 5]]>
print("attr:", raw)
a = DenseIntElementsAttr(raw)
assert len(a) == 6
# CHECK: 0 1 2 3 4 5
for value in a:
print(value, end=" ")
print()
# CHECK: i32
print(ShapedType(a.type).element_type)
raw = Attribute.parse("dense<[true,false,true,false]> : vector<4xi1>")
# CHECK: attr: dense<[true, false, true, false]>
print("attr:", raw)
a = DenseIntElementsAttr(raw)
assert len(a) == 4
# CHECK: 1 0 1 0
for value in a:
print(value, end=" ")
print()
# CHECK: i1
print(ShapedType(a.type).element_type)
# CHECK-LABEL: TEST: testDenseFPAttr
@run
def testDenseFPAttr():
with Context():
raw = Attribute.parse("dense<[0.0, 1.0, 2.0, 3.0]> : vector<4xf32>")
# CHECK: attr: dense<[0.000000e+00, 1.000000e+00, 2.000000e+00, 3.000000e+00]>
print("attr:", raw)
a = DenseFPElementsAttr(raw)
assert len(a) == 4
# CHECK: 0.0 1.0 2.0 3.0
for value in a:
print(value, end=" ")
print()
# CHECK: f32
print(ShapedType(a.type).element_type)
# CHECK-LABEL: TEST: testDictAttr
@run
def testDictAttr():
with Context():
dict_attr = {
'stringattr': StringAttr.get('string'),
'integerattr' : IntegerAttr.get(
IntegerType.get_signless(32), 42)
}
a = DictAttr.get(dict_attr)
# CHECK attr: {integerattr = 42 : i32, stringattr = "string"}
print("attr:", a)
assert len(a) == 2
# CHECK: 42 : i32
print(a['integerattr'])
# CHECK: "string"
print(a['stringattr'])
# CHECK: True
print('stringattr' in a)
# CHECK: False
print('not_in_dict' in a)
# Check that exceptions are raised as expected.
try:
_ = a['does_not_exist']
except KeyError:
pass
else:
assert False, "Exception not produced"
try:
_ = a[42]
except IndexError:
pass
else:
assert False, "expected IndexError on accessing an out-of-bounds attribute"
# CHECK "empty: {}"
print("empty: ", DictAttr.get())
# CHECK-LABEL: TEST: testTypeAttr
@run
def testTypeAttr():
with Context():
raw = Attribute.parse("vector<4xf32>")
# CHECK: attr: vector<4xf32>
print("attr:", raw)
type_attr = TypeAttr(raw)
# CHECK: f32
print(ShapedType(type_attr.value).element_type)
# CHECK-LABEL: TEST: testArrayAttr
@run
def testArrayAttr():
with Context():
raw = Attribute.parse("[42, true, vector<4xf32>]")
# CHECK: attr: [42, true, vector<4xf32>]
print("raw attr:", raw)
# CHECK: - 42
# CHECK: - true
# CHECK: - vector<4xf32>
for attr in ArrayAttr(raw):
print("- ", attr)
with Context():
intAttr = Attribute.parse("42")
vecAttr = Attribute.parse("vector<4xf32>")
boolAttr = BoolAttr.get(True)
raw = ArrayAttr.get([vecAttr, boolAttr, intAttr])
# CHECK: attr: [vector<4xf32>, true, 42]
print("raw attr:", raw)
# CHECK: - vector<4xf32>
# CHECK: - true
# CHECK: - 42
arr = ArrayAttr(raw)
for attr in arr:
print("- ", attr)
# CHECK: attr[0]: vector<4xf32>
print("attr[0]:", arr[0])
# CHECK: attr[1]: true
print("attr[1]:", arr[1])
# CHECK: attr[2]: 42
print("attr[2]:", arr[2])
try:
print("attr[3]:", arr[3])
except IndexError as e:
# CHECK: Error: ArrayAttribute index out of range
print("Error: ", e)
with Context():
try:
ArrayAttr.get([None])
except RuntimeError as e:
# CHECK: Error: Invalid attribute (None?) when attempting to create an ArrayAttribute
print("Error: ", e)
try:
ArrayAttr.get([42])
except RuntimeError as e:
# CHECK: Error: Invalid attribute when attempting to create an ArrayAttribute
print("Error: ", e)
with Context():
array = ArrayAttr.get([StringAttr.get("a"), StringAttr.get("b")])
array = array + [StringAttr.get("c")]
# CHECK: concat: ["a", "b", "c"]
print("concat: ", array)