MLIR  17.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"
16 #include "llvm/ExecutionEngine/Orc/Mangling.h"
17 #include "llvm/Support/TargetSelect.h"
18 
19 using namespace mlir;
20 
21 extern "C" MlirExecutionEngine
22 mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
23  const MlirStringRef *sharedLibPaths,
24  bool enableObjectDump) {
25  static bool initOnce = [] {
26  llvm::InitializeNativeTarget();
27  llvm::InitializeNativeTargetAsmParser(); // needed for inline_asm
28  llvm::InitializeNativeTargetAsmPrinter();
29  return true;
30  }();
31  (void)initOnce;
32 
33  auto &ctx = *unwrap(op)->getContext();
36 
37  auto tmBuilderOrError = llvm::orc::JITTargetMachineBuilder::detectHost();
38  if (!tmBuilderOrError) {
39  llvm::errs() << "Failed to create a JITTargetMachineBuilder for the host\n";
40  return MlirExecutionEngine{nullptr};
41  }
42  auto tmOrError = tmBuilderOrError->createTargetMachine();
43  if (!tmOrError) {
44  llvm::errs() << "Failed to create a TargetMachine for the host\n";
45  return MlirExecutionEngine{nullptr};
46  }
47 
48  SmallVector<StringRef> libPaths;
49  for (unsigned i = 0; i < static_cast<unsigned>(numPaths); ++i)
50  libPaths.push_back(sharedLibPaths[i].data);
51 
52  // Create a transformer to run all LLVM optimization passes at the
53  // specified optimization level.
54  auto llvmOptLevel = static_cast<llvm::CodeGenOpt::Level>(optLevel);
55  auto transformer = mlir::makeOptimizingTransformer(
56  llvmOptLevel, /*sizeLevel=*/0, /*targetMachine=*/tmOrError->get());
57  ExecutionEngineOptions jitOptions;
58  jitOptions.transformer = transformer;
59  jitOptions.jitCodeGenOptLevel = llvmOptLevel;
60  jitOptions.sharedLibPaths = libPaths;
61  jitOptions.enableObjectDump = enableObjectDump;
62  auto jitOrError = ExecutionEngine::create(unwrap(op), jitOptions);
63  if (!jitOrError) {
64  consumeError(jitOrError.takeError());
65  return MlirExecutionEngine{nullptr};
66  }
67  return wrap(jitOrError->release());
68 }
69 
70 extern "C" void mlirExecutionEngineDestroy(MlirExecutionEngine jit) {
71  delete (unwrap(jit));
72 }
73 
74 extern "C" MlirLogicalResult
75 mlirExecutionEngineInvokePacked(MlirExecutionEngine jit, MlirStringRef name,
76  void **arguments) {
77  const std::string ifaceName = ("_mlir_ciface_" + unwrap(name)).str();
78  llvm::Error error = unwrap(jit)->invokePacked(
79  ifaceName, MutableArrayRef<void *>{arguments, (size_t)0});
80  if (error)
81  return wrap(failure());
82  return wrap(success());
83 }
84 
85 extern "C" void *mlirExecutionEngineLookupPacked(MlirExecutionEngine jit,
86  MlirStringRef name) {
87  auto expectedFPtr = unwrap(jit)->lookupPacked(unwrap(name));
88  if (!expectedFPtr)
89  return nullptr;
90  return reinterpret_cast<void *>(*expectedFPtr);
91 }
92 
93 extern "C" void *mlirExecutionEngineLookup(MlirExecutionEngine jit,
94  MlirStringRef name) {
95  auto expectedFPtr = unwrap(jit)->lookup(unwrap(name));
96  if (!expectedFPtr)
97  return nullptr;
98  return reinterpret_cast<void *>(*expectedFPtr);
99 }
100 
101 extern "C" void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit,
102  MlirStringRef name,
103  void *sym) {
104  unwrap(jit)->registerSymbols([&](llvm::orc::MangleAndInterner interner) {
105  llvm::orc::SymbolMap symbolMap;
106  symbolMap[interner(unwrap(name))] =
107  { llvm::orc::ExecutorAddr::fromPtr(sym),
108  llvm::JITSymbolFlags::Exported };
109  return symbolMap;
110  });
111 }
112 
113 extern "C" void mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit,
114  MlirStringRef name) {
115  unwrap(jit)->dumpToObjectFile(unwrap(name));
116 }
void * mlirExecutionEngineLookupPacked(MlirExecutionEngine jit, MlirStringRef name)
Lookup the wrapper of the native function in the execution engine with the given name,...
void mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit, MlirStringRef name)
Dump as an object in fileName.
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...
MlirExecutionEngine mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths, const MlirStringRef *sharedLibPaths, bool enableObjectDump)
Creates an ExecutionEngine for the provided ModuleOp.
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.
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.
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
uint64_t Level
The type of level identifiers, and level-ranks.
Definition: SparseTensor.h:46
This header declares functions that assit transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
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 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:114
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
const char * data
Pointer to the first symbol.
Definition: Support.h:72
std::optional< llvm::CodeGenOpt::Level > 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...