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 Implementation
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>
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>
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</p>
40 <!-- *********************************************************************** -->
41 <div class="doc_section">
42 <a name="desc">Description</a>
44 <!-- *********************************************************************** -->
46 <div class="doc_text">
48 LLVM features powerful intermodular optimizations which can be used at link
49 time. Link Time Optimization is another name for intermodular optimization
50 when performed during the link stage. This document describes the interface
51 and design between the LLVM intermodular optimizer and the linker.</p>
54 <!-- *********************************************************************** -->
55 <div class="doc_section">
56 <a name="design">Design Philosophy</a>
58 <!-- *********************************************************************** -->
60 <div class="doc_text">
62 The LLVM Link Time Optimizer provides complete transparency, while doing
63 intermodular optimization, in the compiler tool chain. Its main goal is to let
64 the developer take advantage of intermodular optimizations without making any
65 significant changes to the developer's makefiles or build system. This is
66 achieved through tight integration with the linker. In this model, the linker
67 treates LLVM bytecode files like native object files and allows mixing and
68 matching among them. The linker uses <a href="#lto">LLVMlto</a>, a dynamically
69 loaded library, to handle LLVM bytecode files. This tight integration between
70 the linker and LLVM optimizer helps to do optimizations that are not possible
71 in other models. The linker input allows the optimizer to avoid relying on
72 conservative escape analysis.
76 <!-- ======================================================================= -->
77 <div class="doc_subsection">
78 <a name="example1">Example of link time optimization</a>
81 <div class="doc_text">
82 <p>The following example illustrates the advantages of LTO's integrated
83 approach and clean interface.</p>
85 <li> Input source file <tt>a.c</tt> is compiled into LLVM byte code form.
86 <li> Input source file <tt>main.c</tt> is compiled into native object code.
90 extern int foo1(void);
91 extern void foo2(void);
92 extern void foo4(void);
96 static signed int i = 0;
110 if (i < 0) { data = foo3(); }
117 #include <stdio.h>
128 --- command lines ---
129 $ llvm-gcc4 --emit-llvm -c a.c -o a.o # <-- a.o is LLVM bytecode file
130 $ llvm-gcc4 -c main.c -o main.o # <-- main.o is native object file
131 $ llvm-gcc4 a.o main.o -o main # <-- standard link command without any modifications
133 <p>In this example, the linker recognizes that <tt>foo2()</tt> is an
134 externally visible symbol defined in LLVM byte code file. This information
135 is collected using <a href="#readllvmobjectfile"> readLLVMObjectFile()</a>.
136 Based on this information, the linker completes its usual symbol resolution
137 pass and finds that <tt>foo2()</tt> is not used anywhere. This information
138 is used by the LLVM optimizer and it removes <tt>foo2()</tt>. As soon as
139 <tt>foo2()</tt> is removed, the optimizer recognizes that condition
140 <tt>i < 0</tt> is always false, which means <tt>foo3()</tt> is never
141 used. Hence, the optimizer removes <tt>foo3()</tt>, also. And this in turn,
142 enables linker to remove <tt>foo4()</tt>. This example illustrates the
143 advantage of tight integration with the linker. Here, the optimizer can not
144 remove <tt>foo3()</tt> without the linker's input.
148 <!-- ======================================================================= -->
149 <div class="doc_subsection">
150 <a name="alternative_approaches">Alternative Approaches</a>
153 <div class="doc_text">
155 <dt><b>Compiler driver invokes link time optimizer separately.</b></dt>
156 <dd>In this model the link time optimizer is not able to take advantage of
157 information collected during the linker's normal symbol resolution phase.
158 In the above example, the optimizer can not remove <tt>foo2()</tt> without
159 the linker's input because it is externally visible. This in turn prohibits
160 the optimizer from removing <tt>foo3()</tt>.</dd>
161 <dt><b>Use separate tool to collect symbol information from all object
163 <dd>In this model, a new, separate, tool or library replicates the linker's
164 capability to collect information for link time optimization. Not only is
165 this code duplication difficult to justify, but it also has several other
166 disadvantages. For example, the linking semantics and the features
167 provided by the linker on various platform are not unique. This means,
168 this new tool needs to support all such features and platforms in one
169 super tool or a separate tool per platform is required. This increases
170 maintance cost for link time optimizer significantly, which is not
171 necessary. This approach also requires staying synchronized with linker
172 developements on various platforms, which is not the main focus of the link
173 time optimizer. Finally, this approach increases end user's build time due
174 to the duplication of work done by this separate tool and the linker itself.
179 <!-- *********************************************************************** -->
180 <div class="doc_section">
181 <a name="multiphase">Multi-phase communication between LLVM and linker</a>
184 <div class="doc_text">
185 <p>The linker collects information about symbol defininitions and uses in
186 various link objects which is more accurate than any information collected
187 by other tools during typical build cycles. The linker collects this
188 information by looking at the definitions and uses of symbols in native .o
189 files and using symbol visibility information. The linker also uses
190 user-supplied information, such as a list of exported symbols. LLVM
191 optimizer collects control flow information, data flow information and knows
192 much more about program structure from the optimizer's point of view.
193 Our goal is to take advantage of tight intergration between the linker and
194 the optimizer by sharing this information during various linking phases.
198 <!-- ======================================================================= -->
199 <div class="doc_subsection">
200 <a name="phase1">Phase 1 : Read LLVM Bytecode Files</a>
203 <div class="doc_text">
204 <p>The linker first reads all object files in natural order and collects
205 symbol information. This includes native object files as well as LLVM byte
206 code files. In this phase, the linker uses
207 <a href="#readllvmobjectfile"> readLLVMObjectFile() </a> to collect symbol
208 information from each LLVM bytecode files and updates its internal global
209 symbol table accordingly. The intent of this interface is to avoid overhead
210 in the non LLVM case, where all input object files are native object files,
211 by putting this code in the error path of the linker. When the linker sees
212 the first llvm .o file, it <tt>dlopen()</tt>s the dynamic library. This is
213 to allow changes to the LLVM LTO code without relinking the linker.
217 <!-- ======================================================================= -->
218 <div class="doc_subsection">
219 <a name="phase2">Phase 2 : Symbol Resolution</a>
222 <div class="doc_text">
223 <p>In this stage, the linker resolves symbols using global symbol table
224 information to report undefined symbol errors, read archive members, resolve
225 weak symbols, etc. The linker is able to do this seamlessly even though it
226 does not know the exact content of input LLVM bytecode files because it uses
227 symbol information provided by
228 <a href="#readllvmobjectfile">readLLVMObjectFile()</a>. If dead code
229 stripping is enabled then the linker collects the list of live symbols.
233 <!-- ======================================================================= -->
234 <div class="doc_subsection">
235 <a name="phase3">Phase 3 : Optimize Bytecode Files</a>
237 <div class="doc_text">
238 <p>After symbol resolution, the linker updates symbol information supplied
239 by LLVM bytecode files appropriately. For example, whether certain LLVM
240 bytecode supplied symbols are used or not. In the example above, the linker
241 reports that <tt>foo2()</tt> is not used anywhere in the program, including
242 native <tt>.o</tt> files. This information is used by the LLVM interprocedural
243 optimizer. The linker uses <a href="#optimizemodules">optimizeModules()</a>
244 and requests an optimized native object file of the LLVM portion of the
249 <!-- ======================================================================= -->
250 <div class="doc_subsection">
251 <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
254 <div class="doc_text">
255 <p>In this phase, the linker reads optimized a native object file and
256 updates the internal global symbol table to reflect any changes. The linker
257 also collects information about any changes in use of external symbols by
258 LLVM bytecode files. In the examle above, the linker notes that
259 <tt>foo4()</tt> is not used any more. If dead code stripping is enabled then
260 the linker refreshes the live symbol information appropriately and performs
261 dead code stripping.</p>
262 <p>After this phase, the linker continues linking as if it never saw LLVM
266 <!-- *********************************************************************** -->
267 <div class="doc_section">
268 <a name="lto">LLVMlto</a>
271 <div class="doc_text">
272 <p><tt>LLVMlto</tt> is a dynamic library that is part of the LLVM tools, and
273 is intended for use by a linker. <tt>LLVMlto</tt> provides an abstract C++
274 interface to use the LLVM interprocedural optimizer without exposing details
275 of LLVM's internals. The intention is to keep the interface as stable as
276 possible even when the LLVM optimizer continues to evolve.</p>
279 <!-- ======================================================================= -->
280 <div class="doc_subsection">
281 <a name="llvmsymbol">LLVMSymbol</a>
284 <div class="doc_text">
285 <p>The <tt>LLVMSymbol</tt> class is used to describe the externally visible
286 functions and global variables, defined in LLVM bytecode files, to the linker.
287 This includes symbol visibility information. This information is used by
288 the linker to do symbol resolution. For example: function <tt>foo2()</tt> is
289 defined inside an LLVM bytecode module and it is an externally visible symbol.
290 This helps the linker connect the use of <tt>foo2()</tt> in native object
291 files with a future definition of the symbol <tt>foo2()</tt>. The linker
292 will see the actual definition of <tt>foo2()</tt> when it receives the
293 optimized native object file in
294 <a href="#phase4">Symbol Resolution after optimization</a> phase. If the
295 linker does not find any uses of <tt>foo2()</tt>, it updates LLVMSymbol
296 visibility information to notify LLVM intermodular optimizer that it is dead.
297 The LLVM intermodular optimizer takes advantage of such information to
298 generate better code.</p>
301 <!-- ======================================================================= -->
302 <div class="doc_subsection">
303 <a name="readllvmobjectfile">readLLVMObjectFile()</a>
306 <div class="doc_text">
307 <p>The <tt>readLLVMObjectFile()</tt> function is used by the linker to read
308 LLVM bytecode files and collect LLVMSymbol nformation. This routine also
309 supplies a list of externally defined symbols that are used by LLVM bytecode
310 files. The linker uses this symbol information to do symbol resolution.
311 Internally, <a href="#lto">LLVMlto</a> maintains LLVM bytecode modules in
312 memory. This function also provides a list of external references used by
316 <!-- ======================================================================= -->
317 <div class="doc_subsection">
318 <a name="optimizemodules">optimizeModules()</a>
321 <div class="doc_text">
322 <p>The linker invokes <tt>optimizeModules</tt> to optimize already read
323 LLVM bytecode files by applying LLVM intermodular optimization techniques.
324 This function runs the LLVM intermodular optimizer and generates native
325 object code as <tt>.o</tt> files at the name and location provided by the
329 <!-- *********************************************************************** -->
330 <div class="doc_section">
331 <a name="debug">Debugging Information</a>
333 <!-- *********************************************************************** -->
335 <div class="doc_text">
337 <p><tt> ... To be completed ... </tt></p>
341 <!-- *********************************************************************** -->
345 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
346 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
347 <a href="http://validator.w3.org/check/referer"><img
348 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
351 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
352 Last modified: $Date$