forked from xdslproject/xdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_traits.py
570 lines (426 loc) · 15.3 KB
/
test_traits.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"""
Test the definition and usage of traits and interfaces.
"""
from __future__ import annotations
from abc import ABC
from collections.abc import Sequence
from dataclasses import dataclass
import pytest
from xdsl.dialects import test
from xdsl.dialects.builtin import (
AnyIntegerAttr,
IntegerAttr,
IntegerType,
StringAttr,
SymbolRefAttr,
i1,
i32,
i64,
)
from xdsl.ir import Operation, OpTrait, OpTraits
from xdsl.irdl import (
Block,
IRDLOperation,
Region,
attr_def,
irdl_op_definition,
lazy_traits_def,
operand_def,
opt_attr_def,
opt_region_def,
prop_def,
region_def,
result_def,
traits_def,
)
from xdsl.traits import (
AlwaysSpeculatable,
ConditionallySpeculatable,
HasAncestor,
HasParent,
OptionalSymbolOpInterface,
RecursivelySpeculatable,
SymbolOpInterface,
SymbolTable,
is_speculatable,
)
from xdsl.utils.exceptions import VerifyException
from xdsl.utils.test_value import TestSSAValue
@dataclass(frozen=True)
class LargerResultTrait(OpTrait):
"""Check that the only result has a larger bitwidth than the operand."""
def verify(self, op: Operation) -> None:
# This function is never called in this test
raise NotImplementedError()
@dataclass(frozen=True)
class LargerOperandTrait(OpTrait):
"""Check that the only operand has a larger bitwidth than the result."""
def verify(self, op: Operation):
# These asserts should be exceptions in a non-testing environment.
assert len(op.results) == 1
assert len(op.operands) == 1
assert isinstance(op.results[0].type, IntegerType)
assert isinstance(op.operands[0].type, IntegerType)
if op.results[0].type.width.data >= op.operands[0].type.width.data:
raise VerifyException(
"Operation has a result bitwidth greater "
"or equal to the operand bitwidth."
)
@dataclass(frozen=True)
class BitwidthSumLessThanTrait(OpTrait):
"""
Check that the sum of the bitwidths of the
operands and results is less than a given value.
"""
parameters: int
@property
def max_sum(self):
return self.parameters
def verify(self, op: Operation):
sum_bitwidth = 0
for operand in op.operands:
# This assert should be an exception in a non-testing environment.
assert isinstance(operand.type, IntegerType)
sum_bitwidth += operand.type.width.data
for result in op.results:
# This assert should be an exception in a non-testing environment.
assert isinstance(result.type, IntegerType)
sum_bitwidth += result.type.width.data
if sum_bitwidth >= self.max_sum:
raise VerifyException(
"Operation has a bitwidth sum " f"greater or equal to {self.max_sum}."
)
@irdl_op_definition
class TestOp(IRDLOperation):
name = "test.test"
traits = traits_def(LargerOperandTrait(), BitwidthSumLessThanTrait(64))
ops = operand_def(IntegerType)
res = result_def(IntegerType)
def test_has_trait_object():
"""
Test the `has_trait` `Operation` method on a simple operation definition.
"""
assert TestOp.has_trait(LargerOperandTrait)
assert not TestOp.has_trait(LargerResultTrait)
assert not TestOp.has_trait(BitwidthSumLessThanTrait(0))
assert TestOp.has_trait(BitwidthSumLessThanTrait(64))
def test_get_traits_of_type():
"""
Test the `get_traits_of_type` `Operation` method
on a simple operation definition.
"""
assert TestOp.get_traits_of_type(LargerOperandTrait) == [LargerOperandTrait()]
assert TestOp.get_traits_of_type(LargerResultTrait) == []
assert TestOp.get_traits_of_type(BitwidthSumLessThanTrait) == [
BitwidthSumLessThanTrait(64)
]
def test_verifier():
"""
Check that the traits verifier are correctly called.
"""
operand64 = TestSSAValue(i64)
operand32 = TestSSAValue(i32)
operand1 = TestSSAValue(i1)
op = TestOp.create(operands=[operand1], result_types=[i32])
message = (
"Operation has a result bitwidth greater or equal to the operand bitwidth."
)
with pytest.raises(VerifyException, match=message):
op.verify()
op = TestOp.create(operands=[operand64], result_types=[i32])
with pytest.raises(
VerifyException, match="Operation has a bitwidth sum greater or equal to 64."
):
op.verify()
op = TestOp.create(operands=[operand32], result_types=[i1])
op.verify()
def test_verifier_order():
"""
Check that trait verifiers are called after IRDL verifiers.
"""
op = TestOp.create(operands=[], result_types=[i1])
with pytest.raises(VerifyException, match="Expected 1 operand, but got 0"):
op.verify()
class LargerOperandOp(IRDLOperation, ABC):
traits = traits_def(LargerOperandTrait())
@irdl_op_definition
class TestCopyOp(LargerOperandOp):
name = "test.test_copy"
traits = OpTraits(LargerOperandOp.traits.traits | {BitwidthSumLessThanTrait(64)})
def test_trait_inheritance():
"""
Check that traits are correctly inherited from parent classes.
"""
assert TestCopyOp.traits == traits_def(
LargerOperandTrait(), BitwidthSumLessThanTrait(64)
)
@irdl_op_definition
class NoTraitsOp(IRDLOperation):
name = "test.no_traits_op"
def test_traits_undefined():
"""Check that traits are defaulted to the empty set."""
assert NoTraitsOp.traits == traits_def()
class WrongTraitsType(IRDLOperation):
name = "test.no_traits"
traits = 1 # pyright: ignore[reportAssignmentType]
def test_traits_wrong_type():
with pytest.raises(Exception):
irdl_op_definition(WrongTraitsType)
class GetNumResultsTrait(OpTrait):
"""
An example of an MLIR interface, using traits.
It provides a method to get the number of results of an operation.
"""
# This is the default implementation of a trait (interface) method.
@staticmethod
def get_num_results(op: Operation):
"""
Get the number of results of the operation
"""
return len(op.results)
class GetNumResultsTraitForOpWithOneResult(GetNumResultsTrait):
"""
Instance of the trait for the case where an operation has only a single result.
"""
@staticmethod
def get_num_results(op: Operation):
return 1
@irdl_op_definition
class HasInterfaceOp(IRDLOperation):
name = "test.op_with_interface"
traits = traits_def(GetNumResultsTraitForOpWithOneResult())
res = result_def(IntegerType)
def test_interface():
"""
Test the features of a trait with methods (An MLIR interface).
"""
op = HasInterfaceOp.create(result_types=(i32,))
trait = HasInterfaceOp.get_trait(GetNumResultsTrait)
assert trait is not None
assert 1 == trait.get_num_results(op)
def test_get_trait_specialized():
"""
Test get_trait and has_trait in the case where the trait is a child class of the
trait we want.
"""
assert HasInterfaceOp.has_trait(GetNumResultsTrait)
assert HasInterfaceOp.has_trait(GetNumResultsTraitForOpWithOneResult)
assert (
HasInterfaceOp.get_trait(GetNumResultsTrait)
== GetNumResultsTraitForOpWithOneResult()
)
assert HasInterfaceOp.get_traits_of_type(GetNumResultsTrait) == [
GetNumResultsTraitForOpWithOneResult()
]
def test_symbol_op_interface():
"""
Test that operations that conform to SymbolOpInterface have necessary attributes.
"""
@irdl_op_definition
class NoSymNameOp(IRDLOperation):
name = "no_sym_name"
traits = traits_def(SymbolOpInterface())
op0 = NoSymNameOp()
with pytest.raises(
VerifyException, match='Operation no_sym_name must have a "sym_name" attribute'
):
op0.verify()
@irdl_op_definition
class SymNameWrongTypeOp(IRDLOperation):
name = "wrong_sym_name_type"
sym_name = attr_def(AnyIntegerAttr)
traits = traits_def(SymbolOpInterface())
op1 = SymNameWrongTypeOp(
attributes={"sym_name": IntegerAttr.from_int_and_width(1, 32)}
)
with pytest.raises(
VerifyException,
match='Operation wrong_sym_name_type must have a "sym_name" attribute',
):
op1.verify()
@irdl_op_definition
class SymNameOp(IRDLOperation):
name = "sym_name"
sym_name = attr_def(StringAttr)
traits = traits_def(SymbolOpInterface())
op2 = SymNameOp(attributes={"sym_name": StringAttr("symbol_name")})
op2.verify()
def test_optional_symbol_op_interface():
"""
Test that operations that conform to OptionalSymbolOpInterface have the necessary attributes.
"""
@irdl_op_definition
class OptionalSymNameOp(IRDLOperation):
name = "no_sym_name"
sym_name = opt_attr_def(StringAttr)
traits = traits_def(OptionalSymbolOpInterface())
no_symbol = OptionalSymNameOp()
interface = no_symbol.get_trait(SymbolOpInterface)
assert interface is not None
assert interface.is_optional_symbol(no_symbol)
no_symbol.verify()
assert interface.get_sym_attr_name(no_symbol) is None
symbol = OptionalSymNameOp(attributes={"sym_name": StringAttr("main")})
interface = symbol.get_trait(SymbolOpInterface)
assert interface is not None
assert interface.is_optional_symbol(symbol)
symbol.verify()
assert interface.get_sym_attr_name(symbol) == StringAttr("main")
@irdl_op_definition
class SymbolOp(IRDLOperation):
name = "test.symbol"
sym_name = attr_def(StringAttr)
traits = traits_def(SymbolOpInterface())
def __init__(self, name: str):
return super().__init__(attributes={"sym_name": StringAttr(name)})
@irdl_op_definition
class PropSymbolOp(IRDLOperation):
name = "test.symbol"
sym_name = prop_def(StringAttr)
traits = traits_def(SymbolOpInterface())
def __init__(self, name: str):
return super().__init__(properties={"sym_name": StringAttr(name)})
@pytest.mark.parametrize("SymbolOp", [SymbolOp, PropSymbolOp])
def test_symbol_table(SymbolOp: type[PropSymbolOp | SymbolOp]):
# Some helper classes
@irdl_op_definition
class SymbolTableOp(IRDLOperation):
name = "test.symbol_table"
sym_name = opt_attr_def(StringAttr)
one = region_def()
two = opt_region_def()
traits = traits_def(SymbolTable(), OptionalSymbolOpInterface())
# Check that having a single region is verified
op = SymbolTableOp(regions=[Region(), Region()])
with pytest.raises(
VerifyException,
match="Operations with a 'SymbolTable' must have exactly one region",
):
op.verify()
# Check that having a single block is verified
blocks = [Block(), Block()]
op = SymbolTableOp(regions=[Region(blocks), []])
with pytest.raises(
VerifyException,
match="Operations with a 'SymbolTable' must have exactly one block",
):
op.verify()
terminator = test.TestTermOp()
# Check that symbol uniqueness is verified
symbol = SymbolOp("name")
dup_symbol = SymbolOp("name")
op = SymbolTableOp(
regions=[Region(Block([symbol, dup_symbol, terminator.clone()])), []]
)
with pytest.raises(
VerifyException,
match='Redefinition of symbol "name"',
):
op.verify()
# Check a flat happy case, with symbol lookup
symbol = SymbolOp("name")
op = SymbolTableOp(regions=[Region(Block([symbol, terminator.clone()])), []])
op.verify()
assert SymbolTable.lookup_symbol(op, "name") is symbol
assert SymbolTable.lookup_symbol(op, "that_other_name") is None
# Check a nested happy case, with symbol lookup
symbol = SymbolOp("name")
nested = SymbolTableOp(
regions=[Region(Block([symbol, terminator.clone()])), []],
attributes={"sym_name": StringAttr("nested")},
)
op = SymbolTableOp(regions=[Region(Block([nested, terminator.clone()])), []])
op.verify()
assert SymbolTable.lookup_symbol(op, "name") is None
assert SymbolTable.lookup_symbol(op, SymbolRefAttr("nested", ["name"])) is symbol
@irdl_op_definition
class HasLazyParentOp(IRDLOperation):
"""An operation with traits that are defined "lazily"."""
name = "test.has_lazy_parent"
traits = lazy_traits_def(lambda: (HasParent(TestOp),))
def test_lazy_parent():
"""Test the trait infrastructure for an operation that defines a trait "lazily"."""
op = HasLazyParentOp.create()
assert len(op.get_traits_of_type(HasParent)) != 0
assert op.get_traits_of_type(HasParent)[0].op_types == (TestOp,)
assert op.has_trait(HasParent(TestOp))
assert op.traits == traits_def(HasParent(TestOp))
@irdl_op_definition
class AncestorOp(IRDLOperation):
name = "test.ancestor"
traits = traits_def(HasAncestor(TestOp))
def test_has_ancestor():
op = AncestorOp()
assert op.get_traits_of_type(HasAncestor) == [HasAncestor(TestOp)]
assert op.has_trait(HasAncestor(TestOp))
with pytest.raises(
VerifyException, match="'test.ancestor' expects ancestor op 'test.test'"
):
op.verify()
def test_insert_or_update():
@irdl_op_definition
class SymbolTableOp(IRDLOperation):
name = "test.symbol_table"
reg = region_def()
traits = traits_def(SymbolTable())
# Check a flat happy case, with symbol lookup
symbol = SymbolOp("name")
symbol2 = SymbolOp("name2")
terminator = test.TestTermOp()
op = SymbolTableOp(regions=[Region(Block([symbol, terminator]))])
op.verify()
trait = op.get_trait(SymbolTable)
assert trait is not None
assert trait.insert_or_update(op, symbol.clone()) is symbol
assert len(op.reg.ops) == 2
assert trait.insert_or_update(op, symbol2) is None
assert len(op.reg.ops) == 3
assert symbol2 in list(op.reg.ops)
def nonpure():
return TestOp.create()
def pure():
return test.TestPureOp.create()
@pytest.mark.parametrize(
("trait", "speculatability", "nested_ops"),
[
([], False, []),
([], False, [nonpure()]),
([], False, [pure()]),
([AlwaysSpeculatable()], True, []),
([AlwaysSpeculatable()], True, [nonpure()]),
([AlwaysSpeculatable()], True, [pure()]),
([RecursivelySpeculatable()], True, []),
([RecursivelySpeculatable()], False, [nonpure()]),
([RecursivelySpeculatable()], True, [pure()]),
],
)
def test_speculability(
trait: tuple[ConditionallySpeculatable] | tuple[()],
speculatability: bool,
nested_ops: Sequence[Operation],
):
@irdl_op_definition
class SupeculatabilityTestOp(IRDLOperation):
name = "test.speculatability"
region = region_def()
traits = traits_def(*trait)
op = SupeculatabilityTestOp(regions=[Region(Block(nested_ops))])
optrait = op.get_trait(ConditionallySpeculatable)
if trait:
assert optrait is not None
assert optrait.is_speculatable(op) is speculatability
else:
assert optrait is None
assert is_speculatable(op) is speculatability
@irdl_op_definition
class TestModifyTraitsOp(IRDLOperation):
name = "test.test_modify_traits"
class AlwaysFailsTrait(OpTrait):
def verify(self, op: Operation) -> None:
raise VerifyException("Nope")
def test_modify_traits():
op = TestModifyTraitsOp()
op.verify()
TestModifyTraitsOp.traits.add_trait(AlwaysFailsTrait())
with pytest.raises(VerifyException, match="Nope"):
op.verify()