Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 731f482

Browse files
Add minimal JIT compiler based on LLJIT
1 parent 81a9d8b commit 731f482

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ endif()
3838

3939
add_executable(JitFromScratch
4040
main.cpp
41+
JitFromScratch.cpp
4142
)
4243

4344
target_include_directories(JitFromScratch PRIVATE
@@ -48,6 +49,7 @@ target_include_directories(JitFromScratch PRIVATE
4849
set(llvm_libs
4950
LLVMAggressiveInstCombine
5051
LLVMAnalysis
52+
LLVMAsmParser
5153
LLVMAsmPrinter
5254
LLVMBinaryFormat
5355
LLVMBitReader
@@ -59,14 +61,21 @@ set(llvm_libs
5961
LLVMDebugInfoDWARF
6062
LLVMDebugInfoMSF
6163
LLVMDemangle
64+
LLVMExecutionEngine
6265
LLVMGlobalISel
6366
LLVMInstCombine
67+
LLVMInstrumentation
68+
LLVMJITLink
69+
LLVMLinker
70+
LLVMIRReader
6471
LLVMMC
6572
LLVMMCDisassembler
6673
LLVMMCParser
6774
LLVMObject
75+
LLVMOrcJIT
6876
LLVMProfileData
6977
LLVMRemarks
78+
LLVMRuntimeDyld
7079
LLVMScalarOpts
7180
LLVMSelectionDAG
7281
LLVMSupport

JitFromScratch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "JitFromScratch.h"
2+
3+
using namespace llvm;
4+
using namespace llvm::orc;
5+
6+
JitFromScratch::JitFromScratch(ExitOnError ExitOnErr)
7+
: LLJIT(ExitOnErr(LLJITBuilder().create())) {}

JitFromScratch.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
4+
5+
#include <memory>
6+
7+
class JitFromScratch {
8+
public:
9+
JitFromScratch(llvm::ExitOnError ExitOnErr);
10+
11+
// Not a value type.
12+
JitFromScratch(const JitFromScratch &) = delete;
13+
JitFromScratch &operator=(const JitFromScratch &) = delete;
14+
JitFromScratch(JitFromScratch &&) = delete;
15+
JitFromScratch &operator=(JitFromScratch &&) = delete;
16+
17+
private:
18+
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
19+
};

main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
#include <llvm/ExecutionEngine/ExecutionEngine.h>
2+
#include <llvm/IR/DataLayout.h>
3+
#include <llvm/Support/Error.h>
14
#include <llvm/Support/Format.h>
25
#include <llvm/Support/InitLLVM.h>
36
#include <llvm/Support/TargetSelect.h>
47
#include <llvm/Support/raw_ostream.h>
58

9+
#include <memory>
10+
11+
#include "JitFromScratch.h"
12+
613
using namespace llvm;
714

815
// Determine the size of a C array at compile-time.
@@ -41,12 +48,18 @@ int *integerDistances(const int (&x)[sizeOfArray], int *y) {
4148
int main(int argc, char **argv) {
4249
InitLLVM X(argc, argv);
4350

51+
ExitOnError ExitOnErr;
52+
ExitOnErr.setBanner("JitFromScratch: ");
53+
4454
InitializeNativeTarget();
4555
InitializeNativeTargetAsmPrinter();
4656
InitializeNativeTargetAsmParser();
4757

4858
int x[]{0, 1, 2};
4959
int y[]{3, 1, -1};
60+
61+
JitFromScratch Jit(ExitOnErr);
62+
5063
int *z = integerDistances(x, y);
5164

5265
outs() << format("Integer Distances: %d, %d, %d\n\n", z[0], z[1], z[2]);

0 commit comments

Comments
 (0)