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 //===----------------------------------------------------------------------===//
9 
10 /* RUN: mlir-capi-execution-engine-test 2>&1 | FileCheck %s
11  */
12 
13 #include "mlir-c/Conversion.h"
14 #include "mlir-c/ExecutionEngine.h"
15 #include "mlir-c/IR.h"
16 #include "mlir-c/Registration.h"
17 
18 #include <assert.h>
19 #include <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
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");
34     exit(2);
35   }
36   mlirPassManagerDestroy(pm);
37 }
38 
39 // CHECK-LABEL: Running test 'testSimpleExecution'
40 void testSimpleExecution() {
41   MlirContext ctx = mlirContextCreate();
42   mlirRegisterAllDialects(ctx);
43   MlirModule module = mlirModuleCreateParse(
44       ctx, mlirStringRefCreateFromCString(
45                // clang-format off
46 "module {                                                                   \n"
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"
50 "  }                                                                        \n"
51 "}"));
52   // clang-format on
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");
59     exit(2);
60   }
61   int input = 42;
62   int result = -1;
63   void *args[2] = {&input, &result};
64   if (mlirLogicalResultIsFailure(mlirExecutionEngineInvokePacked(
65           jit, mlirStringRefCreateFromCString("add"), args))) {
66     fprintf(stderr, "Execution engine creation failed");
67     abort();
68   }
69   // CHECK: Input: 42 Result: 84
70   printf("Input: %d Result: %d\n", input, result);
71   mlirExecutionEngineDestroy(jit);
72   mlirModuleDestroy(module);
73   mlirContextDestroy(ctx);
74 }
75 
76 int main() {
77 
78 #define _STRINGIFY(x) #x
79 #define STRINGIFY(x) _STRINGIFY(x)
80 #define TEST(test)                                                             \
81   printf("Running test '" STRINGIFY(test) "'\n");                              \
82   test();
83 
84   TEST(testSimpleExecution);
85   return 0;
86 }
87