1//===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//
2//
3// Part of the LLVM 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// Contains a simple JIT definition for use in the kaleidoscope tutorials.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
14#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
15
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ExecutionEngine/JITSymbol.h"
18#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
19#include "llvm/ExecutionEngine/Orc/Core.h"
20#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
21#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
22#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
23#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
24#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
26#include "llvm/ExecutionEngine/SectionMemoryManager.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/LLVMContext.h"
29#include <memory>
30
31namespace llvm {
32namespace orc {
33
34class KaleidoscopeJIT {
35private:
36 std::unique_ptr<ExecutionSession> ES;
37
38 DataLayout DL;
39 MangleAndInterner Mangle;
40
41 RTDyldObjectLinkingLayer ObjectLayer;
42 IRCompileLayer CompileLayer;
43
44 JITDylib &MainJD;
45
46public:
47 KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,
48 JITTargetMachineBuilder JTMB, DataLayout DL)
49 : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),
50 ObjectLayer(*this->ES,
51 []() { return std::make_unique<SectionMemoryManager>(); }),
52 CompileLayer(*this->ES, ObjectLayer,
53 std::make_unique<ConcurrentIRCompiler>(args: std::move(JTMB))),
54 MainJD(this->ES->createBareJITDylib(Name: "<main>")) {
55 MainJD.addGenerator(
56 DefGenerator: cantFail(ValOrErr: DynamicLibrarySearchGenerator::GetForCurrentProcess(
57 GlobalPrefix: DL.getGlobalPrefix())));
58 if (JTMB.getTargetTriple().isOSBinFormatCOFF()) {
59 ObjectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
60 ObjectLayer.setAutoClaimResponsibilityForObjectSymbols(true);
61 }
62 }
63
64 ~KaleidoscopeJIT() {
65 if (auto Err = ES->endSession())
66 ES->reportError(Err: std::move(Err));
67 }
68
69 static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
70 auto EPC = SelfExecutorProcessControl::Create();
71 if (!EPC)
72 return EPC.takeError();
73
74 auto ES = std::make_unique<ExecutionSession>(args: std::move(*EPC));
75
76 JITTargetMachineBuilder JTMB(
77 ES->getExecutorProcessControl().getTargetTriple());
78
79 auto DL = JTMB.getDefaultDataLayoutForTarget();
80 if (!DL)
81 return DL.takeError();
82
83 return std::make_unique<KaleidoscopeJIT>(args: std::move(ES), args: std::move(JTMB),
84 args: std::move(*DL));
85 }
86
87 const DataLayout &getDataLayout() const { return DL; }
88
89 JITDylib &getMainJITDylib() { return MainJD; }
90
91 Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {
92 if (!RT)
93 RT = MainJD.getDefaultResourceTracker();
94 return CompileLayer.add(RT, TSM: std::move(TSM));
95 }
96
97 Expected<ExecutorSymbolDef> lookup(StringRef Name) {
98 return ES->lookup(SearchOrder: {&MainJD}, Symbol: Mangle(Name.str()));
99 }
100};
101
102} // end namespace orc
103} // end namespace llvm
104
105#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
106

source code of llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h