1 //===----- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Contains a simple JIT definition for use in the kaleidoscope tutorials.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
15 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
21 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
23 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
24 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
25 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
26 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/Support/DynamicLibrary.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include <algorithm>
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 namespace llvm {
38 namespace orc {
39 
40 class KaleidoscopeJIT {
41 private:
42   std::unique_ptr<TargetMachine> TM;
43   const DataLayout DL;
44   ObjectLinkingLayer<> ObjectLayer;
45   IRCompileLayer<decltype(ObjectLayer)> CompileLayer;
46 
47   typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)>
48     OptimizeFunction;
49 
50   IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
51 
52 public:
53   typedef decltype(OptimizeLayer)::ModuleSetHandleT ModuleHandle;
54 
KaleidoscopeJIT()55   KaleidoscopeJIT()
56       : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
57         CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
58         OptimizeLayer(CompileLayer,
59                       [this](std::unique_ptr<Module> M) {
60                         return optimizeModule(std::move(M));
61                       }) {
62     llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
63   }
64 
getTargetMachine()65   TargetMachine &getTargetMachine() { return *TM; }
66 
addModule(std::unique_ptr<Module> M)67   ModuleHandle addModule(std::unique_ptr<Module> M) {
68     // Build our symbol resolver:
69     // Lambda 1: Look back into the JIT itself to find symbols that are part of
70     //           the same "logical dylib".
71     // Lambda 2: Search for external symbols in the host process.
72     auto Resolver = createLambdaResolver(
73         [&](const std::string &Name) {
74           if (auto Sym = OptimizeLayer.findSymbol(Name, false))
75             return Sym.toRuntimeDyldSymbol();
76           return RuntimeDyld::SymbolInfo(nullptr);
77         },
78         [](const std::string &Name) {
79           if (auto SymAddr =
80                 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
81             return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
82           return RuntimeDyld::SymbolInfo(nullptr);
83         });
84 
85     // Build a singlton module set to hold our module.
86     std::vector<std::unique_ptr<Module>> Ms;
87     Ms.push_back(std::move(M));
88 
89     // Add the set to the JIT with the resolver we created above and a newly
90     // created SectionMemoryManager.
91     return OptimizeLayer.addModuleSet(std::move(Ms),
92                                       make_unique<SectionMemoryManager>(),
93                                       std::move(Resolver));
94   }
95 
findSymbol(const std::string Name)96   JITSymbol findSymbol(const std::string Name) {
97     std::string MangledName;
98     raw_string_ostream MangledNameStream(MangledName);
99     Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
100     return OptimizeLayer.findSymbol(MangledNameStream.str(), true);
101   }
102 
removeModule(ModuleHandle H)103   void removeModule(ModuleHandle H) {
104     OptimizeLayer.removeModuleSet(H);
105   }
106 
107 private:
108 
optimizeModule(std::unique_ptr<Module> M)109   std::unique_ptr<Module> optimizeModule(std::unique_ptr<Module> M) {
110     // Create a function pass manager.
111     auto FPM = llvm::make_unique<legacy::FunctionPassManager>(M.get());
112 
113     // Add some optimizations.
114     FPM->add(createInstructionCombiningPass());
115     FPM->add(createReassociatePass());
116     FPM->add(createGVNPass());
117     FPM->add(createCFGSimplificationPass());
118     FPM->doInitialization();
119 
120     // Run the optimizations over all functions in the module being added to
121     // the JIT.
122     for (auto &F : *M)
123       FPM->run(F);
124 
125     return M;
126   }
127 
128 };
129 
130 } // end namespace orc
131 } // end namespace llvm
132 
133 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
134