LangImpl03.rst 22 KB

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