My Project
ExecutionEngine.h
Go to the documentation of this file.
1 //===- ExecutionEngine.h - MLIR Execution engine and utils -----*- C++ -*--===//
2 //
3 // Part of the MLIR 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 //
9 // This file provides a JIT-backed execution engine for MLIR modules.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_EXECUTIONENGINE_EXECUTIONENGINE_H_
14 #define MLIR_EXECUTIONENGINE_EXECUTIONENGINE_H_
15 
16 #include "mlir/Support/LLVM.h"
17 #include "llvm/ExecutionEngine/ObjectCache.h"
18 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/Support/Error.h"
21 
22 #include <functional>
23 #include <memory>
24 
25 namespace llvm {
26 template <typename T> class Expected;
27 class Module;
28 class ExecutionEngine;
29 class MemoryBuffer;
30 } // namespace llvm
31 
32 namespace mlir {
33 
34 class ModuleOp;
35 
37 class SimpleObjectCache : public llvm::ObjectCache {
38 public:
39  void notifyObjectCompiled(const llvm::Module *M,
40  llvm::MemoryBufferRef ObjBuffer) override;
41  std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module *M) override;
42 
44  void dumpToObjectFile(StringRef filename);
45 
46 private:
47  llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> cachedObjects;
48 };
49 
61 public:
62  ExecutionEngine(bool enableObjectCache);
63 
73  ModuleOp m, std::function<llvm::Error(llvm::Module *)> transformer = {},
74  Optional<llvm::CodeGenOpt::Level> jitCodeGenOptLevel = llvm::None,
75  ArrayRef<StringRef> sharedLibPaths = {}, bool enableObjectCache = false);
76 
79  llvm::Expected<void (*)(void **)> lookup(StringRef name) const;
80 
84  template <typename... Args>
85  llvm::Error invoke(StringRef name, Args &... args);
86 
90  llvm::Error invoke(StringRef name, MutableArrayRef<void *> args);
91 
94  static bool setupTargetTriple(llvm::Module *llvmModule);
95 
97  void dumpToObjectFile(StringRef filename);
98 
99 private:
100  // Ordering of llvmContext and jit is important for destruction purposes: the
101  // jit must be destroyed before the context.
102  llvm::LLVMContext llvmContext;
103 
104  // Underlying LLJIT.
105  std::unique_ptr<llvm::orc::LLJIT> jit;
106 
107  // Underlying cache.
108  std::unique_ptr<SimpleObjectCache> cache;
109 };
110 
111 template <typename... Args>
112 llvm::Error ExecutionEngine::invoke(StringRef name, Args &... args) {
113  auto expectedFPtr = lookup(name);
114  if (!expectedFPtr)
115  return expectedFPtr.takeError();
116  auto fptr = *expectedFPtr;
117 
118  SmallVector<void *, 8> packedArgs{static_cast<void *>(&args)...};
119  (*fptr)(packedArgs.data());
120 
121  return llvm::Error::success();
122 }
123 
124 } // end namespace mlir
125 
126 #endif // MLIR_EXECUTIONENGINE_EXECUTIONENGINE_H_
Definition: InferTypeOpInterface.cpp:20
Definition: PassRegistry.cpp:413
Definition: LLVM.h:40
Definition: ExecutionEngine.h:26
A simple object cache following Lang&#39;s LLJITWithObjectCache example.
Definition: ExecutionEngine.h:37
LogicalResult success(bool isSuccess=true)
Definition: LogicalResult.h:25
Definition: LLVM.h:37
Definition: LLVM.h:38
Definition: ExecutionEngine.h:60
Definition: Module.h:29
Definition: LLVM.h:35
Definition: StandardTypes.h:63