-
Notifications
You must be signed in to change notification settings - Fork 57
/
test_operation_builder.py
712 lines (511 loc) · 18.8 KB
/
test_operation_builder.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
from __future__ import annotations
import pytest
from xdsl.dialects.arith import Constant
from xdsl.dialects.builtin import DenseArrayBase, StringAttr, i32
from xdsl.dialects.test import TestTermOp
from xdsl.ir import Block, OpResult, Region
from xdsl.irdl import (
AttrSizedOperandSegments,
AttrSizedRegionSegments,
AttrSizedResultSegments,
AttrSizedSuccessorSegments,
IRDLOperation,
Operand,
OptOperand,
OptOpResult,
OptRegion,
OptSuccessor,
Successor,
VarOperand,
VarOpResult,
VarRegion,
VarSuccessor,
attr_def,
irdl_op_definition,
operand_def,
opt_attr_def,
opt_operand_def,
opt_prop_def,
opt_region_def,
opt_result_def,
opt_successor_def,
prop_def,
region_def,
result_def,
successor_def,
var_operand_def,
var_region_def,
var_result_def,
var_successor_def,
)
from xdsl.traits import IsTerminator
################################################################################
# Results #
################################################################################
@irdl_op_definition
class ResultOp(IRDLOperation):
name = "test.result_op"
res: OpResult = result_def(StringAttr)
def test_result_builder():
op = ResultOp.build(result_types=[StringAttr("")])
op.verify()
assert [res.type for res in op.results] == [StringAttr("")]
def test_result_builder_exception():
with pytest.raises(ValueError):
ResultOp.build()
@irdl_op_definition
class OptResultOp(IRDLOperation):
name = "test.opt_result_op"
res: OptOpResult = opt_result_def(StringAttr)
def test_opt_result_builder():
op1 = OptResultOp.build(result_types=[[StringAttr("")]])
op2 = OptResultOp.build(result_types=[[]])
op3 = OptResultOp.build(result_types=[None])
op1.verify()
op2.verify()
op3.verify()
assert [res.type for res in op1.results] == [StringAttr("")]
assert len(op2.results) == 0
assert len(op3.results) == 0
def test_opt_result_builder_two_args():
with pytest.raises(ValueError) as _:
OptResultOp.build(result_types=[[StringAttr(""), StringAttr("")]])
@irdl_op_definition
class VarResultOp(IRDLOperation):
name = "test.var_result_op"
res: VarOpResult = var_result_def(StringAttr)
def test_var_result_builder():
op = VarResultOp.build(result_types=[[StringAttr("0"), StringAttr("1")]])
op.verify()
assert [res.type for res in op.results] == [
StringAttr("0"),
StringAttr("1"),
]
@irdl_op_definition
class TwoVarResultOp(IRDLOperation):
name = "test.two_var_result_op"
res1: VarOpResult = var_result_def(StringAttr)
res2: VarOpResult = var_result_def(StringAttr)
irdl_options = [AttrSizedResultSegments()]
def test_two_var_result_builder():
op = TwoVarResultOp.build(
result_types=[
[StringAttr("0"), StringAttr("1")],
[StringAttr("2"), StringAttr("3")],
]
)
op.verify()
assert [res.type for res in op.results] == [
StringAttr("0"),
StringAttr("1"),
StringAttr("2"),
StringAttr("3"),
]
assert op.attributes[
AttrSizedResultSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 2])
def test_two_var_result_builder2():
op = TwoVarResultOp.build(
result_types=[
[StringAttr("0")],
[StringAttr("1"), StringAttr("2"), StringAttr("3")],
]
)
op.verify()
assert [res.type for res in op.results] == [
StringAttr("0"),
StringAttr("1"),
StringAttr("2"),
StringAttr("3"),
]
assert op.attributes[
AttrSizedResultSegments.attribute_name
] == DenseArrayBase.from_list(i32, [1, 3])
@irdl_op_definition
class MixedResultOp(IRDLOperation):
name = "test.mixed"
res1: VarOpResult = var_result_def(StringAttr)
res2: OpResult = result_def(StringAttr)
res3: VarOpResult = var_result_def(StringAttr)
irdl_options = [AttrSizedResultSegments()]
def test_var_mixed_builder():
op = MixedResultOp.build(
result_types=[
[StringAttr("0"), StringAttr("1")],
StringAttr("2"),
[StringAttr("3"), StringAttr("4")],
]
)
op.verify()
assert [res.type for res in op.results] == [
StringAttr("0"),
StringAttr("1"),
StringAttr("2"),
StringAttr("3"),
StringAttr("4"),
]
assert op.attributes[
AttrSizedResultSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 1, 2])
################################################################################
# Operands #
################################################################################
@irdl_op_definition
class OperandOp(IRDLOperation):
name = "test.operand_op"
res: Operand = operand_def(StringAttr)
def test_operand_builder_operation():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = OperandOp.build(operands=[op1])
op2.verify()
assert tuple(op2.operands) == (op1.res,)
def test_operand_builder_value():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = OperandOp.build(operands=[op1.res])
op2.verify()
assert tuple(op2.operands) == (op1.res,)
def test_operand_builder_exception():
with pytest.raises(ValueError):
OperandOp.build()
@irdl_op_definition
class OptOperandOp(IRDLOperation):
name = "test.opt_operand_op"
res: OptOperand = opt_operand_def(StringAttr)
def test_opt_operand_builder():
op = ResultOp.build(result_types=[StringAttr("0")])
op1 = OptOperandOp.build(operands=[[op]])
op2 = OptOperandOp.build(operands=[[]])
op1.verify()
op2.verify()
assert [operand for operand in op1.operands] == [op.res]
assert len(op2.operands) == 0
def test_opt_operand_builder_two_args():
op = ResultOp.build(result_types=[StringAttr("0")])
with pytest.raises(ValueError) as _:
OptOperandOp.build(operands=[[op, op]])
@irdl_op_definition
class VarOperandOp(IRDLOperation):
name = "test.var_operand_op"
res: VarOperand = var_operand_def(StringAttr)
def test_var_operand_builder():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = VarOperandOp.build(operands=[[op1, op1]])
op2.verify()
assert tuple(op2.operands) == (op1.res, op1.res)
@irdl_op_definition
class TwoVarOperandOp(IRDLOperation):
name = "test.two_var_operand_op"
res1: VarOperand = var_operand_def(StringAttr)
res2: VarOperand = var_operand_def(StringAttr)
irdl_options = [AttrSizedOperandSegments()]
# Define a similar operation with the segment sizes as a property to test this case
@irdl_op_definition
class TwoVarOperandPropOp(IRDLOperation):
name = "test.two_var_operand_op"
res1: VarOperand = var_operand_def(StringAttr)
res2: VarOperand = var_operand_def(StringAttr)
irdl_options = [AttrSizedOperandSegments(as_property=True)]
def test_two_var_operand_builder():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = TwoVarOperandOp.build(operands=[[op1, op1], [op1, op1]])
op2.verify()
assert tuple(op2.operands) == (op1.res, op1.res, op1.res, op1.res)
assert op2.attributes[
AttrSizedOperandSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 2])
def test_two_var_operand_builder2():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = TwoVarOperandOp.build(operands=[[op1], [op1, op1, op1]])
op2.verify()
assert tuple(op2.operands) == (op1.res, op1.res, op1.res, op1.res)
assert op2.attributes[
AttrSizedOperandSegments.attribute_name
] == DenseArrayBase.from_list(i32, [1, 3])
def test_two_var_operand_prop_builder():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = TwoVarOperandPropOp.build(operands=[[op1, op1], [op1, op1]])
op2.verify()
assert tuple(op2.operands) == (op1.res, op1.res, op1.res, op1.res)
assert op2.properties[
AttrSizedOperandSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 2])
def test_two_var_operand_prop_builder2():
op1 = ResultOp.build(result_types=[StringAttr("0")])
op2 = TwoVarOperandPropOp.build(operands=[[op1], [op1, op1, op1]])
op2.verify()
assert tuple(op2.operands) == (op1.res, op1.res, op1.res, op1.res)
assert op2.properties[
AttrSizedOperandSegments.attribute_name
] == DenseArrayBase.from_list(i32, [1, 3])
################################################################################
# Attribute #
################################################################################
@irdl_op_definition
class AttrOp(IRDLOperation):
name = "test.attr_op"
attr: StringAttr = attr_def(StringAttr)
def test_attr_op():
op = AttrOp.build(attributes={"attr": StringAttr("0")})
op.verify()
assert op.attr == StringAttr("0")
def test_attr_new_attr_op():
op = AttrOp.build(attributes={"attr": StringAttr("0"), "new_attr": StringAttr("1")})
op.verify()
assert op.attr == StringAttr("0")
assert op.attributes["new_attr"] == StringAttr("1")
@irdl_op_definition
class OptionalAttrOp(IRDLOperation):
name = "test.opt_attr_op"
opt_attr: StringAttr | None = opt_attr_def(StringAttr)
def test_optional_attr_op_empty():
op = OptionalAttrOp.build()
op.verify()
assert op.opt_attr is None
################################################################################
# Property #
################################################################################
@irdl_op_definition
class PropertyOp(IRDLOperation):
name = "test.prop_op"
attr: StringAttr = prop_def(StringAttr)
def test_prop_op():
op = PropertyOp.build(properties={"attr": StringAttr("0")})
op.verify()
assert op.attr == StringAttr("0")
@irdl_op_definition
class OptionalPropertyOp(IRDLOperation):
name = "test.opt_prop_op"
opt_attr: StringAttr | None = opt_prop_def(StringAttr)
def test_optional_prop_op_empty():
op = OptionalPropertyOp.build()
op.verify()
assert op.opt_attr is None
################################################################################
# Region #
################################################################################
@irdl_op_definition
class RegionOp(IRDLOperation):
name = "test.region_op"
region: Region = region_def()
def test_region_op_region():
op = RegionOp.build(regions=[Region()])
op.verify()
assert op.region.blocks == []
def test_region_op_blocks():
op = RegionOp.build(regions=[[Block(), Block()]])
op.verify()
assert len(op.region.blocks) == 2
def test_region_op_ops():
op1 = RegionOp.build(regions=[[Block([TestTermOp.create()])]])
op2 = RegionOp.build(regions=[[Block([TestTermOp.create()])]])
op = RegionOp.build(regions=[[op1, op2, TestTermOp.create()]])
op.verify()
assert len(op.region.blocks) == 1
assert len(op.region.blocks[0].ops) == 3
def test_noop_region():
region0 = Region(Block())
assert len(region0.ops) == 0
def test_singleop_region():
a = Constant.from_int_and_width(1, i32)
region0 = Region(Block([a]))
assert type(region0.op) is Constant
@irdl_op_definition
class SBRegionOp(IRDLOperation):
name = "test.sbregion_op"
region: Region = region_def("single_block")
def test_sbregion_one_block():
op = SBRegionOp.build(regions=[[Block([TestTermOp.create()])]])
op.verify()
assert len(op.region.blocks) == 1
@irdl_op_definition
class OptRegionOp(IRDLOperation):
name = "test.opt_region_op"
reg: OptRegion = opt_region_def()
def test_opt_region_builder():
op1 = OptRegionOp.build(regions=[[[Block(), Block()]]])
op2 = OptRegionOp.build(regions=[[Region()]])
op3 = OptRegionOp.build(regions=[None])
op1.verify()
op2.verify()
op3.verify()
def test_opt_region_builder_two_args():
with pytest.raises(ValueError) as _:
OptRegionOp.build(regions=[[Region(), Region()]])
@irdl_op_definition
class OptSBRegionOp(IRDLOperation):
name = "test.sbregion_op"
region: OptRegion = opt_region_def("single_block")
def test_opt_sbregion_one_block():
op1 = OptSBRegionOp.build(regions=[[[Block([TestTermOp.create()])]]])
op2 = OptSBRegionOp.build(regions=[[]])
op1.verify()
op2.verify()
assert op1.region is not None
assert len(op1.region.blocks) == 1
assert op2.region is None
@irdl_op_definition
class VarRegionOp(IRDLOperation):
name = "test.var_operand_op"
regs: VarRegion = var_region_def()
def test_var_region_builder():
op = VarRegionOp.build(regions=[[Region(), [Block(), Block()]]])
op.verify()
assert len(op.regs[0].blocks) == 0
assert len(op.regs[1].blocks) == 2
@irdl_op_definition
class VarSBRegionOp(IRDLOperation):
name = "test.sbregion_op"
regs: VarRegion = var_region_def("single_block")
def test_var_sbregion_one_block():
op1 = VarSBRegionOp.build(regions=[[[Block([TestTermOp.create()])]]])
op2 = VarSBRegionOp.build(regions=[[Region(), [Block(), Block()]]])
op1.verify()
op2.verify()
assert len(op1.regs) == 1
assert len(op2.regs) == 2
assert len(op2.regs[0].blocks) == 0
assert len(op2.regs[1].blocks) == 2
@irdl_op_definition
class TwoVarRegionOp(IRDLOperation):
name = "test.two_var_region_op"
res1: VarRegion = var_region_def()
res2: VarRegion = var_region_def()
irdl_options = [AttrSizedRegionSegments()]
def test_two_var_region_builder():
region1 = Region()
region2 = Region()
region3 = Region()
region4 = Region()
op2 = TwoVarRegionOp.build(regions=[[region1, region2], [region3, region4]])
op2.verify()
assert op2.regions == [region1, region2, region3, region4]
assert op2.attributes[
AttrSizedRegionSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 2])
def test_two_var_operand_builder3():
region1 = Region()
region2 = Region()
region3 = Region()
region4 = Region()
op2 = TwoVarRegionOp.build(regions=[[region1], [region2, region3, region4]])
op2.verify()
assert op2.regions == [region1, region2, region3, region4]
assert op2.attributes[
AttrSizedRegionSegments.attribute_name
] == DenseArrayBase.from_list(i32, [1, 3])
################################################################################
# Successor #
################################################################################
@irdl_op_definition
class SuccessorOp(IRDLOperation):
name = "test.successor_op"
successor: Successor = successor_def()
traits = frozenset([IsTerminator()])
def test_successor_op_successor():
"""Test operation from IRDL operation definition can have successors"""
block0 = Block()
op = SuccessorOp.build(successors=[block0])
block1 = Block([op])
_ = Region([block0, block1])
op.verify()
assert len(op.successors) == 1
@irdl_op_definition
class OptSuccessorOp(IRDLOperation):
name = "test.opt_successora_op"
successor: OptSuccessor = opt_successor_def()
traits = frozenset([IsTerminator()])
def test_opt_successor_builder():
"""
Test operation from IRDL operation definition can optionally have
successors
"""
block0 = Block()
op1 = OptSuccessorOp.build(successors=[block0])
block1 = Block([op1])
op2 = OptSuccessorOp.build(successors=[None])
block2 = Block([op2])
_ = Region([block0, block1, block2])
op1.verify()
op2.verify()
@irdl_op_definition
class VarSuccessorOp(IRDLOperation):
name = "test.var_succesor_op"
successor: VarSuccessor = var_successor_def()
traits = frozenset([IsTerminator()])
def test_var_successor_builder():
"""
Test operation from IRDL operation definition can have variadic successors
"""
block0 = Block()
op = VarSuccessorOp.build(successors=[[block0, block0, block0]])
block1 = Block([op])
_ = Region([block0, block1])
op.verify()
assert len(op.successors) == 3
@irdl_op_definition
class TwoVarSuccessorOp(IRDLOperation):
name = "test.two_var_successor_op"
res1: VarSuccessor = var_successor_def()
res2: VarSuccessor = var_successor_def()
irdl_options = [AttrSizedSuccessorSegments()]
traits = frozenset([IsTerminator()])
def test_two_var_successor_builder():
"""
Test operation from IRDL operation definition can have variadic successors
along with their sizes as an attribute
"""
block1 = Block()
block2 = Block()
block3 = Block()
block4 = Block()
op2 = TwoVarSuccessorOp.build(successors=[[block1, block2], [block3, block4]])
block0 = Block([op2])
_ = Region([block0, block1, block2, block3, block4])
op2.verify()
assert op2.successors == [block1, block2, block3, block4]
assert op2.attributes[
AttrSizedSuccessorSegments.attribute_name
] == DenseArrayBase.from_list(i32, [2, 2])
def test_two_var_successor_builder2():
"""
Test operation from IRDL operation definition can have variadic successors
along with their sizes as an attribute
"""
block1 = Block()
block2 = Block()
block3 = Block()
block4 = Block()
op2 = TwoVarSuccessorOp.build(successors=[[block1], [block2, block3, block4]])
block0 = Block([op2])
_ = Region([block0, block1, block2, block3, block4])
op2.verify()
assert op2.successors == [block1, block2, block3, block4]
assert op2.attributes[
AttrSizedSuccessorSegments.attribute_name
] == DenseArrayBase.from_list(i32, [1, 3])
################################################################################
# Misc #
################################################################################
def test_parent_pointers():
op = ResultOp.build(result_types=[StringAttr("0")])
block = Block([op])
reg = Region(block)
reg_op = RegionOp.build(regions=[reg])
block_2 = Block([reg_op])
reg_2 = Region(block_2)
reg_op_2 = RegionOp.build(regions=[reg_2])
assert op.parent_block() is block
assert op.parent_region() is reg
assert op.parent_op() is reg_op
assert reg_op_2.parent_block() is None
assert reg_op_2.parent_region() is None
assert reg_op_2.parent_op() is None
assert reg.parent_op() is reg_op
assert reg.parent_block() is block_2
assert reg.parent_region() is reg_2
assert reg_2.parent_block() is None
assert reg_2.parent_region() is None
assert block.parent_region() is reg
assert block.parent_op() is reg_op
assert block.parent_block() is block_2
assert block_2.parent_block() is None