[CMake] add_llvm_external_project: Just warn about nonexistent directories.
[oota-llvm.git] / docs / tutorial / LangImpl3.rst
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 2.2 or
19 later. LLVM 2.1 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     Value *ErrorV(const char *Str) {
77       Error(Str);
78       return nullptr;
79     }
80
81     static Module *TheModule;
82     static IRBuilder<> Builder(getGlobalContext());
83     static std::map<std::string, Value*> NamedValues;
84
85 The static variables will be used during code generation. ``TheModule``
86 is the LLVM construct that contains all of the functions and global
87 variables in a chunk of code. In many ways, it is the top-level
88 structure that the LLVM IR uses to contain code.
89
90 The ``Builder`` object is a helper object that makes it easy to generate
91 LLVM instructions. Instances of the
92 `IRBuilder <http://llvm.org/doxygen/IRBuilder_8h-source.html>`_
93 class template keep track of the current place to insert instructions
94 and has methods to create new instructions.
95
96 The ``NamedValues`` map keeps track of which values are defined in the
97 current scope and what their LLVM representation is. (In other words, it
98 is a symbol table for the code). In this form of Kaleidoscope, the only
99 things that can be referenced are function parameters. As such, function
100 parameters will be in this map when generating code for their function
101 body.
102
103 With these basics in place, we can start talking about how to generate
104 code for each expression. Note that this assumes that the ``Builder``
105 has been set up to generate code *into* something. For now, we'll assume
106 that this has already been done, and we'll just use it to emit code.
107
108 Expression Code Generation
109 ==========================
110
111 Generating LLVM code for expression nodes is very straightforward: less
112 than 45 lines of commented code for all four of our expression nodes.
113 First we'll do numeric literals:
114
115 .. code-block:: c++
116
117     Value *NumberExprAST::Codegen() {
118       return ConstantFP::get(getGlobalContext(), APFloat(Val));
119     }
120
121 In the LLVM IR, numeric constants are represented with the
122 ``ConstantFP`` class, which holds the numeric value in an ``APFloat``
123 internally (``APFloat`` has the capability of holding floating point
124 constants of Arbitrary Precision). This code basically just creates
125 and returns a ``ConstantFP``. Note that in the LLVM IR that constants
126 are all uniqued together and shared. For this reason, the API uses the
127 "foo::get(...)" idiom instead of "new foo(..)" or "foo::Create(..)".
128
129 .. code-block:: c++
130
131     Value *VariableExprAST::Codegen() {
132       // Look this variable up in the function.
133       Value *V = NamedValues[Name];
134       if (!V)
135         ErrorV("Unknown variable name");
136       return V;
137     }
138
139 References to variables are also quite simple using LLVM. In the simple
140 version of Kaleidoscope, we assume that the variable has already been
141 emitted somewhere and its value is available. In practice, the only
142 values that can be in the ``NamedValues`` map are function arguments.
143 This code simply checks to see that the specified name is in the map (if
144 not, an unknown variable is being referenced) and returns the value for
145 it. In future chapters, we'll add support for `loop induction
146 variables <LangImpl5.html#for>`_ in the symbol table, and for `local
147 variables <LangImpl7.html#localvars>`_.
148
149 .. code-block:: c++
150
151     Value *BinaryExprAST::Codegen() {
152       Value *L = LHS->Codegen();
153       Value *R = RHS->Codegen();
154       if (!L || !R)
155         return nullptr;
156
157       switch (Op) {
158       case '+':
159         return Builder.CreateFAdd(L, R, "addtmp");
160       case '-':
161         return Builder.CreateFSub(L, R, "subtmp");
162       case '*':
163         return Builder.CreateFMul(L, R, "multmp");
164       case '<':
165         L = Builder.CreateFCmpULT(L, R, "cmptmp");
166         // Convert bool 0/1 to double 0.0 or 1.0
167         return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
168                                     "booltmp");
169       default:
170         return ErrorV("invalid binary operator");
171       }
172     }
173
174 Binary operators start to get more interesting. The basic idea here is
175 that we recursively emit code for the left-hand side of the expression,
176 then the right-hand side, then we compute the result of the binary
177 expression. In this code, we do a simple switch on the opcode to create
178 the right LLVM instruction.
179
180 In the example above, the LLVM builder class is starting to show its
181 value. IRBuilder knows where to insert the newly created instruction,
182 all you have to do is specify what instruction to create (e.g. with
183 ``CreateFAdd``), which operands to use (``L`` and ``R`` here) and
184 optionally provide a name for the generated instruction.
185
186 One nice thing about LLVM is that the name is just a hint. For instance,
187 if the code above emits multiple "addtmp" variables, LLVM will
188 automatically provide each one with an increasing, unique numeric
189 suffix. Local value names for instructions are purely optional, but it
190 makes it much easier to read the IR dumps.
191
192 `LLVM instructions <../LangRef.html#instref>`_ are constrained by strict
193 rules: for example, the Left and Right operators of an `add
194 instruction <../LangRef.html#i_add>`_ must have the same type, and the
195 result type of the add must match the operand types. Because all values
196 in Kaleidoscope are doubles, this makes for very simple code for add,
197 sub and mul.
198
199 On the other hand, LLVM specifies that the `fcmp
200 instruction <../LangRef.html#i_fcmp>`_ always returns an 'i1' value (a
201 one bit integer). The problem with this is that Kaleidoscope wants the
202 value to be a 0.0 or 1.0 value. In order to get these semantics, we
203 combine the fcmp instruction with a `uitofp
204 instruction <../LangRef.html#i_uitofp>`_. This instruction converts its
205 input integer into a floating point value by treating the input as an
206 unsigned value. In contrast, if we used the `sitofp
207 instruction <../LangRef.html#i_sitofp>`_, the Kaleidoscope '<' operator
208 would return 0.0 and -1.0, depending on the input value.
209
210 .. code-block:: c++
211
212     Value *CallExprAST::Codegen() {
213       // Look up the name in the global module table.
214       Function *CalleeF = TheModule->getFunction(Callee);
215       if (!CalleeF)
216         return ErrorV("Unknown function referenced");
217
218       // If argument mismatch error.
219       if (CalleeF->arg_size() != Args.size())
220         return ErrorV("Incorrect # arguments passed");
221
222       std::vector<Value *> ArgsV;
223       for (unsigned i = 0, e = Args.size(); i != e; ++i) {
224         ArgsV.push_back(Args[i]->Codegen());
225         if (!ArgsV.back())
226           return nullptr;
227       }
228
229       return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
230     }
231
232 Code generation for function calls is quite straightforward with LLVM.
233 The code above initially does a function name lookup in the LLVM
234 Module's symbol table. Recall that the LLVM Module is the container that
235 holds all of the functions we are JIT'ing. By giving each function the
236 same name as what the user specifies, we can use the LLVM symbol table
237 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#i_call>`_. 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 function that the prototype
290 will correspond to. 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, which is used by the function call
297 code above.
298
299 .. code-block:: c++
300
301       // If F conflicted, there was already something named 'Name'.  If it has a
302       // body, don't allow redefinition or reextern.
303       if (F->getName() != Name) {
304         // Delete the one we just made and get the existing one.
305         F->eraseFromParent();
306         F = TheModule->getFunction(Name);
307
308 The Module symbol table works just like the Function symbol table when
309 it comes to name conflicts: if a new function is created with a name
310 that was previously added to the symbol table, the new function will get
311 implicitly renamed when added to the Module. The code above exploits
312 this fact to determine if there was a previous definition of this
313 function.
314
315 In Kaleidoscope, I choose to allow redefinitions of functions in two
316 cases: first, we want to allow 'extern'ing a function more than once, as
317 long as the prototypes for the externs match (since all arguments have
318 the same type, we just have to check that the number of arguments
319 match). Second, we want to allow 'extern'ing a function and then
320 defining a body for it. This is useful when defining mutually recursive
321 functions.
322
323 In order to implement this, the code above first checks to see if there
324 is a collision on the name of the function. If so, it deletes the
325 function we just created (by calling ``eraseFromParent``) and then
326 calling ``getFunction`` to get the existing function with the specified
327 name. Note that many APIs in LLVM have "erase" forms and "remove" forms.
328 The "remove" form unlinks the object from its parent (e.g. a Function
329 from a Module) and returns it. The "erase" form unlinks the object and
330 then deletes it.
331
332 .. code-block:: c++
333
334         // If F already has a body, reject this.
335         if (!F->empty()) {
336           ErrorF("redefinition of function");
337           return nullptr;
338         }
339
340         // If F took a different number of args, reject.
341         if (F->arg_size() != Args.size()) {
342           ErrorF("redefinition of function with different # args");
343           return nullptr;
344         }
345       }
346
347 In order to verify the logic above, we first check to see if the
348 pre-existing function is "empty". In this case, empty means that it has
349 no basic blocks in it, which means it has no body. If it has no body, it
350 is a forward declaration. Since we don't allow anything after a full
351 definition of the function, the code rejects this case. If the previous
352 reference to a function was an 'extern', we simply verify that the
353 number of arguments for that definition and this one match up. If not,
354 we emit an error.
355
356 .. code-block:: c++
357
358       // Set names for all arguments.
359       unsigned Idx = 0;
360       for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
361            ++AI, ++Idx) {
362         AI->setName(Args[Idx]);
363
364         // Add arguments to variable symbol table.
365         NamedValues[Args[Idx]] = AI;
366       }
367
368       return F;
369     }
370
371 The last bit of code for prototypes loops over all of the arguments in
372 the function, setting the name of the LLVM Argument objects to match,
373 and registering the arguments in the ``NamedValues`` map for future use
374 by the ``VariableExprAST`` AST node. Once this is set up, it returns the
375 Function object to the caller. Note that we don't check for conflicting
376 argument names here (e.g. "extern foo(a b a)"). Doing so would be very
377 straight-forward with the mechanics we have already used above.
378
379 .. code-block:: c++
380
381     Function *FunctionAST::Codegen() {
382       NamedValues.clear();
383
384       Function *TheFunction = Proto->Codegen();
385       if (!TheFunction)
386         return nullptr;
387
388 Code generation for function definitions starts out simply enough: we
389 just codegen the prototype (Proto) and verify that it is ok. We then
390 clear out the ``NamedValues`` map to make sure that there isn't anything
391 in it from the last function we compiled. Code generation of the
392 prototype ensures that there is an LLVM Function object that is ready to
393 go for us.
394
395 .. code-block:: c++
396
397       // Create a new basic block to start insertion into.
398       BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
399       Builder.SetInsertPoint(BB);
400
401       if (Value *RetVal = Body->Codegen()) {
402
403 Now we get to the point where the ``Builder`` is set up. The first line
404 creates a new `basic block <http://en.wikipedia.org/wiki/Basic_block>`_
405 (named "entry"), which is inserted into ``TheFunction``. The second line
406 then tells the builder that new instructions should be inserted into the
407 end of the new basic block. Basic blocks in LLVM are an important part
408 of functions that define the `Control Flow
409 Graph <http://en.wikipedia.org/wiki/Control_flow_graph>`_. Since we
410 don't have any control flow, our functions will only contain one block
411 at this point. We'll fix this in `Chapter 5 <LangImpl5.html>`_ :).
412
413 .. code-block:: c++
414
415       if (Value *RetVal = Body->Codegen()) {
416         // Finish off the function.
417         Builder.CreateRet(RetVal);
418
419         // Validate the generated code, checking for consistency.
420         verifyFunction(*TheFunction);
421
422         return TheFunction;
423       }
424
425 Once the insertion point is set up, we call the ``CodeGen()`` method for
426 the root expression of the function. If no error happens, this emits
427 code to compute the expression into the entry block and returns the
428 value that was computed. Assuming no error, we then create an LLVM `ret
429 instruction <../LangRef.html#i_ret>`_, which completes the function.
430 Once the function is built, we call ``verifyFunction``, which is
431 provided by LLVM. This function does a variety of consistency checks on
432 the generated code, to determine if our compiler is doing everything
433 right. Using this is important: it can catch a lot of bugs. Once the
434 function is finished and validated, we return it.
435
436 .. code-block:: c++
437
438       // Error reading body, remove function.
439       TheFunction->eraseFromParent();
440       return nullptr;
441     }
442
443 The only piece left here is handling of the error case. For simplicity,
444 we handle this by merely deleting the function we produced with the
445 ``eraseFromParent`` method. This allows the user to redefine a function
446 that they incorrectly typed in before: if we didn't delete it, it would
447 live in the symbol table, with a body, preventing future redefinition.
448
449 This code does have a bug, though. Since the ``PrototypeAST::Codegen``
450 can return a previously defined forward declaration, our code can
451 actually delete a forward declaration. There are a number of ways to fix
452 this bug, see what you can come up with! Here is a testcase:
453
454 ::
455
456     extern foo(a b);     # ok, defines foo.
457     def foo(a b) c;      # error, 'c' is invalid.
458     def bar() foo(1, 2); # error, unknown function "foo"
459
460 Driver Changes and Closing Thoughts
461 ===================================
462
463 For now, code generation to LLVM doesn't really get us much, except that
464 we can look at the pretty IR calls. The sample code inserts calls to
465 Codegen into the "``HandleDefinition``", "``HandleExtern``" etc
466 functions, and then dumps out the LLVM IR. This gives a nice way to look
467 at the LLVM IR for simple functions. For example:
468
469 ::
470
471     ready> 4+5;
472     Read top-level expression:
473     define double @0() {
474     entry:
475       ret double 9.000000e+00
476     }
477
478 Note how the parser turns the top-level expression into anonymous
479 functions for us. This will be handy when we add `JIT
480 support <LangImpl4.html#jit>`_ in the next chapter. Also note that the
481 code is very literally transcribed, no optimizations are being performed
482 except simple constant folding done by IRBuilder. We will `add
483 optimizations <LangImpl4.html#trivialconstfold>`_ explicitly in the next
484 chapter.
485
486 ::
487
488     ready> def foo(a b) a*a + 2*a*b + b*b;
489     Read function definition:
490     define double @foo(double %a, double %b) {
491     entry:
492       %multmp = fmul double %a, %a
493       %multmp1 = fmul double 2.000000e+00, %a
494       %multmp2 = fmul double %multmp1, %b
495       %addtmp = fadd double %multmp, %multmp2
496       %multmp3 = fmul double %b, %b
497       %addtmp4 = fadd double %addtmp, %multmp3
498       ret double %addtmp4
499     }
500
501 This shows some simple arithmetic. Notice the striking similarity to the
502 LLVM builder calls that we use to create the instructions.
503
504 ::
505
506     ready> def bar(a) foo(a, 4.0) + bar(31337);
507     Read function definition:
508     define double @bar(double %a) {
509     entry:
510       %calltmp = call double @foo(double %a, double 4.000000e+00)
511       %calltmp1 = call double @bar(double 3.133700e+04)
512       %addtmp = fadd double %calltmp, %calltmp1
513       ret double %addtmp
514     }
515
516 This shows some function calls. Note that this function will take a long
517 time to execute if you call it. In the future we'll add conditional
518 control flow to actually make recursion useful :).
519
520 ::
521
522     ready> extern cos(x);
523     Read extern:
524     declare double @cos(double)
525
526     ready> cos(1.234);
527     Read top-level expression:
528     define double @1() {
529     entry:
530       %calltmp = call double @cos(double 1.234000e+00)
531       ret double %calltmp
532     }
533
534 This shows an extern for the libm "cos" function, and a call to it.
535
536 .. TODO:: Abandon Pygments' horrible `llvm` lexer. It just totally gives up
537    on highlighting this due to the first line.
538
539 ::
540
541     ready> ^D
542     ; ModuleID = 'my cool jit'
543
544     define double @0() {
545     entry:
546       %addtmp = fadd double 4.000000e+00, 5.000000e+00
547       ret double %addtmp
548     }
549
550     define double @foo(double %a, double %b) {
551     entry:
552       %multmp = fmul double %a, %a
553       %multmp1 = fmul double 2.000000e+00, %a
554       %multmp2 = fmul double %multmp1, %b
555       %addtmp = fadd double %multmp, %multmp2
556       %multmp3 = fmul double %b, %b
557       %addtmp4 = fadd double %addtmp, %multmp3
558       ret double %addtmp4
559     }
560
561     define double @bar(double %a) {
562     entry:
563       %calltmp = call double @foo(double %a, double 4.000000e+00)
564       %calltmp1 = call double @bar(double 3.133700e+04)
565       %addtmp = fadd double %calltmp, %calltmp1
566       ret double %addtmp
567     }
568
569     declare double @cos(double)
570
571     define double @1() {
572     entry:
573       %calltmp = call double @cos(double 1.234000e+00)
574       ret double %calltmp
575     }
576
577 When you quit the current demo, it dumps out the IR for the entire
578 module generated. Here you can see the big picture with all the
579 functions referencing each other.
580
581 This wraps up the third chapter of the Kaleidoscope tutorial. Up next,
582 we'll describe how to `add JIT codegen and optimizer
583 support <LangImpl4.html>`_ to this so we can actually start running
584 code!
585
586 Full Code Listing
587 =================
588
589 Here is the complete code listing for our running example, enhanced with
590 the LLVM code generator. Because this uses the LLVM libraries, we need
591 to link them in. To do this, we use the
592 `llvm-config <http://llvm.org/cmds/llvm-config.html>`_ tool to inform
593 our makefile/command line about which options to use:
594
595 .. code-block:: bash
596
597     # Compile
598     clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
599     # Run
600     ./toy
601
602 Here is the code:
603
604 .. literalinclude:: ../../examples/Kaleidoscope/Chapter3/toy.cpp
605    :language: c++
606
607 `Next: Adding JIT and Optimizer Support <LangImpl4.html>`_
608