xref: /llvm-project/mlir/test/CAPI/transform.c (revision f42e1a7484796e3d0915abbc35ce6bdb205a1678)
1 //===- transform.c - Test of Transform 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-transform-test 2>&1 | FileCheck %s
11 
12 #include "mlir-c/Dialect/Transform.h"
13 #include "mlir-c/IR.h"
14 #include "mlir-c/Support.h"
15 
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 // CHECK-LABEL: testAnyOpType
testAnyOpType(MlirContext ctx)21 void testAnyOpType(MlirContext ctx) {
22   fprintf(stderr, "testAnyOpType\n");
23 
24   MlirType parsedType = mlirTypeParseGet(
25       ctx, mlirStringRefCreateFromCString("!transform.any_op"));
26   MlirType constructedType = mlirTransformAnyOpTypeGet(ctx);
27 
28   assert(!mlirTypeIsNull(parsedType) && "couldn't parse AnyOpType");
29   assert(!mlirTypeIsNull(constructedType) && "couldn't construct AnyOpType");
30 
31   // CHECK: equal: 1
32   fprintf(stderr, "equal: %d\n", mlirTypeEqual(parsedType, constructedType));
33 
34   // CHECK: parsedType isa AnyOpType: 1
35   fprintf(stderr, "parsedType isa AnyOpType: %d\n",
36           mlirTypeIsATransformAnyOpType(parsedType));
37   // CHECK: parsedType isa OperationType: 0
38   fprintf(stderr, "parsedType isa OperationType: %d\n",
39           mlirTypeIsATransformOperationType(parsedType));
40 
41   // CHECK: !transform.any_op
42   mlirTypeDump(constructedType);
43 
44   fprintf(stderr, "\n\n");
45 }
46 
47 // CHECK-LABEL: testOperationType
testOperationType(MlirContext ctx)48 void testOperationType(MlirContext ctx) {
49   fprintf(stderr, "testOperationType\n");
50 
51   MlirType parsedType = mlirTypeParseGet(
52       ctx, mlirStringRefCreateFromCString("!transform.op<\"foo.bar\">"));
53   MlirType constructedType = mlirTransformOperationTypeGet(
54       ctx, mlirStringRefCreateFromCString("foo.bar"));
55 
56   assert(!mlirTypeIsNull(parsedType) && "couldn't parse AnyOpType");
57   assert(!mlirTypeIsNull(constructedType) && "couldn't construct AnyOpType");
58 
59   // CHECK: equal: 1
60   fprintf(stderr, "equal: %d\n", mlirTypeEqual(parsedType, constructedType));
61 
62   // CHECK: parsedType isa AnyOpType: 0
63   fprintf(stderr, "parsedType isa AnyOpType: %d\n",
64           mlirTypeIsATransformAnyOpType(parsedType));
65   // CHECK: parsedType isa OperationType: 1
66   fprintf(stderr, "parsedType isa OperationType: %d\n",
67           mlirTypeIsATransformOperationType(parsedType));
68 
69   // CHECK: operation name equal: 1
70   MlirStringRef operationName =
71       mlirTransformOperationTypeGetOperationName(constructedType);
72   fprintf(stderr, "operation name equal: %d\n",
73           mlirStringRefEqual(operationName,
74                              mlirStringRefCreateFromCString("foo.bar")));
75 
76   // CHECK: !transform.op<"foo.bar">
77   mlirTypeDump(constructedType);
78 
79   fprintf(stderr, "\n\n");
80 }
81 
main(void)82 int main(void) {
83   MlirContext ctx = mlirContextCreate();
84   mlirDialectHandleRegisterDialect(mlirGetDialectHandle__transform__(), ctx);
85   testAnyOpType(ctx);
86   testOperationType(ctx);
87   mlirContextDestroy(ctx);
88   return EXIT_SUCCESS;
89 }
90