/
TestOps.td
3087 lines (2622 loc) · 106 KB
/
TestOps.td
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- TestOps.td - Test dialect operation definitions ----*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef TEST_OPS
#define TEST_OPS
include "TestDialect.td"
include "TestInterfaces.td"
include "mlir/Dialect/DLTI/DLTIBase.td"
include "mlir/Dialect/Linalg/IR/LinalgInterfaces.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/PatternBase.td"
include "mlir/IR/RegionKindInterface.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/CopyOpInterface.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"
include "mlir/Interfaces/InferIntRangeInterface.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/LoopLikeInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
// Include the attribute definitions.
include "TestAttrDefs.td"
// Include the type definitions.
include "TestTypeDefs.td"
class TEST_Op<string mnemonic, list<Trait> traits = []> :
Op<Test_Dialect, mnemonic, traits>;
//===----------------------------------------------------------------------===//
// Test Types
//===----------------------------------------------------------------------===//
def IntTypesOp : TEST_Op<"int_types"> {
let results = (outs
AnyI16:$any_i16,
SI32:$si32,
UI64:$ui64,
AnyInteger:$any_int
);
}
def ComplexF64 : Complex<F64>;
def ComplexOp : TEST_Op<"complex_f64"> {
let results = (outs ComplexF64);
}
def ComplexTensorOp : TEST_Op<"complex_f64_tensor"> {
let results = (outs TensorOf<[ComplexF64]>);
}
def TupleOp : TEST_Op<"tuple_32_bit"> {
let results = (outs TupleOf<[I32, F32]>);
}
def NestedTupleOp : TEST_Op<"nested_tuple_32_bit"> {
let results = (outs NestedTupleOf<[I32, F32]>);
}
def TakesStaticMemRefOp : TEST_Op<"takes_static_memref"> {
let arguments = (ins AnyStaticShapeMemRef:$x);
}
def RankLessThan2I8F32MemRefOp : TEST_Op<"rank_less_than_2_I8_F32_memref"> {
let results = (outs MemRefRankOf<[I8, F32], [0, 1]>);
}
def NDTensorOfOp : TEST_Op<"nd_tensor_of"> {
let arguments = (ins
0DTensorOf<[F32]>:$arg0,
1DTensorOf<[F32]>:$arg1,
2DTensorOf<[I16]>:$arg2,
3DTensorOf<[I16]>:$arg3,
4DTensorOf<[I16]>:$arg4
);
}
def RankedTensorOp : TEST_Op<"ranked_tensor_op"> {
let arguments = (ins AnyRankedTensor:$input);
}
def MultiTensorRankOf : TEST_Op<"multi_tensor_rank_of"> {
let arguments = (ins
TensorRankOf<[I8, I32, F32], [0, 1]>:$arg0
);
}
def TEST_TestType : DialectType<Test_Dialect,
CPred<"$_self.isa<::test::TestType>()">, "test">,
BuildableType<"$_builder.getType<::test::TestType>()">;
//===----------------------------------------------------------------------===//
// Test Symbols
//===----------------------------------------------------------------------===//
def SymbolOp : TEST_Op<"symbol", [Symbol]> {
let summary = "operation which defines a new symbol";
let arguments = (ins StrAttr:$sym_name,
OptionalAttr<StrAttr>:$sym_visibility);
}
def SymbolScopeOp : TEST_Op<"symbol_scope",
[SymbolTable, SingleBlockImplicitTerminator<"TerminatorOp">]> {
let summary = "operation which defines a new symbol table";
let regions = (region SizedRegion<1>:$region);
}
def SymbolTableRegionOp : TEST_Op<"symbol_table_region", [SymbolTable]> {
let summary = "operation which defines a new symbol table without a "
"restriction on a terminator";
let regions = (region SizedRegion<1>:$region);
}
//===----------------------------------------------------------------------===//
// Test Operands
//===----------------------------------------------------------------------===//
def MixedNormalVariadicOperandOp : TEST_Op<
"mixed_normal_variadic_operand", [SameVariadicOperandSize]> {
let arguments = (ins
Variadic<AnyTensor>:$input1,
AnyTensor:$input2,
Variadic<AnyTensor>:$input3
);
}
def VariadicWithSameOperandsResult :
TEST_Op<"variadic_with_same_operand_results",
[SameOperandsAndResultType]> {
let arguments = (ins Variadic<AnySignlessInteger>);
let results = (outs AnySignlessInteger:$result);
}
def SameOperandsResultType : TEST_Op<
"same_operand_result_type", [SameOperandsAndResultType]> {
let arguments = (ins AnyTensor:$operand);
let results = (outs AnyTensor:$result);
}
//===----------------------------------------------------------------------===//
// Test Results
//===----------------------------------------------------------------------===//
def MixedNormalVariadicResults : TEST_Op<
"mixed_normal_variadic_result", [SameVariadicResultSize]> {
let results = (outs
Variadic<AnyTensor>:$output1,
AnyTensor:$output2,
Variadic<AnyTensor>:$output3
);
}
//===----------------------------------------------------------------------===//
// Test Attributes
//===----------------------------------------------------------------------===//
def AnyAttrOfOp : TEST_Op<"any_attr_of_i32_str"> {
let arguments = (ins AnyAttrOf<[I32Attr, StrAttr]>:$attr);
}
def NonNegIntAttrOp : TEST_Op<"non_negative_int_attr"> {
let arguments = (ins
ConfinedAttr<I32Attr, [IntNonNegative]>:$i32attr,
ConfinedAttr<I64Attr, [IntNonNegative]>:$i64attr
);
}
def PositiveIntAttrOp : TEST_Op<"positive_int_attr"> {
let arguments = (ins
ConfinedAttr<I32Attr, [IntPositive]>:$i32attr,
ConfinedAttr<I64Attr, [IntPositive]>:$i64attr
);
}
def TypeArrayAttrOp : TEST_Op<"type_array_attr"> {
let arguments = (ins TypeArrayAttr:$attr);
}
def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> {
let arguments = (ins DefaultValuedAttr<TypeArrayAttr, "{}">:$attr);
}
def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> {
let arguments = (ins TypedStrAttr<AnyType>:$attr);
let assemblyFormat = "$attr attr-dict";
}
def FloatAttrOp : TEST_Op<"float_attrs"> {
// TODO: Clean up the OpBase float type and attribute selectors so they
// can express all of the types.
let arguments = (ins
AnyAttr:$float_attr
);
}
def I32Case5: I32EnumAttrCase<"case5", 5>;
def I32Case10: I32EnumAttrCase<"case10", 10>;
def SomeI32Enum: I32EnumAttr<
"SomeI32Enum", "", [I32Case5, I32Case10]>;
def I32EnumAttrOp : TEST_Op<"i32_enum_attr"> {
let arguments = (ins SomeI32Enum:$attr);
let results = (outs I32:$val);
}
def I64Case5: I64EnumAttrCase<"case5", 5>;
def I64Case10: I64EnumAttrCase<"case10", 10>;
def SomeI64Enum: I64EnumAttr<
"SomeI64Enum", "", [I64Case5, I64Case10]>;
def I64EnumAttrOp : TEST_Op<"i64_enum_attr"> {
let arguments = (ins SomeI64Enum:$attr);
let results = (outs I32:$val);
}
def IntAttrOp : TEST_Op<"int_attrs"> {
let arguments = (ins
AnyI32Attr:$any_i32_attr,
IndexAttr:$index_attr,
UI32Attr:$ui32_attr,
SI32Attr:$si32_attr
);
}
def FloatElementsAttrOp : TEST_Op<"float_elements_attr"> {
let arguments = (ins
RankedF32ElementsAttr<[2]>:$scalar_f32_attr,
RankedF64ElementsAttr<[4, 8]>:$tensor_f64_attr
);
}
// A pattern that updates dense<[3.0, 4.0]> to dense<[5.0, 6.0]>.
// This tests both matching and generating float elements attributes.
def UpdateFloatElementsAttr : Pat<
(FloatElementsAttrOp
ConstantAttr<RankedF32ElementsAttr<[2]>, "{3.0f, 4.0f}">:$f32attr,
$f64attr),
(FloatElementsAttrOp
ConstantAttr<RankedF32ElementsAttr<[2]>, "{5.0f, 6.0f}">:$f32attr,
$f64attr)>;
def IntElementsAttrOp : TEST_Op<"int_elements_attr"> {
let arguments = (ins
AnyI32ElementsAttr:$any_i32_attr,
I32ElementsAttr:$i32_attr
);
}
def RankedIntElementsAttrOp : TEST_Op<"ranked_int_elements_attr"> {
let arguments = (ins
RankedI32ElementsAttr<[2]>:$vector_i32_attr,
RankedI64ElementsAttr<[4, 8]>:$matrix_i64_attr
);
}
def DerivedTypeAttrOp : TEST_Op<"derived_type_attr", []> {
let results = (outs AnyTensor:$output);
DerivedTypeAttr element_dtype =
DerivedTypeAttr<"return getElementTypeOrSelf(getOutput().getType());">;
DerivedAttr size = DerivedAttr<"int",
"return getOutput().getType().cast<ShapedType>().getSizeInBits();",
"$_builder.getI32IntegerAttr($_self)">;
}
def StringElementsAttrOp : TEST_Op<"string_elements_attr"> {
let arguments = (ins
StringElementsAttr:$scalar_string_attr
);
}
def TypedAttrOp : TEST_Op<"typed_attr"> {
let arguments = (ins TypeAttr:$type, AnyAttr:$attr);
let assemblyFormat = [{
attr-dict $type `=` custom<AttrElideType>(ref($type), $attr)
}];
}
def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
let arguments = (ins
DenseBoolArrayAttr:$i1attr,
DenseI8ArrayAttr:$i8attr,
DenseI16ArrayAttr:$i16attr,
DenseI32ArrayAttr:$i32attr,
DenseI64ArrayAttr:$i64attr,
DenseF32ArrayAttr:$f32attr,
DenseF64ArrayAttr:$f64attr,
DenseI32ArrayAttr:$emptyattr
);
let assemblyFormat = [{
`i1attr` `=` $i1attr `i8attr` `=` $i8attr `i16attr` `=` $i16attr
`i32attr` `=` $i32attr `i64attr` `=` $i64attr `f32attr` `=` $f32attr
`f64attr` `=` $f64attr `emptyattr` `=` $emptyattr attr-dict
}];
}
def ConfinedDenseArrayAttrOp : TEST_Op<"confined_dense_array_attr"> {
let arguments = (ins
ConfinedAttr<DenseI16ArrayAttr,
[DenseArrayStrictlySorted<DenseI16ArrayAttr>]>:$emptyattr,
ConfinedAttr<DenseI32ArrayAttr,
[DenseArraySorted<DenseI32ArrayAttr>]>:$i32attr,
ConfinedAttr<DenseI64ArrayAttr,
[DenseArrayStrictlySorted<DenseI64ArrayAttr>]>:$i64attr
);
}
//===----------------------------------------------------------------------===//
// Test Enum Attributes
//===----------------------------------------------------------------------===//
// Define the C++ enum.
def TestEnum
: I32EnumAttr<"TestEnum", "a test enum", [
I32EnumAttrCase<"First", 0, "first">,
I32EnumAttrCase<"Second", 1, "second">,
I32EnumAttrCase<"Third", 2, "third">,
]> {
let genSpecializedAttr = 0;
let cppNamespace = "test";
}
// Define the enum attribute.
def TestEnumAttr : EnumAttr<Test_Dialect, TestEnum, "enum">;
// Define an op that contains the enum attribute.
def OpWithEnum : TEST_Op<"op_with_enum"> {
let arguments = (ins TestEnumAttr:$value, OptionalAttr<AnyAttr>:$tag);
let assemblyFormat = "$value (`tag` $tag^)? attr-dict";
}
// Define a pattern that matches and creates an enum attribute.
def : Pat<(OpWithEnum ConstantAttr<TestEnumAttr,
"::test::TestEnum::First">:$value,
ConstantAttr<I32Attr, "0">:$tag),
(OpWithEnum ConstantAttr<TestEnumAttr,
"::test::TestEnum::Second">,
ConstantAttr<I32Attr, "1">)>;
//===----------------------------------------------------------------------===//
// Test Bit Enum Attributes
//===----------------------------------------------------------------------===//
// Define the C++ enum.
def TestBitEnum
: I32BitEnumAttr<"TestBitEnum", "a test bit enum", [
I32BitEnumAttrCaseBit<"Read", 0, "read">,
I32BitEnumAttrCaseBit<"Write", 1, "write">,
I32BitEnumAttrCaseBit<"Execute", 2, "execute">,
]> {
let genSpecializedAttr = 0;
let cppNamespace = "test";
let separator = ", ";
}
// Define the enum attribute.
def TestBitEnumAttr : EnumAttr<Test_Dialect, TestBitEnum, "bit_enum"> {
let assemblyFormat = "`<` $value `>`";
}
// Define an op that contains the enum attribute.
def OpWithBitEnum : TEST_Op<"op_with_bit_enum"> {
let arguments = (ins TestBitEnumAttr:$value, OptionalAttr<AnyAttr>:$tag);
let assemblyFormat = "$value (`tag` $tag^)? attr-dict";
}
// Define an enum with a different separator
def TestBitEnumVerticalBar
: I32BitEnumAttr<"TestBitEnumVerticalBar", "another test bit enum", [
I32BitEnumAttrCaseBit<"User", 0, "user">,
I32BitEnumAttrCaseBit<"Group", 1, "group">,
I32BitEnumAttrCaseBit<"Other", 2, "other">,
]> {
let genSpecializedAttr = 0;
let cppNamespace = "test";
let separator = " | ";
}
def TestBitEnumVerticalBarAttr
: EnumAttr<Test_Dialect, TestBitEnumVerticalBar, "bit_enum_vbar"> {
let assemblyFormat = "`<` $value `>`";
}
// Define an op that contains the enum attribute.
def OpWithBitEnumVerticalBar : TEST_Op<"op_with_bit_enum_vbar"> {
let arguments = (ins TestBitEnumVerticalBarAttr:$value,
OptionalAttr<AnyAttr>:$tag);
let assemblyFormat = "$value (`tag` $tag^)? attr-dict";
}
//===----------------------------------------------------------------------===//
// Test Regions
//===----------------------------------------------------------------------===//
def OneRegionOp : TEST_Op<"one_region_op", []> {
let regions = (region AnyRegion);
}
def TwoRegionOp : TEST_Op<"two_region_op", []> {
let regions = (region AnyRegion, AnyRegion);
}
def SizedRegionOp : TEST_Op<"sized_region_op", []> {
let regions = (region SizedRegion<2>:$my_region, SizedRegion<1>);
}
def VariadicRegionInferredTypesOp : TEST_Op<"variadic_region_inferred",
[InferTypeOpInterface]> {
let regions = (region VariadicRegion<AnyRegion>:$bodies);
let results = (outs Variadic<AnyType>);
let extraClassDeclaration = [{
static mlir::LogicalResult inferReturnTypes(mlir::MLIRContext *context,
llvm::Optional<::mlir::Location> location, mlir::ValueRange operands,
mlir::DictionaryAttr attributes, mlir::RegionRange regions,
llvm::SmallVectorImpl<mlir::Type> &inferredReturnTypes) {
inferredReturnTypes.assign({mlir::IntegerType::get(context, 16)});
return mlir::success();
}
}];
}
//===----------------------------------------------------------------------===//
// NoTerminator Operation
//===----------------------------------------------------------------------===//
def SingleNoTerminatorOp : TEST_Op<"single_no_terminator_op",
GraphRegionNoTerminator.traits> {
let regions = (region SizedRegion<1>:$my_region);
let assemblyFormat = "attr-dict `:` $my_region";
}
def SingleNoTerminatorCustomAsmOp : TEST_Op<"single_no_terminator_custom_asm_op",
[SingleBlock, NoTerminator]> {
let regions = (region SizedRegion<1>);
let hasCustomAssemblyFormat = 1;
}
def VariadicNoTerminatorOp : TEST_Op<"variadic_no_terminator_op",
GraphRegionNoTerminator.traits> {
let regions = (region VariadicRegion<SizedRegion<1>>:$my_regions);
let assemblyFormat = "attr-dict `:` $my_regions";
}
//===----------------------------------------------------------------------===//
// Test Call Interfaces
//===----------------------------------------------------------------------===//
def TestCallOp : TEST_Op<"call", [DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands);
let results = (outs Variadic<AnyType>);
let assemblyFormat = [{
$callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
}];
}
def ConversionCallOp : TEST_Op<"conversion_call_op",
[CallOpInterface]> {
let arguments = (ins Variadic<AnyType>:$arg_operands, SymbolRefAttr:$callee);
let results = (outs Variadic<AnyType>);
let extraClassDeclaration = [{
/// Return the callee of this operation.
::mlir::CallInterfaceCallable getCallableForCallee();
}];
let extraClassDefinition = [{
::mlir::CallInterfaceCallable $cppClass::getCallableForCallee() {
return (*this)->getAttrOfType<::mlir::SymbolRefAttr>("callee");
}
}];
}
def FunctionalRegionOp : TEST_Op<"functional_region_op",
[CallableOpInterface]> {
let regions = (region AnyRegion:$body);
let results = (outs FunctionType);
let extraClassDeclaration = [{
::mlir::Region *getCallableRegion() { return &getBody(); }
::llvm::ArrayRef<::mlir::Type> getCallableResults() {
return getType().cast<::mlir::FunctionType>().getResults();
}
}];
}
def FoldToCallOp : TEST_Op<"fold_to_call_op"> {
let arguments = (ins FlatSymbolRefAttr:$callee);
let hasCanonicalizer = 1;
}
//===----------------------------------------------------------------------===//
// Test Traits
//===----------------------------------------------------------------------===//
def SameOperandElementTypeOp : TEST_Op<"same_operand_element_type",
[SameOperandsElementType]> {
let arguments = (ins AnyType, AnyType);
let results = (outs AnyType);
}
def SameOperandAndResultElementTypeOp :
TEST_Op<"same_operand_and_result_element_type",
[SameOperandsAndResultElementType]> {
let arguments = (ins Variadic<AnyType>);
let results = (outs Variadic<AnyType>);
}
def SameOperandShapeOp : TEST_Op<"same_operand_shape", [SameOperandsShape]> {
let arguments = (ins Variadic<AnyShaped>);
}
def SameOperandAndResultShapeOp : TEST_Op<"same_operand_and_result_shape",
[SameOperandsAndResultShape]> {
let arguments = (ins Variadic<AnyShaped>);
let results = (outs Variadic<AnyShaped>);
}
def SameOperandAndResultTypeOp : TEST_Op<"same_operand_and_result_type",
[SameOperandsAndResultType]> {
let arguments = (ins Variadic<AnyType>);
let results = (outs Variadic<AnyType>);
}
def ElementwiseMappableOp : TEST_Op<"elementwise_mappable",
ElementwiseMappable.traits> {
let arguments = (ins Variadic<AnyType>);
let results = (outs Variadic<AnyType>);
}
def ArgAndResHaveFixedElementTypesOp :
TEST_Op<"arg_and_res_have_fixed_element_types",
[PredOpTrait<"fixed type combination",
And<[ElementTypeIsPred<"x", I32>,
ElementTypeIsPred<"y", F32>]>>,
ElementTypeIs<"res", I16>]> {
let arguments = (ins
AnyShaped:$x, AnyShaped:$y);
let results = (outs AnyShaped:$res);
}
def OperandsHaveSameElementType : TEST_Op<"operands_have_same_element_type", [
AllElementTypesMatch<["x", "y"]>]> {
let arguments = (ins AnyType:$x, AnyType:$y);
}
def OperandZeroAndResultHaveSameElementType : TEST_Op<
"operand0_and_result_have_same_element_type",
[AllElementTypesMatch<["x", "res"]>]> {
let arguments = (ins AnyType:$x, AnyType:$y);
let results = (outs AnyType:$res);
}
def OperandsHaveSameType :
TEST_Op<"operands_have_same_type", [AllTypesMatch<["x", "y"]>]> {
let arguments = (ins AnyType:$x, AnyType:$y);
}
def ResultHasSameTypeAsAttr :
TEST_Op<"result_has_same_type_as_attr",
[AllTypesMatch<["attr", "result"]>]> {
let arguments = (ins TypedAttrInterface:$attr);
let results = (outs AnyType:$result);
let assemblyFormat = "$attr `->` type($result) attr-dict";
}
def OperandZeroAndResultHaveSameType :
TEST_Op<"operand0_and_result_have_same_type",
[AllTypesMatch<["x", "res"]>]> {
let arguments = (ins AnyType:$x, AnyType:$y);
let results = (outs AnyType:$res);
}
def OperandsHaveSameRank :
TEST_Op<"operands_have_same_rank", [AllRanksMatch<["x", "y"]>]> {
let arguments = (ins AnyShaped:$x, AnyShaped:$y);
}
def OperandZeroAndResultHaveSameRank :
TEST_Op<"operand0_and_result_have_same_rank",
[AllRanksMatch<["x", "res"]>]> {
let arguments = (ins AnyShaped:$x, AnyShaped:$y);
let results = (outs AnyShaped:$res);
}
def OperandZeroAndResultHaveSameShape :
TEST_Op<"operand0_and_result_have_same_shape",
[AllShapesMatch<["x", "res"]>]> {
let arguments = (ins AnyShaped:$x, AnyShaped:$y);
let results = (outs AnyShaped:$res);
}
def OperandZeroAndResultHaveSameElementCount :
TEST_Op<"operand0_and_result_have_same_element_count",
[AllElementCountsMatch<["x", "res"]>]> {
let arguments = (ins AnyShaped:$x, AnyShaped:$y);
let results = (outs AnyShaped:$res);
}
def FourEqualsFive :
TEST_Op<"four_equals_five", [AllMatch<["5", "4"], "4 equals 5">]>;
def OperandRankEqualsResultSize :
TEST_Op<"operand_rank_equals_result_size",
[AllMatch<[Rank<"operand">.result, ElementCount<"result">.result],
"operand rank equals result size">]> {
let arguments = (ins AnyShaped:$operand);
let results = (outs AnyShaped:$result);
}
def IfFirstOperandIsNoneThenSoIsSecond :
TEST_Op<"if_first_operand_is_none_then_so_is_second", [PredOpTrait<
"has either both none type operands or first is not none",
Or<[
And<[TypeIsPred<"x", NoneType>, TypeIsPred<"y", NoneType>]>,
Neg<TypeIsPred<"x", NoneType>>]>>]> {
let arguments = (ins AnyType:$x, AnyType:$y);
}
def BroadcastableOp : TEST_Op<"broadcastable", [ResultsBroadcastableShape]> {
let arguments = (ins Variadic<AnyTensor>);
let results = (outs AnyTensor);
}
// HasParent trait
def ParentOp : TEST_Op<"parent"> {
let regions = (region AnyRegion);
}
def ChildOp : TEST_Op<"child", [HasParent<"ParentOp">]>;
// ParentOneOf trait
def ParentOp1 : TEST_Op<"parent1"> {
let regions = (region AnyRegion);
}
def ChildWithParentOneOf : TEST_Op<"child_with_parent_one_of",
[ParentOneOf<["ParentOp", "ParentOp1"]>]>;
def TerminatorOp : TEST_Op<"finish", [Terminator]>;
def SingleBlockImplicitTerminatorOp : TEST_Op<"SingleBlockImplicitTerminator",
[SingleBlockImplicitTerminator<"TerminatorOp">]> {
let regions = (region SizedRegion<1>:$region);
}
def I32ElementsAttrOp : TEST_Op<"i32ElementsAttr"> {
let arguments = (ins I32ElementsAttr:$attr);
}
def IndexElementsAttrOp : TEST_Op<"indexElementsAttr"> {
let arguments = (ins IndexElementsAttr:$attr);
}
def OpWithInferTypeInterfaceOp : TEST_Op<"op_with_infer_type_if", [
DeclareOpInterfaceMethods<InferTypeOpInterface>]> {
let arguments = (ins AnyTensor, AnyTensor);
let results = (outs AnyTensor);
}
def OpWithRefineTypeInterfaceOp : TEST_Op<"op_with_refine_type_if", [
DeclareOpInterfaceMethods<InferTypeOpInterface,
["refineReturnTypes"]>]> {
let arguments = (ins AnyTensor, AnyTensor);
let results = (outs AnyTensor);
}
def OpWithShapedTypeInferTypeInterfaceOp : TEST_Op<"op_with_shaped_type_infer_type_if",
[InferTensorTypeWithReify]> {
let arguments = (ins AnyTensor, AnyTensor);
let results = (outs AnyTensor);
}
def OpWithResultShapeInterfaceOp : TEST_Op<"op_with_result_shape_interface",
[DeclareOpInterfaceMethods<InferShapedTypeOpInterface,
["reifyReturnTypeShapes"]>]> {
let arguments = (ins AnyRankedTensor:$operand1, AnyRankedTensor:$operand2);
let results = (outs AnyRankedTensor:$result1, AnyRankedTensor:$result2);
}
def OpWithResultShapePerDimInterfaceOp :
TEST_Op<"op_with_result_shape_per_dim_interface",
[DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
let arguments = (ins AnyRankedTensor:$operand1, AnyRankedTensor:$operand2);
let results = (outs AnyRankedTensor:$result1, AnyRankedTensor:$result2);
}
def IsNotScalar : Constraint<CPred<"$0.getType().getRank() != 0">>;
def UpdateAttr : Pat<(I32ElementsAttrOp $attr),
(I32ElementsAttrOp ConstantAttr<I32ElementsAttr, "0">),
[(IsNotScalar $attr)]>;
def TestBranchOp : TEST_Op<"br",
[DeclareOpInterfaceMethods<BranchOpInterface>, Terminator]> {
let arguments = (ins Variadic<AnyType>:$targetOperands);
let successors = (successor AnySuccessor:$target);
}
def TestProducingBranchOp : TEST_Op<"producing_br",
[DeclareOpInterfaceMethods<BranchOpInterface>, Terminator,
AttrSizedOperandSegments]> {
let arguments = (ins Variadic<AnyType>:$firstOperands,
Variadic<AnyType>:$secondOperands);
let results = (outs I32:$dummy);
let successors = (successor AnySuccessor:$first,AnySuccessor:$second);
}
// Produces an error value on the error path
def TestInternalBranchOp : TEST_Op<"internal_br",
[DeclareOpInterfaceMethods<BranchOpInterface>, Terminator,
AttrSizedOperandSegments]> {
let arguments = (ins Variadic<AnyType>:$successOperands,
Variadic<AnyType>:$errorOperands);
let successors = (successor AnySuccessor:$successPath, AnySuccessor:$errorPath);
}
def AttrSizedOperandOp : TEST_Op<"attr_sized_operands",
[AttrSizedOperandSegments]> {
let arguments = (ins
Variadic<I32>:$a,
Variadic<I32>:$b,
I32:$c,
Variadic<I32>:$d,
DenseI32ArrayAttr:$operand_segment_sizes
);
}
def AttrSizedResultOp : TEST_Op<"attr_sized_results",
[AttrSizedResultSegments]> {
let results = (outs
Variadic<I32>:$a,
Variadic<I32>:$b,
I32:$c,
Variadic<I32>:$d
);
}
def AttrSizedResultCompileTestOp : TEST_Op<"attr_sized_results_compile_test",
[AttrSizedResultSegments]> {
let results = (outs Variadic<I32>:$a, I32:$b, Optional<I32>:$c);
}
// This is used to test that the fallback for a custom op's parser and printer
// is the dialect parser and printer hooks.
def CustomFormatFallbackOp : TEST_Op<"dialect_custom_format_fallback">;
// Ops related to OIList primitive
def OIListTrivial : TEST_Op<"oilist_with_keywords_only"> {
let arguments = (ins UnitAttr:$keyword, UnitAttr:$otherKeyword,
UnitAttr:$diffNameUnitAttrKeyword);
let assemblyFormat = [{
oilist( `keyword` $keyword
| `otherKeyword` $otherKeyword
| `thirdKeyword` $diffNameUnitAttrKeyword) attr-dict
}];
}
def OIListSimple : TEST_Op<"oilist_with_simple_args", [AttrSizedOperandSegments]> {
let arguments = (ins Optional<AnyType>:$arg0,
Optional<AnyType>:$arg1,
Optional<AnyType>:$arg2);
let assemblyFormat = [{
oilist( `keyword` $arg0 `:` type($arg0)
| `otherKeyword` $arg1 `:` type($arg1)
| `thirdKeyword` $arg2 `:` type($arg2) ) attr-dict
}];
}
def OIListVariadic : TEST_Op<"oilist_variadic_with_parens", [AttrSizedOperandSegments]> {
let arguments = (ins Variadic<AnyType>:$arg0,
Variadic<AnyType>:$arg1,
Variadic<AnyType>:$arg2);
let assemblyFormat = [{
oilist( `keyword` `(` $arg0 `:` type($arg0) `)`
| `otherKeyword` `(` $arg1 `:` type($arg1) `)`
| `thirdKeyword` `(` $arg2 `:` type($arg2) `)`) attr-dict
}];
}
def OIListCustom : TEST_Op<"oilist_custom", [AttrSizedOperandSegments]> {
let arguments = (ins Variadic<AnyType>:$arg0,
Optional<I32>:$optOperand,
UnitAttr:$nowait);
let assemblyFormat = [{
oilist( `private` `(` $arg0 `:` type($arg0) `)`
| `reduction` custom<CustomOptionalOperand>($optOperand)
| `nowait` $nowait
) attr-dict
}];
}
def OIListAllowedLiteral : TEST_Op<"oilist_allowed_literal"> {
let assemblyFormat = [{
oilist( `foo` | `bar` ) `buzz` attr-dict
}];
}
def TestEllipsisOp : TEST_Op<"ellipsis"> {
let arguments = (ins Variadic<AnyType>:$operands, UnitAttr:$variadic);
let assemblyFormat = [{
`(` $operands (`...` $variadic^)? `)` attr-dict `:` type($operands) `...`
}];
}
def ElseAnchorOp : TEST_Op<"else_anchor"> {
let arguments = (ins Optional<AnyType>:$a);
let assemblyFormat = "`(` (`?`) : (`` $a^ `:` type($a))? `)` attr-dict";
}
// This is used to test encoding of a string attribute into an SSA name of a
// pretty printed value name.
def StringAttrPrettyNameOp
: TEST_Op<"string_attr_pretty_name",
[DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let arguments = (ins StrArrayAttr:$names);
let results = (outs Variadic<I32>:$r);
let hasCustomAssemblyFormat = 1;
}
// This is used to test encoding of a string attribute into an SSA name of a
// pretty printed value name.
def CustomResultsNameOp
: TEST_Op<"custom_result_name",
[DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let arguments = (ins
Variadic<AnyInteger>:$optional,
StrArrayAttr:$names
);
let results = (outs Variadic<AnyInteger>:$r);
}
// This is used to test the OpAsmOpInterface::getDefaultDialect() feature:
// operations nested in a region under this op will drop the "test." dialect
// prefix.
def DefaultDialectOp : TEST_Op<"default_dialect", [OpAsmOpInterface]> {
let regions = (region AnyRegion:$body);
let extraClassDeclaration = [{
static ::llvm::StringRef getDefaultDialect() {
return "test";
}
void getAsmResultNames(::llvm::function_ref<void(::mlir::Value, ::llvm::StringRef)> setNameFn) {}
}];
let assemblyFormat = "regions attr-dict-with-keyword";
}
// This is used to test that the default dialect is not elided when printing an
// op with dots in the name to avoid parsing ambiguity.
def OpWithDotInNameOp : TEST_Op<"op.with_dot_in_name"> {
let assemblyFormat = "attr-dict";
}
// This is used to test the OpAsmOpInterface::getAsmBlockName() feature:
// blocks nested in a region under this op will have a name defined by the
// interface.
def AsmBlockNameOp : TEST_Op<"block_names", [OpAsmOpInterface]> {
let regions = (region AnyRegion:$body);
let extraClassDeclaration = [{
void getAsmBlockNames(mlir::OpAsmSetBlockNameFn setNameFn) {
std::string name;
int count = 0;
for (::mlir::Block &block : getRegion().getBlocks()) {
name = "foo" + std::to_string(count++);
setNameFn(&block, name);
}
}
}];
let assemblyFormat = "regions attr-dict-with-keyword";
}
// This operation requires its return type to have the trait 'TestTypeTrait'.
def ResultTypeWithTraitOp : TEST_Op<"result_type_with_trait", []> {
let results = (outs AnyType);
let hasVerifier = 1;
}
// This operation requires its "attr" attribute to have the
// trait 'TestAttrTrait'.
def AttrWithTraitOp : TEST_Op<"attr_with_trait", []> {
let arguments = (ins AnyAttr:$attr);
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// Test Locations
//===----------------------------------------------------------------------===//
def TestLocationSrcOp : TEST_Op<"loc_src"> {
let arguments = (ins I32:$input);
let results = (outs I32:$output);
}
def TestLocationDstOp : TEST_Op<"loc_dst", [SameOperandsAndResultType]> {
let arguments = (ins I32:$input);
let results = (outs I32:$output);
}
def TestLocationSrcNoResOp : TEST_Op<"loc_src_no_res"> {
let arguments = (ins I32:$input);
let results = (outs);
}
def TestLocationDstNoResOp : TEST_Op<"loc_dst_no_res"> {
let arguments = (ins I32:$input);
let results = (outs);
}
//===----------------------------------------------------------------------===//
// Test Patterns
//===----------------------------------------------------------------------===//
def OpA : TEST_Op<"op_a"> {
let arguments = (ins I32, I32Attr:$attr);
let results = (outs I32);
}
def OpB : TEST_Op<"op_b"> {
let arguments = (ins I32, I32Attr:$attr);
let results = (outs I32);
}
// Test named pattern.
def TestNamedPatternRule : Pat<(OpA $input, $attr), (OpB $input, $attr)>;
// Test with fused location.
def : Pat<(OpA (OpA $input, $attr), $bttr), (OpB $input, $bttr)>;
// Test added benefit.
def OpD : TEST_Op<"op_d">, Arguments<(ins I32)>, Results<(outs I32)>;
def OpE : TEST_Op<"op_e">, Arguments<(ins I32)>, Results<(outs I32)>;
def OpF : TEST_Op<"op_f">, Arguments<(ins I32)>, Results<(outs I32)>;
def OpG : TEST_Op<"op_g">, Arguments<(ins I32)>, Results<(outs I32)>;
// Verify that bumping benefit results in selecting different op.
def : Pat<(OpD $input), (OpE $input)>;
def : Pat<(OpD $input), (OpF $input), [], (addBenefit 10)>;
// Verify that patterns with more source nodes are selected before those with fewer.
def : Pat<(OpG $input), (OpB $input, ConstantAttr<I32Attr, "20">:$attr)>;
def : Pat<(OpG (OpG $input)), (OpB $input, ConstantAttr<I32Attr, "34">:$attr)>;
// Test patterns for zero-result op.
def OpH : TEST_Op<"op_h">, Arguments<(ins I32)>, Results<(outs)>;
def OpI : TEST_Op<"op_i">, Arguments<(ins I32)>, Results<(outs)>;
def : Pat<(OpH $input), (OpI $input)>;
// Test patterns for zero-input op.
def OpJ : TEST_Op<"op_j">, Arguments<(ins)>, Results<(outs I32)>;
def OpK : TEST_Op<"op_k">, Arguments<(ins)>, Results<(outs I32)>;
def : Pat<(OpJ), (OpK)>;
// Test that natives calls are only called once during rewrites.
def OpM : TEST_Op<"op_m"> {
let arguments = (ins I32, OptionalAttr<I32Attr>:$optional_attr);
let results = (outs I32);
}
def OpN : TEST_Op<"op_n"> {
let arguments = (ins I32, I32);
let results = (outs I32);
}
def OpO : TEST_Op<"op_o"> {
let arguments = (ins I32);
let results = (outs I32);
}
def OpP : TEST_Op<"op_p"> {
let arguments = (ins I32, I32, I32, I32, I32, I32);
let results = (outs I32);
}
// Test same operand name enforces equality condition check.
def TestEqualArgsPattern : Pat<(OpN $a, $a), (OpO $a)>;
// Test when equality is enforced at different depth.
def TestNestedOpEqualArgsPattern :
Pat<(OpN $b, (OpP $a, $b, $c, $d, $e, $f)), (replaceWithValue $b)>;
// Test when equality is enforced on same op and same operand but at different
// depth. We only bound one of the $x to the second operand of outer OpN and
// left another be the default value (which is the value of first operand of
// outer OpN). As a result, it ended up comparing wrong values in some cases.
def TestNestedSameOpAndSameArgEqualityPattern :
Pat<(OpN (OpN $_, $x), $x), (replaceWithValue $x)>;
// Test multiple equal arguments check enforced.
def TestMultipleEqualArgsPattern :
Pat<(OpP $a, $b, $a, $a, $b, $c), (OpN $c, $b)>;
// Test for memrefs normalization of an op with normalizable memrefs.