1=======================================================
2Building a JIT: Starting out with KaleidoscopeJIT
3=======================================================
4
5.. contents::
6   :local:
7
8**This tutorial is under active development. It is incomplete and details may
9change frequently.** Nonetheless we invite you to try it out as it stands, and
10we welcome any feedback.
11
12Chapter 1 Introduction
13======================
14
15Welcome to Chapter 1 of the "Building an ORC-based JIT in LLVM" tutorial. This
16tutorial runs through the implementation of a JIT compiler using LLVM's
17On-Request-Compilation (ORC) APIs. It begins with a simplified version of the
18KaleidoscopeJIT class used in the
19`Implementing a language with LLVM <LangImpl1.html>`_ tutorials and then
20introduces new features like optimization, lazy compilation and remote
21execution.
22
23The goal of this tutorial is to introduce you to LLVM's ORC JIT APIs, show how
24these APIs interact with other parts of LLVM, and to teach you how to recombine
25them to build a custom JIT that is suited to your use-case.
26
27The structure of the tutorial is:
28
29- Chapter #1: Investigate the simple KaleidoscopeJIT class. This will
30  introduce some of the basic concepts of the ORC JIT APIs, including the
31  idea of an ORC *Layer*.
32
33- `Chapter #2 <BuildingAJIT2.html>`_: Extend the basic KaleidoscopeJIT by adding
34  a new layer that will optimize IR and generated code.
35
36- `Chapter #3 <BuildingAJIT3.html>`_: Further extend the JIT by adding a
37  Compile-On-Demand layer to lazily compile IR.
38
39- `Chapter #4 <BuildingAJIT4.html>`_: Improve the laziness of our JIT by
40  replacing the Compile-On-Demand layer with a custom layer that uses the ORC
41  Compile Callbacks API directly to defer IR-generation until functions are
42  called.
43
44- `Chapter #5 <BuildingAJIT5.html>`_: Add process isolation by JITing code into
45  a remote process with reduced privileges using the JIT Remote APIs.
46
47To provide input for our JIT we will use the Kaleidoscope REPL from
48`Chapter 7 <LangImpl7.html>`_ of the "Implementing a language in LLVM tutorial",
49with one minor modification: We will remove the FunctionPassManager from the
50code for that chapter and replace it with optimization support in our JIT class
51in Chapter #2.
52
53Finally, a word on API generations: ORC is the 3rd generation of LLVM JIT API.
54It was preceeded by MCJIT, and before that by the (now deleted) legacy JIT.
55These tutorials don't assume any experience with these earlier APIs, but
56readers acquainted with them will see many familiar elements. Where appropriate
57we will make this connection with the earlier APIs explicit to help people who
58are transitioning from them to ORC.
59
60JIT API Basics
61==============
62
63The purpose of a JIT compiler is to compile code "on-the-fly" as it is needed,
64rather than compiling whole programs to disk ahead of time as a traditional
65compiler does. To support that aim our initial, bare-bones JIT API will be:
66
671. Handle addModule(Module &M) -- Make the given IR module available for
68   execution.
692. JITSymbol findSymbol(const std::string &Name) -- Search for pointers to
70   symbols (functions or variables) that have been added to the JIT.
713. void removeModule(Handle H) -- Remove a module from the JIT, releasing any
72   memory that had been used for the compiled code.
73
74A basic use-case for this API, executing the 'main' function from a module,
75will look like:
76
77.. code-block:: c++
78
79  std::unique_ptr<Module> M = buildModule();
80  JIT J;
81  Handle H = J.addModule(*M);
82  int (*Main)(int, char*[]) =
83    (int(*)(int, char*[])J.findSymbol("main").getAddress();
84  int Result = Main();
85  J.removeModule(H);
86
87The APIs that we build in these tutorials will all be variations on this simple
88theme. Behind the API we will refine the implementation of the JIT to add
89support for optimization and lazy compilation. Eventually we will extend the
90API itself to allow higher-level program representations (e.g. ASTs) to be
91added to the JIT.
92
93KaleidoscopeJIT
94===============
95
96In the previous section we described our API, now we examine a simple
97implementation of it: The KaleidoscopeJIT class [1]_ that was used in the
98`Implementing a language with LLVM <LangImpl1.html>`_ tutorials. We will use
99the REPL code from `Chapter 7 <LangImpl7.html>`_ of that tutorial to supply the
100input for our JIT: Each time the user enters an expression the REPL will add a
101new IR module containing the code for that expression to the JIT. If the
102expression is a top-level expression like '1+1' or 'sin(x)', the REPL will also
103use the findSymbol method of our JIT class find and execute the code for the
104expression, and then use the removeModule method to remove the code again
105(since there's no way to re-invoke an anonymous expression). In later chapters
106of this tutorial we'll modify the REPL to enable new interactions with our JIT
107class, but for now we will take this setup for granted and focus our attention on
108the implementation of our JIT itself.
109
110Our KaleidoscopeJIT class is defined in the KaleidoscopeJIT.h header. After the
111usual include guards and #includes [2]_, we get to the definition of our class:
112
113.. code-block:: c++
114
115  #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
116  #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
117
118  #include "llvm/ExecutionEngine/ExecutionEngine.h"
119  #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
120  #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
121  #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
122  #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
123  #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
124  #include "llvm/IR/Mangler.h"
125  #include "llvm/Support/DynamicLibrary.h"
126
127  namespace llvm {
128  namespace orc {
129
130  class KaleidoscopeJIT {
131  private:
132
133    std::unique_ptr<TargetMachine> TM;
134    const DataLayout DL;
135    ObjectLinkingLayer<> ObjectLayer;
136    IRCompileLayer<decltype(ObjectLayer)> CompileLayer;
137
138  public:
139
140    typedef decltype(CompileLayer)::ModuleSetHandleT ModuleHandleT;
141
142Our class begins with four members: A TargetMachine, TM, which will be used
143to build our LLVM compiler instance; A DataLayout, DL, which will be used for
144symbol mangling (more on that later), and two ORC *layers*: An
145ObjectLinkingLayer, and an IRCompileLayer. The ObjectLinkingLayer is the
146foundation of our JIT: it takes in-memory object files produced by a
147compiler and links them on the fly to make them executable. This
148JIT-on-top-of-a-linker design was introduced in MCJIT, where the linker was
149hidden inside the MCJIT class itself. In ORC we expose the linker as a visible,
150reusable component so that clients can access and configure it directly
151if they need to. In this tutorial our ObjectLinkingLayer will just be used to
152support the next layer in our stack: the IRCompileLayer, which will be
153responsible for taking LLVM IR, compiling it, and passing the resulting
154in-memory object files down to the object linking layer below.
155
156After our member variables comes typedef: ModuleHandle. This is the handle
157type that will be returned from our JIT's addModule method, and which can be
158used to remove a module again using the removeModule method. The IRCompileLayer
159class already provides a convenient handle type
160(IRCompileLayer::ModuleSetHandleT), so we will just provide a type-alias for
161this.
162
163.. code-block:: c++
164
165  KaleidoscopeJIT()
166      : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
167    CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
168    llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
169  }
170
171  TargetMachine &getTargetMachine() { return *TM; }
172
173Next up we have our class constructor. We begin by initializing TM using the
174EngineBuilder::selectTarget helper method, which constructs a TargetMachine for
175the current process. Next we use our newly created TargetMachine to initialize
176DL, our DataLayout. Then we initialize our IRCompileLayer. Our IRCompile layer
177needs two things: (1) A reference to our object linking layer, and (2) a
178compiler instance to use to perform the actual compilation from IR to object
179files. We use the off-the-shelf SimpleCompiler instance for now, but in later
180chapters we will substitute our own configurable compiler classes. Finally, in
181the body of the constructor, we call the DynamicLibrary::LoadLibraryPermanently
182method with a nullptr argument. Normally the LoadLibraryPermanently method is
183called with the path of a dynamic library to load, but when passed a null
184pointer it will 'load' the host process itself, making its exported symbols
185available for execution.
186
187.. code-block:: c++
188
189  ModuleHandle addModule(std::unique_ptr<Module> M) {
190    // Build our symbol resolver:
191    // Lambda 1: Look back into the JIT itself to find symbols that are part of
192    //           the same "logical dylib".
193    // Lambda 2: Search for external symbols in the host process.
194    auto Resolver = createLambdaResolver(
195        [&](const std::string &Name) {
196          if (auto Sym = CompileLayer.findSymbol(Name, false))
197            return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
198          return RuntimeDyld::SymbolInfo(nullptr);
199        },
200        [](const std::string &S) {
201          if (auto SymAddr =
202                RTDyldMemoryManager::getSymbolAddressInProcess(Name))
203            return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported);
204          return RuntimeDyld::SymbolInfo(nullptr);
205        });
206
207    // Build a singlton module set to hold our module.
208    std::vector<std::unique_ptr<Module>> Ms;
209    Ms.push_back(std::move(M));
210
211    // Add the set to the JIT with the resolver we created above and a newly
212    // created SectionMemoryManager.
213    return CompileLayer.addModuleSet(std::move(Ms),
214                                     make_unique<SectionMemoryManager>(),
215                                     std::move(Resolver));
216  }
217
218Now we come to the first of our central JIT API methods: addModule. This method
219is responsible for adding IR to the JIT and making it available for execution.
220In this initial implementation of our JIT we will make our modules "available
221for execution" by compiling them immediately as they are added to the JIT. In
222later chapters we will teach our JIT to be lazier and instead add the Modules
223to a "pending" list to be compiled if and when they are first executed.
224
225To add our module to the IRCompileLayer we need to supply two auxiliary
226objects: a memory manager and a symbol resolver. The memory manager will be
227responsible for managing the memory allocated to JIT'd machine code, applying
228memory protection permissions, and registering JIT'd exception handling tables
229(if the JIT'd code uses exceptions). In our simple use-case we can just supply
230an off-the-shelf SectionMemoryManager instance. The memory, exception handling
231tables, etc. will be released when we remove the module from the JIT again
232(using removeModule) or, if removeModule is never called, when the JIT class
233itself is destructed.
234
235The second auxiliary class, the symbol resolver, is more interesting for us. It
236exists to tell the JIT where to look when it encounters an *external symbol* in
237the module we are adding. External symbols are any symbol not defined within the
238module itself, including calls to functions outside the JIT and calls to
239functions defined in other modules that have already been added to the JIT. It
240may seem as though modules added to the JIT should "know about one another" by
241default, but since we would still have to supply a symbol resolver for
242references to code outside the JIT it turns out to re-use this one mechanism
243for all symbol resolution. This has the added benefit that the user has full
244control over the symbol resolution process. Should we search for definitions
245within the JIT first, then fall back on external definitions? Or should we
246prefer external definitions where available and only JIT code if we don't
247already have an available implementation? By using a single symbol resolution
248scheme we are free to choose whatever makes the most sense for any given use
249case.
250
251Building a symbol resolver is made especially easy by the
252*createLambdaResolver* function. This function takes two lambdas (actually
253they don't have to be lambdas, any object with a call operator will do) and
254returns a RuntimeDyld::SymbolResolver instance. The first lambda is used as
255the implementation of the resolver's findSymbolInLogicalDylib method. This
256method searches for symbol definitions that should be thought of as being part
257of the same "logical" dynamic library as this Module. If you are familiar with
258static linking: this means that findSymbolInLogicalDylib should expose symbols
259with common linkage and hidden visibility. If all this sounds foreign you can
260ignore the details and just remember that this is the first method that the
261linker will use to try to find a symbol definition. If the
262findSymbolInLogicalDylib method returns a null result then the linker will
263call the second symbol resolver method, called findSymbol. This searches for
264symbols that should be thought of as external to (but visibile from) the module
265and its logical dylib.
266
267In this tutorial we will use the following simple breakdown: All modules added
268to the JIT will behave as if they were linked into a single, ever-growing
269logical dylib. To implement this our first lambda (the one defining
270findSymbolInLogicalDylib) will just search for JIT'd code by calling the
271CompileLayer's findSymbol method. If we don't find a symbol in the JIT itself
272we'll fall back to our second lambda, which implements findSymbol. This will
273use the RTDyldMemoyrManager::getSymbolAddressInProcess method to search for
274the symbol within the program itself. If we can't find a symbol definition
275via either of these paths the JIT will refuse to accept our moudle, returning
276a "symbol not found" error.
277
278Now that we've built our symbol resolver we're ready to add our module to the
279JIT. We do this by calling the CompileLayer's addModuleSet method [3]_. Since
280we only have a single Module and addModuleSet expects a collection, we will
281create a vector of modules and add our module as the only member. Since we
282have already typedef'd our ModuleHandle type to be the same as the
283CompileLayer's handle type, we can return the handle from addModuleSet
284directly from our addModule method.
285
286.. code-block:: c++
287
288  JITSymbol findSymbol(const std::string Name) {
289    std::string MangledName;
290    raw_string_ostream MangledNameStream(MangledName);
291    Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
292    return CompileLayer.findSymbol(MangledNameStream.str(), true);
293  }
294
295  void removeModule(ModuleHandle H) {
296    CompileLayer.removeModuleSet(H);
297  }
298
299*To be done: describe findSymbol and removeModule -- why do we mangle? what's
300the relationship between findSymbol and resolvers, why remove modules...*
301
302*To be done: Conclusion, exercises (maybe a utility for a standalone IR JIT,
303like a mini-LLI), feed to next chapter.*
304
305Full Code Listing
306=================
307
308Here is the complete code listing for our running example. To build this
309example, use:
310
311.. code-block:: bash
312
313    # Compile
314    clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orc native` -O3 -o toy
315    # Run
316    ./toy
317
318Here is the code:
319
320.. literalinclude:: ../../examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
321   :language: c++
322
323`Next: Extending the KaleidoscopeJIT <BuildingAJIT2.html>`_
324
325.. [1] Actually we use a cut-down version of KaleidoscopeJIT that makes a
326       simplifying assumption: symbols cannot be re-defined. This will make it
327       impossible to re-define symbols in the REPL, but will make our symbol
328       lookup logic simpler. Re-introducing support for symbol redefinition is
329       left as an exercise for the reader. (The KaleidoscopeJIT.h used in the
330       original tutorials will be a helpful reference).
331
332.. [2] +-----------------------+-----------------------------------------------+
333       |         File          |               Reason for inclusion            |
334       +=======================+===============================================+
335       |   ExecutionEngine.h   | Access to the EngineBuilder::selectTarget     |
336       |                       | method.                                       |
337       +-----------------------+-----------------------------------------------+
338       |                       | Access to the                                 |
339       | RTDyldMemoryManager.h | RTDyldMemoryManager::getSymbolAddressInProcess|
340       |                       | method.                                       |
341       +-----------------------+-----------------------------------------------+
342       |    CompileUtils.h     | Provides the SimpleCompiler class.            |
343       +-----------------------+-----------------------------------------------+
344       |   IRCompileLayer.h    | Provides the IRCompileLayer class.            |
345       +-----------------------+-----------------------------------------------+
346       |                       | Access the createLambdaResolver function,     |
347       |   LambdaResolver.h    | which provides easy construction of symbol    |
348       |                       | resolvers.                                    |
349       +-----------------------+-----------------------------------------------+
350       |  ObjectLinkingLayer.h | Provides the ObjectLinkingLayer class.        |
351       +-----------------------+-----------------------------------------------+
352       |       Mangler.h       | Provides the Mangler class for platform       |
353       |                       | specific name-mangling.                       |
354       +-----------------------+-----------------------------------------------+
355       |   DynamicLibrary.h    | Provides the DynamicLibrary class, which      |
356       |                       | makes symbols in the host process searchable. |
357       +-----------------------+-----------------------------------------------+
358
359.. [3] ORC layers accept sets of Modules, rather than individual ones, so that
360       all Modules in the set could be co-located by the memory manager, though
361       this feature is not yet implemented.
362