[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / mlir / test / CAPI / execution_engine.c
blob38a8fb8c3e2137d734630f9eb16dfbe71ff0d162
1 //===- execution_engine.c - Test for the C bindings for the MLIR JIT-------===//
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 //===----------------------------------------------------------------------===//
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"
20 #include <assert.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.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");
44 exit(2);
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(
56 // clang-format off
57 "module { \n"
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"
61 " } \n"
62 "}"));
63 // clang-format on
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");
71 exit(2);
73 int input = 42;
74 int result = -1;
75 void *args[2] = {&input, &result};
76 if (mlirLogicalResultIsFailure(mlirExecutionEngineInvokePacked(
77 jit, mlirStringRefCreateFromCString("add"), args))) {
78 fprintf(stderr, "Execution engine creation failed");
79 abort();
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(
95 // clang-format off
96 "module { \n"
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"
101 " omp.parallel { \n"
102 " omp.wsloop for (%3) : i32 = (%0) to (%2) step (%1) { \n"
103 " omp.yield \n"
104 " } \n"
105 " omp.terminator \n"
106 " } \n"
107 " llvm.return \n"
108 " } \n"
109 "} \n"
111 // clang-format on
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");
125 exit(2);
127 // CHECK: Engine creation succeeded with OpenMP
128 printf("Engine creation succeeded with OpenMP\n");
129 mlirExecutionEngineDestroy(jit);
130 mlirModuleDestroy(module);
131 mlirContextDestroy(ctx);
134 int main(void) {
136 #define _STRINGIFY(x) #x
137 #define STRINGIFY(x) _STRINGIFY(x)
138 #define TEST(test) \
139 printf("Running test '" STRINGIFY(test) "'\n"); \
140 test();
142 TEST(testSimpleExecution);
143 TEST(testOmpCreation);
144 return 0;