Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 5 | <title>LLVM Link Time Optimization: Design and Implementation</title> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 6 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
| 7 | </head> |
| 8 | |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 9 | <h1> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 10 | LLVM Link Time Optimization: Design and Implementation |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 11 | </h1> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 12 | |
| 13 | <ul> |
| 14 | <li><a href="#desc">Description</a></li> |
| 15 | <li><a href="#design">Design Philosophy</a> |
| 16 | <ul> |
| 17 | <li><a href="#example1">Example of link time optimization</a></li> |
| 18 | <li><a href="#alternative_approaches">Alternative Approaches</a></li> |
| 19 | </ul></li> |
Devang Patel | 2c1292f | 2006-08-14 18:39:35 +0000 | [diff] [blame] | 20 | <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 21 | <ul> |
Duncan Sands | 18d52f2 | 2010-09-29 20:09:55 +0000 | [diff] [blame] | 22 | <li><a href="#phase1">Phase 1 : Read LLVM Bitcode Files</a></li> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 23 | <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 24 | <li><a href="#phase3">Phase 3 : Optimize Bitcode Files</a></li> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 25 | <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li> |
| 26 | </ul></li> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 27 | <li><a href="#lto">libLTO</a> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 28 | <ul> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 29 | <li><a href="#lto_module_t">lto_module_t</a></li> |
| 30 | <li><a href="#lto_code_gen_t">lto_code_gen_t</a></li> |
| 31 | </ul> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 32 | </ul> |
| 33 | |
| 34 | <div class="doc_author"> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 35 | <p>Written by Devang Patel and Nick Kledzik</p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 36 | </div> |
| 37 | |
| 38 | <!-- *********************************************************************** --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 39 | <h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 40 | <a name="desc">Description</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 41 | </h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 42 | <!-- *********************************************************************** --> |
| 43 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 44 | <div> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 45 | <p> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 46 | LLVM features powerful intermodular optimizations which can be used at link |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 47 | time. Link Time Optimization (LTO) is another name for intermodular optimization |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 48 | when performed during the link stage. This document describes the interface |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 49 | and design between the LTO optimizer and the linker.</p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 50 | </div> |
| 51 | |
| 52 | <!-- *********************************************************************** --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 53 | <h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 54 | <a name="design">Design Philosophy</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 55 | </h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 56 | <!-- *********************************************************************** --> |
| 57 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 58 | <div> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 59 | <p> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 60 | The LLVM Link Time Optimizer provides complete transparency, while doing |
| 61 | intermodular optimization, in the compiler tool chain. Its main goal is to let |
| 62 | the developer take advantage of intermodular optimizations without making any |
| 63 | significant changes to the developer's makefiles or build system. This is |
| 64 | achieved through tight integration with the linker. In this model, the linker |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 65 | treates LLVM bitcode files like native object files and allows mixing and |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 66 | matching among them. The linker uses <a href="#lto">libLTO</a>, a shared |
| 67 | object, to handle LLVM bitcode files. This tight integration between |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 68 | the linker and LLVM optimizer helps to do optimizations that are not possible |
| 69 | in other models. The linker input allows the optimizer to avoid relying on |
| 70 | conservative escape analysis. |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 71 | </p> |
| 72 | |
| 73 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 74 | <h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 75 | <a name="example1">Example of link time optimization</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 76 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 77 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 78 | <div> |
Duncan Sands | 6db928a | 2007-04-07 17:43:25 +0000 | [diff] [blame] | 79 | <p>The following example illustrates the advantages of LTO's integrated |
| 80 | approach and clean interface. This example requires a system linker which |
| 81 | supports LTO through the interface described in this document. Here, |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 82 | clang transparently invokes system linker. </p> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 83 | <ul> |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 84 | <li> Input source file <tt>a.c</tt> is compiled into LLVM bitcode form. |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 85 | <li> Input source file <tt>main.c</tt> is compiled into native object code. |
| 86 | </ul> |
Misha Brukman | 64722e5 | 2008-12-16 03:07:49 +0000 | [diff] [blame] | 87 | <pre class="doc_code"> |
Devang Patel | 2c1292f | 2006-08-14 18:39:35 +0000 | [diff] [blame] | 88 | --- a.h --- |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 89 | extern int foo1(void); |
| 90 | extern void foo2(void); |
| 91 | extern void foo4(void); |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 92 | |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 93 | --- a.c --- |
| 94 | #include "a.h" |
| 95 | |
| 96 | static signed int i = 0; |
| 97 | |
| 98 | void foo2(void) { |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 99 | i = -1; |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int foo3() { |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 103 | foo4(); |
| 104 | return 10; |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int foo1(void) { |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 108 | int data = 0; |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 109 | |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 110 | if (i < 0) |
| 111 | data = foo3(); |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 112 | |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 113 | data = data + 42; |
| 114 | return data; |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | --- main.c --- |
| 118 | #include <stdio.h> |
| 119 | #include "a.h" |
| 120 | |
| 121 | void foo4(void) { |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 122 | printf("Hi\n"); |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int main() { |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 126 | return foo1(); |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | --- command lines --- |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 130 | $ clang -emit-llvm -c a.c -o a.o # <-- a.o is LLVM bitcode file |
| 131 | $ clang -c main.c -o main.o # <-- main.o is native object file |
| 132 | $ clang a.o main.o -o main # <-- standard link command without any modifications |
Misha Brukman | 64722e5 | 2008-12-16 03:07:49 +0000 | [diff] [blame] | 133 | </pre> |
Bill Wendling | 88816ec | 2011-09-18 12:51:05 +0000 | [diff] [blame] | 134 | |
| 135 | <ul> |
| 136 | <li>In this example, the linker recognizes that <tt>foo2()</tt> is an |
| 137 | externally visible symbol defined in LLVM bitcode file. The linker |
| 138 | completes its usual symbol resolution pass and finds that <tt>foo2()</tt> |
| 139 | is not used anywhere. This information is used by the LLVM optimizer and |
| 140 | it removes <tt>foo2()</tt>.</li> |
| 141 | <li>As soon as <tt>foo2()</tt> is removed, the optimizer recognizes that condition |
| 142 | <tt>i < 0</tt> is always false, which means <tt>foo3()</tt> is never |
| 143 | used. Hence, the optimizer also removes <tt>foo3()</tt>.</li> |
| 144 | <li>And this in turn, enables linker to remove <tt>foo4()</tt>.</li> |
| 145 | </ul> |
| 146 | |
| 147 | <p>This example illustrates the advantage of tight integration with the |
| 148 | linker. Here, the optimizer can not remove <tt>foo3()</tt> without the |
| 149 | linker's input.</p> |
| 150 | |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 151 | </div> |
| 152 | |
| 153 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 154 | <h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 155 | <a name="alternative_approaches">Alternative Approaches</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 156 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 157 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 158 | <div> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 159 | <dl> |
| 160 | <dt><b>Compiler driver invokes link time optimizer separately.</b></dt> |
| 161 | <dd>In this model the link time optimizer is not able to take advantage of |
| 162 | information collected during the linker's normal symbol resolution phase. |
| 163 | In the above example, the optimizer can not remove <tt>foo2()</tt> without |
| 164 | the linker's input because it is externally visible. This in turn prohibits |
| 165 | the optimizer from removing <tt>foo3()</tt>.</dd> |
| 166 | <dt><b>Use separate tool to collect symbol information from all object |
| 167 | files.</b></dt> |
| 168 | <dd>In this model, a new, separate, tool or library replicates the linker's |
| 169 | capability to collect information for link time optimization. Not only is |
| 170 | this code duplication difficult to justify, but it also has several other |
| 171 | disadvantages. For example, the linking semantics and the features |
| 172 | provided by the linker on various platform are not unique. This means, |
| 173 | this new tool needs to support all such features and platforms in one |
| 174 | super tool or a separate tool per platform is required. This increases |
Benjamin Kramer | 8040cd3 | 2009-10-12 14:46:08 +0000 | [diff] [blame] | 175 | maintenance cost for link time optimizer significantly, which is not |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 176 | necessary. This approach also requires staying synchronized with linker |
| 177 | developements on various platforms, which is not the main focus of the link |
| 178 | time optimizer. Finally, this approach increases end user's build time due |
| 179 | to the duplication of work done by this separate tool and the linker itself. |
| 180 | </dd> |
| 181 | </dl> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 182 | </div> |
| 183 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 184 | </div> |
| 185 | |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 186 | <!-- *********************************************************************** --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 187 | <h2> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 188 | <a name="multiphase">Multi-phase communication between libLTO and linker</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 189 | </h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 190 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 191 | <div> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 192 | <p>The linker collects information about symbol defininitions and uses in |
| 193 | various link objects which is more accurate than any information collected |
| 194 | by other tools during typical build cycles. The linker collects this |
| 195 | information by looking at the definitions and uses of symbols in native .o |
| 196 | files and using symbol visibility information. The linker also uses |
| 197 | user-supplied information, such as a list of exported symbols. LLVM |
| 198 | optimizer collects control flow information, data flow information and knows |
| 199 | much more about program structure from the optimizer's point of view. |
Benjamin Kramer | 8040cd3 | 2009-10-12 14:46:08 +0000 | [diff] [blame] | 200 | Our goal is to take advantage of tight integration between the linker and |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 201 | the optimizer by sharing this information during various linking phases. |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 202 | </p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 203 | |
| 204 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 205 | <h3> |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 206 | <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 207 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 208 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 209 | <div> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 210 | <p>The linker first reads all object files in natural order and collects |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 211 | symbol information. This includes native object files as well as LLVM bitcode |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 212 | files. To minimize the cost to the linker in the case that all .o files |
| 213 | are native object files, the linker only calls <tt>lto_module_create()</tt> |
| 214 | when a supplied object file is found to not be a native object file. If |
| 215 | <tt>lto_module_create()</tt> returns that the file is an LLVM bitcode file, |
| 216 | the linker |
| 217 | then iterates over the module using <tt>lto_module_get_symbol_name()</tt> and |
| 218 | <tt>lto_module_get_symbol_attribute()</tt> to get all symbols defined and |
| 219 | referenced. |
| 220 | This information is added to the linker's global symbol table. |
| 221 | </p> |
| 222 | <p>The lto* functions are all implemented in a shared object libLTO. This |
| 223 | allows the LLVM LTO code to be updated independently of the linker tool. |
| 224 | On platforms that support it, the shared object is lazily loaded. |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 225 | </p> |
| 226 | </div> |
| 227 | |
| 228 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 229 | <h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 230 | <a name="phase2">Phase 2 : Symbol Resolution</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 231 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 232 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 233 | <div> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 234 | <p>In this stage, the linker resolves symbols using global symbol table. |
| 235 | It may report undefined symbol errors, read archive members, replace |
| 236 | weak symbols, etc. The linker is able to do this seamlessly even though it |
| 237 | does not know the exact content of input LLVM bitcode files. If dead code |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 238 | stripping is enabled then the linker collects the list of live symbols. |
| 239 | </p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 240 | </div> |
| 241 | |
| 242 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 243 | <h3> |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 244 | <a name="phase3">Phase 3 : Optimize Bitcode Files</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 245 | </h3> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 246 | <div> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 247 | <p>After symbol resolution, the linker tells the LTO shared object which |
| 248 | symbols are needed by native object files. In the example above, the linker |
| 249 | reports that only <tt>foo1()</tt> is used by native object files using |
| 250 | <tt>lto_codegen_add_must_preserve_symbol()</tt>. Next the linker invokes |
| 251 | the LLVM optimizer and code generators using <tt>lto_codegen_compile()</tt> |
| 252 | which returns a native object file creating by merging the LLVM bitcode files |
| 253 | and applying various optimization passes. |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 254 | </p> |
| 255 | </div> |
| 256 | |
| 257 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 258 | <h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 259 | <a name="phase4">Phase 4 : Symbol Resolution after optimization</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 260 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 261 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 262 | <div> |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 263 | <p>In this phase, the linker reads optimized a native object file and |
| 264 | updates the internal global symbol table to reflect any changes. The linker |
| 265 | also collects information about any changes in use of external symbols by |
Nick Lewycky | 1f04864 | 2010-04-24 22:01:40 +0000 | [diff] [blame] | 266 | LLVM bitcode files. In the example above, the linker notes that |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 267 | <tt>foo4()</tt> is not used any more. If dead code stripping is enabled then |
| 268 | the linker refreshes the live symbol information appropriately and performs |
| 269 | dead code stripping.</p> |
| 270 | <p>After this phase, the linker continues linking as if it never saw LLVM |
Gabor Greif | 04367bf | 2007-07-06 22:07:22 +0000 | [diff] [blame] | 271 | bitcode files.</p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 272 | </div> |
| 273 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 274 | </div> |
| 275 | |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 276 | <!-- *********************************************************************** --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 277 | <h2> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 278 | <a name="lto">libLTO</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 279 | </h2> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 280 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 281 | <div> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 282 | <p><tt>libLTO</tt> is a shared object that is part of the LLVM tools, and |
| 283 | is intended for use by a linker. <tt>libLTO</tt> provides an abstract C |
Reid Spencer | b5fc9f5 | 2006-08-14 19:19:55 +0000 | [diff] [blame] | 284 | interface to use the LLVM interprocedural optimizer without exposing details |
| 285 | of LLVM's internals. The intention is to keep the interface as stable as |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 286 | possible even when the LLVM optimizer continues to evolve. It should even |
| 287 | be possible for a completely different compilation technology to provide |
| 288 | a different libLTO that works with their object files and the standard |
| 289 | linker tool.</p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 290 | |
| 291 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 292 | <h3> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 293 | <a name="lto_module_t">lto_module_t</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 294 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 295 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 296 | <div> |
Misha Brukman | 64722e5 | 2008-12-16 03:07:49 +0000 | [diff] [blame] | 297 | |
| 298 | <p>A non-native object file is handled via an <tt>lto_module_t</tt>. |
| 299 | The following functions allow the linker to check if a file (on disk |
| 300 | or in a memory buffer) is a file which libLTO can process:</p> |
| 301 | |
| 302 | <pre class="doc_code"> |
| 303 | lto_module_is_object_file(const char*) |
| 304 | lto_module_is_object_file_for_target(const char*, const char*) |
| 305 | lto_module_is_object_file_in_memory(const void*, size_t) |
| 306 | lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*) |
| 307 | </pre> |
| 308 | |
| 309 | <p>If the object file can be processed by libLTO, the linker creates a |
| 310 | <tt>lto_module_t</tt> by using one of</p> |
| 311 | |
| 312 | <pre class="doc_code"> |
| 313 | lto_module_create(const char*) |
| 314 | lto_module_create_from_memory(const void*, size_t) |
| 315 | </pre> |
| 316 | |
| 317 | <p>and when done, the handle is released via</p> |
| 318 | |
| 319 | <pre class="doc_code"> |
| 320 | lto_module_dispose(lto_module_t) |
| 321 | </pre> |
| 322 | |
| 323 | <p>The linker can introspect the non-native object file by getting the number of |
| 324 | symbols and getting the name and attributes of each symbol via:</p> |
| 325 | |
| 326 | <pre class="doc_code"> |
| 327 | lto_module_get_num_symbols(lto_module_t) |
| 328 | lto_module_get_symbol_name(lto_module_t, unsigned int) |
| 329 | lto_module_get_symbol_attribute(lto_module_t, unsigned int) |
| 330 | </pre> |
| 331 | |
| 332 | <p>The attributes of a symbol include the alignment, visibility, and kind.</p> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 333 | </div> |
| 334 | |
| 335 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 336 | <h3> |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 337 | <a name="lto_code_gen_t">lto_code_gen_t</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 338 | </h3> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 339 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 340 | <div> |
Misha Brukman | 64722e5 | 2008-12-16 03:07:49 +0000 | [diff] [blame] | 341 | |
| 342 | <p>Once the linker has loaded each non-native object files into an |
| 343 | <tt>lto_module_t</tt>, it can request libLTO to process them all and |
| 344 | generate a native object file. This is done in a couple of steps. |
| 345 | First, a code generator is created with:</p> |
| 346 | |
| 347 | <pre class="doc_code">lto_codegen_create()</pre> |
| 348 | |
| 349 | <p>Then, each non-native object file is added to the code generator with:</p> |
| 350 | |
| 351 | <pre class="doc_code"> |
| 352 | lto_codegen_add_module(lto_code_gen_t, lto_module_t) |
| 353 | </pre> |
| 354 | |
| 355 | <p>The linker then has the option of setting some codegen options. Whether or |
| 356 | not to generate DWARF debug info is set with:</p> |
| 357 | |
| 358 | <pre class="doc_code">lto_codegen_set_debug_model(lto_code_gen_t)</pre> |
| 359 | |
| 360 | <p>Which kind of position independence is set with:</p> |
| 361 | |
| 362 | <pre class="doc_code">lto_codegen_set_pic_model(lto_code_gen_t) </pre> |
| 363 | |
| 364 | <p>And each symbol that is referenced by a native object file or otherwise must |
| 365 | not be optimized away is set with:</p> |
| 366 | |
| 367 | <pre class="doc_code"> |
| 368 | lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*) |
| 369 | </pre> |
| 370 | |
| 371 | <p>After all these settings are done, the linker requests that a native object |
| 372 | file be created from the modules with the settings using:</p> |
| 373 | |
| 374 | <pre class="doc_code">lto_codegen_compile(lto_code_gen_t, size*)</pre> |
| 375 | |
| 376 | <p>which returns a pointer to a buffer containing the generated native |
| 377 | object file. The linker then parses that and links it with the rest |
| 378 | of the native object files.</p> |
| 379 | |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 380 | </div> |
| 381 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 382 | </div> |
| 383 | |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 384 | <!-- *********************************************************************** --> |
| 385 | |
| 386 | <hr> |
| 387 | <address> |
| 388 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 389 | src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 390 | <a href="http://validator.w3.org/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 391 | src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 392 | |
Nick Kledzik | 84e5f77 | 2008-02-29 19:34:52 +0000 | [diff] [blame] | 393 | Devang Patel and Nick Kledzik<br> |
NAKAMURA Takumi | b9a3363 | 2011-04-09 02:13:37 +0000 | [diff] [blame] | 394 | <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br> |
Devang Patel | 93449f1 | 2006-08-14 18:03:40 +0000 | [diff] [blame] | 395 | Last modified: $Date$ |
| 396 | </address> |
| 397 | |
| 398 | </body> |
| 399 | </html> |
Nick Kledzik | 55998ba | 2008-02-26 01:36:52 +0000 | [diff] [blame] | 400 | |