xref: /llvm-project/mlir/include/mlir-c/ExecutionEngine.h (revision 95c083f579d6246a052ed798c6f02e30b2c3cf87)
1 //===-- mlir-c/ExecutionEngine.h - Execution engine management ---*- C -*-====//
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 // This header provides basic access to the MLIR JIT. This is minimalist and
11 // experimental at the moment.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_C_EXECUTIONENGINE_H
16 #define MLIR_C_EXECUTIONENGINE_H
17 
18 #include "mlir-c/IR.h"
19 #include "mlir-c/Support.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define DEFINE_C_API_STRUCT(name, storage)                                     \
26   struct name {                                                                \
27     storage *ptr;                                                              \
28   };                                                                           \
29   typedef struct name name
30 
31 DEFINE_C_API_STRUCT(MlirExecutionEngine, void);
32 
33 #undef DEFINE_C_API_STRUCT
34 
35 /// Creates an ExecutionEngine for the provided ModuleOp. The ModuleOp is
36 /// expected to be "translatable" to LLVM IR (only contains operations in
37 /// dialects that implement the `LLVMTranslationDialectInterface`). The module
38 /// ownership stays with the client and can be destroyed as soon as the call
39 /// returns. `optLevel` is the optimization level to be used for transformation
40 /// and code generation. LLVM passes at `optLevel` are run before code
41 /// generation. The number and array of paths corresponding to shared libraries
42 /// that will be loaded are specified via `numPaths` and `sharedLibPaths`
43 /// respectively.
44 /// TODO: figure out other options.
45 MLIR_CAPI_EXPORTED MlirExecutionEngine mlirExecutionEngineCreate(
46     MlirModule op, int optLevel, int numPaths,
47     const MlirStringRef *sharedLibPaths, bool enableObjectDump);
48 
49 /// Destroy an ExecutionEngine instance.
50 MLIR_CAPI_EXPORTED void mlirExecutionEngineDestroy(MlirExecutionEngine jit);
51 
52 /// Checks whether an execution engine is null.
mlirExecutionEngineIsNull(MlirExecutionEngine jit)53 static inline bool mlirExecutionEngineIsNull(MlirExecutionEngine jit) {
54   return !jit.ptr;
55 }
56 
57 /// Invoke a native function in the execution engine by name with the arguments
58 /// and result of the invoked function passed as an array of pointers. The
59 /// function must have been tagged with the `llvm.emit_c_interface` attribute.
60 /// Returns a failure if the execution fails for any reason (the function name
61 /// can't be resolved for instance).
62 MLIR_CAPI_EXPORTED MlirLogicalResult mlirExecutionEngineInvokePacked(
63     MlirExecutionEngine jit, MlirStringRef name, void **arguments);
64 
65 /// Lookup the wrapper of the native function in the execution engine with the
66 /// given name, returns nullptr if the function can't be looked-up.
67 MLIR_CAPI_EXPORTED void *
68 mlirExecutionEngineLookupPacked(MlirExecutionEngine jit, MlirStringRef name);
69 
70 /// Lookup a native function in the execution engine by name, returns nullptr
71 /// if the name can't be looked-up.
72 MLIR_CAPI_EXPORTED void *mlirExecutionEngineLookup(MlirExecutionEngine jit,
73                                                    MlirStringRef name);
74 
75 /// Register a symbol with the jit: this symbol will be accessible to the jitted
76 /// code.
77 MLIR_CAPI_EXPORTED void
78 mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit, MlirStringRef name,
79                                   void *sym);
80 
81 /// Dump as an object in `fileName`.
82 MLIR_CAPI_EXPORTED void
83 mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit,
84                                     MlirStringRef fileName);
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif // MLIR_C_EXECUTIONENGINE_H
91