blob: 4b9b8c55bd0db954bb165eb82c12edf4faf40a75 [file] [log] [blame]
Chris Lattnerc0b42e92007-10-23 06:27:55 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3
4<html>
5<head>
6 <title>Kaleidoscope: Adding JIT and Optimizer Support</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Chris Lattner">
9 <link rel="stylesheet" href="../llvm.css" type="text/css">
10</head>
11
12<body>
13
14<div class="doc_title">Kaleidoscope: Adding JIT and Optimizer Support</div>
15
Chris Lattner128eb862007-11-05 19:06:59 +000016<ul>
Chris Lattner0e555b12007-11-05 20:04:56 +000017<li><a href="index.html">Up to Tutorial Index</a></li>
Chris Lattner128eb862007-11-05 19:06:59 +000018<li>Chapter 4
19 <ol>
20 <li><a href="#intro">Chapter 4 Introduction</a></li>
21 <li><a href="#trivialconstfold">Trivial Constant Folding</a></li>
22 <li><a href="#optimizerpasses">LLVM Optimization Passes</a></li>
23 <li><a href="#jit">Adding a JIT Compiler</a></li>
24 <li><a href="#code">Full Code Listing</a></li>
25 </ol>
26</li>
Chris Lattner0e555b12007-11-05 20:04:56 +000027<li><a href="LangImpl5.html">Chapter 5</a>: Extending the Language: Control
28Flow</li>
Chris Lattner128eb862007-11-05 19:06:59 +000029</ul>
30
Chris Lattnerc0b42e92007-10-23 06:27:55 +000031<div class="doc_author">
32 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
33</div>
34
35<!-- *********************************************************************** -->
Chris Lattner128eb862007-11-05 19:06:59 +000036<div class="doc_section"><a name="intro">Chapter 4 Introduction</a></div>
Chris Lattnerc0b42e92007-10-23 06:27:55 +000037<!-- *********************************************************************** -->
38
39<div class="doc_text">
40
Chris Lattner128eb862007-11-05 19:06:59 +000041<p>Welcome to Chapter 4 of the "<a href="index.html">Implementing a language
Chris Lattnera54c2012007-11-07 05:28:43 +000042with LLVM</a>" tutorial. Chapters 1-3 described the implementation of a simple
43language and added support for generating LLVM IR. This chapter describes
Chris Lattner128eb862007-11-05 19:06:59 +000044two new techniques: adding optimizer support to your language, and adding JIT
Chris Lattner41fcea32007-11-13 07:06:30 +000045compiler support. These additions will demonstrate how to get nice, efficient code
46for the Kaleidoscope language.</p>
Chris Lattnerc0b42e92007-10-23 06:27:55 +000047
48</div>
49
50<!-- *********************************************************************** -->
Chris Lattner118749e2007-10-25 06:23:36 +000051<div class="doc_section"><a name="trivialconstfold">Trivial Constant
52Folding</a></div>
Chris Lattnerc0b42e92007-10-23 06:27:55 +000053<!-- *********************************************************************** -->
54
55<div class="doc_text">
56
57<p>
Chris Lattner118749e2007-10-25 06:23:36 +000058Our demonstration for Chapter 3 is elegant and easy to extend. Unfortunately,
59it does not produce wonderful code. For example, when compiling simple code,
60we don't get obvious optimizations:</p>
Chris Lattnerc0b42e92007-10-23 06:27:55 +000061
62<div class="doc_code">
63<pre>
Chris Lattner118749e2007-10-25 06:23:36 +000064ready&gt; <b>def test(x) 1+2+x;</b>
65Read function definition:
66define double @test(double %x) {
67entry:
68 %addtmp = add double 1.000000e+00, 2.000000e+00
69 %addtmp1 = add double %addtmp, %x
70 ret double %addtmp1
71}
72</pre>
73</div>
74
Chris Lattner41fcea32007-11-13 07:06:30 +000075<p>This code is a very, very literal transcription of the AST built by parsing
76the input. As such, this transcription lacks optimizations like constant folding (we'd like to get "<tt>add x, 3.0</tt>" in the example above) as well as other more important
77optimizations. Constant folding, in particular, is a very common and very
Chris Lattner118749e2007-10-25 06:23:36 +000078important optimization: so much so that many language implementors implement
79constant folding support in their AST representation.</p>
80
Chris Lattner41fcea32007-11-13 07:06:30 +000081<p>With LLVM, you don't need this support in the AST. Since all calls to build LLVM IR go through
Chris Lattner118749e2007-10-25 06:23:36 +000082the LLVM builder, it would be nice if the builder itself checked to see if there
83was a constant folding opportunity when you call it. If so, it could just do
84the constant fold and return the constant instead of creating an instruction.
85This is exactly what the <tt>LLVMFoldingBuilder</tt> class does. Lets make one
86change:
87
88<div class="doc_code">
89<pre>
90static LLVMFoldingBuilder Builder;
91</pre>
92</div>
93
94<p>All we did was switch from <tt>LLVMBuilder</tt> to
Chris Lattner41fcea32007-11-13 07:06:30 +000095<tt>LLVMFoldingBuilder</tt>. Though we change no other code, we now have all of our
96instructions implicitly constant folded without us having to do anything
97about it. For example, the input above now compiles to:</p>
Chris Lattner118749e2007-10-25 06:23:36 +000098
99<div class="doc_code">
100<pre>
101ready&gt; <b>def test(x) 1+2+x;</b>
102Read function definition:
103define double @test(double %x) {
104entry:
105 %addtmp = add double 3.000000e+00, %x
106 ret double %addtmp
107}
108</pre>
109</div>
110
Chris Lattnera54c2012007-11-07 05:28:43 +0000111<p>Well, that was easy :). In practice, we recommend always using
Owen Anderson6867aec2007-10-25 06:50:30 +0000112<tt>LLVMFoldingBuilder</tt> when generating code like this. It has no
Chris Lattner118749e2007-10-25 06:23:36 +0000113"syntactic overhead" for its use (you don't have to uglify your compiler with
114constant checks everywhere) and it can dramatically reduce the amount of
115LLVM IR that is generated in some cases (particular for languages with a macro
116preprocessor or that use a lot of constants).</p>
117
118<p>On the other hand, the <tt>LLVMFoldingBuilder</tt> is limited by the fact
119that it does all of its analysis inline with the code as it is built. If you
120take a slightly more complex example:</p>
121
122<div class="doc_code">
123<pre>
124ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
125ready> Read function definition:
126define double @test(double %x) {
127entry:
128 %addtmp = add double 3.000000e+00, %x
129 %addtmp1 = add double %x, 3.000000e+00
130 %multmp = mul double %addtmp, %addtmp1
131 ret double %multmp
132}
133</pre>
134</div>
135
136<p>In this case, the LHS and RHS of the multiplication are the same value. We'd
137really like to see this generate "<tt>tmp = x+3; result = tmp*tmp;</tt>" instead
138of computing "<tt>x*3</tt>" twice.</p>
139
140<p>Unfortunately, no amount of local analysis will be able to detect and correct
141this. This requires two transformations: reassociation of expressions (to
142make the add's lexically identical) and Common Subexpression Elimination (CSE)
143to delete the redundant add instruction. Fortunately, LLVM provides a broad
144range of optimizations that you can use, in the form of "passes".</p>
145
146</div>
147
148<!-- *********************************************************************** -->
149<div class="doc_section"><a name="optimizerpasses">LLVM Optimization
150 Passes</a></div>
151<!-- *********************************************************************** -->
152
153<div class="doc_text">
154
Chris Lattner41fcea32007-11-13 07:06:30 +0000155<p>LLVM provides many optimization passes, which do many different sorts of
Chris Lattner118749e2007-10-25 06:23:36 +0000156things and have different tradeoffs. Unlike other systems, LLVM doesn't hold
157to the mistaken notion that one set of optimizations is right for all languages
158and for all situations. LLVM allows a compiler implementor to make complete
159decisions about what optimizations to use, in which order, and in what
160situation.</p>
161
162<p>As a concrete example, LLVM supports both "whole module" passes, which look
163across as large of body of code as they can (often a whole file, but if run
164at link time, this can be a substantial portion of the whole program). It also
165supports and includes "per-function" passes which just operate on a single
166function at a time, without looking at other functions. For more information
Chris Lattner41fcea32007-11-13 07:06:30 +0000167on passes and how they are run, see the <a href="../WritingAnLLVMPass.html">How
Chris Lattnera54c2012007-11-07 05:28:43 +0000168to Write a Pass</a> document and the <a href="../Passes.html">List of LLVM
169Passes</a>.</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000170
171<p>For Kaleidoscope, we are currently generating functions on the fly, one at
172a time, as the user types them in. We aren't shooting for the ultimate
173optimization experience in this setting, but we also want to catch the easy and
174quick stuff where possible. As such, we will choose to run a few per-function
175optimizations as the user types the function in. If we wanted to make a "static
176Kaleidoscope compiler", we would use exactly the code we have now, except that
177we would defer running the optimizer until the entire file has been parsed.</p>
178
179<p>In order to get per-function optimizations going, we need to set up a
180<a href="../WritingAnLLVMPass.html#passmanager">FunctionPassManager</a> to hold and
181organize the LLVM optimizations that we want to run. Once we have that, we can
182add a set of optimizations to run. The code looks like this:</p>
183
184<div class="doc_code">
185<pre>
186 ExistingModuleProvider OurModuleProvider(TheModule);
187 FunctionPassManager OurFPM(&amp;OurModuleProvider);
188
189 // Set up the optimizer pipeline. Start with registering info about how the
190 // target lays out data structures.
191 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
192 // Do simple "peephole" optimizations and bit-twiddling optzns.
193 OurFPM.add(createInstructionCombiningPass());
194 // Reassociate expressions.
195 OurFPM.add(createReassociatePass());
196 // Eliminate Common SubExpressions.
197 OurFPM.add(createGVNPass());
198 // Simplify the control flow graph (deleting unreachable blocks, etc).
199 OurFPM.add(createCFGSimplificationPass());
200
201 // Set the global so the code gen can use this.
202 TheFPM = &amp;OurFPM;
203
204 // Run the main "interpreter loop" now.
205 MainLoop();
206</pre>
207</div>
208
Chris Lattner41fcea32007-11-13 07:06:30 +0000209<p>This code defines two objects, an <tt>ExistingModuleProvider</tt> and a
Chris Lattner118749e2007-10-25 06:23:36 +0000210<tt>FunctionPassManager</tt>. The former is basically a wrapper around our
211<tt>Module</tt> that the PassManager requires. It provides certain flexibility
Chris Lattner41fcea32007-11-13 07:06:30 +0000212that we're not going to take advantage of here, so I won't dive into any details
213about it.</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000214
Chris Lattner41fcea32007-11-13 07:06:30 +0000215<p>The meat of the matter here, is the definition of "<tt>OurFPM</tt>". It
Chris Lattner118749e2007-10-25 06:23:36 +0000216requires a pointer to the <tt>Module</tt> (through the <tt>ModuleProvider</tt>)
217to construct itself. Once it is set up, we use a series of "add" calls to add
218a bunch of LLVM passes. The first pass is basically boilerplate, it adds a pass
219so that later optimizations know how the data structures in the program are
220layed out. The "<tt>TheExecutionEngine</tt>" variable is related to the JIT,
221which we will get to in the next section.</p>
222
223<p>In this case, we choose to add 4 optimization passes. The passes we chose
224here are a pretty standard set of "cleanup" optimizations that are useful for
Chris Lattner41fcea32007-11-13 07:06:30 +0000225a wide variety of code. I won't delve into what they do but, believe me,
Chris Lattnera54c2012007-11-07 05:28:43 +0000226they are a good starting place :).</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000227
Chris Lattnera54c2012007-11-07 05:28:43 +0000228<p>Once the PassManager is set up, we need to make use of it. We do this by
Chris Lattner118749e2007-10-25 06:23:36 +0000229running it after our newly created function is constructed (in
230<tt>FunctionAST::Codegen</tt>), but before it is returned to the client:</p>
231
232<div class="doc_code">
233<pre>
234 if (Value *RetVal = Body->Codegen()) {
235 // Finish off the function.
236 Builder.CreateRet(RetVal);
237
238 // Validate the generated code, checking for consistency.
239 verifyFunction(*TheFunction);
240
Chris Lattnera54c2012007-11-07 05:28:43 +0000241 <b>// Optimize the function.
242 TheFPM-&gt;run(*TheFunction);</b>
Chris Lattner118749e2007-10-25 06:23:36 +0000243
244 return TheFunction;
245 }
246</pre>
247</div>
248
Chris Lattner41fcea32007-11-13 07:06:30 +0000249<p>As you can see, this is pretty straightforward. The
Chris Lattner118749e2007-10-25 06:23:36 +0000250<tt>FunctionPassManager</tt> optimizes and updates the LLVM Function* in place,
251improving (hopefully) its body. With this in place, we can try our test above
252again:</p>
253
254<div class="doc_code">
255<pre>
256ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
257ready> Read function definition:
258define double @test(double %x) {
259entry:
260 %addtmp = add double %x, 3.000000e+00
261 %multmp = mul double %addtmp, %addtmp
262 ret double %multmp
263}
264</pre>
265</div>
266
267<p>As expected, we now get our nicely optimized code, saving a floating point
Chris Lattnera54c2012007-11-07 05:28:43 +0000268add instruction from every execution of this function.</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000269
270<p>LLVM provides a wide variety of optimizations that can be used in certain
Chris Lattner72714232007-10-25 17:52:39 +0000271circumstances. Some <a href="../Passes.html">documentation about the various
272passes</a> is available, but it isn't very complete. Another good source of
Chris Lattner41fcea32007-11-13 07:06:30 +0000273ideas can come from looking at the passes that <tt>llvm-gcc</tt> or
Chris Lattner118749e2007-10-25 06:23:36 +0000274<tt>llvm-ld</tt> run to get started. The "<tt>opt</tt>" tool allows you to
275experiment with passes from the command line, so you can see if they do
276anything.</p>
277
278<p>Now that we have reasonable code coming out of our front-end, lets talk about
279executing it!</p>
280
281</div>
282
283<!-- *********************************************************************** -->
284<div class="doc_section"><a name="jit">Adding a JIT Compiler</a></div>
285<!-- *********************************************************************** -->
286
287<div class="doc_text">
288
Chris Lattnera54c2012007-11-07 05:28:43 +0000289<p>Code that is available in LLVM IR can have a wide variety of tools
Chris Lattner118749e2007-10-25 06:23:36 +0000290applied to it. For example, you can run optimizations on it (as we did above),
291you can dump it out in textual or binary forms, you can compile the code to an
292assembly file (.s) for some target, or you can JIT compile it. The nice thing
Chris Lattnera54c2012007-11-07 05:28:43 +0000293about the LLVM IR representation is that it is the "common currency" between
294many different parts of the compiler.
Chris Lattner118749e2007-10-25 06:23:36 +0000295</p>
296
Chris Lattnera54c2012007-11-07 05:28:43 +0000297<p>In this section, we'll add JIT compiler support to our interpreter. The
Chris Lattner118749e2007-10-25 06:23:36 +0000298basic idea that we want for Kaleidoscope is to have the user enter function
299bodies as they do now, but immediately evaluate the top-level expressions they
300type in. For example, if they type in "1 + 2;", we should evaluate and print
301out 3. If they define a function, they should be able to call it from the
302command line.</p>
303
304<p>In order to do this, we first declare and initialize the JIT. This is done
305by adding a global variable and a call in <tt>main</tt>:</p>
306
307<div class="doc_code">
308<pre>
Chris Lattnera54c2012007-11-07 05:28:43 +0000309<b>static ExecutionEngine *TheExecutionEngine;</b>
Chris Lattner118749e2007-10-25 06:23:36 +0000310...
311int main() {
312 ..
Chris Lattnera54c2012007-11-07 05:28:43 +0000313 <b>// Create the JIT.
314 TheExecutionEngine = ExecutionEngine::create(TheModule);</b>
Chris Lattner118749e2007-10-25 06:23:36 +0000315 ..
316}
317</pre>
318</div>
319
320<p>This creates an abstract "Execution Engine" which can be either a JIT
321compiler or the LLVM interpreter. LLVM will automatically pick a JIT compiler
322for you if one is available for your platform, otherwise it will fall back to
323the interpreter.</p>
324
325<p>Once the <tt>ExecutionEngine</tt> is created, the JIT is ready to be used.
Chris Lattner41fcea32007-11-13 07:06:30 +0000326There are a variety of APIs that are useful, but the simplest one is the
Chris Lattner118749e2007-10-25 06:23:36 +0000327"<tt>getPointerToFunction(F)</tt>" method. This method JIT compiles the
328specified LLVM Function and returns a function pointer to the generated machine
329code. In our case, this means that we can change the code that parses a
330top-level expression to look like this:</p>
331
332<div class="doc_code">
333<pre>
334static void HandleTopLevelExpression() {
335 // Evaluate a top level expression into an anonymous function.
336 if (FunctionAST *F = ParseTopLevelExpr()) {
337 if (Function *LF = F-&gt;Codegen()) {
338 LF->dump(); // Dump the function for exposition purposes.
339
Chris Lattnera54c2012007-11-07 05:28:43 +0000340 <b>// JIT the function, returning a function pointer.
Chris Lattner118749e2007-10-25 06:23:36 +0000341 void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);
342
343 // Cast it to the right type (takes no arguments, returns a double) so we
344 // can call it as a native function.
345 double (*FP)() = (double (*)())FPtr;
Chris Lattnera54c2012007-11-07 05:28:43 +0000346 fprintf(stderr, "Evaluated to %f\n", FP());</b>
Chris Lattner118749e2007-10-25 06:23:36 +0000347 }
348</pre>
349</div>
350
351<p>Recall that we compile top-level expressions into a self-contained LLVM
352function that takes no arguments and returns the computed double. Because the
353LLVM JIT compiler matches the native platform ABI, this means that you can just
354cast the result pointer to a function pointer of that type and call it directly.
Chris Lattner41fcea32007-11-13 07:06:30 +0000355This means, there is no difference between JIT compiled code and native machine
Chris Lattner118749e2007-10-25 06:23:36 +0000356code that is statically linked into your application.</p>
357
358<p>With just these two changes, lets see how Kaleidoscope works now!</p>
359
360<div class="doc_code">
361<pre>
362ready&gt; <b>4+5;</b>
363define double @""() {
364entry:
365 ret double 9.000000e+00
366}
367
368<em>Evaluated to 9.000000</em>
369</pre>
370</div>
371
372<p>Well this looks like it is basically working. The dump of the function
373shows the "no argument function that always returns double" that we synthesize
Chris Lattner41fcea32007-11-13 07:06:30 +0000374for each top level expression that is typed in. This demonstrates very basic
Chris Lattner118749e2007-10-25 06:23:36 +0000375functionality, but can we do more?</p>
376
377<div class="doc_code">
378<pre>
Chris Lattner2e89f3a2007-10-31 07:30:39 +0000379ready&gt; <b>def testfunc(x y) x + y*2; </b>
Chris Lattner118749e2007-10-25 06:23:36 +0000380Read function definition:
381define double @testfunc(double %x, double %y) {
382entry:
383 %multmp = mul double %y, 2.000000e+00
384 %addtmp = add double %multmp, %x
385 ret double %addtmp
386}
387
388ready&gt; <b>testfunc(4, 10);</b>
389define double @""() {
390entry:
391 %calltmp = call double @testfunc( double 4.000000e+00, double 1.000000e+01 )
392 ret double %calltmp
393}
394
395<em>Evaluated to 24.000000</em>
396</pre>
397</div>
398
Chris Lattner41fcea32007-11-13 07:06:30 +0000399<p>This illustrates that we can now call user code, but there is something a bit subtle
400going on here. Note that we only invoke the JIT on the anonymous functions
401that <em>call testfunc</em>, but we never invoked it on <em>testfunc
402</em>itself.</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000403
Chris Lattner41fcea32007-11-13 07:06:30 +0000404<p>What actually happened here is that the anonymous function was
Chris Lattner118749e2007-10-25 06:23:36 +0000405JIT'd when requested. When the Kaleidoscope app calls through the function
406pointer that is returned, the anonymous function starts executing. It ends up
Chris Lattnera54c2012007-11-07 05:28:43 +0000407making the call to the "testfunc" function, and ends up in a stub that invokes
Chris Lattner118749e2007-10-25 06:23:36 +0000408the JIT, lazily, on testfunc. Once the JIT finishes lazily compiling testfunc,
Chris Lattnera54c2012007-11-07 05:28:43 +0000409it returns and the code re-executes the call.</p>
Chris Lattner118749e2007-10-25 06:23:36 +0000410
Chris Lattner41fcea32007-11-13 07:06:30 +0000411<p>In summary, the JIT will lazily JIT code, on the fly, as it is needed. The
Chris Lattner118749e2007-10-25 06:23:36 +0000412JIT provides a number of other more advanced interfaces for things like freeing
413allocated machine code, rejit'ing functions to update them, etc. However, even
414with this simple code, we get some surprisingly powerful capabilities - check
415this out (I removed the dump of the anonymous functions, you should get the idea
416by now :) :</p>
417
418<div class="doc_code">
419<pre>
420ready&gt; <b>extern sin(x);</b>
421Read extern:
422declare double @sin(double)
423
424ready&gt; <b>extern cos(x);</b>
425Read extern:
426declare double @cos(double)
427
428ready&gt; <b>sin(1.0);</b>
429<em>Evaluated to 0.841471</em>
Chris Lattner72714232007-10-25 17:52:39 +0000430
Chris Lattner118749e2007-10-25 06:23:36 +0000431ready&gt; <b>def foo(x) sin(x)*sin(x) + cos(x)*cos(x);</b>
432Read function definition:
433define double @foo(double %x) {
434entry:
435 %calltmp = call double @sin( double %x )
436 %multmp = mul double %calltmp, %calltmp
437 %calltmp2 = call double @cos( double %x )
438 %multmp4 = mul double %calltmp2, %calltmp2
439 %addtmp = add double %multmp, %multmp4
440 ret double %addtmp
441}
442
443ready&gt; <b>foo(4.0);</b>
444<em>Evaluated to 1.000000</em>
445</pre>
446</div>
447
Chris Lattnera54c2012007-11-07 05:28:43 +0000448<p>Whoa, how does the JIT know about sin and cos? The answer is surprisingly
449simple: in this
Chris Lattner118749e2007-10-25 06:23:36 +0000450example, the JIT started execution of a function and got to a function call. It
451realized that the function was not yet JIT compiled and invoked the standard set
452of routines to resolve the function. In this case, there is no body defined
Chris Lattnera54c2012007-11-07 05:28:43 +0000453for the function, so the JIT ended up calling "<tt>dlsym("sin")</tt>" on the
454Kaleidoscope process itself.
Chris Lattner118749e2007-10-25 06:23:36 +0000455Since "<tt>sin</tt>" is defined within the JIT's address space, it simply
456patches up calls in the module to call the libm version of <tt>sin</tt>
457directly.</p>
458
459<p>The LLVM JIT provides a number of interfaces (look in the
460<tt>ExecutionEngine.h</tt> file) for controlling how unknown functions get
461resolved. It allows you to establish explicit mappings between IR objects and
462addresses (useful for LLVM global variables that you want to map to static
463tables, for example), allows you to dynamically decide on the fly based on the
464function name, and even allows you to have the JIT abort itself if any lazy
465compilation is attempted.</p>
466
Chris Lattner72714232007-10-25 17:52:39 +0000467<p>One interesting application of this is that we can now extend the language
468by writing arbitrary C++ code to implement operations. For example, if we add:
469</p>
470
471<div class="doc_code">
472<pre>
473/// putchard - putchar that takes a double and returns 0.
474extern "C"
475double putchard(double X) {
476 putchar((char)X);
477 return 0;
478}
479</pre>
480</div>
481
482<p>Now we can produce simple output to the console by using things like:
483"<tt>extern putchard(x); putchard(120);</tt>", which prints a lowercase 'x' on
Chris Lattnera54c2012007-11-07 05:28:43 +0000484the console (120 is the ASCII code for 'x'). Similar code could be used to
Chris Lattner72714232007-10-25 17:52:39 +0000485implement file I/O, console input, and many other capabilities in
486Kaleidoscope.</p>
487
Chris Lattner118749e2007-10-25 06:23:36 +0000488<p>This completes the JIT and optimizer chapter of the Kaleidoscope tutorial. At
489this point, we can compile a non-Turing-complete programming language, optimize
490and JIT compile it in a user-driven way. Next up we'll look into <a
491href="LangImpl5.html">extending the language with control flow constructs</a>,
492tackling some interesting LLVM IR issues along the way.</p>
493
494</div>
495
496<!-- *********************************************************************** -->
497<div class="doc_section"><a name="code">Full Code Listing</a></div>
498<!-- *********************************************************************** -->
499
500<div class="doc_text">
501
502<p>
503Here is the complete code listing for our running example, enhanced with the
504LLVM JIT and optimizer. To build this example, use:
505</p>
506
507<div class="doc_code">
508<pre>
509 # Compile
510 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
511 # Run
512 ./toy
513</pre>
514</div>
515
516<p>Here is the code:</p>
517
518<div class="doc_code">
519<pre>
520#include "llvm/DerivedTypes.h"
521#include "llvm/ExecutionEngine/ExecutionEngine.h"
522#include "llvm/Module.h"
523#include "llvm/ModuleProvider.h"
524#include "llvm/PassManager.h"
525#include "llvm/Analysis/Verifier.h"
526#include "llvm/Target/TargetData.h"
527#include "llvm/Transforms/Scalar.h"
528#include "llvm/Support/LLVMBuilder.h"
529#include &lt;cstdio&gt;
530#include &lt;string&gt;
531#include &lt;map&gt;
532#include &lt;vector&gt;
533using namespace llvm;
534
535//===----------------------------------------------------------------------===//
536// Lexer
537//===----------------------------------------------------------------------===//
538
539// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
540// of these for known things.
541enum Token {
542 tok_eof = -1,
543
544 // commands
545 tok_def = -2, tok_extern = -3,
546
547 // primary
548 tok_identifier = -4, tok_number = -5,
549};
550
551static std::string IdentifierStr; // Filled in if tok_identifier
552static double NumVal; // Filled in if tok_number
553
554/// gettok - Return the next token from standard input.
555static int gettok() {
556 static int LastChar = ' ';
557
558 // Skip any whitespace.
559 while (isspace(LastChar))
560 LastChar = getchar();
561
562 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
563 IdentifierStr = LastChar;
564 while (isalnum((LastChar = getchar())))
565 IdentifierStr += LastChar;
566
567 if (IdentifierStr == "def") return tok_def;
568 if (IdentifierStr == "extern") return tok_extern;
569 return tok_identifier;
570 }
571
572 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
573 std::string NumStr;
574 do {
575 NumStr += LastChar;
576 LastChar = getchar();
577 } while (isdigit(LastChar) || LastChar == '.');
578
579 NumVal = strtod(NumStr.c_str(), 0);
580 return tok_number;
581 }
582
583 if (LastChar == '#') {
584 // Comment until end of line.
585 do LastChar = getchar();
Chris Lattnerc80c23f2007-12-02 22:46:01 +0000586 while (LastChar != EOF &amp;&amp; LastChar != '\n' &amp;&amp; LastChar != '\r');
Chris Lattner118749e2007-10-25 06:23:36 +0000587
588 if (LastChar != EOF)
589 return gettok();
590 }
591
592 // Check for end of file. Don't eat the EOF.
593 if (LastChar == EOF)
594 return tok_eof;
595
596 // Otherwise, just return the character as its ascii value.
597 int ThisChar = LastChar;
598 LastChar = getchar();
599 return ThisChar;
600}
601
602//===----------------------------------------------------------------------===//
603// Abstract Syntax Tree (aka Parse Tree)
604//===----------------------------------------------------------------------===//
605
Chris Lattnerc0b42e92007-10-23 06:27:55 +0000606/// ExprAST - Base class for all expression nodes.
607class ExprAST {
608public:
609 virtual ~ExprAST() {}
610 virtual Value *Codegen() = 0;
611};
612
613/// NumberExprAST - Expression class for numeric literals like "1.0".
614class NumberExprAST : public ExprAST {
615 double Val;
616public:
Chris Lattner118749e2007-10-25 06:23:36 +0000617 NumberExprAST(double val) : Val(val) {}
Chris Lattnerc0b42e92007-10-23 06:27:55 +0000618 virtual Value *Codegen();
619};
Chris Lattner118749e2007-10-25 06:23:36 +0000620
621/// VariableExprAST - Expression class for referencing a variable, like "a".
622class VariableExprAST : public ExprAST {
623 std::string Name;
624public:
625 VariableExprAST(const std::string &amp;name) : Name(name) {}
626 virtual Value *Codegen();
627};
628
629/// BinaryExprAST - Expression class for a binary operator.
630class BinaryExprAST : public ExprAST {
631 char Op;
632 ExprAST *LHS, *RHS;
633public:
634 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
635 : Op(op), LHS(lhs), RHS(rhs) {}
636 virtual Value *Codegen();
637};
638
639/// CallExprAST - Expression class for function calls.
640class CallExprAST : public ExprAST {
641 std::string Callee;
642 std::vector&lt;ExprAST*&gt; Args;
643public:
644 CallExprAST(const std::string &amp;callee, std::vector&lt;ExprAST*&gt; &amp;args)
645 : Callee(callee), Args(args) {}
646 virtual Value *Codegen();
647};
648
649/// PrototypeAST - This class represents the "prototype" for a function,
650/// which captures its argument names as well as if it is an operator.
651class PrototypeAST {
652 std::string Name;
653 std::vector&lt;std::string&gt; Args;
654public:
655 PrototypeAST(const std::string &amp;name, const std::vector&lt;std::string&gt; &amp;args)
656 : Name(name), Args(args) {}
657
658 Function *Codegen();
659};
660
661/// FunctionAST - This class represents a function definition itself.
662class FunctionAST {
663 PrototypeAST *Proto;
664 ExprAST *Body;
665public:
666 FunctionAST(PrototypeAST *proto, ExprAST *body)
667 : Proto(proto), Body(body) {}
668
669 Function *Codegen();
670};
671
672//===----------------------------------------------------------------------===//
673// Parser
674//===----------------------------------------------------------------------===//
675
676/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
677/// token the parser it looking at. getNextToken reads another token from the
678/// lexer and updates CurTok with its results.
679static int CurTok;
680static int getNextToken() {
681 return CurTok = gettok();
682}
683
684/// BinopPrecedence - This holds the precedence for each binary operator that is
685/// defined.
686static std::map&lt;char, int&gt; BinopPrecedence;
687
688/// GetTokPrecedence - Get the precedence of the pending binary operator token.
689static int GetTokPrecedence() {
690 if (!isascii(CurTok))
691 return -1;
692
693 // Make sure it's a declared binop.
694 int TokPrec = BinopPrecedence[CurTok];
695 if (TokPrec &lt;= 0) return -1;
696 return TokPrec;
697}
698
699/// Error* - These are little helper functions for error handling.
700ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
701PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
702FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
703
704static ExprAST *ParseExpression();
705
706/// identifierexpr
Chris Lattner20a0c802007-11-05 17:54:34 +0000707/// ::= identifier
708/// ::= identifier '(' expression* ')'
Chris Lattner118749e2007-10-25 06:23:36 +0000709static ExprAST *ParseIdentifierExpr() {
710 std::string IdName = IdentifierStr;
711
Chris Lattner20a0c802007-11-05 17:54:34 +0000712 getNextToken(); // eat identifier.
Chris Lattner118749e2007-10-25 06:23:36 +0000713
714 if (CurTok != '(') // Simple variable ref.
715 return new VariableExprAST(IdName);
716
717 // Call.
718 getNextToken(); // eat (
719 std::vector&lt;ExprAST*&gt; Args;
Chris Lattner71155212007-11-06 01:39:12 +0000720 if (CurTok != ')') {
721 while (1) {
722 ExprAST *Arg = ParseExpression();
723 if (!Arg) return 0;
724 Args.push_back(Arg);
Chris Lattner118749e2007-10-25 06:23:36 +0000725
Chris Lattner71155212007-11-06 01:39:12 +0000726 if (CurTok == ')') break;
Chris Lattner118749e2007-10-25 06:23:36 +0000727
Chris Lattner71155212007-11-06 01:39:12 +0000728 if (CurTok != ',')
729 return Error("Expected ')'");
730 getNextToken();
731 }
Chris Lattner118749e2007-10-25 06:23:36 +0000732 }
733
734 // Eat the ')'.
735 getNextToken();
736
737 return new CallExprAST(IdName, Args);
738}
739
740/// numberexpr ::= number
741static ExprAST *ParseNumberExpr() {
742 ExprAST *Result = new NumberExprAST(NumVal);
743 getNextToken(); // consume the number
744 return Result;
745}
746
747/// parenexpr ::= '(' expression ')'
748static ExprAST *ParseParenExpr() {
749 getNextToken(); // eat (.
750 ExprAST *V = ParseExpression();
751 if (!V) return 0;
752
753 if (CurTok != ')')
754 return Error("expected ')'");
755 getNextToken(); // eat ).
756 return V;
757}
758
759/// primary
760/// ::= identifierexpr
761/// ::= numberexpr
762/// ::= parenexpr
763static ExprAST *ParsePrimary() {
764 switch (CurTok) {
765 default: return Error("unknown token when expecting an expression");
766 case tok_identifier: return ParseIdentifierExpr();
767 case tok_number: return ParseNumberExpr();
768 case '(': return ParseParenExpr();
769 }
770}
771
772/// binoprhs
773/// ::= ('+' primary)*
774static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
775 // If this is a binop, find its precedence.
776 while (1) {
777 int TokPrec = GetTokPrecedence();
778
779 // If this is a binop that binds at least as tightly as the current binop,
780 // consume it, otherwise we are done.
781 if (TokPrec &lt; ExprPrec)
782 return LHS;
783
784 // Okay, we know this is a binop.
785 int BinOp = CurTok;
786 getNextToken(); // eat binop
787
788 // Parse the primary expression after the binary operator.
789 ExprAST *RHS = ParsePrimary();
790 if (!RHS) return 0;
791
792 // If BinOp binds less tightly with RHS than the operator after RHS, let
793 // the pending operator take RHS as its LHS.
794 int NextPrec = GetTokPrecedence();
795 if (TokPrec &lt; NextPrec) {
796 RHS = ParseBinOpRHS(TokPrec+1, RHS);
797 if (RHS == 0) return 0;
798 }
799
800 // Merge LHS/RHS.
801 LHS = new BinaryExprAST(BinOp, LHS, RHS);
802 }
803}
804
805/// expression
806/// ::= primary binoprhs
807///
808static ExprAST *ParseExpression() {
809 ExprAST *LHS = ParsePrimary();
810 if (!LHS) return 0;
811
812 return ParseBinOpRHS(0, LHS);
813}
814
815/// prototype
816/// ::= id '(' id* ')'
817static PrototypeAST *ParsePrototype() {
818 if (CurTok != tok_identifier)
819 return ErrorP("Expected function name in prototype");
820
821 std::string FnName = IdentifierStr;
822 getNextToken();
823
824 if (CurTok != '(')
825 return ErrorP("Expected '(' in prototype");
826
827 std::vector&lt;std::string&gt; ArgNames;
828 while (getNextToken() == tok_identifier)
829 ArgNames.push_back(IdentifierStr);
830 if (CurTok != ')')
831 return ErrorP("Expected ')' in prototype");
832
833 // success.
834 getNextToken(); // eat ')'.
835
836 return new PrototypeAST(FnName, ArgNames);
837}
838
839/// definition ::= 'def' prototype expression
840static FunctionAST *ParseDefinition() {
841 getNextToken(); // eat def.
842 PrototypeAST *Proto = ParsePrototype();
843 if (Proto == 0) return 0;
844
845 if (ExprAST *E = ParseExpression())
846 return new FunctionAST(Proto, E);
847 return 0;
848}
849
850/// toplevelexpr ::= expression
851static FunctionAST *ParseTopLevelExpr() {
852 if (ExprAST *E = ParseExpression()) {
853 // Make an anonymous proto.
854 PrototypeAST *Proto = new PrototypeAST("", std::vector&lt;std::string&gt;());
855 return new FunctionAST(Proto, E);
856 }
857 return 0;
858}
859
860/// external ::= 'extern' prototype
861static PrototypeAST *ParseExtern() {
862 getNextToken(); // eat extern.
863 return ParsePrototype();
864}
865
866//===----------------------------------------------------------------------===//
867// Code Generation
868//===----------------------------------------------------------------------===//
869
870static Module *TheModule;
871static LLVMFoldingBuilder Builder;
872static std::map&lt;std::string, Value*&gt; NamedValues;
873static FunctionPassManager *TheFPM;
874
875Value *ErrorV(const char *Str) { Error(Str); return 0; }
876
877Value *NumberExprAST::Codegen() {
878 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
879}
880
881Value *VariableExprAST::Codegen() {
882 // Look this variable up in the function.
883 Value *V = NamedValues[Name];
884 return V ? V : ErrorV("Unknown variable name");
885}
886
887Value *BinaryExprAST::Codegen() {
888 Value *L = LHS-&gt;Codegen();
889 Value *R = RHS-&gt;Codegen();
890 if (L == 0 || R == 0) return 0;
891
892 switch (Op) {
893 case '+': return Builder.CreateAdd(L, R, "addtmp");
894 case '-': return Builder.CreateSub(L, R, "subtmp");
895 case '*': return Builder.CreateMul(L, R, "multmp");
896 case '&lt;':
Chris Lattner71155212007-11-06 01:39:12 +0000897 L = Builder.CreateFCmpULT(L, R, "cmptmp");
Chris Lattner118749e2007-10-25 06:23:36 +0000898 // Convert bool 0/1 to double 0.0 or 1.0
899 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
900 default: return ErrorV("invalid binary operator");
901 }
902}
903
904Value *CallExprAST::Codegen() {
905 // Look up the name in the global module table.
906 Function *CalleeF = TheModule-&gt;getFunction(Callee);
907 if (CalleeF == 0)
908 return ErrorV("Unknown function referenced");
909
910 // If argument mismatch error.
911 if (CalleeF-&gt;arg_size() != Args.size())
912 return ErrorV("Incorrect # arguments passed");
913
914 std::vector&lt;Value*&gt; ArgsV;
915 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
916 ArgsV.push_back(Args[i]-&gt;Codegen());
917 if (ArgsV.back() == 0) return 0;
918 }
919
920 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
921}
922
923Function *PrototypeAST::Codegen() {
924 // Make the function type: double(double,double) etc.
925 std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::DoubleTy);
926 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
927
928 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
929
930 // If F conflicted, there was already something named 'Name'. If it has a
931 // body, don't allow redefinition or reextern.
932 if (F-&gt;getName() != Name) {
933 // Delete the one we just made and get the existing one.
934 F-&gt;eraseFromParent();
935 F = TheModule-&gt;getFunction(Name);
936
937 // If F already has a body, reject this.
938 if (!F-&gt;empty()) {
939 ErrorF("redefinition of function");
940 return 0;
941 }
942
943 // If F took a different number of args, reject.
944 if (F-&gt;arg_size() != Args.size()) {
945 ErrorF("redefinition of function with different # args");
946 return 0;
947 }
948 }
949
950 // Set names for all arguments.
951 unsigned Idx = 0;
952 for (Function::arg_iterator AI = F-&gt;arg_begin(); Idx != Args.size();
953 ++AI, ++Idx) {
954 AI-&gt;setName(Args[Idx]);
955
956 // Add arguments to variable symbol table.
957 NamedValues[Args[Idx]] = AI;
958 }
959
960 return F;
961}
962
963Function *FunctionAST::Codegen() {
964 NamedValues.clear();
965
966 Function *TheFunction = Proto-&gt;Codegen();
967 if (TheFunction == 0)
968 return 0;
969
970 // Create a new basic block to start insertion into.
971 BasicBlock *BB = new BasicBlock("entry", TheFunction);
972 Builder.SetInsertPoint(BB);
973
974 if (Value *RetVal = Body-&gt;Codegen()) {
975 // Finish off the function.
976 Builder.CreateRet(RetVal);
977
978 // Validate the generated code, checking for consistency.
979 verifyFunction(*TheFunction);
980
981 // Optimize the function.
982 TheFPM-&gt;run(*TheFunction);
983
984 return TheFunction;
985 }
986
987 // Error reading body, remove function.
988 TheFunction-&gt;eraseFromParent();
989 return 0;
990}
991
992//===----------------------------------------------------------------------===//
993// Top-Level parsing and JIT Driver
994//===----------------------------------------------------------------------===//
995
996static ExecutionEngine *TheExecutionEngine;
997
998static void HandleDefinition() {
999 if (FunctionAST *F = ParseDefinition()) {
1000 if (Function *LF = F-&gt;Codegen()) {
1001 fprintf(stderr, "Read function definition:");
1002 LF-&gt;dump();
1003 }
1004 } else {
1005 // Skip token for error recovery.
1006 getNextToken();
1007 }
1008}
1009
1010static void HandleExtern() {
1011 if (PrototypeAST *P = ParseExtern()) {
1012 if (Function *F = P-&gt;Codegen()) {
1013 fprintf(stderr, "Read extern: ");
1014 F-&gt;dump();
1015 }
1016 } else {
1017 // Skip token for error recovery.
1018 getNextToken();
1019 }
1020}
1021
1022static void HandleTopLevelExpression() {
1023 // Evaluate a top level expression into an anonymous function.
1024 if (FunctionAST *F = ParseTopLevelExpr()) {
1025 if (Function *LF = F-&gt;Codegen()) {
1026 // JIT the function, returning a function pointer.
1027 void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);
1028
1029 // Cast it to the right type (takes no arguments, returns a double) so we
1030 // can call it as a native function.
1031 double (*FP)() = (double (*)())FPtr;
1032 fprintf(stderr, "Evaluated to %f\n", FP());
1033 }
1034 } else {
1035 // Skip token for error recovery.
1036 getNextToken();
1037 }
1038}
1039
1040/// top ::= definition | external | expression | ';'
1041static void MainLoop() {
1042 while (1) {
1043 fprintf(stderr, "ready&gt; ");
1044 switch (CurTok) {
1045 case tok_eof: return;
1046 case ';': getNextToken(); break; // ignore top level semicolons.
1047 case tok_def: HandleDefinition(); break;
1048 case tok_extern: HandleExtern(); break;
1049 default: HandleTopLevelExpression(); break;
1050 }
1051 }
1052}
1053
1054
1055
1056//===----------------------------------------------------------------------===//
1057// "Library" functions that can be "extern'd" from user code.
1058//===----------------------------------------------------------------------===//
1059
1060/// putchard - putchar that takes a double and returns 0.
1061extern "C"
1062double putchard(double X) {
1063 putchar((char)X);
1064 return 0;
1065}
1066
1067//===----------------------------------------------------------------------===//
1068// Main driver code.
1069//===----------------------------------------------------------------------===//
1070
1071int main() {
1072 // Install standard binary operators.
1073 // 1 is lowest precedence.
1074 BinopPrecedence['&lt;'] = 10;
1075 BinopPrecedence['+'] = 20;
1076 BinopPrecedence['-'] = 20;
1077 BinopPrecedence['*'] = 40; // highest.
1078
1079 // Prime the first token.
1080 fprintf(stderr, "ready&gt; ");
1081 getNextToken();
1082
1083 // Make the module, which holds all the code.
1084 TheModule = new Module("my cool jit");
1085
1086 // Create the JIT.
1087 TheExecutionEngine = ExecutionEngine::create(TheModule);
1088
1089 {
1090 ExistingModuleProvider OurModuleProvider(TheModule);
1091 FunctionPassManager OurFPM(&amp;OurModuleProvider);
1092
1093 // Set up the optimizer pipeline. Start with registering info about how the
1094 // target lays out data structures.
1095 OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
1096 // Do simple "peephole" optimizations and bit-twiddling optzns.
1097 OurFPM.add(createInstructionCombiningPass());
1098 // Reassociate expressions.
1099 OurFPM.add(createReassociatePass());
1100 // Eliminate Common SubExpressions.
1101 OurFPM.add(createGVNPass());
1102 // Simplify the control flow graph (deleting unreachable blocks, etc).
1103 OurFPM.add(createCFGSimplificationPass());
1104
1105 // Set the global so the code gen can use this.
1106 TheFPM = &amp;OurFPM;
1107
1108 // Run the main "interpreter loop" now.
1109 MainLoop();
1110
1111 TheFPM = 0;
Chris Lattner515686b2008-02-05 06:18:42 +00001112
1113 // Print out all of the generated code.
1114 TheModule-&gt;dump();
1115 } // Free module provider (and thus the module) and pass manager.
Chris Lattner118749e2007-10-25 06:23:36 +00001116
Chris Lattner118749e2007-10-25 06:23:36 +00001117 return 0;
1118}
Chris Lattnerc0b42e92007-10-23 06:27:55 +00001119</pre>
1120</div>
1121
Chris Lattner729eb142008-02-10 19:11:04 +00001122<a href="LangImpl5.html">Next: Extending the language: control flow</a>
Chris Lattnerc0b42e92007-10-23 06:27:55 +00001123</div>
1124
1125<!-- *********************************************************************** -->
1126<hr>
1127<address>
1128 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1129 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1130 <a href="http://validator.w3.org/check/referer"><img
Chris Lattner8eef4b22007-10-23 06:30:50 +00001131 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
Chris Lattnerc0b42e92007-10-23 06:27:55 +00001132
1133 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1134 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1135 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $
1136</address>
1137</body>
1138</html>