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