1 //===- execution_engine.c - Test for the C bindings for the MLIR JIT-------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 /* RUN: mlir-capi-execution-engine-test 2>&1 | FileCheck %s
13 #include "mlir-c/Conversion.h"
14 #include "mlir-c/ExecutionEngine.h"
15 #include "mlir-c/IR.h"
16 #include "mlir-c/Registration.h"
24 void lowerModuleToLLVM(MlirContext ctx
, MlirModule module
) {
25 MlirPassManager pm
= mlirPassManagerCreate(ctx
);
26 MlirOpPassManager opm
= mlirPassManagerGetNestedUnder(
27 pm
, mlirStringRefCreateFromCString("builtin.func"));
28 mlirPassManagerAddOwnedPass(pm
, mlirCreateConversionConvertStandardToLLVM());
29 mlirOpPassManagerAddOwnedPass(opm
,
30 mlirCreateConversionConvertArithmeticToLLVM());
31 MlirLogicalResult status
= mlirPassManagerRun(pm
, module
);
32 if (mlirLogicalResultIsFailure(status
)) {
33 fprintf(stderr
, "Unexpected failure running pass pipeline\n");
36 mlirPassManagerDestroy(pm
);
39 // CHECK-LABEL: Running test 'testSimpleExecution'
40 void testSimpleExecution() {
41 MlirContext ctx
= mlirContextCreate();
42 mlirRegisterAllDialects(ctx
);
43 MlirModule module
= mlirModuleCreateParse(
44 ctx
, mlirStringRefCreateFromCString(
47 " func @add(%arg0 : i32) -> i32 attributes { llvm.emit_c_interface } { \n"
48 " %res = arith.addi %arg0, %arg0 : i32 \n"
49 " return %res : i32 \n"
53 lowerModuleToLLVM(ctx
, module
);
54 mlirRegisterAllLLVMTranslations(ctx
);
55 MlirExecutionEngine jit
= mlirExecutionEngineCreate(
56 module
, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL
);
57 if (mlirExecutionEngineIsNull(jit
)) {
58 fprintf(stderr
, "Execution engine creation failed");
63 void *args
[2] = {&input
, &result
};
64 if (mlirLogicalResultIsFailure(mlirExecutionEngineInvokePacked(
65 jit
, mlirStringRefCreateFromCString("add"), args
))) {
66 fprintf(stderr
, "Execution engine creation failed");
69 // CHECK: Input: 42 Result: 84
70 printf("Input: %d Result: %d\n", input
, result
);
71 mlirExecutionEngineDestroy(jit
);
72 mlirModuleDestroy(module
);
73 mlirContextDestroy(ctx
);
78 #define _STRINGIFY(x) #x
79 #define STRINGIFY(x) _STRINGIFY(x)
81 printf("Running test '" STRINGIFY(test) "'\n"); \
84 TEST(testSimpleExecution
);