llvm.org GIT mirror llvm / 94419b4
[BuildingAJIT] Update chapter 2 to use the ORCv2 APIs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346726 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 10 months ago
4 changed file(s) with 291 addition(s) and 354 deletion(s). Raw diff Collapse all Expand all
1111 Chapter 2 Introduction
1212 ======================
1313
14 **Warning: This text is currently out of date due to ORC API updates.**
15
16 **The example code has been updated and can be used. The text will be updated
17 once the API churn dies down.**
14 **Warning: This tutorial is currently being updated to account for ORC API
15 changes. Only Chapters 1 and 2 are up-to-date.**
16
17 **Example code from Chapters 3 to 5 will compile and run, but has not been
18 updated**
1819
1920 Welcome to Chapter 2 of the "Building an ORC-based JIT in LLVM" tutorial. In
2021 `Chapter 1 `_ of this series we examined a basic JIT
4142 instead. For now this will provide us a motivation to learn more about ORC
4243 layers, but in the long term making optimization part of our JIT will yield an
4344 important benefit: When we begin lazily compiling code (i.e. deferring
44 compilation of each function until the first time it's run), having
45 compilation of each function until the first time it's run) having
4546 optimization managed by our JIT will allow us to optimize lazily too, rather
4647 than having to do all our optimization up-front.
4748
4849 To add optimization support to our JIT we will take the KaleidoscopeJIT from
4950 Chapter 1 and compose an ORC *IRTransformLayer* on top. We will look at how the
5051 IRTransformLayer works in more detail below, but the interface is simple: the
51 constructor for this layer takes a reference to the layer below (as all layers
52 do) plus an *IR optimization function* that it will apply to each Module that
53 is added via addModule:
52 constructor for this layer takes a reference to the execution session and the
53 layer below (as all layers do) plus an *IR optimization function* that it will
54 apply to each Module that is added via addModule:
5455
5556 .. code-block:: c++
5657
5758 class KaleidoscopeJIT {
5859 private:
59 std::unique_ptr TM;
60 const DataLayout DL;
61 RTDyldObjectLinkingLayer<> ObjectLayer;
62 IRCompileLayer CompileLayer;
63
64 using OptimizeFunction =
65 std::function(std::shared_ptr)>;
66
67 IRTransformLayer OptimizeLayer;
60 ExecutionSession ES;
61 RTDyldObjectLinkingLayer ObjectLayer;
62 IRCompileLayer CompileLayer;
63 IRTransformLayer TransformLayer;
64
65 DataLayout DL;
66 MangleAndInterner Mangle;
67 ThreadSafeContext Ctx;
6868
6969 public:
70 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
71
72 KaleidoscopeJIT()
73 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
74 ObjectLayer([]() { return std::make_shared(); }),
75 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
76 OptimizeLayer(CompileLayer,
77 [this](std::unique_ptr M) {
78 return optimizeModule(std::move(M));
79 }) {
80 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
70
71 KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
72 : ObjectLayer(ES,
73 []() { return llvm::make_unique(); }),
74 CompileLayer(ES, ObjectLayer, ConcurrentIRCompiler(std::move(JTMB))),
75 TransformLayer(ES, CompileLayer, optimizeModule),
76 DL(std::move(DL)), Mangle(ES, this->DL),
77 Ctx(llvm::make_unique()) {
78 ES.getMainJITDylib().setGenerator(
79 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
8180 }
8281
8382 Our extended KaleidoscopeJIT class starts out the same as it did in Chapter 1,
84 but after the CompileLayer we introduce a typedef for our optimization function.
85 In this case we use a std::function (a handy wrapper for "function-like" things)
86 from a single unique_ptr input to a std::unique_ptr output. With
87 our optimization function typedef in place we can declare our OptimizeLayer,
88 which sits on top of our CompileLayer.
89
90 To initialize our OptimizeLayer we pass it a reference to the CompileLayer
91 below (standard practice for layers), and we initialize the OptimizeFunction
92 using a lambda that calls out to an "optimizeModule" function that we will
93 define below.
94
95 .. code-block:: c++
96
97 // ...
98 auto Resolver = createLambdaResolver(
99 [&](const std::string &Name) {
100 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
101 return Sym;
102 return JITSymbol(nullptr);
103 },
104 // ...
83 but after the CompileLayer we introduce a new member, TransformLayer, which sits
84 on top of our CompileLayer. We initialize our OptimizeLayer with a reference to
85 the ExecutionSession and output layer (standard practice for layers), along with
86 a *transform function*. For our transform function we supply our classes
87 optimizeModule static method.
10588
10689 .. code-block:: c++
10790
11093 std::move(Resolver)));
11194 // ...
11295
113 .. code-block:: c++
114
115 // ...
116 return OptimizeLayer.findSymbol(MangledNameStream.str(), true);
117 // ...
118
119 .. code-block:: c++
120
121 // ...
122 cantFail(OptimizeLayer.removeModule(H));
123 // ...
124
125 Next we need to replace references to 'CompileLayer' with references to
126 OptimizeLayer in our key methods: addModule, findSymbol, and removeModule. In
127 addModule we need to be careful to replace both references: the findSymbol call
128 inside our resolver, and the call through to addModule.
129
130 .. code-block:: c++
131
132 std::shared_ptr optimizeModule(std::shared_ptr M) {
96 Next we need to update our addModule method to replace the call to
97 ``CompileLayer::add`` with a call to ``OptimizeLayer::add`` instead.
98
99 .. code-block:: c++
100
101 ThreadSafeModule optimizeModule(ThreadSafeModule M,
102 const MaterializationResponsibility &R) {
133103 // Create a function pass manager.
134104 auto FPM = llvm::make_unique(M.get());
135105
149119 }
150120
151121 At the bottom of our JIT we add a private method to do the actual optimization:
152 *optimizeModule*. This function sets up a FunctionPassManager, adds some passes
153 to it, runs it over every function in the module, and then returns the mutated
154 module. The specific optimizations are the same ones used in
155 `Chapter 4 `_ of the "Implementing a language with LLVM"
156 tutorial series. Readers may visit that chapter for a more in-depth
157 discussion of these, and of IR optimization in general.
122 *optimizeModule*. This function takes the module to be transformed as input (as
123 a ThreadSafeModule) along with a reference to a reference to a new class:
124 ``MaterializationResponsibility``. The MaterializationResponsibility argument
125 can be used to query JIT state for the module being transformed, such as the set
126 of definitions in the module that JIT'd code is actively trying to call/access.
127 For now we will ignore this argument and use a standard optimization
128 pipeline. To do this we set up a FunctionPassManager, add some passes to it, run
129 it over every function in the module, and then return the mutated module. The
130 specific optimizations are the same ones used in `Chapter 4 `_
131 of the "Implementing a language with LLVM" tutorial series. Readers may visit
132 that chapter for a more in-depth discussion of these, and of IR optimization in
133 general.
158134
159135 And that's it in terms of changes to KaleidoscopeJIT: When a module is added via
160136 addModule the OptimizeLayer will call our optimizeModule function before passing
162138 called optimizeModule directly in our addModule function and not gone to the
163139 bother of using the IRTransformLayer, but doing so gives us another opportunity
164140 to see how layers compose. It also provides a neat entry point to the *layer*
165 concept itself, because IRTransformLayer turns out to be one of the simplest
166 implementations of the layer concept that can be devised:
167
168 .. code-block:: c++
169
170 template
171 class IRTransformLayer {
141 concept itself, because IRTransformLayer is one of the simplest layers that
142 can be implemented.
143
144 .. code-block:: c++
145
146 // From IRTransformLayer.h:
147 class IRTransformLayer : public IRLayer {
172148 public:
173 using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
174
175 IRTransformLayer(BaseLayerT &BaseLayer,
176 TransformFtor Transform = TransformFtor())
177 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
178
179 Expected
180 addModule(std::shared_ptr M,
181 std::shared_ptr Resolver) {
182 return BaseLayer.addModule(Transform(std::move(M)), std::move(Resolver));
183 }
184
185 void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
186
187 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
188 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
189 }
190
191 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
192 bool ExportedSymbolsOnly) {
193 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
194 }
195
196 void emitAndFinalize(ModuleHandleT H) {
197 BaseLayer.emitAndFinalize(H);
198 }
199
200 TransformFtor& getTransform() { return Transform; }
201
202 const TransformFtor& getTransform() const { return Transform; }
149 using TransformFunction = std::function(
150 ThreadSafeModule, const MaterializationResponsibility &R)>;
151
152 IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer,
153 TransformFunction Transform = identityTransform);
154
155 void setTransform(TransformFunction Transform) {
156 this->Transform = std::move(Transform);
157 }
158
159 static ThreadSafeModule
160 identityTransform(ThreadSafeModule TSM,
161 const MaterializationResponsibility &R) {
162 return TSM;
163 }
164
165 void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
203166
204167 private:
205 BaseLayerT &BaseLayer;
206 TransformFtor Transform;
168 IRLayer &BaseLayer;
169 TransformFunction Transform;
207170 };
208171
172 // From IRTransfomrLayer.cpp:
173
174 IRTransformLayer::IRTransformLayer(ExecutionSession &ES,
175 IRLayer &BaseLayer,
176 TransformFunction Transform)
177 : IRLayer(ES), BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
178
179 void IRTransformLayer::emit(MaterializationResponsibility R,
180 ThreadSafeModule TSM) {
181 assert(TSM.getModule() && "Module must not be null");
182
183 if (auto TransformedTSM = Transform(std::move(TSM), R))
184 BaseLayer.emit(std::move(R), std::move(*TransformedTSM));
185 else {
186 R.failMaterialization();
187 getExecutionSession().reportError(TransformedTSM.takeError());
188 }
189 }
190
209191 This is the whole definition of IRTransformLayer, from
210 ``llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h``, stripped of its
211 comments. It is a template class with two template arguments: ``BaesLayerT`` and
212 ``TransformFtor`` that provide the type of the base layer and the type of the
213 "transform functor" (in our case a std::function) respectively. This class is
214 concerned with two very simple jobs: (1) Running every IR Module that is added
215 with addModule through the transform functor, and (2) conforming to the ORC
216 layer interface. The interface consists of one typedef and five methods:
217
218 +------------------+-----------------------------------------------------------+
219 | Interface | Description |
220 +==================+===========================================================+
221 | | Provides a handle that can be used to identify a module |
222 | ModuleHandleT | set when calling findSymbolIn, removeModule, or |
223 | | emitAndFinalize. |
224 +------------------+-----------------------------------------------------------+
225 | | Takes a given set of Modules and makes them "available |
226 | | for execution". This means that symbols in those modules |
227 | | should be searchable via findSymbol and findSymbolIn, and |
228 | | the address of the symbols should be read/writable (for |
229 | | data symbols), or executable (for function symbols) after |
230 | | JITSymbol::getAddress() is called. Note: This means that |
231 | addModule | addModule doesn't have to compile (or do any other |
232 | | work) up-front. It *can*, like IRCompileLayer, act |
233 | | eagerly, but it can also simply record the module and |
234 | | take no further action until somebody calls |
235 | | JITSymbol::getAddress(). In IRTransformLayer's case |
236 | | addModule eagerly applies the transform functor to |
237 | | each module in the set, then passes the resulting set |
238 | | of mutated modules down to the layer below. |
239 +------------------+-----------------------------------------------------------+
240 | | Removes a set of modules from the JIT. Code or data |
241 | removeModule | defined in these modules will no longer be available, and |
242 | | the memory holding the JIT'd definitions will be freed. |
243 +------------------+-----------------------------------------------------------+
244 | | Searches for the named symbol in all modules that have |
245 | | previously been added via addModule (and not yet |
246 | findSymbol | removed by a call to removeModule). In |
247 | | IRTransformLayer we just pass the query on to the layer |
248 | | below. In our REPL this is our default way to search for |
249 | | function definitions. |
250 +------------------+-----------------------------------------------------------+
251 | | Searches for the named symbol in the module set indicated |
252 | | by the given ModuleHandleT. This is just an optimized |
253 | | search, better for lookup-speed when you know exactly |
254 | | a symbol definition should be found. In IRTransformLayer |
255 | findSymbolIn | we just pass this query on to the layer below. In our |
256 | | REPL we use this method to search for functions |
257 | | representing top-level expressions, since we know exactly |
258 | | where we'll find them: in the top-level expression module |
259 | | we just added. |
260 +------------------+-----------------------------------------------------------+
261 | | Forces all of the actions required to make the code and |
262 | | data in a module set (represented by a ModuleHandleT) |
263 | | accessible. Behaves as if some symbol in the set had been |
264 | | searched for and JITSymbol::getSymbolAddress called. This |
265 | emitAndFinalize | is rarely needed, but can be useful when dealing with |
266 | | layers that usually behave lazily if the user wants to |
267 | | trigger early compilation (for example, to use idle CPU |
268 | | time to eagerly compile code in the background). |
269 +------------------+-----------------------------------------------------------+
270
271 This interface attempts to capture the natural operations of a JIT (with some
272 wrinkles like emitAndFinalize for performance), similar to the basic JIT API
273 operations we identified in Chapter 1. Conforming to the layer concept allows
274 classes to compose neatly by implementing their behaviors in terms of the these
275 same operations, carried out on the layer below. For example, an eager layer
276 (like IRTransformLayer) can implement addModule by running each module in the
277 set through its transform up-front and immediately passing the result to the
278 layer below. A lazy layer, by contrast, could implement addModule by
279 squirreling away the modules doing no other up-front work, but applying the
280 transform (and calling addModule on the layer below) when the client calls
281 findSymbol instead. The JIT'd program behavior will be the same either way, but
282 these choices will have different performance characteristics: Doing work
283 eagerly means the JIT takes longer up-front, but proceeds smoothly once this is
284 done. Deferring work allows the JIT to get up-and-running quickly, but will
285 force the JIT to pause and wait whenever some code or data is needed that hasn't
286 already been processed.
287
288 Our current REPL is eager: Each function definition is optimized and compiled as
289 soon as it's typed in. If we were to make the transform layer lazy (but not
290 change things otherwise) we could defer optimization until the first time we
291 reference a function in a top-level expression (see if you can figure out why,
292 then check out the answer below [1]_). In the next chapter, however we'll
293 introduce fully lazy compilation, in which function's aren't compiled until
294 they're first called at run-time. At this point the trade-offs get much more
192 ``llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h`` and
193 ``llvm/lib/ExecutionEngine/Orc/IRTransformLayer.cpp``. This class is concerned
194 with two very simple jobs: (1) Running every IR Module that is emitted via this
195 layer through the transform function object, and (2) implementing the ORC
196 ``IRLayer`` interface (which itself conforms to the general ORC Layer concept,
197 more on that below). Most of the class is straightforward: a typedef for the
198 transform function, a constructor to initialize the members, a setter for the
199 transform function value, and a default no-op transform. The most important
200 method is ``emit`` as this is half of our IRLayer interface. The emit method
201 applies our transform to each module that it is called on and, if the transform
202 succeeds, passes the transformed module to the base layer. If the transform
203 fails, our emit function calls
204 ``MaterializationResponsibility::failMaterialization`` (this JIT clients who
205 may be waiting on other threads know that the code they were waiting for has
206 failed to compile) and logs the error with the execution session before bailing
207 out.
208
209 The other half of the IRLayer interface we inherit unmodified from the IRLayer
210 class:
211
212 .. code-block:: c++
213
214 Error IRLayer::add(JITDylib &JD, ThreadSafeModule TSM, VModuleKey K) {
215 return JD.define(llvm::make_unique(
216 *this, std::move(K), std::move(TSM)));
217 }
218
219 This code, from ``llvm/lib/ExecutionEngine/Orc/Layer.cpp``, adds a
220 ThreadSafeModule to a given JITDylib by wrapping it up in a
221 ``MaterializationUnit`` (in this case a ``BasicIRLayerMaterializationUnit``).
222 Most layers that derived from IRLayer can rely on this default implementation
223 of the ``add`` method.
224
225 These two operations, ``add`` and ``emit``, together constitute the layer
226 concept: A layer is a way to wrap a portion of a compiler pipeline (in this case
227 the "opt" phase of an LLVM compiler) whose API is is opaque to ORC in an
228 interface that allows ORC to invoke it when needed. The add method takes an
229 module in some input program representation (in this case an LLVM IR module) and
230 stores it in the target JITDylib, arranging for it to be passed back to the
231 Layer's emit method when any symbol defined by that module is requested. Layers
232 can compose neatly by calling the 'emit' method of a base layer to complete
233 their work. For example, in this tutorial our IRTransformLayer calls through to
234 our IRCompileLayer to compile the transformed IR, and our IRCompileLayer in turn
235 calls our ObjectLayer to link the object file produced by our compiler.
236
237
238 So far we have learned how to optimize and compile our LLVM IR, but we have not
239 focused on when compilation happens. Our current REPL is eager: Each function
240 definition is optimized and compiled as soon as it is referenced by any other
241 code, regardless of whether it is ever called at runtime. In the next chapter we
242 will introduce fully lazy compilation, in which functions are not compiled until
243 they are first called at run-time. At this point the trade-offs get much more
295244 interesting: the lazier we are, the quicker we can start executing the first
296 function, but the more often we'll have to pause to compile newly encountered
297 functions. If we only code-gen lazily, but optimize eagerly, we'll have a slow
298 startup (which everything is optimized) but relatively short pauses as each
299 function just passes through code-gen. If we both optimize and code-gen lazily
300 we can start executing the first function more quickly, but we'll have longer
301 pauses as each function has to be both optimized and code-gen'd when it's first
302 executed. Things become even more interesting if we consider interproceedural
303 optimizations like inlining, which must be performed eagerly. These are
304 complex trade-offs, and there is no one-size-fits all solution to them, but by
305 providing composable layers we leave the decisions to the person implementing
306 the JIT, and make it easy for them to experiment with different configurations.
245 function, but the more often we will have to pause to compile newly encountered
246 functions. If we only code-gen lazily, but optimize eagerly, we will have a
247 longer startup time (as everything is optimized) but relatively short pauses as
248 each function just passes through code-gen. If we both optimize and code-gen
249 lazily we can start executing the first function more quickly, but we will have
250 longer pauses as each function has to be both optimized and code-gen'd when it
251 is first executed. Things become even more interesting if we consider
252 interproceedural optimizations like inlining, which must be performed eagerly.
253 These are complex trade-offs, and there is no one-size-fits all solution to
254 them, but by providing composable layers we leave the decisions to the person
255 implementing the JIT, and make it easy for them to experiment with different
256 configurations.
307257
308258 `Next: Adding Per-function Lazy Compilation `_
309259
324274
325275 .. literalinclude:: ../../examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
326276 :language: c++
327
328 .. [1] When we add our top-level expression to the JIT, any calls to functions
329 that we defined earlier will appear to the RTDyldObjectLinkingLayer as
330 external symbols. The RTDyldObjectLinkingLayer will call the SymbolResolver
331 that we defined in addModule, which in turn calls findSymbol on the
332 OptimizeLayer, at which point even a lazy transform layer will have to
333 do its work.
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
1414 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
1515
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ADT/StringRef.h"
1817 #include "llvm/ExecutionEngine/JITSymbol.h"
1918 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
19 #include "llvm/ExecutionEngine/Orc/Core.h"
20 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
2021 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2122 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
22 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
23 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
2324 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2525 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2626 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/LLVMContext.h"
2728 #include "llvm/IR/LegacyPassManager.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"
3229 #include "llvm/Transforms/InstCombine/InstCombine.h"
3330 #include "llvm/Transforms/Scalar.h"
3431 #include "llvm/Transforms/Scalar/GVN.h"
35 #include
3632 #include
37 #include
38 #include
3933
4034 namespace llvm {
4135 namespace orc {
4337 class KaleidoscopeJIT {
4438 private:
4539 ExecutionSession ES;
46 std::shared_ptr Resolver;
47 std::unique_ptr TM;
48 const DataLayout DL;
49 LegacyRTDyldObjectLinkingLayer ObjectLayer;
50 LegacyIRCompileLayer CompileLayer;
40 RTDyldObjectLinkingLayer ObjectLayer;
41 IRCompileLayer CompileLayer;
42 IRTransformLayer OptimizeLayer;
5143
52 using OptimizeFunction =
53 std::function(std::unique_ptr)>;
54
55 LegacyIRTransformLayer OptimizeLayer;
44 DataLayout DL;
45 MangleAndInterner Mangle;
46 ThreadSafeContext Ctx;
5647
5748 public:
58 KaleidoscopeJIT()
59 : Resolver(createLegacyLookupResolver(
60 ES,
61 [this](const std::string &Name) -> JITSymbol {
62 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
63 return Sym;
64 else if (auto Err = Sym.takeError())
65 return std::move(Err);
66 if (auto SymAddr =
67 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
68 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
69 return nullptr;
70 },
71 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
72 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
73 ObjectLayer(ES,
74 [this](VModuleKey) {
75 return LegacyRTDyldObjectLinkingLayer::Resources{
76 std::make_shared(), Resolver};
77 }),
78 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
79 OptimizeLayer(CompileLayer, [this](std::unique_ptr M) {
80 return optimizeModule(std::move(M));
81 }) {
82 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
49
50 KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
51 : ObjectLayer(ES,
52 []() { return llvm::make_unique(); }),
53 CompileLayer(ES, ObjectLayer, ConcurrentIRCompiler(std::move(JTMB))),
54 OptimizeLayer(ES, CompileLayer, optimizeModule),
55 DL(std::move(DL)), Mangle(ES, this->DL),
56 Ctx(llvm::make_unique()) {
57 ES.getMainJITDylib().setGenerator(
58 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
8359 }
8460
85 TargetMachine &getTargetMachine() { return *TM; }
61 const DataLayout &getDataLayout() const { return DL; }
8662
87 VModuleKey addModule(std::unique_ptr M) {
88 // Add the module to the JIT with a new VModuleKey.
89 auto K = ES.allocateVModule();
90 cantFail(OptimizeLayer.addModule(K, std::move(M)));
91 return K;
63 LLVMContext &getContext() { return *Ctx.getContext(); }
64
65 static Expected> Create() {
66 auto JTMB = JITTargetMachineBuilder::detectHost();
67
68 if (!JTMB)
69 return JTMB.takeError();
70
71 auto DL = JTMB->getDefaultDataLayoutForTarget();
72 if (!DL)
73 return DL.takeError();
74
75 return llvm::make_unique(std::move(*JTMB), std::move(*DL));
9276 }
9377
94 JITSymbol findSymbol(const std::string Name) {
95 std::string MangledName;
96 raw_string_ostream MangledNameStream(MangledName);
97 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
98 return OptimizeLayer.findSymbol(MangledNameStream.str(), true);
78 Error addModule(std::unique_ptr M) {
79 return OptimizeLayer.add(ES.getMainJITDylib(),
80 ThreadSafeModule(std::move(M), Ctx));
9981 }
10082
101 void removeModule(VModuleKey K) {
102 cantFail(OptimizeLayer.removeModule(K));
83 Expected lookup(StringRef Name) {
84 return ES.lookup({&ES.getMainJITDylib()}, Mangle(Name.str()));
10385 }
10486
10587 private:
106 std::unique_ptr optimizeModule(std::unique_ptr M) {
88
89 static Expected
90 optimizeModule(ThreadSafeModule TSM,
91 const MaterializationResponsibility &R) {
10792 // Create a function pass manager.
108 auto FPM = llvm::make_unique(M.get());
93 auto FPM = llvm::make_unique(TSM.getModule());
10994
11095 // Add some optimizations.
11196 FPM->add(createInstructionCombiningPass());
116101
117102 // Run the optimizations over all functions in the module being added to
118103 // the JIT.
119 for (auto &F : *M)
104 for (auto &F : *TSM.getModule())
120105 FPM->run(F);
121106
122 return M;
107 return TSM;
123108 }
124109 };
125110
675675 }
676676
677677 /// toplevelexpr ::= expression
678 static std::unique_ptr ParseTopLevelExpr() {
678 static std::unique_ptr ParseTopLevelExpr(unsigned ExprCount) {
679679 if (auto E = ParseExpression()) {
680680 // Make an anonymous proto.
681 auto Proto = llvm::make_unique("__anon_expr",
681 auto Proto = llvm::make_unique(("__anon_expr" +
682 Twine(ExprCount)).str(),
682683 std::vector());
683684 return llvm::make_unique(std::move(Proto), std::move(E));
684685 }
695696 // Code Generation
696697 //===----------------------------------------------------------------------===//
697698
698 static LLVMContext TheContext;
699 static IRBuilder<> Builder(TheContext);
699 static std::unique_ptr TheJIT;
700 static LLVMContext *TheContext;
701 static std::unique_ptr> Builder;
700702 static std::unique_ptr TheModule;
701703 static std::map NamedValues;
702 static std::unique_ptr TheJIT;
703704 static std::map> FunctionProtos;
705 static ExitOnError ExitOnErr;
704706
705707 Value *LogErrorV(const char *Str) {
706708 LogError(Str);
728730 const std::string &VarName) {
729731 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
730732 TheFunction->getEntryBlock().begin());
731 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), nullptr, VarName);
733 return TmpB.CreateAlloca(Type::getDoubleTy(*TheContext), nullptr, VarName);
732734 }
733735
734736 Value *NumberExprAST::codegen() {
735 return ConstantFP::get(TheContext, APFloat(Val));
737 return ConstantFP::get(*TheContext, APFloat(Val));
736738 }
737739
738740 Value *VariableExprAST::codegen() {
742744 return LogErrorV("Unknown variable name");
743745
744746 // Load the value.
745 return Builder.CreateLoad(V, Name.c_str());
747 return Builder->CreateLoad(V, Name.c_str());
746748 }
747749
748750 Value *UnaryExprAST::codegen() {
754756 if (!F)
755757 return LogErrorV("Unknown unary operator");
756758
757 return Builder.CreateCall(F, OperandV, "unop");
759 return Builder->CreateCall(F, OperandV, "unop");
758760 }
759761
760762 Value *BinaryExprAST::codegen() {
777779 if (!Variable)
778780 return LogErrorV("Unknown variable name");
779781
780 Builder.CreateStore(Val, Variable);
782 Builder->CreateStore(Val, Variable);
781783 return Val;
782784 }
783785
788790
789791 switch (Op) {
790792 case '+':
791 return Builder.CreateFAdd(L, R, "addtmp");
793 return Builder->CreateFAdd(L, R, "addtmp");
792794 case '-':
793 return Builder.CreateFSub(L, R, "subtmp");
795 return Builder->CreateFSub(L, R, "subtmp");
794796 case '*':
795 return Builder.CreateFMul(L, R, "multmp");
797 return Builder->CreateFMul(L, R, "multmp");
796798 case '<':
797 L = Builder.CreateFCmpULT(L, R, "cmptmp");
799 L = Builder->CreateFCmpULT(L, R, "cmptmp");
798800 // Convert bool 0/1 to double 0.0 or 1.0
799 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
801 return Builder->CreateUIToFP(L, Type::getDoubleTy(*TheContext), "booltmp");
800802 default:
801803 break;
802804 }
807809 assert(F && "binary operator not found!");
808810
809811 Value *Ops[] = {L, R};
810 return Builder.CreateCall(F, Ops, "binop");
812 return Builder->CreateCall(F, Ops, "binop");
811813 }
812814
813815 Value *CallExprAST::codegen() {
827829 return nullptr;
828830 }
829831
830 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
832 return Builder->CreateCall(CalleeF, ArgsV, "calltmp");
831833 }
832834
833835 Value *IfExprAST::codegen() {
836838 return nullptr;
837839
838840 // Convert condition to a bool by comparing equal to 0.0.
839 CondV = Builder.CreateFCmpONE(
840 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
841
842 Function *TheFunction = Builder.GetInsertBlock()->getParent();
841 CondV = Builder->CreateFCmpONE(
842 CondV, ConstantFP::get(*TheContext, APFloat(0.0)), "ifcond");
843
844 Function *TheFunction = Builder->GetInsertBlock()->getParent();
843845
844846 // Create blocks for the then and else cases. Insert the 'then' block at the
845847 // end of the function.
846 BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
847 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
848 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
849
850 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
848 BasicBlock *ThenBB = BasicBlock::Create(*TheContext, "then", TheFunction);
849 BasicBlock *ElseBB = BasicBlock::Create(*TheContext, "else");
850 BasicBlock *MergeBB = BasicBlock::Create(*TheContext, "ifcont");
851
852 Builder->CreateCondBr(CondV, ThenBB, ElseBB);
851853
852854 // Emit then value.
853 Builder.SetInsertPoint(ThenBB);
855 Builder->SetInsertPoint(ThenBB);
854856
855857 Value *ThenV = Then->codegen();
856858 if (!ThenV)
857859 return nullptr;
858860
859 Builder.CreateBr(MergeBB);
861 Builder->CreateBr(MergeBB);
860862 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
861 ThenBB = Builder.GetInsertBlock();
863 ThenBB = Builder->GetInsertBlock();
862864
863865 // Emit else block.
864866 TheFunction->getBasicBlockList().push_back(ElseBB);
865 Builder.SetInsertPoint(ElseBB);
867 Builder->SetInsertPoint(ElseBB);
866868
867869 Value *ElseV = Else->codegen();
868870 if (!ElseV)
869871 return nullptr;
870872
871 Builder.CreateBr(MergeBB);
873 Builder->CreateBr(MergeBB);
872874 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
873 ElseBB = Builder.GetInsertBlock();
875 ElseBB = Builder->GetInsertBlock();
874876
875877 // Emit merge block.
876878 TheFunction->getBasicBlockList().push_back(MergeBB);
877 Builder.SetInsertPoint(MergeBB);
878 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
879 Builder->SetInsertPoint(MergeBB);
880 PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
879881
880882 PN->addIncoming(ThenV, ThenBB);
881883 PN->addIncoming(ElseV, ElseBB);
902904 // br endcond, loop, endloop
903905 // outloop:
904906 Value *ForExprAST::codegen() {
905 Function *TheFunction = Builder.GetInsertBlock()->getParent();
907 Function *TheFunction = Builder->GetInsertBlock()->getParent();
906908
907909 // Create an alloca for the variable in the entry block.
908910 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
913915 return nullptr;
914916
915917 // Store the value into the alloca.
916 Builder.CreateStore(StartVal, Alloca);
918 Builder->CreateStore(StartVal, Alloca);
917919
918920 // Make the new basic block for the loop header, inserting after current
919921 // block.
920 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
922 BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop", TheFunction);
921923
922924 // Insert an explicit fall through from the current block to the LoopBB.
923 Builder.CreateBr(LoopBB);
925 Builder->CreateBr(LoopBB);
924926
925927 // Start insertion in LoopBB.
926 Builder.SetInsertPoint(LoopBB);
928 Builder->SetInsertPoint(LoopBB);
927929
928930 // Within the loop, the variable is defined equal to the PHI node. If it
929931 // shadows an existing variable, we have to restore it, so save it now.
944946 return nullptr;
945947 } else {
946948 // If not specified, use 1.0.
947 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
949 StepVal = ConstantFP::get(*TheContext, APFloat(1.0));
948950 }
949951
950952 // Compute the end condition.
954956
955957 // Reload, increment, and restore the alloca. This handles the case where
956958 // the body of the loop mutates the variable.
957 Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
958 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
959 Builder.CreateStore(NextVar, Alloca);
959 Value *CurVar = Builder->CreateLoad(Alloca, VarName.c_str());
960 Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
961 Builder->CreateStore(NextVar, Alloca);
960962
961963 // Convert condition to a bool by comparing equal to 0.0.
962 EndCond = Builder.CreateFCmpONE(
963 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
964 EndCond = Builder->CreateFCmpONE(
965 EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
964966
965967 // Create the "after loop" block and insert it.
966968 BasicBlock *AfterBB =
967 BasicBlock::Create(TheContext, "afterloop", TheFunction);
969 BasicBlock::Create(*TheContext, "afterloop", TheFunction);
968970
969971 // Insert the conditional branch into the end of LoopEndBB.
970 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
972 Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
971973
972974 // Any new code will be inserted in AfterBB.
973 Builder.SetInsertPoint(AfterBB);
975 Builder->SetInsertPoint(AfterBB);
974976
975977 // Restore the unshadowed variable.
976978 if (OldVal)
979981 NamedValues.erase(VarName);
980982
981983 // for expr always returns 0.0.
982 return Constant::getNullValue(Type::getDoubleTy(TheContext));
984 return Constant::getNullValue(Type::getDoubleTy(*TheContext));
983985 }
984986
985987 Value *VarExprAST::codegen() {
986988 std::vector OldBindings;
987989
988 Function *TheFunction = Builder.GetInsertBlock()->getParent();
990 Function *TheFunction = Builder->GetInsertBlock()->getParent();
989991
990992 // Register all variables and emit their initializer.
991993 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
10031005 if (!InitVal)
10041006 return nullptr;
10051007 } else { // If not specified, use 0.0.
1006 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
1008 InitVal = ConstantFP::get(*TheContext, APFloat(0.0));
10071009 }
10081010
10091011 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1010 Builder.CreateStore(InitVal, Alloca);
1012 Builder->CreateStore(InitVal, Alloca);
10111013
10121014 // Remember the old variable binding so that we can restore the binding when
10131015 // we unrecurse.
10321034
10331035 Function *PrototypeAST::codegen() {
10341036 // Make the function type: double(double,double) etc.
1035 std::vector Doubles(Args.size(), Type::getDoubleTy(TheContext));
1037 std::vector Doubles(Args.size(), Type::getDoubleTy(*TheContext));
10361038 FunctionType *FT =
1037 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
1039 FunctionType::get(Type::getDoubleTy(*TheContext), Doubles, false);
10381040
10391041 Function *F =
10401042 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
10611063 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
10621064
10631065 // Create a new basic block to start insertion into.
1064 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
1065 Builder.SetInsertPoint(BB);
1066 BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", TheFunction);
1067 Builder->SetInsertPoint(BB);
10661068
10671069 // Record the function arguments in the NamedValues map.
10681070 NamedValues.clear();
10711073 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
10721074
10731075 // Store the initial value into the alloca.
1074 Builder.CreateStore(&Arg, Alloca);
1076 Builder->CreateStore(&Arg, Alloca);
10751077
10761078 // Add arguments to variable symbol table.
10771079 NamedValues[Arg.getName()] = Alloca;
10791081
10801082 if (Value *RetVal = Body->codegen()) {
10811083 // Finish off the function.
1082 Builder.CreateRet(RetVal);
1084 Builder->CreateRet(RetVal);
10831085
10841086 // Validate the generated code, checking for consistency.
10851087 verifyFunction(*TheFunction);
11011103
11021104 static void InitializeModule() {
11031105 // Open a new module.
1104 TheModule = llvm::make_unique("my cool jit", TheContext);
1105 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
1106 TheModule = llvm::make_unique("my cool jit", *TheContext);
1107 TheModule->setDataLayout(TheJIT->getDataLayout());
1108
1109 // Create a new builder for the module.
1110 Builder = llvm::make_unique>(*TheContext);
11061111 }
11071112
11081113 static void HandleDefinition() {
11111116 fprintf(stderr, "Read function definition:");
11121117 FnIR->print(errs());
11131118 fprintf(stderr, "\n");
1114 TheJIT->addModule(std::move(TheModule));
1119 ExitOnErr(TheJIT->addModule(std::move(TheModule)));
11151120 InitializeModule();
11161121 }
11171122 } else {
11351140 }
11361141
11371142 static void HandleTopLevelExpression() {
1143 static unsigned ExprCount = 0;
1144
1145 // Update ExprCount. This number will be added to anonymous expressions to
1146 // prevent them from clashing.
1147 ++ExprCount;
1148
11381149 // Evaluate a top-level expression into an anonymous function.
1139 if (auto FnAST = ParseTopLevelExpr()) {
1150 if (auto FnAST = ParseTopLevelExpr(ExprCount)) {
11401151 if (FnAST->codegen()) {
11411152 // JIT the module containing the anonymous expression, keeping a handle so
11421153 // we can free it later.
1143 auto H = TheJIT->addModule(std::move(TheModule));
1154 ExitOnErr(TheJIT->addModule(std::move(TheModule)));
11441155 InitializeModule();
11451156
1146 // Search the JIT for the __anon_expr symbol.
1147 auto ExprSymbol = TheJIT->findSymbol("__anon_expr");
1148 assert(ExprSymbol && "Function not found");
1149
1150 // Get the symbol's address and cast it to the right type (takes no
1151 // arguments, returns a double) so we can call it as a native function.
1152 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
1157 // Get the anonymous expression's JITSymbol.
1158 auto Sym =
1159 ExitOnErr(TheJIT->lookup(("__anon_expr" + Twine(ExprCount)).str()));
1160
1161 auto *FP = (double (*)())(intptr_t)Sym.getAddress();
1162 assert(FP && "Failed to codegen function");
11531163 fprintf(stderr, "Evaluated to %f\n", FP());
1154
1155 // Delete the anonymous expression module from the JIT.
1156 TheJIT->removeModule(H);
11571164 }
11581165 } else {
11591166 // Skip token for error recovery.
12211228 fprintf(stderr, "ready> ");
12221229 getNextToken();
12231230
1224 TheJIT = llvm::make_unique();
1231 TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
1232 TheContext = &TheJIT->getContext();
12251233
12261234 InitializeModule();
12271235
19461946 SymbolStringPtr Name) {
19471947 SymbolNameSet Names({Name});
19481948
1949 JITDylibSearchList FullSearchOrder(SearchOrder.size());
1949 JITDylibSearchList FullSearchOrder;
1950 FullSearchOrder.reserve(SearchOrder.size());
19501951 for (auto *JD : SearchOrder)
19511952 FullSearchOrder.push_back({JD, false});
19521953