0f8a0ab0dfc3f46e15495ff50bb57064df575bac
[oota-llvm.git] / docs / tutorial / LangImpl8.rst
1 ======================================
2 Kaleidoscope: Adding Debug Information
3 ======================================
4
5 .. contents::
6    :local:
7
8 Chapter 8 Introduction
9 ======================
10
11 Welcome to Chapter 8 of the "`Implementing a language with
12 LLVM <index.html>`_" tutorial. In chapters 1 through 7, we've built a
13 decent little programming language with functions and variables.
14 What happens if something goes wrong though, how do you debug your
15 program?
16
17 Source level debugging uses formatted data that helps a debugger
18 translate from binary and the state of the machine back to the
19 source that the programmer wrote. In LLVM we generally use a format
20 called `DWARF <http://dwarfstd.org>`_. DWARF is a compact encoding
21 that represents types, source locations, and variable locations. 
22
23 The short summary of this chapter is that we'll go through the
24 various things you have to add to a programming language to
25 support debug info, and how you translate that into DWARF.
26
27 Caveat: For now we can't debug via the JIT, so we'll need to compile
28 our program down to something small and standalone. As part of this
29 we'll make a few modifications to the running of the language and
30 how programs are compiled. This means that we'll have a source file
31 with a simple program written in Kaleidoscope rather than the
32 interactive JIT. It does involve a limitation that we can only
33 have one "top level" command at a time to reduce the number of
34 changes necessary.
35
36 Here's the sample program we'll be compiling:
37
38 .. code-block:: python
39
40    def fib(x)
41      if x < 3 then
42        1
43      else
44        fib(x-1)+fib(x-2);
45
46    fib(10)
47
48
49 Why is this a hard problem?
50 ===========================
51
52 Debug information is a hard problem for a few different reasons - mostly
53 centered around optimized code. First, optimization makes keeping source
54 locations more difficult. In LLVM IR we keep the original source location
55 for each IR level instruction on the instruction. Optimization passes
56 should keep the source locations for newly created instructions, but merged
57 instructions only get to keep a single location - this can cause jumping
58 around when stepping through optimized programs. Secondly, optimization
59 can move variables in ways that are either optimized out, shared in memory
60 with other variables, or difficult to track. For the purposes of this
61 tutorial we're going to avoid optimization (as you'll see with one of the
62 next sets of patches).
63
64 Ahead-of-Time Compilation Mode
65 ==============================
66
67 To highlight only the aspects of adding debug information to a source
68 language without needing to worry about the complexities of JIT debugging
69 we're going to make a few changes to Kaleidoscope to support compiling
70 the IR emitted by the front end into a simple standalone program that
71 you can execute, debug, and see results.
72
73 First we make our anonymous function that contains our top level
74 statement be our "main":
75
76 .. code-block:: udiff
77
78   -    auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
79   +    auto Proto = llvm::make_unique<PrototypeAST>("main", std::vector<std::string>());
80
81 just with the simple change of giving it a name.
82
83 Then we're going to remove the command line code wherever it exists:
84
85 .. code-block:: udiff
86
87   @@ -1129,7 +1129,6 @@ static void HandleTopLevelExpression() {
88    /// top ::= definition | external | expression | ';'
89    static void MainLoop() {
90      while (1) {
91   -    fprintf(stderr, "ready> ");
92        switch (CurTok) {
93        case tok_eof:
94          return;
95   @@ -1184,7 +1183,6 @@ int main() {
96      BinopPrecedence['*'] = 40; // highest.
97  
98      // Prime the first token.
99   -  fprintf(stderr, "ready> ");
100      getNextToken();
101  
102 Lastly we're going to disable all of the optimization passes and the JIT so
103 that the only thing that happens after we're done parsing and generating
104 code is that the llvm IR goes to standard error:
105
106 .. code-block:: udiff
107
108   @@ -1108,17 +1108,8 @@ static void HandleExtern() {
109    static void HandleTopLevelExpression() {
110      // Evaluate a top-level expression into an anonymous function.
111      if (auto FnAST = ParseTopLevelExpr()) {
112   -    if (auto *FnIR = FnAST->codegen()) {
113   -      // We're just doing this to make sure it executes.
114   -      TheExecutionEngine->finalizeObject();
115   -      // JIT the function, returning a function pointer.
116   -      void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);
117   -
118   -      // Cast it to the right type (takes no arguments, returns a double) so we
119   -      // can call it as a native function.
120   -      double (*FP)() = (double (*)())(intptr_t)FPtr;
121   -      // Ignore the return value for this.
122   -      (void)FP;
123   +    if (!F->codegen()) {
124   +      fprintf(stderr, "Error generating code for top level expr");
125        }
126      } else {
127        // Skip token for error recovery.
128   @@ -1439,11 +1459,11 @@ int main() {
129      // target lays out data structures.
130      TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
131      OurFPM.add(new DataLayoutPass());
132   +#if 0
133      OurFPM.add(createBasicAliasAnalysisPass());
134      // Promote allocas to registers.
135      OurFPM.add(createPromoteMemoryToRegisterPass());
136   @@ -1218,7 +1210,7 @@ int main() {
137      OurFPM.add(createGVNPass());
138      // Simplify the control flow graph (deleting unreachable blocks, etc).
139      OurFPM.add(createCFGSimplificationPass());
140   -
141   +  #endif
142      OurFPM.doInitialization();
143  
144      // Set the global so the code gen can use this.
145
146 This relatively small set of changes get us to the point that we can compile
147 our piece of Kaleidoscope language down to an executable program via this
148 command line:
149
150 .. code-block:: bash
151
152   Kaleidoscope-Ch8 < fib.ks | & clang -x ir -
153
154 which gives an a.out/a.exe in the current working directory.
155
156 Compile Unit
157 ============
158
159 The top level container for a section of code in DWARF is a compile unit.
160 This contains the type and function data for an individual translation unit
161 (read: one file of source code). So the first thing we need to do is
162 construct one for our fib.ks file.
163
164 DWARF Emission Setup
165 ====================
166
167 Similar to the ``IRBuilder`` class we have a
168 ```DIBuilder`` <http://llvm.org/doxygen/classllvm_1_1DIBuilder.html>`_ class
169 that helps in constructing debug metadata for an llvm IR file. It
170 corresponds 1:1 similarly to ``IRBuilder`` and llvm IR, but with nicer names.
171 Using it does require that you be more familiar with DWARF terminology than
172 you needed to be with ``IRBuilder`` and ``Instruction`` names, but if you
173 read through the general documentation on the
174 ```Metadata Format`` <http://llvm.org/docs/SourceLevelDebugging.html>`_ it
175 should be a little more clear. We'll be using this class to construct all
176 of our IR level descriptions. Construction for it takes a module so we
177 need to construct it shortly after we construct our module. We've left it
178 as a global static variable to make it a bit easier to use.
179
180 Next we're going to create a small container to cache some of our frequent
181 data. The first will be our compile unit, but we'll also write a bit of
182 code for our one type since we won't have to worry about multiple typed
183 expressions:
184
185 .. code-block:: c++
186
187   static DIBuilder *DBuilder;
188
189   struct DebugInfo {
190     DICompileUnit *TheCU;
191     DIType *DblTy;
192
193     DIType *getDoubleTy();
194   } KSDbgInfo;
195
196   DIType *DebugInfo::getDoubleTy() {
197     if (DblTy.isValid())
198       return DblTy;
199
200     DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
201     return DblTy;
202   }
203
204 And then later on in ``main`` when we're constructing our module:
205
206 .. code-block:: c++
207
208   DBuilder = new DIBuilder(*TheModule);
209
210   KSDbgInfo.TheCU = DBuilder->createCompileUnit(
211       dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
212
213 There are a couple of things to note here. First, while we're producing a
214 compile unit for a language called Kaleidoscope we used the language
215 constant for C. This is because a debugger wouldn't necessarily understand
216 the calling conventions or default ABI for a language it doesn't recognize
217 and we follow the C ABI in our llvm code generation so it's the closest
218 thing to accurate. This ensures we can actually call functions from the
219 debugger and have them execute. Secondly, you'll see the "fib.ks" in the
220 call to ``createCompileUnit``. This is a default hard coded value since
221 we're using shell redirection to put our source into the Kaleidoscope
222 compiler. In a usual front end you'd have an input file name and it would
223 go there.
224
225 One last thing as part of emitting debug information via DIBuilder is that
226 we need to "finalize" the debug information. The reasons are part of the
227 underlying API for DIBuilder, but make sure you do this near the end of
228 main:
229
230 .. code-block:: c++
231
232   DBuilder->finalize();
233
234 before you dump out the module.
235
236 Functions
237 =========
238
239 Now that we have our ``Compile Unit`` and our source locations, we can add
240 function definitions to the debug info. So in ``PrototypeAST::codegen()`` we
241 add a few lines of code to describe a context for our subprogram, in this
242 case the "File", and the actual definition of the function itself.
243
244 So the context:
245
246 .. code-block:: c++
247
248   DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
249                                       KSDbgInfo.TheCU.getDirectory());
250
251 giving us an DIFile and asking the ``Compile Unit`` we created above for the
252 directory and filename where we are currently. Then, for now, we use some
253 source locations of 0 (since our AST doesn't currently have source location
254 information) and construct our function definition:
255
256 .. code-block:: c++
257
258   DIScope *FContext = Unit;
259   unsigned LineNo = 0;
260   unsigned ScopeLine = 0;
261   DISubprogram *SP = DBuilder->createFunction(
262       FContext, Name, StringRef(), Unit, LineNo,
263       CreateFunctionType(Args.size(), Unit), false /* internal linkage */,
264       true /* definition */, ScopeLine, DINode::FlagPrototyped, false);
265   F->setSubprogram(SP);
266
267 and we now have an DISubprogram that contains a reference to all of our
268 metadata for the function.
269
270 Source Locations
271 ================
272
273 The most important thing for debug information is accurate source location -
274 this makes it possible to map your source code back. We have a problem though,
275 Kaleidoscope really doesn't have any source location information in the lexer
276 or parser so we'll need to add it.
277
278 .. code-block:: c++
279
280    struct SourceLocation {
281      int Line;
282      int Col;
283    };
284    static SourceLocation CurLoc;
285    static SourceLocation LexLoc = {1, 0};
286
287    static int advance() {
288      int LastChar = getchar();
289
290      if (LastChar == '\n' || LastChar == '\r') {
291        LexLoc.Line++;
292        LexLoc.Col = 0;
293      } else
294        LexLoc.Col++;
295      return LastChar;
296    }
297
298 In this set of code we've added some functionality on how to keep track of the
299 line and column of the "source file". As we lex every token we set our current
300 current "lexical location" to the assorted line and column for the beginning
301 of the token. We do this by overriding all of the previous calls to
302 ``getchar()`` with our new ``advance()`` that keeps track of the information
303 and then we have added to all of our AST classes a source location:
304
305 .. code-block:: c++
306
307    class ExprAST {
308      SourceLocation Loc;
309
310      public:
311        ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {}
312        virtual ~ExprAST() {}
313        virtual Value* codegen() = 0;
314        int getLine() const { return Loc.Line; }
315        int getCol() const { return Loc.Col; }
316        virtual raw_ostream &dump(raw_ostream &out, int ind) {
317          return out << ':' << getLine() << ':' << getCol() << '\n';
318        }
319
320 that we pass down through when we create a new expression:
321
322 .. code-block:: c++
323
324    LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),
325                                           std::move(RHS));
326
327 giving us locations for each of our expressions and variables.
328
329 From this we can make sure to tell ``DIBuilder`` when we're at a new source
330 location so it can use that when we generate the rest of our code and make
331 sure that each instruction has source location information. We do this
332 by constructing another small function:
333
334 .. code-block:: c++
335
336   void DebugInfo::emitLocation(ExprAST *AST) {
337     DIScope *Scope;
338     if (LexicalBlocks.empty())
339       Scope = TheCU;
340     else
341       Scope = LexicalBlocks.back();
342     Builder.SetCurrentDebugLocation(
343         DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
344   }
345
346 that both tells the main ``IRBuilder`` where we are, but also what scope
347 we're in. Since we've just created a function above we can either be in
348 the main file scope (like when we created our function), or now we can be
349 in the function scope we just created. To represent this we create a stack
350 of scopes:
351
352 .. code-block:: c++
353
354    std::vector<DIScope *> LexicalBlocks;
355    std::map<const PrototypeAST *, DIScope *> FnScopeMap;
356
357 and keep a map of each function to the scope that it represents (an
358 DISubprogram is also an DIScope).
359
360 Then we make sure to:
361
362 .. code-block:: c++
363
364    KSDbgInfo.emitLocation(this);
365
366 emit the location every time we start to generate code for a new AST, and
367 also:
368
369 .. code-block:: c++
370
371   KSDbgInfo.FnScopeMap[this] = SP;
372
373 store the scope (function) when we create it and use it:
374
375   KSDbgInfo.LexicalBlocks.push_back(&KSDbgInfo.FnScopeMap[Proto]);
376
377 when we start generating the code for each function.
378
379 also, don't forget to pop the scope back off of your scope stack at the
380 end of the code generation for the function:
381
382 .. code-block:: c++
383
384   // Pop off the lexical block for the function since we added it
385   // unconditionally.
386   KSDbgInfo.LexicalBlocks.pop_back();
387
388 Variables
389 =========
390
391 Now that we have functions, we need to be able to print out the variables
392 we have in scope. Let's get our function arguments set up so we can get
393 decent backtraces and see how our functions are being called. It isn't
394 a lot of code, and we generally handle it when we're creating the
395 argument allocas in ``PrototypeAST::CreateArgumentAllocas``.
396
397 .. code-block:: c++
398
399   DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
400   DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
401                                       KSDbgInfo.TheCU.getDirectory());
402   DILocalVariable D = DBuilder->createParameterVariable(
403       Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true);
404
405   DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
406                           DebugLoc::get(Line, 0, Scope),
407                           Builder.GetInsertBlock());
408
409 Here we're doing a few things. First, we're grabbing our current scope
410 for the variable so we can say what range of code our variable is valid
411 through. Second, we're creating the variable, giving it the scope,
412 the name, source location, type, and since it's an argument, the argument
413 index. Third, we create an ``lvm.dbg.declare`` call to indicate at the IR
414 level that we've got a variable in an alloca (and it gives a starting
415 location for the variable), and setting a source location for the
416 beginning of the scope on the declare.
417
418 One interesting thing to note at this point is that various debuggers have
419 assumptions based on how code and debug information was generated for them
420 in the past. In this case we need to do a little bit of a hack to avoid
421 generating line information for the function prologue so that the debugger
422 knows to skip over those instructions when setting a breakpoint. So in
423 ``FunctionAST::CodeGen`` we add a couple of lines:
424
425 .. code-block:: c++
426
427   // Unset the location for the prologue emission (leading instructions with no
428   // location in a function are considered part of the prologue and the debugger
429   // will run past them when breaking on a function)
430   KSDbgInfo.emitLocation(nullptr);
431
432 and then emit a new location when we actually start generating code for the
433 body of the function:
434
435 .. code-block:: c++
436
437   KSDbgInfo.emitLocation(Body);
438
439 With this we have enough debug information to set breakpoints in functions,
440 print out argument variables, and call functions. Not too bad for just a
441 few simple lines of code!
442
443 Full Code Listing
444 =================
445
446 Here is the complete code listing for our running example, enhanced with
447 debug information. To build this example, use:
448
449 .. code-block:: bash
450
451     # Compile
452     clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy
453     # Run
454     ./toy
455
456 Here is the code:
457
458 .. literalinclude:: ../../examples/Kaleidoscope/Chapter8/toy.cpp
459    :language: c++
460
461 `Next: Conclusion and other useful LLVM tidbits <LangImpl9.html>`_
462