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/IRCompileLayer.h"
22 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
23 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
25 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include <memory>
29 
30 namespace llvm {
31 namespace orc {
32 
33 class KaleidoscopeJIT {
34 private:
35   std::unique_ptr<TargetProcessControl> TPC;
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 
46 public:
KaleidoscopeJIT(std::unique_ptr<TargetProcessControl> TPC,std::unique_ptr<ExecutionSession> ES,JITTargetMachineBuilder JTMB,DataLayout DL)47   KaleidoscopeJIT(std::unique_ptr<TargetProcessControl> TPC,
48                   std::unique_ptr<ExecutionSession> ES,
49                   JITTargetMachineBuilder JTMB, DataLayout DL)
50       : TPC(std::move(TPC)), ES(std::move(ES)), DL(std::move(DL)),
51         Mangle(*this->ES, this->DL),
52         ObjectLayer(*this->ES,
53                     []() { return std::make_unique<SectionMemoryManager>(); }),
54         CompileLayer(*this->ES, ObjectLayer,
55                      std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
56         MainJD(this->ES->createBareJITDylib("<main>")) {
57     MainJD.addGenerator(
58         cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
59             DL.getGlobalPrefix())));
60   }
61 
~KaleidoscopeJIT()62   ~KaleidoscopeJIT() {
63     if (auto Err = ES->endSession())
64       ES->reportError(std::move(Err));
65   }
66 
Create()67   static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
68     auto SSP = std::make_shared<SymbolStringPool>();
69     auto TPC = SelfTargetProcessControl::Create(SSP);
70     if (!TPC)
71       return TPC.takeError();
72 
73     auto ES = std::make_unique<ExecutionSession>(std::move(SSP));
74 
75     JITTargetMachineBuilder JTMB((*TPC)->getTargetTriple());
76 
77     auto DL = JTMB.getDefaultDataLayoutForTarget();
78     if (!DL)
79       return DL.takeError();
80 
81     return std::make_unique<KaleidoscopeJIT>(std::move(*TPC), std::move(ES),
82                                              std::move(JTMB), std::move(*DL));
83   }
84 
getDataLayout()85   const DataLayout &getDataLayout() const { return DL; }
86 
getMainJITDylib()87   JITDylib &getMainJITDylib() { return MainJD; }
88 
89   Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {
90     if (!RT)
91       RT = MainJD.getDefaultResourceTracker();
92     return CompileLayer.add(RT, std::move(TSM));
93   }
94 
lookup(StringRef Name)95   Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
96     return ES->lookup({&MainJD}, Mangle(Name.str()));
97   }
98 };
99 
100 } // end namespace orc
101 } // end namespace llvm
102 
103 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
104