-
Notifications
You must be signed in to change notification settings - Fork 57
/
test_builtin_traits.py
516 lines (403 loc) · 14.9 KB
/
test_builtin_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
"""
Test the usage of builtin traits.
"""
import pytest
from xdsl.dialects import arith, builtin
from xdsl.dialects.builtin import ModuleOp
from xdsl.dialects.test import TestOp
from xdsl.ir import Block, Region
from xdsl.irdl import (
AttrSizedRegionSegments,
IRDLOperation,
OptRegion,
OptSuccessor,
irdl_op_definition,
opt_region_def,
opt_successor_def,
region_def,
traits_def,
)
from xdsl.traits import (
HasParent,
IsolatedFromAbove,
IsTerminator,
NoTerminator,
SingleBlockImplicitTerminator,
ensure_terminator,
)
from xdsl.utils.exceptions import VerifyException
@irdl_op_definition
class ParentOp(IRDLOperation):
name = "test.parent"
region: Region = region_def()
traits = frozenset([NoTerminator()])
@irdl_op_definition
class Parent2Op(IRDLOperation):
name = "test.parent2"
region: Region = region_def()
traits = frozenset([NoTerminator()])
@irdl_op_definition
class HasParentOp(IRDLOperation):
"""
An operation expecting a 'test.parent_op' parent.
"""
name = "test.has_parent"
traits = frozenset([HasParent(ParentOp)])
@irdl_op_definition
class HasMultipleParentOp(IRDLOperation):
"""
An operation expecting one of multiple parent types.
"""
name = "test.has_multiple_parent"
traits = frozenset([HasParent(ParentOp, Parent2Op)])
def test_has_parent_no_parent():
"""
Test that an operation with an HasParentOp trait
fails with no parents.
"""
has_parent_op = HasParentOp()
with pytest.raises(
VerifyException, match="'test.has_parent' expects parent op 'test.parent'"
):
has_parent_op.verify()
has_multiple_parent_op = HasMultipleParentOp()
message = (
"'test.has_multiple_parent' expects parent op to "
"be one of 'test.parent', 'test.parent2'"
)
with pytest.raises(VerifyException, match=message):
has_multiple_parent_op.verify()
def test_has_parent_wrong_parent():
"""
Test that an operation with an HasParentOp trait
fails with a wrong parent.
"""
module = ModuleOp([HasParentOp()])
with pytest.raises(
VerifyException, match="'test.has_parent' expects parent op 'test.parent'"
):
module.verify()
module = ModuleOp([HasMultipleParentOp()])
message = (
"'test.has_multiple_parent' expects parent op to "
"be one of 'test.parent', 'test.parent2'"
)
with pytest.raises(VerifyException, match=message):
module.verify()
def test_has_parent_verify():
"""
Test that an operation with an HasParentOp trait
expects a parent operation of the right type.
"""
op = ParentOp(regions=[[HasParentOp()]])
op.verify()
op = ParentOp(regions=[[HasMultipleParentOp()]])
op.verify()
op = Parent2Op(regions=[[HasMultipleParentOp()]])
op.verify()
@irdl_op_definition
class HasNoTerminatorOp(IRDLOperation):
"""
An operation that can opt out from having a terminator.
This requires the operation to have a single block.
"""
name = "test.has_no_terminator"
region: Region = region_def()
traits = frozenset([NoTerminator()])
def test_has_no_terminator_empty_block_with_single_block_region_requires_no_terminator():
"""
Tests that an empty block belonging to a single-block region with parent
operation requires no terminator operation if it has the NoTerminator trait.
"""
block0 = Block([])
region0 = Region([block0])
_ = HasNoTerminatorOp.create(regions=[region0])
block0.verify()
def test_has_no_terminator_empty_block_with_multi_block_region_fails():
"""
Tests that an empty block belonging to a multi-block region with parent
operation fails if it has the NoTerminator trait.
"""
block0 = Block([])
block1 = Block([])
region0 = Region([block0, block1])
op0 = HasNoTerminatorOp.create(regions=[region0])
with pytest.raises(VerifyException, match="does not contain single-block regions"):
op0.verify()
@irdl_op_definition
class IsTerminatorOp(IRDLOperation):
"""
An operation that provides the IsTerminator trait.
"""
name = "test.is_terminator"
successor: OptSuccessor = opt_successor_def()
traits = frozenset([IsTerminator()])
def test_is_terminator_without_successors_multi_block_parent_region_verify():
"""
Test that an operation with an IsTerminator trait may not have successor
blocks in a multi-block parent region.
"""
block0 = Block([])
# term op is the single op in its block
block1 = Block([IsTerminatorOp.create()])
region0 = Region([block0, block1])
op0 = TestOp.create(regions=[region0])
op0.verify()
block2 = Block([])
# term op with other ops in its block
block3 = Block([TestOp.create(), IsTerminatorOp.create()])
region1 = Region([block2, block3])
op1 = TestOp.create(regions=[region1])
op1.verify()
def test_is_terminator_without_successors_single_block_parent_region_verify():
"""
Test that an operation with an IsTerminator trait may not have successor
blocks in a single-block parent region.
"""
# term op is the single op in its block
block0 = Block([IsTerminatorOp.create()])
region0 = Region([block0])
op0 = TestOp.create(regions=[region0])
op0.verify()
# term op with other ops in its block
block1 = Block([TestOp.create(), IsTerminatorOp.create()])
region1 = Region([block1])
op1 = TestOp.create(regions=[region1])
op1.verify()
def test_is_terminator_fails_if_not_last_op_parent_block_in_single_block_region():
"""
Test that an operation with an IsTerminator trait fails if it is not the
last operation in its parent block in a single-block region.
"""
block0 = Block([IsTerminatorOp.create(), TestOp.create()])
region0 = Region([block0])
op0 = TestOp.create(regions=[region0])
with pytest.raises(
VerifyException, match="must be the last operation in its parent block"
):
op0.verify()
def test_is_terminator_fails_if_not_last_op_parent_block_in_multi_block_region():
"""
Test that an operation without an IsTerminator trait verifies if it is not
the last operation in its parent block in a multi-block region.
"""
block0 = Block([IsTerminatorOp.create(), TestOp.create()])
block1 = Block([])
region0 = Region([block0, block1])
op0 = TestOp.create(regions=[region0])
with pytest.raises(
VerifyException, match="must be the last operation in its parent block"
):
op0.verify()
def test_no_terminator_op_with_is_terminator_op():
"""
Test that an operation with a NoTerminator trait verifies if it contains a
terminator operation (i.e., has the IsTerminator trait).
"""
block0 = Block([IsTerminatorOp.create()])
region0 = Region([block0])
op0 = HasNoTerminatorOp.create(regions=[region0])
op0.verify()
@irdl_op_definition
class IsSingleBlockImplicitTerminatorOp(IRDLOperation):
"""
An operation that implements terminator to be used with an operation that uses the
SingleBlockImplicitTerminator trait.
"""
name = "test.is_single_block_implicit_terminator"
traits = traits_def(
lambda: frozenset(
[IsTerminator(), HasParent(HasSingleBlockImplicitTerminatorOp)]
)
)
@irdl_op_definition
class HasSingleBlockImplicitTerminatorOp(IRDLOperation):
"""
An operation that expects a single-block region and an implicit terminator trait for
that block.
"""
name = "test.has_single_block_implicit_terminator"
irdl_options = [AttrSizedRegionSegments()]
region: Region = region_def()
opt_region: OptRegion = opt_region_def()
traits = frozenset(
[SingleBlockImplicitTerminator(IsSingleBlockImplicitTerminatorOp)]
)
def __post_init__(self):
for trait in self.get_traits_of_type(SingleBlockImplicitTerminator):
ensure_terminator(self, trait)
@irdl_op_definition
class HasSingleBlockImplicitTerminatorWrongCreationOp(IRDLOperation):
"""
An operation that expects a single-block region and an implicit terminator trait for
that block, but ensure_terminator() has not been called during construction.
This exercises the SingleBlockImplicitTerminator.verify() checks.
"""
name = "test.has_single_block_implicit_terminator_wrong_creation"
irdl_options = [AttrSizedRegionSegments()]
region: Region = region_def()
opt_region: OptRegion = opt_region_def()
traits = frozenset(
[SingleBlockImplicitTerminator(IsSingleBlockImplicitTerminatorOp)]
)
@irdl_op_definition
class HasSingleBlockImplicitTerminatorWrongCreationOp2(IRDLOperation):
"""
An operation that expects a single-block region and an implicit terminator trait for
that block, but ensure_terminator() has not been called during construction.
This exercises the SingleBlockImplicitTerminator.verify() checks that expects at
least a terminator.
This is achieved by adding the trait NoTerminator, but it should catch cases where
these traits are used in a conflicting manner.
"""
name = "test.has_single_block_implicit_terminator_wrong_creation2"
irdl_options = [AttrSizedRegionSegments()]
region: Region = region_def()
opt_region: OptRegion = opt_region_def()
traits = frozenset(
[
NoTerminator(),
SingleBlockImplicitTerminator(IsSingleBlockImplicitTerminatorOp),
]
)
def test_single_block_implicit_terminator_verify():
# test empty single-region op
op0 = HasSingleBlockImplicitTerminatorOp(regions=[Region(), []])
op0.verify()
assert len(op0.region.blocks[0].ops) == 1
# test empty multi-region op
op1 = HasSingleBlockImplicitTerminatorOp(regions=[Region(), Region()])
op1.verify()
assert len(op1.region.blocks[0].ops) == 1
assert op1.opt_region is not None
assert len(op1.opt_region.blocks[0].ops) == 1
# test non-empty multi-region op
op2 = HasSingleBlockImplicitTerminatorOp(regions=[Region(Block()), Region()])
op2.verify()
assert len(op2.region.blocks[0].ops) == 1
assert op2.opt_region is not None
assert len(op2.opt_region.blocks[0].ops) == 1
# test non-empty multi-region op with non-terminator operation
op3 = HasSingleBlockImplicitTerminatorOp(
regions=[Region(Block([TestOp.create()])), Region()]
)
op3.verify()
assert len(op3.region.blocks[0].ops) == 2
assert op3.opt_region is not None
assert len(op3.opt_region.blocks[0].ops) == 1
# test non-empty multi-region op with correct terminator already there
op4 = HasSingleBlockImplicitTerminatorOp(
regions=[Region(Block([IsSingleBlockImplicitTerminatorOp.create()])), Region()]
)
op4.verify()
assert len(op4.region.blocks[0].ops) == 1
assert op4.opt_region is not None
assert len(op4.opt_region.blocks[0].ops) == 1
def test_single_block_implicit_terminator_with_correct_construction_fail():
"""
Tests SingleBlockImplicitTerminator when ensure_terminator has been called during
operation creation
"""
# test multi-block region op
with pytest.raises(VerifyException, match="does not contain single-block regions"):
HasSingleBlockImplicitTerminatorOp(
regions=[Region([Block(), Block()]), Region()]
)
# test single-block region op with wrong terminator
with pytest.raises(
VerifyException, match="terminates with operation test.is_terminator"
):
HasSingleBlockImplicitTerminatorOp(
regions=[Region(Block([IsTerminatorOp.create()])), Region()]
)
def test_single_block_implicit_terminator_with_wrong_construction_fail():
"""
Tests SingleBlockImplicitTerminator when ensure_terminator has not been called during
operation creation
"""
op0 = HasSingleBlockImplicitTerminatorWrongCreationOp(
regions=[Region([Block(), Block()]), Region()]
)
# test multi-block region op
with pytest.raises(VerifyException, match="does not contain single-block regions"):
op0.verify()
op1 = HasSingleBlockImplicitTerminatorWrongCreationOp(
regions=[Region(Block([IsTerminatorOp.create()])), Region()]
)
# test single-block region op with wrong terminator
with pytest.raises(
VerifyException, match="terminates with operation test.is_terminator"
):
op1.verify()
op2 = HasSingleBlockImplicitTerminatorWrongCreationOp2(
regions=[Region(Block()), Region()]
)
# test single-block region op with wrong terminator
with pytest.raises(
VerifyException,
match="contains empty block instead of at least terminating with",
):
op2.verify()
@irdl_op_definition
class IsolatedFromAboveOp(IRDLOperation):
"""
An isolated from above operation.
"""
name = "test.isolated_from_above"
region: Region = region_def()
traits = frozenset([IsolatedFromAbove(), NoTerminator()])
def test_isolated_from_above():
# Empty Isolated is fine
op = IsolatedFromAboveOp(regions=[Region()])
op.verify()
block = Block(arg_types=[builtin.i32])
block.add_op(arith.Addi(block.args[0], block.args[0]))
# Test a simple, properly Isolated
op = IsolatedFromAboveOp(regions=[Region([block])])
op.verify()
# Check a simple isolation violation
out_cst = arith.Constant.from_int_and_width(0, builtin.i32)
out_block = Block(
[
out_cst,
IsolatedFromAboveOp(
regions=[Region(Block([arith.Addi(out_cst, out_cst)]))]
),
]
)
message = "Operation using value defined out of its IsolatedFromAbove parent!"
with pytest.raises(VerifyException, match=message):
out_block.verify()
# Check a nested isolation violation
out_cst = arith.Constant.from_int_and_width(0, builtin.i32)
out_block = Block(
[
# This one is fine
out_isolated := IsolatedFromAboveOp(
regions=[
Region(
Block(
[
out_cst,
# This one is not!
in_isolated := IsolatedFromAboveOp(
regions=[
Region(Block([arith.Addi(out_cst, out_cst)]))
]
),
],
)
)
]
),
]
)
# Check that the IR as a whole is wrong
message = "Operation using value defined out of its IsolatedFromAbove parent!"
with pytest.raises(VerifyException, match=message):
out_block.verify()
# Check that the outer one in itself is fine
out_isolated.verify(verify_nested_ops=False)
# Check that the inner one is indeed failing to verify.
with pytest.raises(VerifyException, match=message):
in_isolated.verify()