Home | History | Annotate | Download | only in tutorial
      1 ========================================
      2 Kaleidoscope: Code generation to LLVM IR
      3 ========================================
      4 
      5 .. contents::
      6    :local:
      7 
      8 Chapter 3 Introduction
      9 ======================
     10 
     11 Welcome to Chapter 3 of the "`Implementing a language with
     12 LLVM <index.html>`_" tutorial. This chapter shows you how to transform
     13 the `Abstract Syntax Tree <LangImpl2.html>`_, built in Chapter 2, into
     14 LLVM IR. This will teach you a little bit about how LLVM does things, as
     15 well as demonstrate how easy it is to use. It's much more work to build
     16 a lexer and parser than it is to generate LLVM IR code. :)
     17 
     18 **Please note**: the code in this chapter and later require LLVM 3.7 or
     19 later. LLVM 3.6 and before will not work with it. Also note that you
     20 need to use a version of this tutorial that matches your LLVM release:
     21 If you are using an official LLVM release, use the version of the
     22 documentation included with your release or on the `llvm.org releases
     23 page <http://llvm.org/releases/>`_.
     24 
     25 Code Generation Setup
     26 =====================
     27 
     28 In order to generate LLVM IR, we want some simple setup to get started.
     29 First we define virtual code generation (codegen) methods in each AST
     30 class:
     31 
     32 .. code-block:: c++
     33 
     34     /// ExprAST - Base class for all expression nodes.
     35     class ExprAST {
     36     public:
     37       virtual ~ExprAST() {}
     38       virtual Value *codegen() = 0;
     39     };
     40 
     41     /// NumberExprAST - Expression class for numeric literals like "1.0".
     42     class NumberExprAST : public ExprAST {
     43       double Val;
     44 
     45     public:
     46       NumberExprAST(double Val) : Val(Val) {}
     47       virtual Value *codegen();
     48     };
     49     ...
     50 
     51 The codegen() method says to emit IR for that AST node along with all
     52 the things it depends on, and they all return an LLVM Value object.
     53 "Value" is the class used to represent a "`Static Single Assignment
     54 (SSA) <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
     55 register" or "SSA value" in LLVM. The most distinct aspect of SSA values
     56 is that their value is computed as the related instruction executes, and
     57 it does not get a new value until (and if) the instruction re-executes.
     58 In other words, there is no way to "change" an SSA value. For more
     59 information, please read up on `Static Single
     60 Assignment <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
     61 - the concepts are really quite natural once you grok them.
     62 
     63 Note that instead of adding virtual methods to the ExprAST class
     64 hierarchy, it could also make sense to use a `visitor
     65 pattern <http://en.wikipedia.org/wiki/Visitor_pattern>`_ or some other
     66 way to model this. Again, this tutorial won't dwell on good software
     67 engineering practices: for our purposes, adding a virtual method is
     68 simplest.
     69 
     70 The second thing we want is an "Error" method like we used for the
     71 parser, which will be used to report errors found during code generation
     72 (for example, use of an undeclared parameter):
     73 
     74 .. code-block:: c++
     75 
     76     static std::unique_ptr<Module> *TheModule;
     77     static IRBuilder<> Builder(getGlobalContext());
     78     static std::map<std::string, Value*> NamedValues;
     79 
     80     Value *ErrorV(const char *Str) {
     81       Error(Str);
     82       return nullptr;
     83     }
     84 
     85 The static variables will be used during code generation. ``TheModule``
     86 is an LLVM construct that contains functions and global variables. In many
     87 ways, it is the top-level structure that the LLVM IR uses to contain code.
     88 It will own the memory for all of the IR that we generate, which is why
     89 the codegen() method returns a raw Value\*, rather than a unique_ptr<Value>.
     90 
     91 The ``Builder`` object is a helper object that makes it easy to generate
     92 LLVM instructions. Instances of the
     93 `IRBuilder <http://llvm.org/doxygen/IRBuilder_8h-source.html>`_
     94 class template keep track of the current place to insert instructions
     95 and has methods to create new instructions.
     96 
     97 The ``NamedValues`` map keeps track of which values are defined in the
     98 current scope and what their LLVM representation is. (In other words, it
     99 is a symbol table for the code). In this form of Kaleidoscope, the only
    100 things that can be referenced are function parameters. As such, function
    101 parameters will be in this map when generating code for their function
    102 body.
    103 
    104 With these basics in place, we can start talking about how to generate
    105 code for each expression. Note that this assumes that the ``Builder``
    106 has been set up to generate code *into* something. For now, we'll assume
    107 that this has already been done, and we'll just use it to emit code.
    108 
    109 Expression Code Generation
    110 ==========================
    111 
    112 Generating LLVM code for expression nodes is very straightforward: less
    113 than 45 lines of commented code for all four of our expression nodes.
    114 First we'll do numeric literals:
    115 
    116 .. code-block:: c++
    117 
    118     Value *NumberExprAST::codegen() {
    119       return ConstantFP::get(getGlobalContext(), APFloat(Val));
    120     }
    121 
    122 In the LLVM IR, numeric constants are represented with the
    123 ``ConstantFP`` class, which holds the numeric value in an ``APFloat``
    124 internally (``APFloat`` has the capability of holding floating point
    125 constants of Arbitrary Precision). This code basically just creates
    126 and returns a ``ConstantFP``. Note that in the LLVM IR that constants
    127 are all uniqued together and shared. For this reason, the API uses the
    128 "foo::get(...)" idiom instead of "new foo(..)" or "foo::Create(..)".
    129 
    130 .. code-block:: c++
    131 
    132     Value *VariableExprAST::codegen() {
    133       // Look this variable up in the function.
    134       Value *V = NamedValues[Name];
    135       if (!V)
    136         ErrorV("Unknown variable name");
    137       return V;
    138     }
    139 
    140 References to variables are also quite simple using LLVM. In the simple
    141 version of Kaleidoscope, we assume that the variable has already been
    142 emitted somewhere and its value is available. In practice, the only
    143 values that can be in the ``NamedValues`` map are function arguments.
    144 This code simply checks to see that the specified name is in the map (if
    145 not, an unknown variable is being referenced) and returns the value for
    146 it. In future chapters, we'll add support for `loop induction
    147 variables <LangImpl5.html#for-loop-expression>`_ in the symbol table, and for `local
    148 variables <LangImpl7.html#user-defined-local-variables>`_.
    149 
    150 .. code-block:: c++
    151 
    152     Value *BinaryExprAST::codegen() {
    153       Value *L = LHS->codegen();
    154       Value *R = RHS->codegen();
    155       if (!L || !R)
    156         return nullptr;
    157 
    158       switch (Op) {
    159       case '+':
    160         return Builder.CreateFAdd(L, R, "addtmp");
    161       case '-':
    162         return Builder.CreateFSub(L, R, "subtmp");
    163       case '*':
    164         return Builder.CreateFMul(L, R, "multmp");
    165       case '<':
    166         L = Builder.CreateFCmpULT(L, R, "cmptmp");
    167         // Convert bool 0/1 to double 0.0 or 1.0
    168         return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
    169                                     "booltmp");
    170       default:
    171         return ErrorV("invalid binary operator");
    172       }
    173     }
    174 
    175 Binary operators start to get more interesting. The basic idea here is
    176 that we recursively emit code for the left-hand side of the expression,
    177 then the right-hand side, then we compute the result of the binary
    178 expression. In this code, we do a simple switch on the opcode to create
    179 the right LLVM instruction.
    180 
    181 In the example above, the LLVM builder class is starting to show its
    182 value. IRBuilder knows where to insert the newly created instruction,
    183 all you have to do is specify what instruction to create (e.g. with
    184 ``CreateFAdd``), which operands to use (``L`` and ``R`` here) and
    185 optionally provide a name for the generated instruction.
    186 
    187 One nice thing about LLVM is that the name is just a hint. For instance,
    188 if the code above emits multiple "addtmp" variables, LLVM will
    189 automatically provide each one with an increasing, unique numeric
    190 suffix. Local value names for instructions are purely optional, but it
    191 makes it much easier to read the IR dumps.
    192 
    193 `LLVM instructions <../LangRef.html#instruction-reference>`_ are constrained by strict
    194 rules: for example, the Left and Right operators of an `add
    195 instruction <../LangRef.html#add-instruction>`_ must have the same type, and the
    196 result type of the add must match the operand types. Because all values
    197 in Kaleidoscope are doubles, this makes for very simple code for add,
    198 sub and mul.
    199 
    200 On the other hand, LLVM specifies that the `fcmp
    201 instruction <../LangRef.html#fcmp-instruction>`_ always returns an 'i1' value (a
    202 one bit integer). The problem with this is that Kaleidoscope wants the
    203 value to be a 0.0 or 1.0 value. In order to get these semantics, we
    204 combine the fcmp instruction with a `uitofp
    205 instruction <../LangRef.html#uitofp-to-instruction>`_. This instruction converts its
    206 input integer into a floating point value by treating the input as an
    207 unsigned value. In contrast, if we used the `sitofp
    208 instruction <../LangRef.html#sitofp-to-instruction>`_, the Kaleidoscope '<' operator
    209 would return 0.0 and -1.0, depending on the input value.
    210 
    211 .. code-block:: c++
    212 
    213     Value *CallExprAST::codegen() {
    214       // Look up the name in the global module table.
    215       Function *CalleeF = TheModule->getFunction(Callee);
    216       if (!CalleeF)
    217         return ErrorV("Unknown function referenced");
    218 
    219       // If argument mismatch error.
    220       if (CalleeF->arg_size() != Args.size())
    221         return ErrorV("Incorrect # arguments passed");
    222 
    223       std::vector<Value *> ArgsV;
    224       for (unsigned i = 0, e = Args.size(); i != e; ++i) {
    225         ArgsV.push_back(Args[i]->codegen());
    226         if (!ArgsV.back())
    227           return nullptr;
    228       }
    229 
    230       return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
    231     }
    232 
    233 Code generation for function calls is quite straightforward with LLVM. The code
    234 above initially does a function name lookup in the LLVM Module's symbol table.
    235 Recall that the LLVM Module is the container that holds the functions we are
    236 JIT'ing. By giving each function the same name as what the user specifies, we
    237 can use the LLVM symbol table to resolve function names for us.
    238 
    239 Once we have the function to call, we recursively codegen each argument
    240 that is to be passed in, and create an LLVM `call
    241 instruction <../LangRef.html#call-instruction>`_. Note that LLVM uses the native C
    242 calling conventions by default, allowing these calls to also call into
    243 standard library functions like "sin" and "cos", with no additional
    244 effort.
    245 
    246 This wraps up our handling of the four basic expressions that we have so
    247 far in Kaleidoscope. Feel free to go in and add some more. For example,
    248 by browsing the `LLVM language reference <../LangRef.html>`_ you'll find
    249 several other interesting instructions that are really easy to plug into
    250 our basic framework.
    251 
    252 Function Code Generation
    253 ========================
    254 
    255 Code generation for prototypes and functions must handle a number of
    256 details, which make their code less beautiful than expression code
    257 generation, but allows us to illustrate some important points. First,
    258 lets talk about code generation for prototypes: they are used both for
    259 function bodies and external function declarations. The code starts
    260 with:
    261 
    262 .. code-block:: c++
    263 
    264     Function *PrototypeAST::codegen() {
    265       // Make the function type:  double(double,double) etc.
    266       std::vector<Type*> Doubles(Args.size(),
    267                                  Type::getDoubleTy(getGlobalContext()));
    268       FunctionType *FT =
    269         FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
    270 
    271       Function *F =
    272         Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
    273 
    274 This code packs a lot of power into a few lines. Note first that this
    275 function returns a "Function\*" instead of a "Value\*". Because a
    276 "prototype" really talks about the external interface for a function
    277 (not the value computed by an expression), it makes sense for it to
    278 return the LLVM Function it corresponds to when codegen'd.
    279 
    280 The call to ``FunctionType::get`` creates the ``FunctionType`` that
    281 should be used for a given Prototype. Since all function arguments in
    282 Kaleidoscope are of type double, the first line creates a vector of "N"
    283 LLVM double types. It then uses the ``Functiontype::get`` method to
    284 create a function type that takes "N" doubles as arguments, returns one
    285 double as a result, and that is not vararg (the false parameter
    286 indicates this). Note that Types in LLVM are uniqued just like Constants
    287 are, so you don't "new" a type, you "get" it.
    288 
    289 The final line above actually creates the IR Function corresponding to
    290 the Prototype. This indicates the type, linkage and name to use, as
    291 well as which module to insert into. "`external
    292 linkage <../LangRef.html#linkage>`_" means that the function may be
    293 defined outside the current module and/or that it is callable by
    294 functions outside the module. The Name passed in is the name the user
    295 specified: since "``TheModule``" is specified, this name is registered
    296 in "``TheModule``"s symbol table.
    297 
    298 .. code-block:: c++
    299 
    300   // Set names for all arguments.
    301   unsigned Idx = 0;
    302   for (auto &Arg : F->args())
    303     Arg.setName(Args[Idx++]);
    304 
    305   return F;
    306 
    307 Finally, we set the name of each of the function's arguments according to the
    308 names given in the Prototype. This step isn't strictly necessary, but keeping
    309 the names consistent makes the IR more readable, and allows subsequent code to
    310 refer directly to the arguments for their names, rather than having to look up
    311 them up in the Prototype AST.
    312 
    313 At this point we have a function prototype with no body. This is how LLVM IR
    314 represents function declarations. For extern statements in Kaleidoscope, this
    315 is as far as we need to go. For function definitions however, we need to
    316 codegen and attach a function body.
    317 
    318 .. code-block:: c++
    319 
    320   Function *FunctionAST::codegen() {
    321       // First, check for an existing function from a previous 'extern' declaration.
    322     Function *TheFunction = TheModule->getFunction(Proto->getName());
    323 
    324     if (!TheFunction)
    325       TheFunction = Proto->codegen();
    326 
    327     if (!TheFunction)
    328       return nullptr;
    329 
    330     if (!TheFunction->empty())
    331       return (Function*)ErrorV("Function cannot be redefined.");
    332 
    333 
    334 For function definitions, we start by searching TheModule's symbol table for an
    335 existing version of this function, in case one has already been created using an
    336 'extern' statement. If Module::getFunction returns null then no previous version
    337 exists, so we'll codegen one from the Prototype. In either case, we want to
    338 assert that the function is empty (i.e. has no body yet) before we start.
    339 
    340 .. code-block:: c++
    341 
    342   // Create a new basic block to start insertion into.
    343   BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
    344   Builder.SetInsertPoint(BB);
    345 
    346   // Record the function arguments in the NamedValues map.
    347   NamedValues.clear();
    348   for (auto &Arg : TheFunction->args())
    349     NamedValues[Arg.getName()] = &Arg;
    350 
    351 Now we get to the point where the ``Builder`` is set up. The first line
    352 creates a new `basic block <http://en.wikipedia.org/wiki/Basic_block>`_
    353 (named "entry"), which is inserted into ``TheFunction``. The second line
    354 then tells the builder that new instructions should be inserted into the
    355 end of the new basic block. Basic blocks in LLVM are an important part
    356 of functions that define the `Control Flow
    357 Graph <http://en.wikipedia.org/wiki/Control_flow_graph>`_. Since we
    358 don't have any control flow, our functions will only contain one block
    359 at this point. We'll fix this in `Chapter 5 <LangImpl5.html>`_ :).
    360 
    361 Next we add the function arguments to the NamedValues map (after first clearing
    362 it out) so that they're accessible to ``VariableExprAST`` nodes.
    363 
    364 .. code-block:: c++
    365 
    366       if (Value *RetVal = Body->codegen()) {
    367         // Finish off the function.
    368         Builder.CreateRet(RetVal);
    369 
    370         // Validate the generated code, checking for consistency.
    371         verifyFunction(*TheFunction);
    372 
    373         return TheFunction;
    374       }
    375 
    376 Once the insertion point has been set up and the NamedValues map populated,
    377 we call the ``codegen()`` method for the root expression of the function. If no
    378 error happens, this emits code to compute the expression into the entry block
    379 and returns the value that was computed. Assuming no error, we then create an
    380 LLVM `ret instruction <../LangRef.html#ret-instruction>`_, which completes the function.
    381 Once the function is built, we call ``verifyFunction``, which is
    382 provided by LLVM. This function does a variety of consistency checks on
    383 the generated code, to determine if our compiler is doing everything
    384 right. Using this is important: it can catch a lot of bugs. Once the
    385 function is finished and validated, we return it.
    386 
    387 .. code-block:: c++
    388 
    389       // Error reading body, remove function.
    390       TheFunction->eraseFromParent();
    391       return nullptr;
    392     }
    393 
    394 The only piece left here is handling of the error case. For simplicity,
    395 we handle this by merely deleting the function we produced with the
    396 ``eraseFromParent`` method. This allows the user to redefine a function
    397 that they incorrectly typed in before: if we didn't delete it, it would
    398 live in the symbol table, with a body, preventing future redefinition.
    399 
    400 This code does have a bug, though: If the ``FunctionAST::codegen()`` method
    401 finds an existing IR Function, it does not validate its signature against the
    402 definition's own prototype. This means that an earlier 'extern' declaration will
    403 take precedence over the function definition's signature, which can cause
    404 codegen to fail, for instance if the function arguments are named differently.
    405 There are a number of ways to fix this bug, see what you can come up with! Here
    406 is a testcase:
    407 
    408 ::
    409 
    410     extern foo(a);     # ok, defines foo.
    411     def foo(b) b;      # Error: Unknown variable name. (decl using 'a' takes precedence).
    412 
    413 Driver Changes and Closing Thoughts
    414 ===================================
    415 
    416 For now, code generation to LLVM doesn't really get us much, except that
    417 we can look at the pretty IR calls. The sample code inserts calls to
    418 codegen into the "``HandleDefinition``", "``HandleExtern``" etc
    419 functions, and then dumps out the LLVM IR. This gives a nice way to look
    420 at the LLVM IR for simple functions. For example:
    421 
    422 ::
    423 
    424     ready> 4+5;
    425     Read top-level expression:
    426     define double @0() {
    427     entry:
    428       ret double 9.000000e+00
    429     }
    430 
    431 Note how the parser turns the top-level expression into anonymous
    432 functions for us. This will be handy when we add `JIT
    433 support <LangImpl4.html#adding-a-jit-compiler>`_ in the next chapter. Also note that the
    434 code is very literally transcribed, no optimizations are being performed
    435 except simple constant folding done by IRBuilder. We will `add
    436 optimizations <LangImpl4.html#trivial-constant-folding>`_ explicitly in the next
    437 chapter.
    438 
    439 ::
    440 
    441     ready> def foo(a b) a*a + 2*a*b + b*b;
    442     Read function definition:
    443     define double @foo(double %a, double %b) {
    444     entry:
    445       %multmp = fmul double %a, %a
    446       %multmp1 = fmul double 2.000000e+00, %a
    447       %multmp2 = fmul double %multmp1, %b
    448       %addtmp = fadd double %multmp, %multmp2
    449       %multmp3 = fmul double %b, %b
    450       %addtmp4 = fadd double %addtmp, %multmp3
    451       ret double %addtmp4
    452     }
    453 
    454 This shows some simple arithmetic. Notice the striking similarity to the
    455 LLVM builder calls that we use to create the instructions.
    456 
    457 ::
    458 
    459     ready> def bar(a) foo(a, 4.0) + bar(31337);
    460     Read function definition:
    461     define double @bar(double %a) {
    462     entry:
    463       %calltmp = call double @foo(double %a, double 4.000000e+00)
    464       %calltmp1 = call double @bar(double 3.133700e+04)
    465       %addtmp = fadd double %calltmp, %calltmp1
    466       ret double %addtmp
    467     }
    468 
    469 This shows some function calls. Note that this function will take a long
    470 time to execute if you call it. In the future we'll add conditional
    471 control flow to actually make recursion useful :).
    472 
    473 ::
    474 
    475     ready> extern cos(x);
    476     Read extern:
    477     declare double @cos(double)
    478 
    479     ready> cos(1.234);
    480     Read top-level expression:
    481     define double @1() {
    482     entry:
    483       %calltmp = call double @cos(double 1.234000e+00)
    484       ret double %calltmp
    485     }
    486 
    487 This shows an extern for the libm "cos" function, and a call to it.
    488 
    489 .. TODO:: Abandon Pygments' horrible `llvm` lexer. It just totally gives up
    490    on highlighting this due to the first line.
    491 
    492 ::
    493 
    494     ready> ^D
    495     ; ModuleID = 'my cool jit'
    496 
    497     define double @0() {
    498     entry:
    499       %addtmp = fadd double 4.000000e+00, 5.000000e+00
    500       ret double %addtmp
    501     }
    502 
    503     define double @foo(double %a, double %b) {
    504     entry:
    505       %multmp = fmul double %a, %a
    506       %multmp1 = fmul double 2.000000e+00, %a
    507       %multmp2 = fmul double %multmp1, %b
    508       %addtmp = fadd double %multmp, %multmp2
    509       %multmp3 = fmul double %b, %b
    510       %addtmp4 = fadd double %addtmp, %multmp3
    511       ret double %addtmp4
    512     }
    513 
    514     define double @bar(double %a) {
    515     entry:
    516       %calltmp = call double @foo(double %a, double 4.000000e+00)
    517       %calltmp1 = call double @bar(double 3.133700e+04)
    518       %addtmp = fadd double %calltmp, %calltmp1
    519       ret double %addtmp
    520     }
    521 
    522     declare double @cos(double)
    523 
    524     define double @1() {
    525     entry:
    526       %calltmp = call double @cos(double 1.234000e+00)
    527       ret double %calltmp
    528     }
    529 
    530 When you quit the current demo, it dumps out the IR for the entire
    531 module generated. Here you can see the big picture with all the
    532 functions referencing each other.
    533 
    534 This wraps up the third chapter of the Kaleidoscope tutorial. Up next,
    535 we'll describe how to `add JIT codegen and optimizer
    536 support <LangImpl4.html>`_ to this so we can actually start running
    537 code!
    538 
    539 Full Code Listing
    540 =================
    541 
    542 Here is the complete code listing for our running example, enhanced with
    543 the LLVM code generator. Because this uses the LLVM libraries, we need
    544 to link them in. To do this, we use the
    545 `llvm-config <http://llvm.org/cmds/llvm-config.html>`_ tool to inform
    546 our makefile/command line about which options to use:
    547 
    548 .. code-block:: bash
    549 
    550     # Compile
    551     clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
    552     # Run
    553     ./toy
    554 
    555 Here is the code:
    556 
    557 .. literalinclude:: ../../examples/Kaleidoscope/Chapter3/toy.cpp
    558    :language: c++
    559 
    560 `Next: Adding JIT and Optimizer Support <LangImpl4.html>`_
    561 
    562