[clang-tidy][NFC]remove deps of clang in clang tidy test (#116588)
[llvm-project.git] / mlir / test / python / ir / attributes.py
blob00c3e1b4decdb72e2aa26de7b71a17eddae05be4
1 # RUN: %PYTHON %s | FileCheck %s
3 import gc
5 from mlir.ir import *
8 def run(f):
9 print("\nTEST:", f.__name__)
10 f()
11 gc.collect()
12 assert Context._get_live_count() == 0
13 return f
16 # CHECK-LABEL: TEST: testParsePrint
17 @run
18 def testParsePrint():
19 with Context() as ctx:
20 t = Attribute.parse('"hello"')
21 assert t.context is ctx
22 ctx = None
23 gc.collect()
24 # CHECK: "hello"
25 print(str(t))
26 # CHECK: StringAttr("hello")
27 print(repr(t))
30 # CHECK-LABEL: TEST: testParseError
31 @run
32 def testParseError():
33 with Context():
34 try:
35 t = Attribute.parse("BAD_ATTR_DOES_NOT_EXIST")
36 except MLIRError as e:
37 # CHECK: testParseError: <
38 # CHECK: Unable to parse attribute:
39 # CHECK: error: "BAD_ATTR_DOES_NOT_EXIST":1:1: expected attribute value
40 # CHECK: >
41 print(f"testParseError: <{e}>")
42 else:
43 print("Exception not produced")
46 # CHECK-LABEL: TEST: testAttrEq
47 @run
48 def testAttrEq():
49 with Context():
50 a1 = Attribute.parse('"attr1"')
51 a2 = Attribute.parse('"attr2"')
52 a3 = Attribute.parse('"attr1"')
53 # CHECK: a1 == a1: True
54 print("a1 == a1:", a1 == a1)
55 # CHECK: a1 == a2: False
56 print("a1 == a2:", a1 == a2)
57 # CHECK: a1 == a3: True
58 print("a1 == a3:", a1 == a3)
59 # CHECK: a1 is None: False
60 print("a1 is None:", a1 is None)
63 # CHECK-LABEL: TEST: testAttrHash
64 @run
65 def testAttrHash():
66 with Context():
67 a1 = Attribute.parse('"attr1"')
68 a2 = Attribute.parse('"attr2"')
69 a3 = Attribute.parse('"attr1"')
70 # CHECK: hash(a1) == hash(a3): True
71 print("hash(a1) == hash(a3):", a1.__hash__() == a3.__hash__())
73 s = set()
74 s.add(a1)
75 s.add(a2)
76 s.add(a3)
77 # CHECK: len(s): 2
78 print("len(s): ", len(s))
81 # CHECK-LABEL: TEST: testAttrCast
82 @run
83 def testAttrCast():
84 with Context():
85 a1 = Attribute.parse('"attr1"')
86 a2 = Attribute(a1)
87 # CHECK: a1 == a2: True
88 print("a1 == a2:", a1 == a2)
91 # CHECK-LABEL: TEST: testAttrIsInstance
92 @run
93 def testAttrIsInstance():
94 with Context():
95 a1 = Attribute.parse("42")
96 a2 = Attribute.parse("[42]")
97 assert IntegerAttr.isinstance(a1)
98 assert not IntegerAttr.isinstance(a2)
99 assert not ArrayAttr.isinstance(a1)
100 assert ArrayAttr.isinstance(a2)
103 # CHECK-LABEL: TEST: testAttrEqDoesNotRaise
104 @run
105 def testAttrEqDoesNotRaise():
106 with Context():
107 a1 = Attribute.parse('"attr1"')
108 not_an_attr = "foo"
109 # CHECK: False
110 print(a1 == not_an_attr)
111 # CHECK: False
112 print(a1 is None)
113 # CHECK: True
114 print(a1 is not None)
117 # CHECK-LABEL: TEST: testAttrCapsule
118 @run
119 def testAttrCapsule():
120 with Context() as ctx:
121 a1 = Attribute.parse('"attr1"')
122 # CHECK: mlir.ir.Attribute._CAPIPtr
123 attr_capsule = a1._CAPIPtr
124 print(attr_capsule)
125 a2 = Attribute._CAPICreate(attr_capsule)
126 assert a2 == a1
127 assert a2.context is ctx
130 # CHECK-LABEL: TEST: testStandardAttrCasts
131 @run
132 def testStandardAttrCasts():
133 with Context():
134 a1 = Attribute.parse('"attr1"')
135 astr = StringAttr(a1)
136 aself = StringAttr(astr)
137 # CHECK: StringAttr("attr1")
138 print(repr(astr))
139 try:
140 tillegal = StringAttr(Attribute.parse("1.0"))
141 except ValueError as e:
142 # CHECK: ValueError: Cannot cast attribute to StringAttr (from Attribute(1.000000e+00 : f64))
143 print("ValueError:", e)
144 else:
145 print("Exception not produced")
148 # CHECK-LABEL: TEST: testAffineMapAttr
149 @run
150 def testAffineMapAttr():
151 with Context() as ctx:
152 d0 = AffineDimExpr.get(0)
153 d1 = AffineDimExpr.get(1)
154 c2 = AffineConstantExpr.get(2)
155 map0 = AffineMap.get(2, 3, [])
157 # CHECK: affine_map<(d0, d1)[s0, s1, s2] -> ()>
158 attr_built = AffineMapAttr.get(map0)
159 print(str(attr_built))
160 assert attr_built.value == map0
161 attr_parsed = Attribute.parse(str(attr_built))
162 assert attr_built == attr_parsed
165 # CHECK-LABEL: TEST: testIntegerSetAttr
166 @run
167 def testIntegerSetAttr():
168 with Context() as ctx:
169 d0 = AffineDimExpr.get(0)
170 d1 = AffineDimExpr.get(1)
171 s0 = AffineSymbolExpr.get(0)
172 c42 = AffineConstantExpr.get(42)
173 set0 = IntegerSet.get(2, 1, [d0 - d1, s0 - c42], [True, False])
175 # CHECK: affine_set<(d0, d1)[s0] : (d0 - d1 == 0, s0 - 42 >= 0)>
176 attr_built = IntegerSetAttr.get(set0)
177 print(str(attr_built))
179 attr_parsed = Attribute.parse(str(attr_built))
180 assert attr_built == attr_parsed
183 # CHECK-LABEL: TEST: testFloatAttr
184 @run
185 def testFloatAttr():
186 with Context(), Location.unknown():
187 fattr = FloatAttr(Attribute.parse("42.0 : f32"))
188 # CHECK: fattr value: 42.0
189 print("fattr value:", fattr.value)
190 # CHECK: fattr float: 42.0 <class 'float'>
191 print("fattr float:", float(fattr), type(float(fattr)))
193 # Test factory methods.
194 # CHECK: default_get: 4.200000e+01 : f32
195 print("default_get:", FloatAttr.get(F32Type.get(), 42.0))
196 # CHECK: f32_get: 4.200000e+01 : f32
197 print("f32_get:", FloatAttr.get_f32(42.0))
198 # CHECK: f64_get: 4.200000e+01 : f64
199 print("f64_get:", FloatAttr.get_f64(42.0))
200 try:
201 fattr_invalid = FloatAttr.get(IntegerType.get_signless(32), 42)
202 except MLIRError as e:
203 # CHECK: Invalid attribute:
204 # CHECK: error: unknown: expected floating point type
205 print(e)
206 else:
207 print("Exception not produced")
210 # CHECK-LABEL: TEST: testIntegerAttr
211 @run
212 def testIntegerAttr():
213 with Context() as ctx:
214 i_attr = IntegerAttr(Attribute.parse("42"))
215 # CHECK: i_attr value: 42
216 print("i_attr value:", i_attr.value)
217 # CHECK: i_attr type: i64
218 print("i_attr type:", i_attr.type)
219 # CHECK: i_attr int: 42 <class 'int'>
220 print("i_attr int:", int(i_attr), type(int(i_attr)))
221 si_attr = IntegerAttr(Attribute.parse("-1 : si8"))
222 # CHECK: si_attr value: -1
223 print("si_attr value:", si_attr.value)
224 ui_attr = IntegerAttr(Attribute.parse("255 : ui8"))
225 # CHECK: i_attr int: -1 <class 'int'>
226 print("si_attr int:", int(si_attr), type(int(si_attr)))
227 # CHECK: ui_attr value: 255
228 print("ui_attr value:", ui_attr.value)
229 # CHECK: i_attr int: 255 <class 'int'>
230 print("ui_attr int:", int(ui_attr), type(int(ui_attr)))
231 idx_attr = IntegerAttr(Attribute.parse("-1 : index"))
232 # CHECK: idx_attr value: -1
233 print("idx_attr value:", idx_attr.value)
234 # CHECK: idx_attr int: -1 <class 'int'>
235 print("idx_attr int:", int(idx_attr), type(int(idx_attr)))
237 # Test factory methods.
238 # CHECK: default_get: 42 : i32
239 print("default_get:", IntegerAttr.get(IntegerType.get_signless(32), 42))
242 # CHECK-LABEL: TEST: testBoolAttr
243 @run
244 def testBoolAttr():
245 with Context() as ctx:
246 battr = BoolAttr(Attribute.parse("true"))
247 # CHECK: iattr value: True
248 print("iattr value:", battr.value)
249 # CHECK: iattr bool: True <class 'bool'>
250 print("iattr bool:", bool(battr), type(bool(battr)))
252 # Test factory methods.
253 # CHECK: default_get: true
254 print("default_get:", BoolAttr.get(True))
257 # CHECK-LABEL: TEST: testFlatSymbolRefAttr
258 @run
259 def testFlatSymbolRefAttr():
260 with Context() as ctx:
261 sattr = Attribute.parse("@symbol")
262 # CHECK: symattr value: symbol
263 print("symattr value:", sattr.value)
265 # Test factory methods.
266 # CHECK: default_get: @foobar
267 print("default_get:", FlatSymbolRefAttr.get("foobar"))
270 # CHECK-LABEL: TEST: testSymbolRefAttr
271 @run
272 def testSymbolRefAttr():
273 with Context() as ctx:
274 sattr = Attribute.parse("@symbol1::@symbol2")
275 # CHECK: symattr value: ['symbol1', 'symbol2']
276 print("symattr value:", sattr.value)
278 # CHECK: default_get: @symbol1::@symbol2
279 print("default_get:", SymbolRefAttr.get(["symbol1", "symbol2"]))
281 # CHECK: default_get: @"@symbol1"::@"@symbol2"
282 print("default_get:", SymbolRefAttr.get(["@symbol1", "@symbol2"]))
285 # CHECK-LABEL: TEST: testOpaqueAttr
286 @run
287 def testOpaqueAttr():
288 with Context() as ctx:
289 ctx.allow_unregistered_dialects = True
290 oattr = OpaqueAttr(Attribute.parse("#pytest_dummy.dummyattr<>"))
291 # CHECK: oattr value: pytest_dummy
292 print("oattr value:", oattr.dialect_namespace)
293 # CHECK: oattr value: b'dummyattr<>'
294 print("oattr value:", oattr.data)
296 # Test factory methods.
297 # CHECK: default_get: #foobar<123>
298 print(
299 "default_get:",
300 OpaqueAttr.get("foobar", bytes("123", "utf-8"), NoneType.get()),
304 # CHECK-LABEL: TEST: testStringAttr
305 @run
306 def testStringAttr():
307 with Context() as ctx:
308 sattr = StringAttr(Attribute.parse('"stringattr"'))
309 # CHECK: sattr value: stringattr
310 print("sattr value:", sattr.value)
311 # CHECK: sattr value: b'stringattr'
312 print("sattr value:", sattr.value_bytes)
314 # Test factory methods.
315 # CHECK: default_get: "foobar"
316 print("default_get:", StringAttr.get("foobar"))
317 # CHECK: typed_get: "12345" : i32
318 print("typed_get:", StringAttr.get_typed(IntegerType.get_signless(32), "12345"))
321 # CHECK-LABEL: TEST: testNamedAttr
322 @run
323 def testNamedAttr():
324 with Context():
325 a = Attribute.parse('"stringattr"')
326 named = a.get_named("foobar") # Note: under the small object threshold
327 # CHECK: attr: "stringattr"
328 print("attr:", named.attr)
329 # CHECK: name: foobar
330 print("name:", named.name)
331 # CHECK: named: NamedAttribute(foobar="stringattr")
332 print("named:", named)
335 # CHECK-LABEL: TEST: testDenseIntAttr
336 @run
337 def testDenseIntAttr():
338 with Context():
339 raw = Attribute.parse("dense<[[0,1,2],[3,4,5]]> : vector<2x3xi32>")
340 # CHECK: attr: dense<[{{\[}}0, 1, 2], [3, 4, 5]]>
341 print("attr:", raw)
343 a = DenseIntElementsAttr(raw)
344 assert len(a) == 6
346 # CHECK: 0 1 2 3 4 5
347 for value in a:
348 print(value, end=" ")
349 print()
351 # CHECK: i32
352 print(ShapedType(a.type).element_type)
354 raw = Attribute.parse("dense<[true,false,true,false]> : vector<4xi1>")
355 # CHECK: attr: dense<[true, false, true, false]>
356 print("attr:", raw)
358 a = DenseIntElementsAttr(raw)
359 assert len(a) == 4
361 # CHECK: 1 0 1 0
362 for value in a:
363 print(value, end=" ")
364 print()
366 # CHECK: i1
367 print(ShapedType(a.type).element_type)
370 @run
371 def testDenseArrayGetItem():
372 def print_item(attr_asm):
373 attr = Attribute.parse(attr_asm)
374 print(f"{len(attr)}: {attr[0]}, {attr[1]}")
376 with Context():
377 # CHECK: 2: False, True
378 print_item("array<i1: false, true>")
379 # CHECK: 2: 2, 3
380 print_item("array<i8: 2, 3>")
381 # CHECK: 2: 4, 5
382 print_item("array<i16: 4, 5>")
383 # CHECK: 2: 6, 7
384 print_item("array<i32: 6, 7>")
385 # CHECK: 2: 8, 9
386 print_item("array<i64: 8, 9>")
387 # CHECK: 2: 1.{{0+}}, 2.{{0+}}
388 print_item("array<f32: 1.0, 2.0>")
389 # CHECK: 2: 3.{{0+}}, 4.{{0+}}
390 print_item("array<f64: 3.0, 4.0>")
392 class MyBool:
393 def __bool__(self):
394 return True
396 # CHECK: myboolarray: array<i1: true>
397 print("myboolarray:", DenseBoolArrayAttr.get([MyBool()]))
400 # CHECK-LABEL: TEST: testDenseArrayAttrConstruction
401 @run
402 def testDenseArrayAttrConstruction():
403 with Context(), Location.unknown():
405 def create_and_print(cls, x):
406 try:
407 darr = cls.get(x)
408 print(f"input: {x} ({type(x)}), result: {darr}")
409 except Exception as ex:
410 print(f"input: {x} ({type(x)}), error: {ex}")
412 # CHECK: input: [4, 2] (<class 'list'>),
413 # CHECK-SAME: result: array<i8: 4, 2>
414 create_and_print(DenseI8ArrayAttr, [4, 2])
416 # CHECK: input: [4, 2.0] (<class 'list'>),
417 # CHECK-SAME: error: get(): incompatible function arguments
418 create_and_print(DenseI8ArrayAttr, [4, 2.0])
420 # CHECK: input: [40000, 2] (<class 'list'>),
421 # CHECK-SAME: error: get(): incompatible function arguments
422 create_and_print(DenseI8ArrayAttr, [40000, 2])
424 # CHECK: input: range(0, 4) (<class 'range'>),
425 # CHECK-SAME: result: array<i8: 0, 1, 2, 3>
426 create_and_print(DenseI8ArrayAttr, range(4))
428 # CHECK: input: [IntegerAttr(4 : i64), IntegerAttr(2 : i64)] (<class 'list'>),
429 # CHECK-SAME: result: array<i8: 4, 2>
430 create_and_print(DenseI8ArrayAttr, [Attribute.parse(f"{x}") for x in [4, 2]])
432 # CHECK: input: [IntegerAttr(4000 : i64), IntegerAttr(2 : i64)] (<class 'list'>),
433 # CHECK-SAME: error: get(): incompatible function arguments
434 create_and_print(DenseI8ArrayAttr, [Attribute.parse(f"{x}") for x in [4000, 2]])
436 # CHECK: input: [IntegerAttr(4 : i64), FloatAttr(2.000000e+00 : f64)] (<class 'list'>),
437 # CHECK-SAME: error: get(): incompatible function arguments
438 create_and_print(DenseI8ArrayAttr, [Attribute.parse(f"{x}") for x in [4, 2.0]])
440 # CHECK: input: [IntegerAttr(4 : i8), IntegerAttr(2 : ui16)] (<class 'list'>),
441 # CHECK-SAME: result: array<i8: 4, 2>
442 create_and_print(
443 DenseI8ArrayAttr, [Attribute.parse(s) for s in ["4 : i8", "2 : ui16"]]
446 # CHECK: input: [FloatAttr(4.000000e+00 : f64), FloatAttr(2.000000e+00 : f64)] (<class 'list'>)
447 # CHECK-SAME: result: array<f32: 4.000000e+00, 2.000000e+00>
448 create_and_print(
449 DenseF32ArrayAttr, [Attribute.parse(f"{x}") for x in [4.0, 2.0]]
452 # CHECK: [BoolAttr(true), BoolAttr(false)] (<class 'list'>),
453 # CHECK-SAME: result: array<i1: true, false>
454 create_and_print(
455 DenseBoolArrayAttr, [Attribute.parse(f"{x}") for x in ["true", "false"]]
459 # CHECK-LABEL: TEST: testDenseIntAttrGetItem
460 @run
461 def testDenseIntAttrGetItem():
462 def print_item(attr_asm):
463 attr = Attribute.parse(attr_asm)
464 dtype = ShapedType(attr.type).element_type
465 try:
466 item = attr[0]
467 print(f"{dtype}:", item)
468 except TypeError as e:
469 print(f"{dtype}:", e)
471 with Context():
472 # CHECK: i1: 1
473 print_item("dense<true> : tensor<i1>")
474 # CHECK: i8: 123
475 print_item("dense<123> : tensor<i8>")
476 # CHECK: i16: 123
477 print_item("dense<123> : tensor<i16>")
478 # CHECK: i32: 123
479 print_item("dense<123> : tensor<i32>")
480 # CHECK: i64: 123
481 print_item("dense<123> : tensor<i64>")
482 # CHECK: ui8: 123
483 print_item("dense<123> : tensor<ui8>")
484 # CHECK: ui16: 123
485 print_item("dense<123> : tensor<ui16>")
486 # CHECK: ui32: 123
487 print_item("dense<123> : tensor<ui32>")
488 # CHECK: ui64: 123
489 print_item("dense<123> : tensor<ui64>")
490 # CHECK: si8: -123
491 print_item("dense<-123> : tensor<si8>")
492 # CHECK: si16: -123
493 print_item("dense<-123> : tensor<si16>")
494 # CHECK: si32: -123
495 print_item("dense<-123> : tensor<si32>")
496 # CHECK: si64: -123
497 print_item("dense<-123> : tensor<si64>")
499 # CHECK: i7: Unsupported integer type
500 print_item("dense<123> : tensor<i7>")
503 # CHECK-LABEL: TEST: testDenseFPAttr
504 @run
505 def testDenseFPAttr():
506 with Context():
507 raw = Attribute.parse("dense<[0.0, 1.0, 2.0, 3.0]> : vector<4xf32>")
508 # CHECK: attr: dense<[0.000000e+00, 1.000000e+00, 2.000000e+00, 3.000000e+00]>
510 print("attr:", raw)
512 a = DenseFPElementsAttr(raw)
513 assert len(a) == 4
515 # CHECK: 0.0 1.0 2.0 3.0
516 for value in a:
517 print(value, end=" ")
518 print()
520 # CHECK: f32
521 print(ShapedType(a.type).element_type)
524 # CHECK-LABEL: TEST: testDictAttr
525 @run
526 def testDictAttr():
527 with Context():
528 dict_attr = {
529 "stringattr": StringAttr.get("string"),
530 "integerattr": IntegerAttr.get(IntegerType.get_signless(32), 42),
533 a = DictAttr.get(dict_attr)
535 # CHECK: attr: {integerattr = 42 : i32, stringattr = "string"}
536 print("attr:", a)
538 assert len(a) == 2
540 # CHECK: integerattr: IntegerAttr(42 : i32)
541 print("integerattr:", repr(a["integerattr"]))
543 # CHECK: stringattr: StringAttr("string")
544 print("stringattr:", repr(a["stringattr"]))
546 # CHECK: True
547 print("stringattr" in a)
549 # CHECK: False
550 print("not_in_dict" in a)
552 # Check that exceptions are raised as expected.
553 try:
554 _ = a["does_not_exist"]
555 except KeyError:
556 pass
557 else:
558 assert False, "Exception not produced"
560 try:
561 _ = a[42]
562 except IndexError:
563 pass
564 else:
565 assert False, "expected IndexError on accessing an out-of-bounds attribute"
567 # CHECK: empty: {}
568 print("empty: ", DictAttr.get())
571 # CHECK-LABEL: TEST: testTypeAttr
572 @run
573 def testTypeAttr():
574 with Context():
575 raw = Attribute.parse("vector<4xf32>")
576 # CHECK: attr: vector<4xf32>
577 print("attr:", raw)
578 type_attr = TypeAttr(raw)
579 # CHECK: f32
580 print(ShapedType(type_attr.value).element_type)
583 # CHECK-LABEL: TEST: testArrayAttr
584 @run
585 def testArrayAttr():
586 with Context():
587 arr = Attribute.parse("[42, true, vector<4xf32>]")
588 # CHECK: arr: [42, true, vector<4xf32>]
589 print("arr:", arr)
590 # CHECK: - IntegerAttr(42 : i64)
591 # CHECK: - BoolAttr(true)
592 # CHECK: - TypeAttr(vector<4xf32>)
593 for attr in arr:
594 print("- ", repr(attr))
596 with Context():
597 intAttr = Attribute.parse("42")
598 vecAttr = Attribute.parse("vector<4xf32>")
599 boolAttr = BoolAttr.get(True)
600 raw = ArrayAttr.get([vecAttr, boolAttr, intAttr])
601 # CHECK: attr: [vector<4xf32>, true, 42]
602 print("raw attr:", raw)
603 # CHECK: - TypeAttr(vector<4xf32>)
604 # CHECK: - BoolAttr(true
605 # CHECK: - IntegerAttr(42 : i64)
606 arr = raw
607 for attr in arr:
608 print("- ", repr(attr))
609 # CHECK: attr[0]: TypeAttr(vector<4xf32>)
610 print("attr[0]:", repr(arr[0]))
611 # CHECK: attr[1]: BoolAttr(true)
612 print("attr[1]:", repr(arr[1]))
613 # CHECK: attr[2]: IntegerAttr(42 : i64)
614 print("attr[2]:", repr(arr[2]))
615 try:
616 print("attr[3]:", arr[3])
617 except IndexError as e:
618 # CHECK: Error: ArrayAttribute index out of range
619 print("Error: ", e)
620 with Context():
621 try:
622 ArrayAttr.get([None])
623 except RuntimeError as e:
624 # CHECK: Error: Invalid attribute (None?) when attempting to create an ArrayAttribute
625 print("Error: ", e)
626 try:
627 ArrayAttr.get([42])
628 except RuntimeError as e:
629 # CHECK: Error: Invalid attribute when attempting to create an ArrayAttribute
630 print("Error: ", e)
632 with Context():
633 array = ArrayAttr.get([StringAttr.get("a"), StringAttr.get("b")])
634 array = array + [StringAttr.get("c")]
635 # CHECK: concat: ["a", "b", "c"]
636 print("concat: ", array)
639 # CHECK-LABEL: TEST: testStridedLayoutAttr
640 @run
641 def testStridedLayoutAttr():
642 with Context():
643 attr = StridedLayoutAttr.get(42, [5, 7, 13])
644 # CHECK: strided<[5, 7, 13], offset: 42>
645 print(attr)
646 # CHECK: 42
647 print(attr.offset)
648 # CHECK: 3
649 print(len(attr.strides))
650 # CHECK: 5
651 print(attr.strides[0])
652 # CHECK: 7
653 print(attr.strides[1])
654 # CHECK: 13
655 print(attr.strides[2])
657 attr = StridedLayoutAttr.get_fully_dynamic(3)
658 dynamic = ShapedType.get_dynamic_stride_or_offset()
659 # CHECK: strided<[?, ?, ?], offset: ?>
660 print(attr)
661 # CHECK: offset is dynamic: True
662 print(f"offset is dynamic: {attr.offset == dynamic}")
663 # CHECK: rank: 3
664 print(f"rank: {len(attr.strides)}")
665 # CHECK: strides are dynamic: [True, True, True]
666 print(f"strides are dynamic: {[s == dynamic for s in attr.strides]}")
669 # CHECK-LABEL: TEST: testConcreteTypesRoundTrip
670 @run
671 def testConcreteTypesRoundTrip():
672 with Context(), Location.unknown():
674 def print_item(attr):
675 print(repr(attr.type))
677 # CHECK: F32Type(f32)
678 print_item(Attribute.parse("42.0 : f32"))
679 # CHECK: F32Type(f32)
680 print_item(FloatAttr.get_f32(42.0))
681 # CHECK: IntegerType(i64)
682 print_item(IntegerAttr.get(IntegerType.get_signless(64), 42))
684 def print_container_item(attr_asm):
685 attr = DenseElementsAttr(Attribute.parse(attr_asm))
686 print(repr(attr.type))
687 print(repr(attr.type.element_type))
689 # CHECK: RankedTensorType(tensor<i16>)
690 # CHECK: IntegerType(i16)
691 print_container_item("dense<123> : tensor<i16>")
693 # CHECK: RankedTensorType(tensor<f64>)
694 # CHECK: F64Type(f64)
695 print_container_item("dense<1.0> : tensor<f64>")
697 raw = Attribute.parse("vector<4xf32>")
698 # CHECK: attr: vector<4xf32>
699 print("attr:", raw)
700 type_attr = TypeAttr(raw)
702 # CHECK: VectorType(vector<4xf32>)
703 print(repr(type_attr.value))
704 # CHECK: F32Type(f32)
705 print(repr(type_attr.value.element_type))
708 # CHECK-LABEL: TEST: testConcreteAttributesRoundTrip
709 @run
710 def testConcreteAttributesRoundTrip():
711 with Context(), Location.unknown():
712 # CHECK: FloatAttr(4.200000e+01 : f32)
713 print(repr(Attribute.parse("42.0 : f32")))
715 assert IntegerAttr.static_typeid is not None