blob: b4c6261c65d8010ff215669691b440b544458a58 [file] [log] [blame]
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +00001.. _faq:
2
3================================
4Frequently Asked Questions (FAQ)
5================================
6
7.. contents::
8 :local:
9
10
11License
12=======
13
14Does the University of Illinois Open Source License really qualify as an "open source" license?
15-----------------------------------------------------------------------------------------------
16Yes, the license is `certified
17<http://www.opensource.org/licenses/UoI-NCSA.php>`_ by the Open Source
18Initiative (OSI).
19
20
21Can I modify LLVM source code and redistribute the modified source?
22-------------------------------------------------------------------
23Yes. The modified source distribution must retain the copyright notice and
24follow the three bulletted conditions listed in the `LLVM license
25<http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT>`_.
26
27
28Can I modify the LLVM source code and redistribute binaries or other tools based on it, without redistributing the source?
29--------------------------------------------------------------------------------------------------------------------------
30Yes. This is why we distribute LLVM under a less restrictive license than GPL,
31as explained in the first question above.
32
33
34Source Code
35===========
36
37In what language is LLVM written?
38---------------------------------
39All of the LLVM tools and libraries are written in C++ with extensive use of
40the STL.
41
42
43How portable is the LLVM source code?
44-------------------------------------
45The LLVM source code should be portable to most modern Unix-like operating
46systems. Most of the code is written in standard C++ with operating system
47services abstracted to a support library. The tools required to build and
48test LLVM have been ported to a plethora of platforms.
49
50Some porting problems may exist in the following areas:
51
52* The autoconf/makefile build system relies heavily on UNIX shell tools,
53 like the Bourne Shell and sed. Porting to systems without these tools
54 (MacOS 9, Plan 9) will require more effort.
55
Sean Silvabdb0c0a2012-12-27 10:23:04 +000056What API do I use to store a value to one of the virtual registers in LLVM IR's SSA representation?
57---------------------------------------------------------------------------------------------------
58
59In short: you can't. It's actually kind of a silly question once you grok
60what's going on. Basically, in code like:
61
62.. code-block:: llvm
63
64 %result = add i32 %foo, %bar
65
66, ``%result`` is just a name given to the ``Value`` of the ``add``
67instruction. In other words, ``%result`` *is* the add instruction. The
68"assignment" doesn't explicitly "store" anything to any "virtual register";
69the "``=``" is more like the mathematical sense of equality.
70
71Longer explanation: In order to generate a textual representation of the
72IR, some kind of name has to be given to each instruction so that other
73instructions can textually reference it. However, the isomorphic in-memory
74representation that you manipulate from C++ has no such restriction since
75instructions can simply keep pointers to any other ``Value``'s that they
76reference. In fact, the names of dummy numbered temporaries like ``%1`` are
77not explicitly represented in the in-memory representation at all (see
78``Value::getName()``).
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +000079
80Build Problems
81==============
82
83When I run configure, it finds the wrong C compiler.
84----------------------------------------------------
85The ``configure`` script attempts to locate first ``gcc`` and then ``cc``,
86unless it finds compiler paths set in ``CC`` and ``CXX`` for the C and C++
87compiler, respectively.
88
89If ``configure`` finds the wrong compiler, either adjust your ``PATH``
90environment variable or set ``CC`` and ``CXX`` explicitly.
91
92
93The ``configure`` script finds the right C compiler, but it uses the LLVM tools from a previous build. What do I do?
94---------------------------------------------------------------------------------------------------------------------
95The ``configure`` script uses the ``PATH`` to find executables, so if it's
96grabbing the wrong linker/assembler/etc, there are two ways to fix it:
97
98#. Adjust your ``PATH`` environment variable so that the correct program
99 appears first in the ``PATH``. This may work, but may not be convenient
100 when you want them *first* in your path for other work.
101
102#. Run ``configure`` with an alternative ``PATH`` that is correct. In a
103 Bourne compatible shell, the syntax would be:
104
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000105.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000106
107 % PATH=[the path without the bad program] ./configure ...
108
109This is still somewhat inconvenient, but it allows ``configure`` to do its
110work without having to adjust your ``PATH`` permanently.
111
112
113When creating a dynamic library, I get a strange GLIBC error.
114-------------------------------------------------------------
115Under some operating systems (i.e. Linux), libtool does not work correctly if
116GCC was compiled with the ``--disable-shared option``. To work around this,
117install your own version of GCC that has shared libraries enabled by default.
118
119
120I've updated my source tree from Subversion, and now my build is trying to use a file/directory that doesn't exist.
121-------------------------------------------------------------------------------------------------------------------
122You need to re-run configure in your object directory. When new Makefiles
123are added to the source tree, they have to be copied over to the object tree
124in order to be used by the build.
125
126
127I've modified a Makefile in my source tree, but my build tree keeps using the old version. What do I do?
128---------------------------------------------------------------------------------------------------------
129If the Makefile already exists in your object tree, you can just run the
130following command in the top level directory of your object tree:
131
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000132.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000133
134 % ./config.status <relative path to Makefile>;
135
136If the Makefile is new, you will have to modify the configure script to copy
137it over.
138
139
140I've upgraded to a new version of LLVM, and I get strange build errors.
141-----------------------------------------------------------------------
142Sometimes, changes to the LLVM source code alters how the build system works.
143Changes in ``libtool``, ``autoconf``, or header file dependencies are
144especially prone to this sort of problem.
145
146The best thing to try is to remove the old files and re-build. In most cases,
147this takes care of the problem. To do this, just type ``make clean`` and then
148``make`` in the directory that fails to build.
149
150
151I've built LLVM and am testing it, but the tests freeze.
152--------------------------------------------------------
153This is most likely occurring because you built a profile or release
154(optimized) build of LLVM and have not specified the same information on the
155``gmake`` command line.
156
157For example, if you built LLVM with the command:
158
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000159.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000160
161 % gmake ENABLE_PROFILING=1
162
163...then you must run the tests with the following commands:
164
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000165.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000166
167 % cd llvm/test
168 % gmake ENABLE_PROFILING=1
169
170Why do test results differ when I perform different types of builds?
171--------------------------------------------------------------------
172The LLVM test suite is dependent upon several features of the LLVM tools and
173libraries.
174
175First, the debugging assertions in code are not enabled in optimized or
176profiling builds. Hence, tests that used to fail may pass.
177
178Second, some tests may rely upon debugging options or behavior that is only
179available in the debug build. These tests will fail in an optimized or
180profile build.
181
182
183Compiling LLVM with GCC 3.3.2 fails, what should I do?
184------------------------------------------------------
185This is `a bug in GCC <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392>`_,
186and affects projects other than LLVM. Try upgrading or downgrading your GCC.
187
188
189Compiling LLVM with GCC succeeds, but the resulting tools do not work, what can be wrong?
190-----------------------------------------------------------------------------------------
191Several versions of GCC have shown a weakness in miscompiling the LLVM
192codebase. Please consult your compiler version (``gcc --version``) to find
193out whether it is `broken <GettingStarted.html#brokengcc>`_. If so, your only
194option is to upgrade GCC to a known good version.
195
196
197After Subversion update, rebuilding gives the error "No rule to make target".
198-----------------------------------------------------------------------------
199If the error is of the form:
200
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000201.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000202
203 gmake[2]: *** No rule to make target `/path/to/somefile',
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000204 needed by `/path/to/another/file.d'.
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000205 Stop.
206
207This may occur anytime files are moved within the Subversion repository or
208removed entirely. In this case, the best solution is to erase all ``.d``
209files, which list dependencies for source files, and rebuild:
210
Dmitri Gribenko527036d2012-12-12 13:56:37 +0000211.. code-block:: console
Michael J. Spencer0ed5cf42012-06-18 20:21:38 +0000212
213 % cd $LLVM_OBJ_DIR
214 % rm -f `find . -name \*\.d`
215 % gmake
216
217In other cases, it may be necessary to run ``make clean`` before rebuilding.
218
219
220Source Languages
221================
222
223What source languages are supported?
224------------------------------------
225LLVM currently has full support for C and C++ source languages. These are
226available through both `Clang <http://clang.llvm.org/>`_ and `DragonEgg
227<http://dragonegg.llvm.org/>`_.
228
229The PyPy developers are working on integrating LLVM into the PyPy backend so
230that PyPy language can translate to LLVM.
231
232
233I'd like to write a self-hosting LLVM compiler. How should I interface with the LLVM middle-end optimizers and back-end code generators?
234----------------------------------------------------------------------------------------------------------------------------------------
235Your compiler front-end will communicate with LLVM by creating a module in the
236LLVM intermediate representation (IR) format. Assuming you want to write your
237language's compiler in the language itself (rather than C++), there are 3
238major ways to tackle generating LLVM IR from a front-end:
239
2401. **Call into the LLVM libraries code using your language's FFI (foreign
241 function interface).**
242
243 * *for:* best tracks changes to the LLVM IR, .ll syntax, and .bc format
244
245 * *for:* enables running LLVM optimization passes without a emit/parse
246 overhead
247
248 * *for:* adapts well to a JIT context
249
250 * *against:* lots of ugly glue code to write
251
2522. **Emit LLVM assembly from your compiler's native language.**
253
254 * *for:* very straightforward to get started
255
256 * *against:* the .ll parser is slower than the bitcode reader when
257 interfacing to the middle end
258
259 * *against:* it may be harder to track changes to the IR
260
2613. **Emit LLVM bitcode from your compiler's native language.**
262
263 * *for:* can use the more-efficient bitcode reader when interfacing to the
264 middle end
265
266 * *against:* you'll have to re-engineer the LLVM IR object model and bitcode
267 writer in your language
268
269 * *against:* it may be harder to track changes to the IR
270
271If you go with the first option, the C bindings in include/llvm-c should help
272a lot, since most languages have strong support for interfacing with C. The
273most common hurdle with calling C from managed code is interfacing with the
274garbage collector. The C interface was designed to require very little memory
275management, and so is straightforward in this regard.
276
277What support is there for a higher level source language constructs for building a compiler?
278--------------------------------------------------------------------------------------------
279Currently, there isn't much. LLVM supports an intermediate representation
280which is useful for code representation but will not support the high level
281(abstract syntax tree) representation needed by most compilers. There are no
282facilities for lexical nor semantic analysis.
283
284
285I don't understand the ``GetElementPtr`` instruction. Help!
286-----------------------------------------------------------
287See `The Often Misunderstood GEP Instruction <GetElementPtr.html>`_.
288
289
290Using the C and C++ Front Ends
291==============================
292
293Can I compile C or C++ code to platform-independent LLVM bitcode?
294-----------------------------------------------------------------
295No. C and C++ are inherently platform-dependent languages. The most obvious
296example of this is the preprocessor. A very common way that C code is made
297portable is by using the preprocessor to include platform-specific code. In
298practice, information about other platforms is lost after preprocessing, so
299the result is inherently dependent on the platform that the preprocessing was
300targeting.
301
302Another example is ``sizeof``. It's common for ``sizeof(long)`` to vary
303between platforms. In most C front-ends, ``sizeof`` is expanded to a
304constant immediately, thus hard-wiring a platform-specific detail.
305
306Also, since many platforms define their ABIs in terms of C, and since LLVM is
307lower-level than C, front-ends currently must emit platform-specific IR in
308order to have the result conform to the platform ABI.
309
310
311Questions about code generated by the demo page
312===============================================
313
314What is this ``llvm.global_ctors`` and ``_GLOBAL__I_a...`` stuff that happens when I ``#include <iostream>``?
315-------------------------------------------------------------------------------------------------------------
316If you ``#include`` the ``<iostream>`` header into a C++ translation unit,
317the file will probably use the ``std::cin``/``std::cout``/... global objects.
318However, C++ does not guarantee an order of initialization between static
319objects in different translation units, so if a static ctor/dtor in your .cpp
320file used ``std::cout``, for example, the object would not necessarily be
321automatically initialized before your use.
322
323To make ``std::cout`` and friends work correctly in these scenarios, the STL
324that we use declares a static object that gets created in every translation
325unit that includes ``<iostream>``. This object has a static constructor
326and destructor that initializes and destroys the global iostream objects
327before they could possibly be used in the file. The code that you see in the
328``.ll`` file corresponds to the constructor and destructor registration code.
329
330If you would like to make it easier to *understand* the LLVM code generated
331by the compiler in the demo page, consider using ``printf()`` instead of
332``iostream``\s to print values.
333
334
335Where did all of my code go??
336-----------------------------
337If you are using the LLVM demo page, you may often wonder what happened to
338all of the code that you typed in. Remember that the demo script is running
339the code through the LLVM optimizers, so if your code doesn't actually do
340anything useful, it might all be deleted.
341
342To prevent this, make sure that the code is actually needed. For example, if
343you are computing some expression, return the value from the function instead
344of leaving it in a local variable. If you really want to constrain the
345optimizer, you can read from and assign to ``volatile`` global variables.
346
347
348What is this "``undef``" thing that shows up in my code?
349--------------------------------------------------------
350``undef`` is the LLVM way of representing a value that is not defined. You
351can get these if you do not initialize a variable before you use it. For
352example, the C function:
353
354.. code-block:: c
355
356 int X() { int i; return i; }
357
358Is compiled to "``ret i32 undef``" because "``i``" never has a value specified
359for it.
360
361
362Why does instcombine + simplifycfg turn a call to a function with a mismatched calling convention into "unreachable"? Why not make the verifier reject it?
363----------------------------------------------------------------------------------------------------------------------------------------------------------
364This is a common problem run into by authors of front-ends that are using
365custom calling conventions: you need to make sure to set the right calling
366convention on both the function and on each call to the function. For
367example, this code:
368
369.. code-block:: llvm
370
371 define fastcc void @foo() {
372 ret void
373 }
374 define void @bar() {
375 call void @foo()
376 ret void
377 }
378
379Is optimized to:
380
381.. code-block:: llvm
382
383 define fastcc void @foo() {
384 ret void
385 }
386 define void @bar() {
387 unreachable
388 }
389
390... with "``opt -instcombine -simplifycfg``". This often bites people because
391"all their code disappears". Setting the calling convention on the caller and
392callee is required for indirect calls to work, so people often ask why not
393make the verifier reject this sort of thing.
394
395The answer is that this code has undefined behavior, but it is not illegal.
396If we made it illegal, then every transformation that could potentially create
397this would have to ensure that it doesn't, and there is valid code that can
398create this sort of construct (in dead code). The sorts of things that can
399cause this to happen are fairly contrived, but we still need to accept them.
400Here's an example:
401
402.. code-block:: llvm
403
404 define fastcc void @foo() {
405 ret void
406 }
407 define internal void @bar(void()* %FP, i1 %cond) {
408 br i1 %cond, label %T, label %F
409 T:
410 call void %FP()
411 ret void
412 F:
413 call fastcc void %FP()
414 ret void
415 }
416 define void @test() {
417 %X = or i1 false, false
418 call void @bar(void()* @foo, i1 %X)
419 ret void
420 }
421
422In this example, "test" always passes ``@foo``/``false`` into ``bar``, which
423ensures that it is dynamically called with the right calling conv (thus, the
424code is perfectly well defined). If you run this through the inliner, you
425get this (the explicit "or" is there so that the inliner doesn't dead code
426eliminate a bunch of stuff):
427
428.. code-block:: llvm
429
430 define fastcc void @foo() {
431 ret void
432 }
433 define void @test() {
434 %X = or i1 false, false
435 br i1 %X, label %T.i, label %F.i
436 T.i:
437 call void @foo()
438 br label %bar.exit
439 F.i:
440 call fastcc void @foo()
441 br label %bar.exit
442 bar.exit:
443 ret void
444 }
445
446Here you can see that the inlining pass made an undefined call to ``@foo``
447with the wrong calling convention. We really don't want to make the inliner
448have to know about this sort of thing, so it needs to be valid code. In this
449case, dead code elimination can trivially remove the undefined code. However,
450if ``%X`` was an input argument to ``@test``, the inliner would produce this:
451
452.. code-block:: llvm
453
454 define fastcc void @foo() {
455 ret void
456 }
457
458 define void @test(i1 %X) {
459 br i1 %X, label %T.i, label %F.i
460 T.i:
461 call void @foo()
462 br label %bar.exit
463 F.i:
464 call fastcc void @foo()
465 br label %bar.exit
466 bar.exit:
467 ret void
468 }
469
470The interesting thing about this is that ``%X`` *must* be false for the
471code to be well-defined, but no amount of dead code elimination will be able
472to delete the broken call as unreachable. However, since
473``instcombine``/``simplifycfg`` turns the undefined call into unreachable, we
474end up with a branch on a condition that goes to unreachable: a branch to
475unreachable can never happen, so "``-inline -instcombine -simplifycfg``" is
476able to produce:
477
478.. code-block:: llvm
479
480 define fastcc void @foo() {
481 ret void
482 }
483 define void @test(i1 %X) {
484 F.i:
485 call fastcc void @foo()
486 ret void
487 }