-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleJIT.h
More file actions
executable file
·86 lines (69 loc) · 2.41 KB
/
SimpleJIT.h
File metadata and controls
executable file
·86 lines (69 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
#include <llvm/ExecutionEngine/Orc/ThreadSafeModule.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <memory>
#include <string>
using namespace llvm;
using namespace llvm::orc;
/// A simple JIT engine that encapsulates the LLVM ORC JIT API.
/// (JIT = Just In Time, ORC = On Request Compilation)
class SimpleJIT {
public:
/// Construct JIT engine, initializing the execution session and layers.
SimpleJIT() : m_initialized(false), m_jit(nullptr) {
m_initialized = init();
if (m_initialized) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
}
/// Initialize LLVM target infrastructure. This is safe to call multiple times.
/// Returns true if initialization was successful or already done.
static bool initializeLLVM() {
static bool initialized = false;
if (initialized) {
return true;
}
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
initialized = true;
return true;
}
/// Add the given module to the JIT engine.
Error addModule(std::unique_ptr<Module> module) {
if (!m_initialized) {
return make_error<StringError>("JIT not initialized", inconvertibleErrorCode());
}
// Create a new context for this module
auto context = std::make_unique<LLVMContext>();
ThreadSafeModule tsm(std::move(module), std::move(context));
return m_jit->addIRModule(std::move(tsm));
}
/// Find the specified symbol in the JIT.
Expected<ExecutorAddr> findSymbol(const std::string& name) {
if (!m_initialized) {
return make_error<StringError>("JIT not initialized", inconvertibleErrorCode());
}
return m_jit->lookup(name);
}
private:
bool m_initialized;
std::unique_ptr<LLJIT> m_jit;
// Perform prerequisite initialization.
bool init() {
// Ensure LLVM target infrastructure is initialized
if (!initializeLLVM()) {
return false;
}
auto jitOrError = LLJITBuilder().create();
if (!jitOrError) {
return false;
}
m_jit = std::move(*jitOrError);
return true;
}
};