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
12 /* REQUIRES: host-supports-jit
15 #include "mlir-c/Conversion.h"
16 #include "mlir-c/ExecutionEngine.h"
17 #include "mlir-c/IR.h"
18 #include "mlir-c/RegisterEverything.h"
26 static void registerAllUpstreamDialects(MlirContext ctx
) {
27 MlirDialectRegistry registry
= mlirDialectRegistryCreate();
28 mlirRegisterAllDialects(registry
);
29 mlirContextAppendDialectRegistry(ctx
, registry
);
30 mlirDialectRegistryDestroy(registry
);
33 void lowerModuleToLLVM(MlirContext ctx
, MlirModule module
) {
34 MlirPassManager pm
= mlirPassManagerCreate(ctx
);
35 MlirOpPassManager opm
= mlirPassManagerGetNestedUnder(
36 pm
, mlirStringRefCreateFromCString("func.func"));
37 mlirPassManagerAddOwnedPass(pm
, mlirCreateConversionConvertFuncToLLVMPass());
38 mlirOpPassManagerAddOwnedPass(
39 opm
, mlirCreateConversionArithToLLVMConversionPass());
40 MlirLogicalResult status
=
41 mlirPassManagerRunOnOp(pm
, mlirModuleGetOperation(module
));
42 if (mlirLogicalResultIsFailure(status
)) {
43 fprintf(stderr
, "Unexpected failure running pass pipeline\n");
46 mlirPassManagerDestroy(pm
);
49 // CHECK-LABEL: Running test 'testSimpleExecution'
50 void testSimpleExecution(void) {
51 MlirContext ctx
= mlirContextCreate();
52 registerAllUpstreamDialects(ctx
);
54 MlirModule module
= mlirModuleCreateParse(
55 ctx
, mlirStringRefCreateFromCString(
58 " func.func @add(%arg0 : i32) -> i32 attributes { llvm.emit_c_interface } { \n"
59 " %res = arith.addi %arg0, %arg0 : i32 \n"
60 " return %res : i32 \n"
64 lowerModuleToLLVM(ctx
, module
);
65 mlirRegisterAllLLVMTranslations(ctx
);
66 MlirExecutionEngine jit
= mlirExecutionEngineCreate(
67 module
, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL
,
68 /*enableObjectDump=*/false);
69 if (mlirExecutionEngineIsNull(jit
)) {
70 fprintf(stderr
, "Execution engine creation failed");
75 void *args
[2] = {&input
, &result
};
76 if (mlirLogicalResultIsFailure(mlirExecutionEngineInvokePacked(
77 jit
, mlirStringRefCreateFromCString("add"), args
))) {
78 fprintf(stderr
, "Execution engine creation failed");
81 // CHECK: Input: 42 Result: 84
82 printf("Input: %d Result: %d\n", input
, result
);
83 mlirExecutionEngineDestroy(jit
);
84 mlirModuleDestroy(module
);
85 mlirContextDestroy(ctx
);
88 // CHECK-LABEL: Running test 'testOmpCreation'
89 void testOmpCreation(void) {
90 MlirContext ctx
= mlirContextCreate();
91 registerAllUpstreamDialects(ctx
);
93 MlirModule module
= mlirModuleCreateParse(
94 ctx
, mlirStringRefCreateFromCString(
97 " func.func @main() attributes { llvm.emit_c_interface } { \n"
98 " %0 = arith.constant 0 : i32 \n"
99 " %1 = arith.constant 1 : i32 \n"
100 " %2 = arith.constant 2 : i32 \n"
102 " omp.wsloop for (%3) : i32 = (%0) to (%2) step (%1) { \n"
112 lowerModuleToLLVM(ctx
, module
);
114 // At this point all operations in the MLIR module have been lowered to the
115 // 'llvm' dialect except 'omp' operations. The goal of this test is
116 // guaranteeing that the execution engine C binding has registered OpenMP
117 // translations and therefore does not fail when it encounters 'omp' ops.
118 // We don't attempt to run the engine, since that would force us to link
119 // against the OpenMP library.
120 MlirExecutionEngine jit
= mlirExecutionEngineCreate(
121 module
, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL
,
122 /*enableObjectDump=*/false);
123 if (mlirExecutionEngineIsNull(jit
)) {
124 fprintf(stderr
, "Engine creation failed with OpenMP");
127 // CHECK: Engine creation succeeded with OpenMP
128 printf("Engine creation succeeded with OpenMP\n");
129 mlirExecutionEngineDestroy(jit
);
130 mlirModuleDestroy(module
);
131 mlirContextDestroy(ctx
);
136 #define _STRINGIFY(x) #x
137 #define STRINGIFY(x) _STRINGIFY(x)
139 printf("Running test '" STRINGIFY(test) "'\n"); \
142 TEST(testSimpleExecution
);
143 TEST(testOmpCreation
);