1//===- pdl.c - Test of PDL dialect C API ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4// Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10// RUN: mlir-capi-pdl-test 2>&1 | FileCheck %s
11
12#include "mlir-c/Dialect/PDL.h"
13#include "mlir-c/BuiltinTypes.h"
14#include "mlir-c/IR.h"
15
16#include <assert.h>
17#include <inttypes.h>
18#include <stdio.h>
19#include <stdlib.h>
20
21// CHECK-LABEL: testAttributeType
22void testAttributeType(MlirContext ctx) {
23 fprintf(stderr, format: "testAttributeType\n");
24
25 MlirType parsedType = mlirTypeParseGet(
26 context: ctx, type: mlirStringRefCreateFromCString(str: "!pdl.attribute"));
27 MlirType constructedType = mlirPDLAttributeTypeGet(ctx);
28
29 assert(!mlirTypeIsNull(parsedType) && "couldn't parse PDLAttributeType");
30 assert(!mlirTypeIsNull(constructedType) && "couldn't construct PDLAttributeType");
31
32 // CHECK: parsedType isa PDLType: 1
33 fprintf(stderr, format: "parsedType isa PDLType: %d\n",
34 mlirTypeIsAPDLType(type: parsedType));
35 // CHECK: parsedType isa PDLAttributeType: 1
36 fprintf(stderr, format: "parsedType isa PDLAttributeType: %d\n",
37 mlirTypeIsAPDLAttributeType(type: parsedType));
38 // CHECK: parsedType isa PDLOperationType: 0
39 fprintf(stderr, format: "parsedType isa PDLOperationType: %d\n",
40 mlirTypeIsAPDLOperationType(type: parsedType));
41 // CHECK: parsedType isa PDLRangeType: 0
42 fprintf(stderr, format: "parsedType isa PDLRangeType: %d\n",
43 mlirTypeIsAPDLRangeType(type: parsedType));
44 // CHECK: parsedType isa PDLTypeType: 0
45 fprintf(stderr, format: "parsedType isa PDLTypeType: %d\n",
46 mlirTypeIsAPDLTypeType(type: parsedType));
47 // CHECK: parsedType isa PDLValueType: 0
48 fprintf(stderr, format: "parsedType isa PDLValueType: %d\n",
49 mlirTypeIsAPDLValueType(type: parsedType));
50
51 // CHECK: constructedType isa PDLType: 1
52 fprintf(stderr, format: "constructedType isa PDLType: %d\n",
53 mlirTypeIsAPDLType(type: constructedType));
54 // CHECK: constructedType isa PDLAttributeType: 1
55 fprintf(stderr, format: "constructedType isa PDLAttributeType: %d\n",
56 mlirTypeIsAPDLAttributeType(type: constructedType));
57 // CHECK: constructedType isa PDLOperationType: 0
58 fprintf(stderr, format: "constructedType isa PDLOperationType: %d\n",
59 mlirTypeIsAPDLOperationType(type: constructedType));
60 // CHECK: constructedType isa PDLRangeType: 0
61 fprintf(stderr, format: "constructedType isa PDLRangeType: %d\n",
62 mlirTypeIsAPDLRangeType(type: constructedType));
63 // CHECK: constructedType isa PDLTypeType: 0
64 fprintf(stderr, format: "constructedType isa PDLTypeType: %d\n",
65 mlirTypeIsAPDLTypeType(type: constructedType));
66 // CHECK: constructedType isa PDLValueType: 0
67 fprintf(stderr, format: "constructedType isa PDLValueType: %d\n",
68 mlirTypeIsAPDLValueType(type: constructedType));
69
70 // CHECK: equal: 1
71 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: parsedType, t2: constructedType));
72
73 // CHECK: !pdl.attribute
74 mlirTypeDump(type: parsedType);
75 // CHECK: !pdl.attribute
76 mlirTypeDump(type: constructedType);
77
78 fprintf(stderr, format: "\n\n");
79}
80
81// CHECK-LABEL: testOperationType
82void testOperationType(MlirContext ctx) {
83 fprintf(stderr, format: "testOperationType\n");
84
85 MlirType parsedType = mlirTypeParseGet(
86 context: ctx, type: mlirStringRefCreateFromCString(str: "!pdl.operation"));
87 MlirType constructedType = mlirPDLOperationTypeGet(ctx);
88
89 assert(!mlirTypeIsNull(parsedType) && "couldn't parse PDLAttributeType");
90 assert(!mlirTypeIsNull(constructedType) && "couldn't construct PDLAttributeType");
91
92 // CHECK: parsedType isa PDLType: 1
93 fprintf(stderr, format: "parsedType isa PDLType: %d\n",
94 mlirTypeIsAPDLType(type: parsedType));
95 // CHECK: parsedType isa PDLAttributeType: 0
96 fprintf(stderr, format: "parsedType isa PDLAttributeType: %d\n",
97 mlirTypeIsAPDLAttributeType(type: parsedType));
98 // CHECK: parsedType isa PDLOperationType: 1
99 fprintf(stderr, format: "parsedType isa PDLOperationType: %d\n",
100 mlirTypeIsAPDLOperationType(type: parsedType));
101 // CHECK: parsedType isa PDLRangeType: 0
102 fprintf(stderr, format: "parsedType isa PDLRangeType: %d\n",
103 mlirTypeIsAPDLRangeType(type: parsedType));
104 // CHECK: parsedType isa PDLTypeType: 0
105 fprintf(stderr, format: "parsedType isa PDLTypeType: %d\n",
106 mlirTypeIsAPDLTypeType(type: parsedType));
107 // CHECK: parsedType isa PDLValueType: 0
108 fprintf(stderr, format: "parsedType isa PDLValueType: %d\n",
109 mlirTypeIsAPDLValueType(type: parsedType));
110
111 // CHECK: constructedType isa PDLType: 1
112 fprintf(stderr, format: "constructedType isa PDLType: %d\n",
113 mlirTypeIsAPDLType(type: constructedType));
114 // CHECK: constructedType isa PDLAttributeType: 0
115 fprintf(stderr, format: "constructedType isa PDLAttributeType: %d\n",
116 mlirTypeIsAPDLAttributeType(type: constructedType));
117 // CHECK: constructedType isa PDLOperationType: 1
118 fprintf(stderr, format: "constructedType isa PDLOperationType: %d\n",
119 mlirTypeIsAPDLOperationType(type: constructedType));
120 // CHECK: constructedType isa PDLRangeType: 0
121 fprintf(stderr, format: "constructedType isa PDLRangeType: %d\n",
122 mlirTypeIsAPDLRangeType(type: constructedType));
123 // CHECK: constructedType isa PDLTypeType: 0
124 fprintf(stderr, format: "constructedType isa PDLTypeType: %d\n",
125 mlirTypeIsAPDLTypeType(type: constructedType));
126 // CHECK: constructedType isa PDLValueType: 0
127 fprintf(stderr, format: "constructedType isa PDLValueType: %d\n",
128 mlirTypeIsAPDLValueType(type: constructedType));
129
130 // CHECK: equal: 1
131 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: parsedType, t2: constructedType));
132
133 // CHECK: !pdl.operation
134 mlirTypeDump(type: parsedType);
135 // CHECK: !pdl.operation
136 mlirTypeDump(type: constructedType);
137
138 fprintf(stderr, format: "\n\n");
139}
140
141// CHECK-LABEL: testRangeType
142void testRangeType(MlirContext ctx) {
143 fprintf(stderr, format: "testRangeType\n");
144
145 MlirType typeType = mlirPDLTypeTypeGet(ctx);
146 MlirType parsedType = mlirTypeParseGet(
147 context: ctx, type: mlirStringRefCreateFromCString(str: "!pdl.range<type>"));
148 MlirType constructedType = mlirPDLRangeTypeGet(elementType: typeType);
149 MlirType elementType = mlirPDLRangeTypeGetElementType(type: constructedType);
150
151 assert(!mlirTypeIsNull(typeType) && "couldn't get PDLTypeType");
152 assert(!mlirTypeIsNull(parsedType) && "couldn't parse PDLAttributeType");
153 assert(!mlirTypeIsNull(constructedType) && "couldn't construct PDLAttributeType");
154
155 // CHECK: parsedType isa PDLType: 1
156 fprintf(stderr, format: "parsedType isa PDLType: %d\n",
157 mlirTypeIsAPDLType(type: parsedType));
158 // CHECK: parsedType isa PDLAttributeType: 0
159 fprintf(stderr, format: "parsedType isa PDLAttributeType: %d\n",
160 mlirTypeIsAPDLAttributeType(type: parsedType));
161 // CHECK: parsedType isa PDLOperationType: 0
162 fprintf(stderr, format: "parsedType isa PDLOperationType: %d\n",
163 mlirTypeIsAPDLOperationType(type: parsedType));
164 // CHECK: parsedType isa PDLRangeType: 1
165 fprintf(stderr, format: "parsedType isa PDLRangeType: %d\n",
166 mlirTypeIsAPDLRangeType(type: parsedType));
167 // CHECK: parsedType isa PDLTypeType: 0
168 fprintf(stderr, format: "parsedType isa PDLTypeType: %d\n",
169 mlirTypeIsAPDLTypeType(type: parsedType));
170 // CHECK: parsedType isa PDLValueType: 0
171 fprintf(stderr, format: "parsedType isa PDLValueType: %d\n",
172 mlirTypeIsAPDLValueType(type: parsedType));
173
174 // CHECK: constructedType isa PDLType: 1
175 fprintf(stderr, format: "constructedType isa PDLType: %d\n",
176 mlirTypeIsAPDLType(type: constructedType));
177 // CHECK: constructedType isa PDLAttributeType: 0
178 fprintf(stderr, format: "constructedType isa PDLAttributeType: %d\n",
179 mlirTypeIsAPDLAttributeType(type: constructedType));
180 // CHECK: constructedType isa PDLOperationType: 0
181 fprintf(stderr, format: "constructedType isa PDLOperationType: %d\n",
182 mlirTypeIsAPDLOperationType(type: constructedType));
183 // CHECK: constructedType isa PDLRangeType: 1
184 fprintf(stderr, format: "constructedType isa PDLRangeType: %d\n",
185 mlirTypeIsAPDLRangeType(type: constructedType));
186 // CHECK: constructedType isa PDLTypeType: 0
187 fprintf(stderr, format: "constructedType isa PDLTypeType: %d\n",
188 mlirTypeIsAPDLTypeType(type: constructedType));
189 // CHECK: constructedType isa PDLValueType: 0
190 fprintf(stderr, format: "constructedType isa PDLValueType: %d\n",
191 mlirTypeIsAPDLValueType(type: constructedType));
192
193 // CHECK: equal: 1
194 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: parsedType, t2: constructedType));
195 // CHECK: equal: 1
196 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: typeType, t2: elementType));
197
198 // CHECK: !pdl.range<type>
199 mlirTypeDump(type: parsedType);
200 // CHECK: !pdl.range<type>
201 mlirTypeDump(type: constructedType);
202 // CHECK: !pdl.type
203 mlirTypeDump(type: elementType);
204
205 fprintf(stderr, format: "\n\n");
206}
207
208// CHECK-LABEL: testTypeType
209void testTypeType(MlirContext ctx) {
210 fprintf(stderr, format: "testTypeType\n");
211
212 MlirType parsedType = mlirTypeParseGet(
213 context: ctx, type: mlirStringRefCreateFromCString(str: "!pdl.type"));
214 MlirType constructedType = mlirPDLTypeTypeGet(ctx);
215
216 assert(!mlirTypeIsNull(parsedType) && "couldn't parse PDLAttributeType");
217 assert(!mlirTypeIsNull(constructedType) && "couldn't construct PDLAttributeType");
218
219 // CHECK: parsedType isa PDLType: 1
220 fprintf(stderr, format: "parsedType isa PDLType: %d\n",
221 mlirTypeIsAPDLType(type: parsedType));
222 // CHECK: parsedType isa PDLAttributeType: 0
223 fprintf(stderr, format: "parsedType isa PDLAttributeType: %d\n",
224 mlirTypeIsAPDLAttributeType(type: parsedType));
225 // CHECK: parsedType isa PDLOperationType: 0
226 fprintf(stderr, format: "parsedType isa PDLOperationType: %d\n",
227 mlirTypeIsAPDLOperationType(type: parsedType));
228 // CHECK: parsedType isa PDLRangeType: 0
229 fprintf(stderr, format: "parsedType isa PDLRangeType: %d\n",
230 mlirTypeIsAPDLRangeType(type: parsedType));
231 // CHECK: parsedType isa PDLTypeType: 1
232 fprintf(stderr, format: "parsedType isa PDLTypeType: %d\n",
233 mlirTypeIsAPDLTypeType(type: parsedType));
234 // CHECK: parsedType isa PDLValueType: 0
235 fprintf(stderr, format: "parsedType isa PDLValueType: %d\n",
236 mlirTypeIsAPDLValueType(type: parsedType));
237
238 // CHECK: constructedType isa PDLType: 1
239 fprintf(stderr, format: "constructedType isa PDLType: %d\n",
240 mlirTypeIsAPDLType(type: constructedType));
241 // CHECK: constructedType isa PDLAttributeType: 0
242 fprintf(stderr, format: "constructedType isa PDLAttributeType: %d\n",
243 mlirTypeIsAPDLAttributeType(type: constructedType));
244 // CHECK: constructedType isa PDLOperationType: 0
245 fprintf(stderr, format: "constructedType isa PDLOperationType: %d\n",
246 mlirTypeIsAPDLOperationType(type: constructedType));
247 // CHECK: constructedType isa PDLRangeType: 0
248 fprintf(stderr, format: "constructedType isa PDLRangeType: %d\n",
249 mlirTypeIsAPDLRangeType(type: constructedType));
250 // CHECK: constructedType isa PDLTypeType: 1
251 fprintf(stderr, format: "constructedType isa PDLTypeType: %d\n",
252 mlirTypeIsAPDLTypeType(type: constructedType));
253 // CHECK: constructedType isa PDLValueType: 0
254 fprintf(stderr, format: "constructedType isa PDLValueType: %d\n",
255 mlirTypeIsAPDLValueType(type: constructedType));
256
257 // CHECK: equal: 1
258 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: parsedType, t2: constructedType));
259
260 // CHECK: !pdl.type
261 mlirTypeDump(type: parsedType);
262 // CHECK: !pdl.type
263 mlirTypeDump(type: constructedType);
264
265 fprintf(stderr, format: "\n\n");
266}
267
268// CHECK-LABEL: testValueType
269void testValueType(MlirContext ctx) {
270 fprintf(stderr, format: "testValueType\n");
271
272 MlirType parsedType = mlirTypeParseGet(
273 context: ctx, type: mlirStringRefCreateFromCString(str: "!pdl.value"));
274 MlirType constructedType = mlirPDLValueTypeGet(ctx);
275
276 assert(!mlirTypeIsNull(parsedType) && "couldn't parse PDLAttributeType");
277 assert(!mlirTypeIsNull(constructedType) && "couldn't construct PDLAttributeType");
278
279 // CHECK: parsedType isa PDLType: 1
280 fprintf(stderr, format: "parsedType isa PDLType: %d\n",
281 mlirTypeIsAPDLType(type: parsedType));
282 // CHECK: parsedType isa PDLAttributeType: 0
283 fprintf(stderr, format: "parsedType isa PDLAttributeType: %d\n",
284 mlirTypeIsAPDLAttributeType(type: parsedType));
285 // CHECK: parsedType isa PDLOperationType: 0
286 fprintf(stderr, format: "parsedType isa PDLOperationType: %d\n",
287 mlirTypeIsAPDLOperationType(type: parsedType));
288 // CHECK: parsedType isa PDLRangeType: 0
289 fprintf(stderr, format: "parsedType isa PDLRangeType: %d\n",
290 mlirTypeIsAPDLRangeType(type: parsedType));
291 // CHECK: parsedType isa PDLTypeType: 0
292 fprintf(stderr, format: "parsedType isa PDLTypeType: %d\n",
293 mlirTypeIsAPDLTypeType(type: parsedType));
294 // CHECK: parsedType isa PDLValueType: 1
295 fprintf(stderr, format: "parsedType isa PDLValueType: %d\n",
296 mlirTypeIsAPDLValueType(type: parsedType));
297
298 // CHECK: constructedType isa PDLType: 1
299 fprintf(stderr, format: "constructedType isa PDLType: %d\n",
300 mlirTypeIsAPDLType(type: constructedType));
301 // CHECK: constructedType isa PDLAttributeType: 0
302 fprintf(stderr, format: "constructedType isa PDLAttributeType: %d\n",
303 mlirTypeIsAPDLAttributeType(type: constructedType));
304 // CHECK: constructedType isa PDLOperationType: 0
305 fprintf(stderr, format: "constructedType isa PDLOperationType: %d\n",
306 mlirTypeIsAPDLOperationType(type: constructedType));
307 // CHECK: constructedType isa PDLRangeType: 0
308 fprintf(stderr, format: "constructedType isa PDLRangeType: %d\n",
309 mlirTypeIsAPDLRangeType(type: constructedType));
310 // CHECK: constructedType isa PDLTypeType: 0
311 fprintf(stderr, format: "constructedType isa PDLTypeType: %d\n",
312 mlirTypeIsAPDLTypeType(type: constructedType));
313 // CHECK: constructedType isa PDLValueType: 1
314 fprintf(stderr, format: "constructedType isa PDLValueType: %d\n",
315 mlirTypeIsAPDLValueType(type: constructedType));
316
317 // CHECK: equal: 1
318 fprintf(stderr, format: "equal: %d\n", mlirTypeEqual(t1: parsedType, t2: constructedType));
319
320 // CHECK: !pdl.value
321 mlirTypeDump(type: parsedType);
322 // CHECK: !pdl.value
323 mlirTypeDump(type: constructedType);
324
325 fprintf(stderr, format: "\n\n");
326}
327
328int main(void) {
329 MlirContext ctx = mlirContextCreate();
330 mlirDialectHandleRegisterDialect(mlirGetDialectHandle__pdl__(), ctx);
331 testAttributeType(ctx);
332 testOperationType(ctx);
333 testRangeType(ctx);
334 testTypeType(ctx);
335 testValueType(ctx);
336 mlirContextDestroy(context: ctx);
337 return EXIT_SUCCESS;
338}
339

source code of mlir/test/CAPI/pdl.c