FAQ.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ================================
  2. Frequently Asked Questions (FAQ)
  3. ================================
  4. .. contents::
  5. :local:
  6. License
  7. =======
  8. Does the University of Illinois Open Source License really qualify as an "open source" license?
  9. -----------------------------------------------------------------------------------------------
  10. Yes, the license is `certified
  11. <http://www.opensource.org/licenses/UoI-NCSA.php>`_ by the Open Source
  12. Initiative (OSI).
  13. Can I modify LLVM source code and redistribute the modified source?
  14. -------------------------------------------------------------------
  15. Yes. The modified source distribution must retain the copyright notice and
  16. follow the three bulleted conditions listed in the `LLVM license
  17. <http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT>`_.
  18. Can I modify the LLVM source code and redistribute binaries or other tools based on it, without redistributing the source?
  19. --------------------------------------------------------------------------------------------------------------------------
  20. Yes. This is why we distribute LLVM under a less restrictive license than GPL,
  21. as explained in the first question above.
  22. Source Code
  23. ===========
  24. In what language is LLVM written?
  25. ---------------------------------
  26. All of the LLVM tools and libraries are written in C++ with extensive use of
  27. the STL.
  28. How portable is the LLVM source code?
  29. -------------------------------------
  30. The LLVM source code should be portable to most modern Unix-like operating
  31. systems. Most of the code is written in standard C++ with operating system
  32. services abstracted to a support library. The tools required to build and
  33. test LLVM have been ported to a plethora of platforms.
  34. What API do I use to store a value to one of the virtual registers in LLVM IR's SSA representation?
  35. ---------------------------------------------------------------------------------------------------
  36. In short: you can't. It's actually kind of a silly question once you grok
  37. what's going on. Basically, in code like:
  38. .. code-block:: llvm
  39. %result = add i32 %foo, %bar
  40. , ``%result`` is just a name given to the ``Value`` of the ``add``
  41. instruction. In other words, ``%result`` *is* the add instruction. The
  42. "assignment" doesn't explicitly "store" anything to any "virtual register";
  43. the "``=``" is more like the mathematical sense of equality.
  44. Longer explanation: In order to generate a textual representation of the
  45. IR, some kind of name has to be given to each instruction so that other
  46. instructions can textually reference it. However, the isomorphic in-memory
  47. representation that you manipulate from C++ has no such restriction since
  48. instructions can simply keep pointers to any other ``Value``'s that they
  49. reference. In fact, the names of dummy numbered temporaries like ``%1`` are
  50. not explicitly represented in the in-memory representation at all (see
  51. ``Value::getName()``).
  52. Source Languages
  53. ================
  54. What source languages are supported?
  55. ------------------------------------
  56. LLVM currently has full support for C and C++ source languages through
  57. `Clang <http://clang.llvm.org/>`_. Many other language frontends have
  58. been written using LLVM, and an incomplete list is available at
  59. `projects with LLVM <http://llvm.org/ProjectsWithLLVM/>`_.
  60. I'd like to write a self-hosting LLVM compiler. How should I interface with the LLVM middle-end optimizers and back-end code generators?
  61. ----------------------------------------------------------------------------------------------------------------------------------------
  62. Your compiler front-end will communicate with LLVM by creating a module in the
  63. LLVM intermediate representation (IR) format. Assuming you want to write your
  64. language's compiler in the language itself (rather than C++), there are 3
  65. major ways to tackle generating LLVM IR from a front-end:
  66. 1. **Call into the LLVM libraries code using your language's FFI (foreign
  67. function interface).**
  68. * *for:* best tracks changes to the LLVM IR, .ll syntax, and .bc format
  69. * *for:* enables running LLVM optimization passes without a emit/parse
  70. overhead
  71. * *for:* adapts well to a JIT context
  72. * *against:* lots of ugly glue code to write
  73. 2. **Emit LLVM assembly from your compiler's native language.**
  74. * *for:* very straightforward to get started
  75. * *against:* the .ll parser is slower than the bitcode reader when
  76. interfacing to the middle end
  77. * *against:* it may be harder to track changes to the IR
  78. 3. **Emit LLVM bitcode from your compiler's native language.**
  79. * *for:* can use the more-efficient bitcode reader when interfacing to the
  80. middle end
  81. * *against:* you'll have to re-engineer the LLVM IR object model and bitcode
  82. writer in your language
  83. * *against:* it may be harder to track changes to the IR
  84. If you go with the first option, the C bindings in include/llvm-c should help
  85. a lot, since most languages have strong support for interfacing with C. The
  86. most common hurdle with calling C from managed code is interfacing with the
  87. garbage collector. The C interface was designed to require very little memory
  88. management, and so is straightforward in this regard.
  89. What support is there for a higher level source language constructs for building a compiler?
  90. --------------------------------------------------------------------------------------------
  91. Currently, there isn't much. LLVM supports an intermediate representation
  92. which is useful for code representation but will not support the high level
  93. (abstract syntax tree) representation needed by most compilers. There are no
  94. facilities for lexical nor semantic analysis.
  95. I don't understand the ``GetElementPtr`` instruction. Help!
  96. -----------------------------------------------------------
  97. See `The Often Misunderstood GEP Instruction <GetElementPtr.html>`_.
  98. Using the C and C++ Front Ends
  99. ==============================
  100. Can I compile C or C++ code to platform-independent LLVM bitcode?
  101. -----------------------------------------------------------------
  102. No. C and C++ are inherently platform-dependent languages. The most obvious
  103. example of this is the preprocessor. A very common way that C code is made
  104. portable is by using the preprocessor to include platform-specific code. In
  105. practice, information about other platforms is lost after preprocessing, so
  106. the result is inherently dependent on the platform that the preprocessing was
  107. targeting.
  108. Another example is ``sizeof``. It's common for ``sizeof(long)`` to vary
  109. between platforms. In most C front-ends, ``sizeof`` is expanded to a
  110. constant immediately, thus hard-wiring a platform-specific detail.
  111. Also, since many platforms define their ABIs in terms of C, and since LLVM is
  112. lower-level than C, front-ends currently must emit platform-specific IR in
  113. order to have the result conform to the platform ABI.
  114. Questions about code generated by the demo page
  115. ===============================================
  116. What is this ``llvm.global_ctors`` and ``_GLOBAL__I_a...`` stuff that happens when I ``#include <iostream>``?
  117. -------------------------------------------------------------------------------------------------------------
  118. If you ``#include`` the ``<iostream>`` header into a C++ translation unit,
  119. the file will probably use the ``std::cin``/``std::cout``/... global objects.
  120. However, C++ does not guarantee an order of initialization between static
  121. objects in different translation units, so if a static ctor/dtor in your .cpp
  122. file used ``std::cout``, for example, the object would not necessarily be
  123. automatically initialized before your use.
  124. To make ``std::cout`` and friends work correctly in these scenarios, the STL
  125. that we use declares a static object that gets created in every translation
  126. unit that includes ``<iostream>``. This object has a static constructor
  127. and destructor that initializes and destroys the global iostream objects
  128. before they could possibly be used in the file. The code that you see in the
  129. ``.ll`` file corresponds to the constructor and destructor registration code.
  130. If you would like to make it easier to *understand* the LLVM code generated
  131. by the compiler in the demo page, consider using ``printf()`` instead of
  132. ``iostream``\s to print values.
  133. Where did all of my code go??
  134. -----------------------------
  135. If you are using the LLVM demo page, you may often wonder what happened to
  136. all of the code that you typed in. Remember that the demo script is running
  137. the code through the LLVM optimizers, so if your code doesn't actually do
  138. anything useful, it might all be deleted.
  139. To prevent this, make sure that the code is actually needed. For example, if
  140. you are computing some expression, return the value from the function instead
  141. of leaving it in a local variable. If you really want to constrain the
  142. optimizer, you can read from and assign to ``volatile`` global variables.
  143. What is this "``undef``" thing that shows up in my code?
  144. --------------------------------------------------------
  145. ``undef`` is the LLVM way of representing a value that is not defined. You
  146. can get these if you do not initialize a variable before you use it. For
  147. example, the C function:
  148. .. code-block:: c
  149. int X() { int i; return i; }
  150. Is compiled to "``ret i32 undef``" because "``i``" never has a value specified
  151. for it.
  152. Why does instcombine + simplifycfg turn a call to a function with a mismatched calling convention into "unreachable"? Why not make the verifier reject it?
  153. ----------------------------------------------------------------------------------------------------------------------------------------------------------
  154. This is a common problem run into by authors of front-ends that are using
  155. custom calling conventions: you need to make sure to set the right calling
  156. convention on both the function and on each call to the function. For
  157. example, this code:
  158. .. code-block:: llvm
  159. define fastcc void @foo() {
  160. ret void
  161. }
  162. define void @bar() {
  163. call void @foo()
  164. ret void
  165. }
  166. Is optimized to:
  167. .. code-block:: llvm
  168. define fastcc void @foo() {
  169. ret void
  170. }
  171. define void @bar() {
  172. unreachable
  173. }
  174. ... with "``opt -instcombine -simplifycfg``". This often bites people because
  175. "all their code disappears". Setting the calling convention on the caller and
  176. callee is required for indirect calls to work, so people often ask why not
  177. make the verifier reject this sort of thing.
  178. The answer is that this code has undefined behavior, but it is not illegal.
  179. If we made it illegal, then every transformation that could potentially create
  180. this would have to ensure that it doesn't, and there is valid code that can
  181. create this sort of construct (in dead code). The sorts of things that can
  182. cause this to happen are fairly contrived, but we still need to accept them.
  183. Here's an example:
  184. .. code-block:: llvm
  185. define fastcc void @foo() {
  186. ret void
  187. }
  188. define internal void @bar(void()* %FP, i1 %cond) {
  189. br i1 %cond, label %T, label %F
  190. T:
  191. call void %FP()
  192. ret void
  193. F:
  194. call fastcc void %FP()
  195. ret void
  196. }
  197. define void @test() {
  198. %X = or i1 false, false
  199. call void @bar(void()* @foo, i1 %X)
  200. ret void
  201. }
  202. In this example, "test" always passes ``@foo``/``false`` into ``bar``, which
  203. ensures that it is dynamically called with the right calling conv (thus, the
  204. code is perfectly well defined). If you run this through the inliner, you
  205. get this (the explicit "or" is there so that the inliner doesn't dead code
  206. eliminate a bunch of stuff):
  207. .. code-block:: llvm
  208. define fastcc void @foo() {
  209. ret void
  210. }
  211. define void @test() {
  212. %X = or i1 false, false
  213. br i1 %X, label %T.i, label %F.i
  214. T.i:
  215. call void @foo()
  216. br label %bar.exit
  217. F.i:
  218. call fastcc void @foo()
  219. br label %bar.exit
  220. bar.exit:
  221. ret void
  222. }
  223. Here you can see that the inlining pass made an undefined call to ``@foo``
  224. with the wrong calling convention. We really don't want to make the inliner
  225. have to know about this sort of thing, so it needs to be valid code. In this
  226. case, dead code elimination can trivially remove the undefined code. However,
  227. if ``%X`` was an input argument to ``@test``, the inliner would produce this:
  228. .. code-block:: llvm
  229. define fastcc void @foo() {
  230. ret void
  231. }
  232. define void @test(i1 %X) {
  233. br i1 %X, label %T.i, label %F.i
  234. T.i:
  235. call void @foo()
  236. br label %bar.exit
  237. F.i:
  238. call fastcc void @foo()
  239. br label %bar.exit
  240. bar.exit:
  241. ret void
  242. }
  243. The interesting thing about this is that ``%X`` *must* be false for the
  244. code to be well-defined, but no amount of dead code elimination will be able
  245. to delete the broken call as unreachable. However, since
  246. ``instcombine``/``simplifycfg`` turns the undefined call into unreachable, we
  247. end up with a branch on a condition that goes to unreachable: a branch to
  248. unreachable can never happen, so "``-inline -instcombine -simplifycfg``" is
  249. able to produce:
  250. .. code-block:: llvm
  251. define fastcc void @foo() {
  252. ret void
  253. }
  254. define void @test(i1 %X) {
  255. F.i:
  256. call fastcc void @foo()
  257. ret void
  258. }