xref: /llvm-project/mlir/test/tblgen-to-irdl/TestDialect.td (revision 69d3ba3db922fca8cfc47b5f115b6bea6a737aab)
1// RUN: tblgen-to-irdl %s -I=%S/../../include --gen-dialect-irdl-defs --dialect=test | FileCheck %s
2
3include "mlir/IR/OpBase.td"
4include "mlir/IR/AttrTypeBase.td"
5
6// CHECK-LABEL: irdl.dialect @test {
7def Test_Dialect : Dialect {
8  let name = "test";
9}
10
11class Test_Type<string name, string typeMnemonic, list<Trait> traits = []>
12: TypeDef<Test_Dialect, name, traits> {
13  let mnemonic = typeMnemonic;
14}
15
16class Test_Attr<string name, string attrMnemonic> : AttrDef<Test_Dialect, name> {
17  let mnemonic = attrMnemonic;
18}
19
20class Test_Op<string mnemonic, list<Trait> traits = []>
21    : Op<Test_Dialect, mnemonic, traits>;
22
23// CHECK: irdl.type @"!singleton_a"
24def Test_SingletonAType : Test_Type<"SingletonAType", "singleton_a"> {}
25// CHECK: irdl.type @"!singleton_b"
26def Test_SingletonBType : Test_Type<"SingletonBType", "singleton_b"> {}
27// CHECK: irdl.type @"!singleton_c"
28def Test_SingletonCType : Test_Type<"SingletonCType", "singleton_c"> {}
29// CHECK: irdl.attribute @"#test"
30def Test_TestAttr : Test_Attr<"Test", "test"> {}
31
32
33// Check that AllOfType is converted correctly.
34def Test_AndOp : Test_Op<"and"> {
35  let arguments = (ins AllOfType<[Test_SingletonAType, AnyType]>:$in);
36}
37// CHECK-LABEL: irdl.operation @and {
38// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
39// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.any
40// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.all_of(%[[v0]], %[[v1]])
41// CHECK-NEXT:    irdl.operands(in: %[[v2]])
42// CHECK-NEXT:  }
43
44
45// Check that AnyType is converted correctly.
46def Test_AnyOp : Test_Op<"any"> {
47  let arguments = (ins AnyType:$in);
48}
49// CHECK-LABEL: irdl.operation @any {
50// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.any
51// CHECK-NEXT:    irdl.operands(in: %[[v0]])
52// CHECK-NEXT:  }
53
54// Check attributes are converted correctly.
55def Test_AttributesOp : Test_Op<"attributes"> {
56  let arguments = (ins I16Attr:$int_attr,
57                       Test_TestAttr:$test_attr);
58}
59// CHECK-LABEL: irdl.operation @attributes {
60// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.base "!builtin.integer"
61// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.base @test::@"#test"
62// CHECK-NEXT:    irdl.attributes {"int_attr" = %[[v0]], "test_attr" = %[[v1]]}
63// CHECK-NEXT:  }
64
65// Check confined types are converted correctly.
66def Test_ConfinedOp : Test_Op<"confined"> {
67  let arguments = (ins ConfinedType<AnyType, [CPred<"::llvm::isa<::mlir::TensorType>($_self)">]>:$tensor,
68                       ConfinedType<AnyType, [And<[CPred<"::llvm::isa<::mlir::VectorType>($_self)">
69                                             , CPred<"::llvm::cast<::mlir::VectorType>($_self).getRank() > 0">]>]>:$vector);
70}
71// CHECK-LABEL: irdl.operation @confined {
72// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.any
73// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.c_pred "(::llvm::isa<::mlir::TensorType>($_self))"
74// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.all_of(%[[v0]], %[[v1]])
75// CHECK-NEXT:    %[[v3:[^ ]*]] = irdl.any
76// CHECK-NEXT:    %[[v4:[^ ]*]] = irdl.c_pred "(::llvm::isa<::mlir::VectorType>($_self))"
77// CHECK-NEXT:    %[[v5:[^ ]*]] = irdl.c_pred "(::llvm::cast<::mlir::VectorType>($_self).getRank() > 0)"
78// CHECK-NEXT:    %[[v6:[^ ]*]] = irdl.all_of(%[[v4]], %[[v5]])
79// CHECK-NEXT:    %[[v7:[^ ]*]] = irdl.all_of(%[[v3]], %[[v6]])
80// CHECK-NEXT:    irdl.operands(tensor: %[[v2]], vector: %[[v7]])
81// CHECK-NEXT:  }
82
83// Check generic integer types are converted correctly.
84def Test_Integers : Test_Op<"integers"> {
85  let arguments = (ins AnyI8:$any_int,
86                       AnyInteger:$any_integer);
87}
88// CHECK-LABEL: irdl.operation @integers {
89// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.is i8
90// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.is si8
91// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.is ui8
92// CHECK-NEXT:    %[[v3:[^ ]*]] = irdl.any_of(%[[v0]], %[[v1]], %[[v2]])
93// CHECK-NEXT:    %[[v4:[^ ]*]] = irdl.base "!builtin.integer"
94// CHECK-NEXT:    irdl.operands(any_int: %[[v3]], any_integer: %[[v4]])
95// CHECK-NEXT:  }
96
97// Check that IRDL names are properly generated when needed.
98def Test_NamesOp : Test_Op<"names"> {
99  let arguments = (ins I32:$a,
100                       I32:$_hello,
101                       I32:$unnamed0,
102                       I32);
103  let regions = (region AnyRegion:$unnamed1);
104  let results = (outs I32:$unnamed2);
105}
106// CHECK-LABEL: irdl.operation @names {
107// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.is i32
108// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.is i32
109// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.is i32
110// CHECK-NEXT:    %[[v3:[^ ]*]] = irdl.is i32
111// CHECK-NEXT:    %[[v4:[^ ]*]] = irdl.is i32
112// CHECK-NEXT:    %[[reg:[^ ]*]] = irdl.region
113// CHECK-NEXT:    irdl.operands(a: %[[v0]], _hello: %[[v1]], unnamed0: %[[v2]], unnamed3: %[[v3]])
114// CHECK-NEXT:    irdl.results(unnamed2: %[[v4]])
115// CHECK-NEXT:    irdl.regions(unnamed1: %[[reg]])
116// CHECK-NEXT:  }
117
118// Check that AnyTypeOf is converted correctly.
119def Test_OrOp : Test_Op<"or"> {
120  let arguments = (ins AnyTypeOf<[Test_SingletonAType, Test_SingletonBType, Test_SingletonCType]>:$in);
121}
122// CHECK-LABEL: irdl.operation @or {
123// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
124// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.base @test::@"!singleton_b"
125// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.base @test::@"!singleton_c"
126// CHECK-NEXT:    %[[v3:[^ ]*]] = irdl.any_of(%[[v0]], %[[v1]], %[[v2]])
127// CHECK-NEXT:    irdl.operands(in: %[[v3]])
128// CHECK-NEXT:  }
129
130// Check regions are converted correctly.
131def Test_RegionsOp : Test_Op<"regions"> {
132  let regions = (region AnyRegion:$any_region,
133                        SizedRegion<1>:$single_block_region);
134}
135// CHECK-LABEL: irdl.operation @regions {
136// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.region
137// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.region with size 1
138// CHECK-NEXT:    irdl.regions(any_region: %[[v0]], single_block_region: %[[v1]])
139// CHECK-NEXT:  }
140
141// Check that various types are converted correctly.
142def Test_TypesOp : Test_Op<"types"> {
143  let arguments = (ins I32:$a,
144                       SI64:$b,
145                       UI8:$c,
146                       Index:$d,
147                       F32:$e,
148                       NoneType:$f,
149                       Complex<F8E4M3FN>);
150}
151// CHECK-LABEL: irdl.operation @types {
152// CHECK-NEXT:    %{{.*}} = irdl.is i32
153// CHECK-NEXT:    %{{.*}} = irdl.is si64
154// CHECK-NEXT:    %{{.*}} = irdl.is ui8
155// CHECK-NEXT:    %{{.*}} = irdl.is index
156// CHECK-NEXT:    %{{.*}} = irdl.is f32
157// CHECK-NEXT:    %{{.*}} = irdl.is none
158// CHECK-NEXT:    %{{.*}} = irdl.is complex<f8E4M3FN>
159// CHECK-NEXT:    irdl.operands({{.*}})
160// CHECK-NEXT:  }
161
162// Check that variadics and optionals are converted correctly.
163def Test_VariadicityOp : Test_Op<"variadicity"> {
164  let arguments = (ins Variadic<Test_SingletonAType>:$variadic,
165                       Optional<Test_SingletonBType>:$optional,
166                       Test_SingletonCType:$required);
167}
168// CHECK-LABEL: irdl.operation @variadicity {
169// CHECK-NEXT:    %[[v0:[^ ]*]] = irdl.base @test::@"!singleton_a"
170// CHECK-NEXT:    %[[v1:[^ ]*]] = irdl.base @test::@"!singleton_b"
171// CHECK-NEXT:    %[[v2:[^ ]*]] = irdl.base @test::@"!singleton_c"
172// CHECK-NEXT:    irdl.operands(variadic: variadic %[[v0]], optional: optional %[[v1]], required: %[[v2]])
173// CHECK-NEXT:  }
174