xref: /llvm-project/mlir/test/CAPI/transform_interpreter.c (revision fd4efecac21d92428d2f804f43e85bdfa460bdd5)
1 //===- transform_interpreter.c - Test of the Transform interpreter 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-interpreter-test 2>&1 | FileCheck %s
11 
12 #include "mlir-c/Dialect/Transform.h"
13 #include "mlir-c/Dialect/Transform/Interpreter.h"
14 #include "mlir-c/IR.h"
15 #include "mlir-c/Support.h"
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 
testApplyNamedSequence(MlirContext ctx)20 int testApplyNamedSequence(MlirContext ctx) {
21   fprintf(stderr, "%s\n", __func__);
22 
23   const char module[] =
24       "module attributes {transform.with_named_sequence} {"
25       "  transform.named_sequence @__transform_main(%root: !transform.any_op) {"
26       "    transform.print %root { name = \"from interpreter\" }: "
27       "!transform.any_op"
28       "    transform.yield"
29       "  }"
30       "}";
31 
32   MlirStringRef moduleStringRef = mlirStringRefCreateFromCString(module);
33   MlirStringRef nameStringRef = mlirStringRefCreateFromCString("inline-module");
34 
35   MlirOperation root =
36       mlirOperationCreateParse(ctx, moduleStringRef, nameStringRef);
37   if (mlirOperationIsNull(root))
38     return 1;
39   MlirBlock body = mlirRegionGetFirstBlock(mlirOperationGetRegion(root, 0));
40   MlirOperation entry = mlirBlockGetFirstOperation(body);
41 
42   MlirTransformOptions options = mlirTransformOptionsCreate();
43   mlirTransformOptionsEnableExpensiveChecks(options, true);
44   mlirTransformOptionsEnforceSingleTopLevelTransformOp(options, true);
45 
46   MlirLogicalResult result =
47       mlirTransformApplyNamedSequence(root, entry, root, options);
48   mlirTransformOptionsDestroy(options);
49   mlirOperationDestroy(root);
50   if (mlirLogicalResultIsFailure(result))
51     return 2;
52 
53   return 0;
54 }
55 // CHECK-LABEL: testApplyNamedSequence
56 // CHECK: from interpreter
57 // CHECK: transform.named_sequence @__transform_main
58 // CHECK:   transform.print %arg0
59 // CHECK:   transform.yield
60 
main(void)61 int main(void) {
62   MlirContext ctx = mlirContextCreate();
63   mlirDialectHandleRegisterDialect(mlirGetDialectHandle__transform__(), ctx);
64   int result = testApplyNamedSequence(ctx);
65   mlirContextDestroy(ctx);
66   if (result)
67     return result;
68 
69   return EXIT_SUCCESS;
70 }
71