1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM Link Time Optimization: design and implementation</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
9 <div class="doc_title">
10 LLVM Link Time Optimization: design and implentation
14 <li><a href="#desc">Description</a></li>
15 <li><a href="#design">Design Philosophy</a>
17 <li><a href="#example1">Example of link time optimization</a></li>
18 <li><a href="#alternative_approaches">Alternative Approaches</a></li>
20 <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a></li>
22 <li><a href="#phase1">Phase 1 : Read LLVM Bytecode Files</a></li>
23 <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
24 <li><a href="#phase3">Phase 3 : Optimize Bytecode Files</a></li>
25 <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
27 <li><a href="#lto">LLVMlto</a></li>
29 <li><a href="#llvmsymbol">LLVMSymbol</a></li>
30 <li><a href="#readllvmobjectfile">readLLVMObjectFile()</a></li>
31 <li><a href="#optimizemodules">optimizeModules()</a></li>
33 <li><a href="#debug">Debugging Information</a></li>
36 <div class="doc_author">
37 <p>Written by Devang Patel</a></p>
40 <!-- *********************************************************************** -->
41 <div class="doc_section">
42 <a name="desc">Description</a>
44 <!-- *********************************************************************** -->
46 <div class="doc_text">
48 LLVM features powerful intermodular optimization which can be used at link time.
49 Link Time Optimization is another name of intermodular optimization when it
50 is done during link stage. This document describes the interface between LLVM
51 intermodular optimizer and the linker and its design.
55 <!-- *********************************************************************** -->
56 <div class="doc_section">
57 <a name="design">Design Philosophy</a>
59 <!-- *********************************************************************** -->
61 <div class="doc_text">
63 The LLVM Link Time Optimizer seeks complete transparency, while doing intermodular
64 optimization, in compiler tool chain. Its main goal is to let developer take
65 advantage of intermodular optimizer without making any significant changes to
66 their makefiles or build system. This is achieved through tight integration with
67 linker. In this model, linker treates LLVM bytecode files like native objects
68 file and allows mixing and matching among them. The linker uses
69 <a href="#lto">LLVMlto</a>, a dynamically loaded library, to handle LLVM bytecode
70 files. This tight integration between the linker and LLVM optimizer helps to do
71 optimizations that are not possible in other models. The linker input allows
72 optimizer to avoid relying on conservative escape analysis.
75 <!-- ======================================================================= -->
76 <div class="doc_subsection">
77 <a name="example1">Example of link time optimization</a>
80 <div class="doc_text">
82 <p>Following example illustrates advantage of integrated approach that uses
84 <li> Input source file <tt>a.c</tt> is compiled into LLVM byte code form.
85 <li> Input source file <tt>main.c</tt> is compiled into native object code.
89 <br>extern int foo1(void);
90 <br>extern void foo2(void);
91 <br>extern void foo4(void);
95 <br>static signed int i = 0;
101 <br>static int foo3() {
109 <br>if (i < 0) { data = foo3(); }
111 <br>data = data + 42;
116 <br>#include <stdio.h>
119 <br>void foo4(void) {
120 <br> printf ("Hi\n");
127 <br>--- command lines ---
128 <br> $ llvm-gcc4 --emit-llvm -c a.c -o a.o # <-- a.o is LLVM bytecode file
129 <br> $ llvm-gcc4 -c main.c -o main.o # <-- main.o is native object file
130 <br> $ llvm-gcc4 a.o main.o -o main # <-- standard link command without any modifications
135 In this example, the linker recognizes that <tt>foo2()</tt> is a externally visible
136 symbol defined in LLVM byte code file. This information is collected using
137 <a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>. Based on this
138 information, linker completes its usual symbol resolution pass and finds that
139 <tt>foo2()</tt> is not used anywhere. This information is used by LLVM optimizer
140 and it removes <tt>foo2()</tt>. As soon as <tt>foo2()</tt> is removed, optimizer
141 recognizes that condition <tt> i < 0 </tt> is always false, which means
142 <tt>foo3()</tt> is never used. Hence, optimizer removes <tt>foo3()</tt> also.
143 And this in turn, enables linker to remove <tt>foo4()</tt>.
144 This example illustrates advantage of tight integration with linker. Here,
145 optimizer can not remove <tt>foo3()</tt> without the linker's input.
149 <!-- ======================================================================= -->
150 <div class="doc_subsection">
151 <a name="alternative_approaches">Alternative Approaches</a>
154 <div class="doc_text">
156 <li> Compiler driver invokes link time optimizer separately.
157 <br><br>In this model link time optimizer is not able to take advantage of information
158 collected during normal linker's symbol resolution phase. In above example,
159 optimizer can not remove <tt>foo2()</tt> without linker's input because it is
160 externally visible. And this in turn prohibits optimizer from removing <tt>foo3()</tt>.
162 <li> Use separate tool to collect symbol information from all object file.
163 <br><br>In this model, this new separate tool or library replicates linker's
164 capabilities to collect information for link time optimizer. Not only such code
165 duplication is difficult to justify but it also has several other disadvantages.
166 For example, the linking semantics and the features provided by linker on
167 various platform are not unique. This means, this new tool needs to support all
168 such features and platforms in one super tool or one new separate tool per
169 platform is required. This increases maintance cost for link time optimizer
170 significantly, which is not necessary. Plus, this approach requires staying
171 synchronized with linker developements on various platforms, which is not the
172 main focus of link time optimizer. Finally, this approach increases end user's build
173 time due to duplicate work done by this separate tool and linker itself.
177 <!-- *********************************************************************** -->
178 <div class="doc_section">
179 <a name="multiphase">Multi-phase communication between LLVM and linker</a>
182 <div class="doc_text">
184 The linker collects information about symbol defininitions and uses in various
185 link objects which is more accurate than any information collected by other tools
186 during typical build cycle.
187 The linker collects this information by looking at definitions and uses of
188 symbols in native .o files and using symbol visibility information. The linker
189 also uses user supplied information, such as list of exported symbol.
190 LLVM optimizer collects control flow information, data flow information and
191 knows much more about program structure from optimizer's point of view. Our
192 goal is to take advantage of tight intergration between the linker and
193 optimizer by sharing this information during various linking phases.
197 <!-- ======================================================================= -->
198 <div class="doc_subsection">
199 <a name="phase1">Phase 1 : Read LLVM Bytecode Files</a>
202 <div class="doc_text">
204 The linker first reads all object files in natural order and collects symbol
205 information. This includes native object files as well as LLVM byte code files.
206 In this phase, the linker uses <a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>
207 to collect symbol information from each LLVM bytecode files and updates its
208 internal global symbol table accordingly. The intent of this interface is to
209 avoid overhead in the non LLVM case, where all input object files are native
210 object files, by putting this code in the error path of the linker. When the
211 linker sees the first llvm .o file, it dlopen()s the dynamic library. This is
212 to allow changes to LLVM part without relinking the linker.
216 <!-- ======================================================================= -->
217 <div class="doc_subsection">
218 <a name="phase2">Phase 2 : Symbol Resolution</a>
221 <div class="doc_text">
223 In this stage, the linker resolves symbols using global symbol table information
224 to report undefined symbol errors, read archive members, resolve weak
225 symbols etc... The linker is able to do this seamlessly even though it does not
226 know exact content of input LLVM bytecode files because it uses symbol information
227 provided by <a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>.
228 If dead code stripping is enabled then linker collects list of live symbols.
232 <!-- ======================================================================= -->
233 <div class="doc_subsection">
234 <a name="phase3">Phase 3 : Optimize Bytecode Files</a>
236 <div class="doc_text">
238 After symbol resolution, the linker updates symbol information supplied by LLVM
239 bytecode files appropriately. For example, whether certain LLVM bytecode
240 supplied symbols are used or not. In the example above, the linker reports
241 that <tt>foo2()</tt> is not used anywhere in the program, including native .o
242 files. This information is used by LLVM interprocedural optimizer. The
243 linker uses <a href="#optimizemodules"> optimizeModules()</a> and requests
244 optimized native object file of the LLVM portion of the program.
248 <!-- ======================================================================= -->
249 <div class="doc_subsection">
250 <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
253 <div class="doc_text">
255 In this phase, the linker reads optimized native object file and updates internal
256 global symbol table to reflect any changes. Linker also collects information
257 about any change in use of external symbols by LLVM bytecode files. In the examle
258 above, the linker notes that <tt>foo4()</tt> is not used any more. If dead code
259 striping is enabled then linker refreshes live symbol information appropriately
260 and performs dead code stripping.
262 After this phase, the linker continues linking as if it never saw LLVM bytecode
267 <!-- *********************************************************************** -->
268 <div class="doc_section">
269 <a name="lto">LLVMlto</a>
272 <div class="doc_text">
274 <tt>LLVMlto</tt> is a dynamic library that is part of the LLVM tools, and is
275 intended for use by a linker. <tt>LLVMlto</tt> provides an abstract C++ interface
276 to use the LLVM interprocedural optimizer without exposing details of LLVM
277 internals. The intention is to keep the interface as stable as possible even
278 when the LLVM optimizer continues to evolve.
282 <!-- ======================================================================= -->
283 <div class="doc_subsection">
284 <a name="llvmsymbol">LLVMSymbol</a>
287 <div class="doc_text">
289 <tt>LLVMSymbol</tt> class is used to describe the externally visible functions
290 and global variables, tdefined in LLVM bytecode files, to linker.
291 This includes symbol visibility information. This information is used by linker
292 to do symbol resolution. For example : function <tt>foo2()</tt> is defined inside
293 a LLVM bytecode module and it is externally visible symbol.
294 This helps linker connect use of <tt>foo2()</tt> in native object file with
295 future definition of symbol <tt>foo2()</tt>. The linker will see actual definition
296 of <tt>foo2()</tt> when it receives optimized native object file in <a href="#phase4">
297 Symbol Resolution after optimization</a> phase. If the linker does not find any
298 use of <tt>foo2()</tt>, it updates LLVMSymbol visibility information to notify
299 LLVM intermodular optimizer that it is dead. The LLVM intermodular optimizer
300 takes advantage of such information to generate better code.
304 <!-- ======================================================================= -->
305 <div class="doc_subsection">
306 <a name="readllvmobjectfile">readLLVMObjectFile()</a>
309 <div class="doc_text">
311 <tt>readLLVMObjectFile()</tt> is used by the linker to read LLVM bytecode files
312 and collect LLVMSymbol nformation. This routine also
313 supplies list of externally defined symbols that are used by LLVM bytecode
314 files. Linker uses this symbol information to do symbol resolution. Internally,
315 <a href="#lto">LLVMlto</a> maintains LLVM bytecode modules in memory. This
316 function also provides list of external references used by bytecode file.<br>
320 <!-- ======================================================================= -->
321 <div class="doc_subsection">
322 <a name="optimizemodules">optimizeModules()</a>
325 <div class="doc_text">
327 The linker invokes <tt>optimizeModules</tt> to optimize already read LLVM
328 bytecode files by applying LLVM intermodular optimization techniques. This
329 function runs LLVM intermodular optimizer and generates native object code
330 as .o file at name and location provided by the linker.
334 <!-- *********************************************************************** -->
335 <div class="doc_section">
336 <a name="debug">Debugging Information</a>
338 <!-- *********************************************************************** -->
340 <div class="doc_text">
342 <p><tt> ... incomplete ... </tt></p>
346 <!-- *********************************************************************** -->
350 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
351 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
352 <a href="http://validator.w3.org/check/referer"><img
353 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
356 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
357 Last modified: $Date$