| 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 | |
| 20 | int testApplyNamedSequence(MlirContext ctx) { |
| 21 | fprintf(stderr, format: "%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(str: module); |
| 33 | MlirStringRef nameStringRef = mlirStringRefCreateFromCString(str: "inline-module" ); |
| 34 | |
| 35 | MlirOperation root = |
| 36 | mlirOperationCreateParse(context: ctx, sourceStr: moduleStringRef, sourceName: nameStringRef); |
| 37 | if (mlirOperationIsNull(op: root)) |
| 38 | return 1; |
| 39 | MlirBlock body = mlirRegionGetFirstBlock(region: mlirOperationGetRegion(op: root, pos: 0)); |
| 40 | MlirOperation entry = mlirBlockGetFirstOperation(block: body); |
| 41 | |
| 42 | MlirTransformOptions options = mlirTransformOptionsCreate(); |
| 43 | mlirTransformOptionsEnableExpensiveChecks(transformOptions: options, true); |
| 44 | mlirTransformOptionsEnforceSingleTopLevelTransformOp(transformOptions: options, true); |
| 45 | |
| 46 | MlirLogicalResult result = |
| 47 | mlirTransformApplyNamedSequence(payload: root, transformRoot: entry, transformModule: root, transformOptions: options); |
| 48 | mlirTransformOptionsDestroy(transformOptions: options); |
| 49 | mlirOperationDestroy(op: root); |
| 50 | if (mlirLogicalResultIsFailure(res: 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 | |
| 61 | int main(void) { |
| 62 | MlirContext ctx = mlirContextCreate(); |
| 63 | mlirDialectHandleRegisterDialect(mlirGetDialectHandle__transform__(), ctx); |
| 64 | int result = testApplyNamedSequence(ctx); |
| 65 | mlirContextDestroy(context: ctx); |
| 66 | if (result) |
| 67 | return result; |
| 68 | |
| 69 | return EXIT_SUCCESS; |
| 70 | } |
| 71 | |