MLIR 22.0.0git
ExecutionEngine.cpp
Go to the documentation of this file.
1//===- ExecutionEngine.cpp - C API for MLIR JIT ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "mlir/CAPI/IR.h"
12#include "mlir/CAPI/Support.h"
17#include "llvm/ExecutionEngine/Orc/Mangling.h"
18#include "llvm/Support/TargetSelect.h"
19
20using namespace mlir;
21
22extern "C" MlirExecutionEngine
23mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
24 const MlirStringRef *sharedLibPaths,
25 bool enableObjectDump, bool enablePIC) {
26 static bool initOnce = [] {
27 llvm::InitializeNativeTarget();
28 llvm::InitializeNativeTargetAsmParser(); // needed for inline_asm
29 llvm::InitializeNativeTargetAsmPrinter();
30 return true;
31 }();
32 (void)initOnce;
33
34 auto &ctx = *unwrap(op)->getContext();
38
39 auto tmBuilderOrError = llvm::orc::JITTargetMachineBuilder::detectHost();
40 if (!tmBuilderOrError) {
41 consumeError(tmBuilderOrError.takeError());
42 return MlirExecutionEngine{nullptr};
43 }
44 if (enablePIC)
45 tmBuilderOrError->setRelocationModel(llvm::Reloc::PIC_);
46 auto tmOrError = tmBuilderOrError->createTargetMachine();
47 if (!tmOrError) {
48 consumeError(tmOrError.takeError());
49 return MlirExecutionEngine{nullptr};
50 }
51
53 for (unsigned i = 0; i < static_cast<unsigned>(numPaths); ++i)
54 libPaths.push_back(sharedLibPaths[i].data);
55
56 // Create a transformer to run all LLVM optimization passes at the
57 // specified optimization level.
58 auto transformer = mlir::makeOptimizingTransformer(
59 optLevel, /*sizeLevel=*/0, /*targetMachine=*/tmOrError->get());
60 ExecutionEngineOptions jitOptions;
61 jitOptions.transformer = transformer;
62 jitOptions.jitCodeGenOptLevel = static_cast<llvm::CodeGenOptLevel>(optLevel);
63 jitOptions.sharedLibPaths = libPaths;
64 jitOptions.enableObjectDump = enableObjectDump;
65 auto jitOrError = ExecutionEngine::create(unwrap(op), jitOptions,
66 std::move(tmOrError.get()));
67 if (!jitOrError) {
68 consumeError(jitOrError.takeError());
69 return MlirExecutionEngine{nullptr};
70 }
71 return wrap(jitOrError->release());
72}
73
74extern "C" void mlirExecutionEngineInitialize(MlirExecutionEngine jit) {
75 unwrap(jit)->initialize();
76}
77
78extern "C" void mlirExecutionEngineDestroy(MlirExecutionEngine jit) {
79 delete (unwrap(jit));
80}
81
82extern "C" MlirLogicalResult
83mlirExecutionEngineInvokePacked(MlirExecutionEngine jit, MlirStringRef name,
84 void **arguments) {
85 const std::string ifaceName = ("_mlir_ciface_" + unwrap(name)).str();
86 llvm::Error error = unwrap(jit)->invokePacked(
87 ifaceName, MutableArrayRef<void *>{arguments, (size_t)0});
88 if (error)
89 return wrap(failure());
90 return wrap(success());
91}
92
93extern "C" void *mlirExecutionEngineLookupPacked(MlirExecutionEngine jit,
94 MlirStringRef name) {
95 auto optionalFPtr =
96 llvm::expectedToOptional(unwrap(jit)->lookupPacked(unwrap(name)));
97 if (!optionalFPtr)
98 return nullptr;
99 return reinterpret_cast<void *>(*optionalFPtr);
100}
101
102extern "C" void *mlirExecutionEngineLookup(MlirExecutionEngine jit,
103 MlirStringRef name) {
104 auto optionalFPtr =
105 llvm::expectedToOptional(unwrap(jit)->lookup(unwrap(name)));
106 if (!optionalFPtr)
107 return nullptr;
108 return *optionalFPtr;
109}
110
111extern "C" void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit,
112 MlirStringRef name,
113 void *sym) {
114 unwrap(jit)->registerSymbols([&](llvm::orc::MangleAndInterner interner) {
115 llvm::orc::SymbolMap symbolMap;
116 symbolMap[interner(unwrap(name))] = {llvm::orc::ExecutorAddr::fromPtr(sym),
117 llvm::JITSymbolFlags::Exported};
118 return symbolMap;
119 });
120}
121
122extern "C" void mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit,
123 MlirStringRef name) {
124 unwrap(jit)->dumpToObjectFile(unwrap(name));
125}
return success()
void mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit, MlirStringRef name)
Dump as an object in fileName.
void mlirExecutionEngineInitialize(MlirExecutionEngine jit)
Initialize the ExecutionEngine.
MlirLogicalResult mlirExecutionEngineInvokePacked(MlirExecutionEngine jit, MlirStringRef name, void **arguments)
Invoke a native function in the execution engine by name with the arguments and result of the invoked...
void * mlirExecutionEngineLookupPacked(MlirExecutionEngine jit, MlirStringRef name)
Lookup the wrapper of the native function in the execution engine with the given name,...
void * mlirExecutionEngineLookup(MlirExecutionEngine jit, MlirStringRef name)
Lookup a native function in the execution engine by name, returns nullptr if the name can't be looked...
void mlirExecutionEngineDestroy(MlirExecutionEngine jit)
Destroy an ExecutionEngine instance.
MlirExecutionEngine mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths, const MlirStringRef *sharedLibPaths, bool enableObjectDump, bool enablePIC)
Creates an ExecutionEngine for the provided ModuleOp.
void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit, MlirStringRef name, void *sym)
Register a symbol with the jit: this symbol will be accessible to the jitted code.
static llvm::Expected< std::unique_ptr< ExecutionEngine > > create(Operation *op, const ExecutionEngineOptions &options={}, std::unique_ptr< llvm::TargetMachine > tm=nullptr)
Creates an execution engine for the given MLIR IR.
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
Include the generated interface declarations.
std::function< llvm::Error(llvm::Module *)> makeOptimizingTransformer(unsigned optLevel, unsigned sizeLevel, llvm::TargetMachine *targetMachine)
Create a module transformer function for MLIR ExecutionEngine that runs LLVM IR passes corresponding ...
void registerOpenMPDialectTranslation(DialectRegistry &registry)
Register the OpenMP dialect and the translation from it to the LLVM IR in the given registry;.
void registerBuiltinDialectTranslation(DialectRegistry &registry)
Register the translation from the builtin dialect to the LLVM IR in the given registry.
void registerLLVMDialectTranslation(DialectRegistry &registry)
Register the LLVM dialect and the translation from it to the LLVM IR in the given registry;.
A logical result value, essentially a boolean with named states.
Definition Support.h:116
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:73
const char * data
Pointer to the first symbol.
Definition Support.h:74
std::optional< llvm::CodeGenOptLevel > jitCodeGenOptLevel
jitCodeGenOptLevel, when provided, is used as the optimization level for target code generation.
ArrayRef< StringRef > sharedLibPaths
If sharedLibPaths are provided, the underlying JIT-compilation will open and link the shared librarie...
bool enableObjectDump
If enableObjectCache is set, the JIT compiler will create one to store the object generated for the g...
llvm::function_ref< llvm::Error(llvm::Module *)> transformer
If transformer is provided, it will be called on the LLVM module during JIT-compilation and can be us...