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

Commit

Permalink
Add minimal JIT compiler based on LLJIT
Browse files Browse the repository at this point in the history
  • Loading branch information
weliveindetail committed Dec 8, 2019
1 parent 81a9d8b commit 731f482
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ endif()

add_executable(JitFromScratch
main.cpp
JitFromScratch.cpp
)

target_include_directories(JitFromScratch PRIVATE
Expand All @@ -48,6 +49,7 @@ target_include_directories(JitFromScratch PRIVATE
set(llvm_libs
LLVMAggressiveInstCombine
LLVMAnalysis
LLVMAsmParser
LLVMAsmPrinter
LLVMBinaryFormat
LLVMBitReader
Expand All @@ -59,14 +61,21 @@ set(llvm_libs
LLVMDebugInfoDWARF
LLVMDebugInfoMSF
LLVMDemangle
LLVMExecutionEngine
LLVMGlobalISel
LLVMInstCombine
LLVMInstrumentation
LLVMJITLink
LLVMLinker
LLVMIRReader
LLVMMC
LLVMMCDisassembler
LLVMMCParser
LLVMObject
LLVMOrcJIT
LLVMProfileData
LLVMRemarks
LLVMRuntimeDyld
LLVMScalarOpts
LLVMSelectionDAG
LLVMSupport
Expand Down
7 changes: 7 additions & 0 deletions JitFromScratch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "JitFromScratch.h"

using namespace llvm;
using namespace llvm::orc;

JitFromScratch::JitFromScratch(ExitOnError ExitOnErr)
: LLJIT(ExitOnErr(LLJITBuilder().create())) {}
19 changes: 19 additions & 0 deletions JitFromScratch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <llvm/ExecutionEngine/Orc/LLJIT.h>

#include <memory>

class JitFromScratch {
public:
JitFromScratch(llvm::ExitOnError ExitOnErr);

// Not a value type.
JitFromScratch(const JitFromScratch &) = delete;
JitFromScratch &operator=(const JitFromScratch &) = delete;
JitFromScratch(JitFromScratch &&) = delete;
JitFromScratch &operator=(JitFromScratch &&) = delete;

private:
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
};
13 changes: 13 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/Format.h>
#include <llvm/Support/InitLLVM.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_ostream.h>

#include <memory>

#include "JitFromScratch.h"

using namespace llvm;

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

ExitOnError ExitOnErr;
ExitOnErr.setBanner("JitFromScratch: ");

InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();

int x[]{0, 1, 2};
int y[]{3, 1, -1};

JitFromScratch Jit(ExitOnErr);

int *z = integerDistances(x, y);

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

0 comments on commit 731f482

Please sign in to comment.