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