1 ======================================================
2 LLVM Link Time Optimization: Design and Implementation
3 ======================================================
11 LLVM features powerful intermodular optimizations which can be used at link
12 time. Link Time Optimization (LTO) is another name for intermodular
13 optimization when performed during the link stage. This document describes the
14 interface and design between the LTO optimizer and the linker.
19 The LLVM Link Time Optimizer provides complete transparency, while doing
20 intermodular optimization, in the compiler tool chain. Its main goal is to let
21 the developer take advantage of intermodular optimizations without making any
22 significant changes to the developer's makefiles or build system. This is
23 achieved through tight integration with the linker. In this model, the linker
24 treats LLVM bitcode files like native object files and allows mixing and
25 matching among them. The linker uses `libLTO`_, a shared object, to handle LLVM
26 bitcode files. This tight integration between the linker and LLVM optimizer
27 helps to do optimizations that are not possible in other models. The linker
28 input allows the optimizer to avoid relying on conservative escape analysis.
32 Example of link time optimization
33 ---------------------------------
35 The following example illustrates the advantages of LTO's integrated approach
36 and clean interface. This example requires a system linker which supports LTO
37 through the interface described in this document. Here, clang transparently
38 invokes system linker.
40 * Input source file ``a.c`` is compiled into LLVM bitcode form.
41 * Input source file ``main.c`` is compiled into native object code.
46 extern int foo1(void);
47 extern void foo2(void);
48 extern void foo4(void);
53 static signed int i = 0;
88 .. code-block:: console
90 % clang -flto -c a.c -o a.o # <-- a.o is LLVM bitcode file
91 % clang -c main.c -o main.o # <-- main.o is native object file
92 % clang -flto a.o main.o -o main # <-- standard link command with -flto
94 * In this example, the linker recognizes that ``foo2()`` is an externally
95 visible symbol defined in LLVM bitcode file. The linker completes its usual
96 symbol resolution pass and finds that ``foo2()`` is not used
97 anywhere. This information is used by the LLVM optimizer and it
100 * As soon as ``foo2()`` is removed, the optimizer recognizes that condition ``i
101 < 0`` is always false, which means ``foo3()`` is never used. Hence, the
102 optimizer also removes ``foo3()``.
104 * And this in turn, enables linker to remove ``foo4()``.
106 This example illustrates the advantage of tight integration with the
107 linker. Here, the optimizer can not remove ``foo3()`` without the linker's
110 Alternative Approaches
111 ----------------------
113 **Compiler driver invokes link time optimizer separately.**
114 In this model the link time optimizer is not able to take advantage of
115 information collected during the linker's normal symbol resolution phase.
116 In the above example, the optimizer can not remove ``foo2()`` without the
117 linker's input because it is externally visible. This in turn prohibits the
118 optimizer from removing ``foo3()``.
120 **Use separate tool to collect symbol information from all object files.**
121 In this model, a new, separate, tool or library replicates the linker's
122 capability to collect information for link time optimization. Not only is
123 this code duplication difficult to justify, but it also has several other
124 disadvantages. For example, the linking semantics and the features provided
125 by the linker on various platform are not unique. This means, this new tool
126 needs to support all such features and platforms in one super tool or a
127 separate tool per platform is required. This increases maintenance cost for
128 link time optimizer significantly, which is not necessary. This approach
129 also requires staying synchronized with linker developments on various
130 platforms, which is not the main focus of the link time optimizer. Finally,
131 this approach increases end user's build time due to the duplication of work
132 done by this separate tool and the linker itself.
134 Multi-phase communication between ``libLTO`` and linker
135 =======================================================
137 The linker collects information about symbol definitions and uses in various
138 link objects which is more accurate than any information collected by other
139 tools during typical build cycles. The linker collects this information by
140 looking at the definitions and uses of symbols in native .o files and using
141 symbol visibility information. The linker also uses user-supplied information,
142 such as a list of exported symbols. LLVM optimizer collects control flow
143 information, data flow information and knows much more about program structure
144 from the optimizer's point of view. Our goal is to take advantage of tight
145 integration between the linker and the optimizer by sharing this information
146 during various linking phases.
148 Phase 1 : Read LLVM Bitcode Files
149 ---------------------------------
151 The linker first reads all object files in natural order and collects symbol
152 information. This includes native object files as well as LLVM bitcode files.
153 To minimize the cost to the linker in the case that all .o files are native
154 object files, the linker only calls ``lto_module_create()`` when a supplied
155 object file is found to not be a native object file. If ``lto_module_create()``
156 returns that the file is an LLVM bitcode file, the linker then iterates over the
157 module using ``lto_module_get_symbol_name()`` and
158 ``lto_module_get_symbol_attribute()`` to get all symbols defined and referenced.
159 This information is added to the linker's global symbol table.
162 The lto* functions are all implemented in a shared object libLTO. This allows
163 the LLVM LTO code to be updated independently of the linker tool. On platforms
164 that support it, the shared object is lazily loaded.
166 Phase 2 : Symbol Resolution
167 ---------------------------
169 In this stage, the linker resolves symbols using global symbol table. It may
170 report undefined symbol errors, read archive members, replace weak symbols, etc.
171 The linker is able to do this seamlessly even though it does not know the exact
172 content of input LLVM bitcode files. If dead code stripping is enabled then the
173 linker collects the list of live symbols.
175 Phase 3 : Optimize Bitcode Files
176 --------------------------------
178 After symbol resolution, the linker tells the LTO shared object which symbols
179 are needed by native object files. In the example above, the linker reports
180 that only ``foo1()`` is used by native object files using
181 ``lto_codegen_add_must_preserve_symbol()``. Next the linker invokes the LLVM
182 optimizer and code generators using ``lto_codegen_compile()`` which returns a
183 native object file creating by merging the LLVM bitcode files and applying
184 various optimization passes.
186 Phase 4 : Symbol Resolution after optimization
187 ----------------------------------------------
189 In this phase, the linker reads optimized a native object file and updates the
190 internal global symbol table to reflect any changes. The linker also collects
191 information about any changes in use of external symbols by LLVM bitcode
192 files. In the example above, the linker notes that ``foo4()`` is not used any
193 more. If dead code stripping is enabled then the linker refreshes the live
194 symbol information appropriately and performs dead code stripping.
196 After this phase, the linker continues linking as if it never saw LLVM bitcode
204 ``libLTO`` is a shared object that is part of the LLVM tools, and is intended
205 for use by a linker. ``libLTO`` provides an abstract C interface to use the LLVM
206 interprocedural optimizer without exposing details of LLVM's internals. The
207 intention is to keep the interface as stable as possible even when the LLVM
208 optimizer continues to evolve. It should even be possible for a completely
209 different compilation technology to provide a different libLTO that works with
210 their object files and the standard linker tool.
215 A non-native object file is handled via an ``lto_module_t``. The following
216 functions allow the linker to check if a file (on disk or in a memory buffer) is
217 a file which libLTO can process:
221 lto_module_is_object_file(const char*)
222 lto_module_is_object_file_for_target(const char*, const char*)
223 lto_module_is_object_file_in_memory(const void*, size_t)
224 lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*)
226 If the object file can be processed by ``libLTO``, the linker creates a
227 ``lto_module_t`` by using one of:
231 lto_module_create(const char*)
232 lto_module_create_from_memory(const void*, size_t)
234 and when done, the handle is released via
238 lto_module_dispose(lto_module_t)
241 The linker can introspect the non-native object file by getting the number of
242 symbols and getting the name and attributes of each symbol via:
246 lto_module_get_num_symbols(lto_module_t)
247 lto_module_get_symbol_name(lto_module_t, unsigned int)
248 lto_module_get_symbol_attribute(lto_module_t, unsigned int)
250 The attributes of a symbol include the alignment, visibility, and kind.
255 Once the linker has loaded each non-native object files into an
256 ``lto_module_t``, it can request ``libLTO`` to process them all and generate a
257 native object file. This is done in a couple of steps. First, a code generator
264 Then, each non-native object file is added to the code generator with:
268 lto_codegen_add_module(lto_code_gen_t, lto_module_t)
270 The linker then has the option of setting some codegen options. Whether or not
271 to generate DWARF debug info is set with:
275 lto_codegen_set_debug_model(lto_code_gen_t)
277 which kind of position independence is set with:
281 lto_codegen_set_pic_model(lto_code_gen_t)
283 And each symbol that is referenced by a native object file or otherwise must not
284 be optimized away is set with:
288 lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*)
290 After all these settings are done, the linker requests that a native object file
291 be created from the modules with the settings using:
295 lto_codegen_compile(lto_code_gen_t, size*)
297 which returns a pointer to a buffer containing the generated native object file.
298 The linker then parses that and links it with the rest of the native object