Skip to content

Commit 3a11ca7

Browse files
ezhulenevd0k
authored andcommitted
[MLIR] Add symbol map to mlir ExecutionEngine
Add additional symbol mapping to be able to provide custom symbols to jitted code at runtime. Differential Revision: https://reviews.llvm.org/D79812
1 parent bc5565f commit 3a11ca7

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

mlir/include/mlir/ExecutionEngine/ExecutionEngine.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,28 @@ class ExecutionEngine {
6363
ExecutionEngine(bool enableObjectCache, bool enableGDBNotificationListener,
6464
bool enablePerfNotificationListener);
6565

66-
/// Creates an execution engine for the given module. If `transformer` is
67-
/// provided, it will be called on the LLVM module during JIT-compilation and
68-
/// can be used, e.g., for reporting or optimization. `jitCodeGenOptLevel`,
69-
/// when provided, is used as the optimization level for target code
70-
/// generation. If `sharedLibPaths` are provided, the underlying
71-
/// JIT-compilation will open and link the shared libraries for symbol
72-
/// resolution. If `enableObjectCache` is set, the JIT compiler will create
73-
/// one to store the object generated for the given module. If enable
74-
/// `enableGDBNotificationListener` is set, the JIT compiler will notify
75-
/// the llvm's global GDB notification listener. If
76-
/// `enablePerfNotificationListener` is set, the JIT compiler will notify
66+
/// Creates an execution engine for the given module.
67+
///
68+
/// If `transformer` is provided, it will be called on the LLVM module during
69+
/// JIT-compilation and can be used, e.g., for reporting or optimization.
70+
///
71+
/// `jitCodeGenOptLevel`, when provided, is used as the optimization level for
72+
/// target code generation.
73+
///
74+
/// If `sharedLibPaths` are provided, the underlying JIT-compilation will
75+
/// open and link the shared libraries for symbol resolution.
76+
///
77+
/// If `enableObjectCache` is set, the JIT compiler will create one to store
78+
/// the object generated for the given module.
79+
///
80+
/// If enable `enableGDBNotificationListener` is set, the JIT compiler will
81+
/// notify the llvm's global GDB notification listener.
82+
///
83+
/// If `enablePerfNotificationListener` is set, the JIT compiler will notify
7784
/// the llvm's global Perf notification listener.
7885
static llvm::Expected<std::unique_ptr<ExecutionEngine>>
7986
create(ModuleOp m,
80-
std::function<llvm::Error(llvm::Module *)> transformer = {},
87+
llvm::function_ref<llvm::Error(llvm::Module *)> transformer = {},
8188
Optional<llvm::CodeGenOpt::Level> jitCodeGenOptLevel = llvm::None,
8289
ArrayRef<StringRef> sharedLibPaths = {}, bool enableObjectCache = true,
8390
bool enableGDBNotificationListener = true,
@@ -105,6 +112,11 @@ class ExecutionEngine {
105112
/// Dump object code to output file `filename`.
106113
void dumpToObjectFile(StringRef filename);
107114

115+
/// Register symbols with this ExecutionEngine.
116+
void registerSymbols(
117+
llvm::function_ref<llvm::orc::SymbolMap(llvm::orc::MangleAndInterner)>
118+
symbolMap);
119+
108120
private:
109121
/// Ordering of llvmContext and jit is important for destruction purposes: the
110122
/// jit must be destroyed before the context.

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ using llvm::orc::DynamicLibrarySearchGenerator;
5151
using llvm::orc::ExecutionSession;
5252
using llvm::orc::IRCompileLayer;
5353
using llvm::orc::JITTargetMachineBuilder;
54+
using llvm::orc::MangleAndInterner;
5455
using llvm::orc::RTDyldObjectLinkingLayer;
56+
using llvm::orc::SymbolMap;
5557
using llvm::orc::ThreadSafeModule;
5658
using llvm::orc::TMOwningSimpleCompiler;
5759

@@ -99,6 +101,14 @@ void ExecutionEngine::dumpToObjectFile(StringRef filename) {
99101
cache->dumpToObjectFile(filename);
100102
}
101103

104+
void ExecutionEngine::registerSymbols(
105+
llvm::function_ref<SymbolMap(MangleAndInterner)> symbolMap) {
106+
auto &mainJitDylib = jit->getMainJITDylib();
107+
cantFail(mainJitDylib.define(
108+
absoluteSymbols(symbolMap(llvm::orc::MangleAndInterner(
109+
mainJitDylib.getExecutionSession(), jit->getDataLayout())))));
110+
}
111+
102112
// Setup LLVM target triple from the current machine.
103113
bool ExecutionEngine::setupTargetTriple(Module *llvmModule) {
104114
// Setup the machine properties from the current architecture.
@@ -194,7 +204,7 @@ ExecutionEngine::ExecutionEngine(bool enableObjectCache,
194204
: nullptr) {}
195205

196206
Expected<std::unique_ptr<ExecutionEngine>> ExecutionEngine::create(
197-
ModuleOp m, std::function<Error(llvm::Module *)> transformer,
207+
ModuleOp m, llvm::function_ref<Error(llvm::Module *)> transformer,
198208
Optional<llvm::CodeGenOpt::Level> jitCodeGenOptLevel,
199209
ArrayRef<StringRef> sharedLibPaths, bool enableObjectCache,
200210
bool enableGDBNotificationListener, bool enablePerfNotificationListener) {

0 commit comments

Comments
 (0)