aboutsummaryrefslogtreecommitdiff
path: root/docs/LinkTimeOptimization.html
blob: 019ba7574dc244907aecff79fc89431483f5ea7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 <title>LLVM Link Time Optimization: design and implementation</title>
  <link rel="stylesheet" href="llvm.css" type="text/css">
</head>

<div class="doc_title">
  LLVM Link Time Optimization: design and implementation
</div>

<ul>
  <li><a href="#desc">Description</a></li>
  <li><a href="#design">Design Philosophy</a>
  <ul>
    <li><a href="#example1">Example of link time optimization</a></li>
    <li><a href="#alternative_approaches">Alternative Approaches</a></li>
  </ul></li>
  <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a>
  <ul>
    <li><a href="#phase1">Phase 1 : Read LLVM Bytecode Files</a></li>
    <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
    <li><a href="#phase3">Phase 3 : Optimize Bytecode Files</a></li>
    <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
  </ul></li>
  <li><a href="#lto">LLVMlto</a>
  <ul>
    <li><a href="#llvmsymbol">LLVMSymbol</a></li>
    <li><a href="#readllvmobjectfile">readLLVMObjectFile()</a></li>
    <li><a href="#optimizemodules">optimizeModules()</a></li>
  </ul></li>
  <li><a href="#debug">Debugging Information</a></li>
</ul>

<div class="doc_author">
<p>Written by Devang Patel</p>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
<a name="desc">Description</a>
</div>
<!-- *********************************************************************** -->

<div class="doc_text">
<p>
LLVM features powerful intermodular optimization which can be used at link time.
Link Time Optimization is another name of intermodular optimization when it 
is done during link stage. This document describes the interface between LLVM
intermodular optimizer and the linker and its design.
</p>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
<a name="design">Design Philosophy</a>
</div>
<!-- *********************************************************************** -->

<div class="doc_text">
<p>
The LLVM Link Time Optimizer seeks complete transparency, while doing intermodular
optimization, in compiler tool chain. Its main goal is to let developer take 
advantage of intermodular optimizer without making any significant changes to 
their makefiles or build system. This is achieved through tight integration with
linker. In this model, linker treates LLVM bytecode files like native objects 
file and allows mixing and matching among them. The linker uses 
<a href="#lto">LLVMlto</a>, a dynamically loaded library, to handle LLVM bytecode 
files. This tight integration between the linker and LLVM optimizer helps to do
optimizations that are not possible in other models. The linker input allows 
optimizer to avoid relying on conservative escape analysis.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="example1">Example of link time optimization</a>
</div>

<div class="doc_text">

<p>Following example illustrates advantage of integrated approach that uses
clean interface.  
<ul>
<li> Input source file <tt>a.c</tt> is compiled into LLVM byte code form.
<li> Input source file <tt>main.c</tt> is compiled into native object code.
</ul>
<code>
--- a.h ---
<br>extern int foo1(void);
<br>extern void foo2(void);
<br>extern void foo4(void);
<br>--- a.c ---
<br>#include "a.h"
<br>
<br>static signed int i = 0;
<br>
<br>void foo2(void) {
<br> i = -1;
<br>}
<br>
<br>static int foo3() {
<br>foo4();
<br>return 10;
<br>}
<br>
<br>int foo1(void) {
<br>int data = 0;
<br>
<br>if (i < 0) { data = foo3(); }
<br>
<br>data = data + 42;
<br>return data;
<br>}
<br>
<br>--- main.c ---
<br>#include &lt;stdio.h&gt;
<br>#include "a.h"
<br>
<br>void foo4(void) {
<br> printf ("Hi\n");
<br>}
<br>
<br>int main() {
<br> return foo1();
<br>}
<br>
<br>--- command lines ---
<br> $ llvm-gcc4 --emit-llvm -c a.c -o a.o  # <-- a.o is LLVM bytecode file
<br> $ llvm-gcc4 -c main.c -o main.o # <-- main.o is native object file
<br> $ llvm-gcc4 a.o main.o -o main # <-- standard link command without any modifications
<br>
</code>
<p>
In this example, the linker recognizes that <tt>foo2()</tt> is a externally visible
symbol defined in LLVM byte code file. This information is collected using
<a href="#readllvmobjectfile"> readLLVMObjectFile() </a>. Based on this
information, linker completes its usual symbol resolution pass and finds that
<tt>foo2()</tt> is not used anywhere. This information is used by LLVM optimizer
and it removes <tt>foo2()</tt>. As soon as <tt>foo2()</tt> is removed, optimizer
recognizes that condition <tt> i < 0 </tt> is always false, which means 
<tt>foo3()</tt> is never used. Hence, optimizer removes <tt>foo3()</tt> also.
And this in turn, enables linker to remove <tt>foo4()</tt>.
This example illustrates advantage of tight integration with linker. Here,
optimizer can not remove <tt>foo3()</tt> without the linker's input.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="alternative_approaches">Alternative Approaches</a>
</div>

<div class="doc_text">
<p>
<ul>
<li> Compiler driver invokes link time optimizer separately. 
<br><br>In this model link time optimizer is not able to take advantage of information
collected during normal linker's symbol resolution phase. In above example,
optimizer can not remove <tt>foo2()</tt> without linker's input because it is
externally visible. And this in turn prohibits optimizer from removing <tt>foo3()</tt>.
<br><br>
<li> Use separate tool to collect symbol information from all object file.
<br><br>In this model, this new separate tool or library replicates linker's 
capabilities to collect information for link time optimizer. Not only such code
duplication is difficult to justify but it also has several other disadvantages. 
For example, the linking semantics and the features provided by linker on 
various platform are not unique. This means, this new tool needs to support all 
such features and platforms in one super tool or one new separate tool per 
platform is required. This increases maintance cost for link time optimizer 
significantly, which is not necessary. Plus, this approach requires staying 
synchronized with linker developements on various platforms, which is not the 
main focus of link time optimizer. Finally, this approach increases end user's build 
time due to duplicate work done by this separate tool and linker itself.
</ul>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
  <a name="multiphase">Multi-phase communication between LLVM and linker</a>
</div>

<div class="doc_text">
<p>
The linker collects information about symbol defininitions and uses in various 
link objects which is more accurate than any information collected by other tools 
during typical build cycle. 
The linker collects this information by looking at definitions and uses of
symbols in native .o files and using symbol visibility information. The linker
also uses user supplied information, such as list of exported symbol.
LLVM optimizer collects control flow information, data flow information and 
knows much more about program structure from optimizer's point of view. Our
goal is to take advantage of tight intergration between the linker and 
optimizer by sharing this information during various linking phases.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="phase1">Phase 1 : Read LLVM Bytecode Files</a>
</div>

<div class="doc_text">
<p>
The linker first reads all object files in natural order and collects symbol 
information. This includes native object files as well as LLVM byte code files. 
In this phase, the linker uses <a href="#readllvmobjectfile"> readLLVMObjectFile() </a>  
to collect symbol  information from each LLVM bytecode files and updates its 
internal global symbol table accordingly. The intent of this interface is to
avoid overhead in the non LLVM case, where all input object files are native
object files, by putting this code in the error path of the linker. When the
linker sees the first llvm .o file, it dlopen()s the dynamic library. This is
to allow changes to LLVM part without relinking the linker.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="phase2">Phase 2 : Symbol Resolution</a>
</div>

<div class="doc_text">
<p>
In this stage, the linker resolves symbols using global symbol table information
to report undefined symbol errors, read archive members, resolve weak
symbols etc... The linker is able to do this seamlessly even though it does not 
know exact content of input LLVM bytecode files because it uses symbol information
provided by <a href="#readllvmobjectfile"> readLLVMObjectFile() </a>. 
If dead code stripping is enabled then linker collects list of live symbols.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="phase3">Phase 3 : Optimize Bytecode Files</a>
</div>
<div class="doc_text">
<p>
After symbol resolution, the linker updates symbol information supplied by LLVM 
bytecode files appropriately. For example, whether certain LLVM bytecode 
supplied symbols are used or not. In the example above, the linker reports
that <tt>foo2()</tt> is not used anywhere in the program, including native .o 
files. This information is used by LLVM interprocedural optimizer. The
linker uses <a href="#optimizemodules"> optimizeModules()</a> and requests 
optimized native object file of the LLVM portion of the program. 
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
</div>

<div class="doc_text">
<p>
In this phase, the linker reads optimized native object file and updates internal
global symbol table to reflect any changes. Linker also collects information 
about any change in use of external symbols by LLVM bytecode files. In the examle
above, the linker notes that <tt>foo4()</tt> is not used any more. If dead code
striping is enabled then linker refreshes live symbol information appropriately
and performs dead code stripping. 
<br>
After this phase, the linker continues linking as if it never saw LLVM bytecode 
files.
</p>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
<a name="lto">LLVMlto</a>
</div>

<div class="doc_text">
<p>
<tt>LLVMlto</tt> is a dynamic library that is part of the LLVM tools, and is 
intended for use by a linker. <tt>LLVMlto</tt> provides an abstract C++ interface 
to use the LLVM interprocedural optimizer without exposing details of LLVM 
internals. The intention is to keep the interface as stable as possible even 
when the LLVM optimizer continues to evolve.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="llvmsymbol">LLVMSymbol</a>
</div>

<div class="doc_text">
<p>
<tt>LLVMSymbol</tt> class is used to describe the externally visible functions
and global variables, tdefined in LLVM bytecode files, to linker. 
This includes symbol visibility information. This information is used by linker 
to do symbol resolution. For example : function <tt>foo2()</tt> is defined inside 
a LLVM bytecode module and it is externally visible symbol. 
This helps linker connect use of <tt>foo2()</tt> in native object file with 
future definition of symbol <tt>foo2()</tt>. The linker will see actual definition
of <tt>foo2()</tt> when it receives optimized native object file in <a href="#phase4">
Symbol Resolution after optimization</a> phase. If the linker does not find any 
use of <tt>foo2()</tt>, it updates LLVMSymbol visibility information to notify 
LLVM intermodular optimizer that it is dead. The LLVM intermodular optimizer
takes advantage of such information to generate better code.
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="readllvmobjectfile">readLLVMObjectFile()</a>
</div>

<div class="doc_text">
<p>
<tt>readLLVMObjectFile()</tt>  is used by the linker to read LLVM bytecode files 
and collect LLVMSymbol nformation. This routine also
supplies list of externally defined symbols that are used by LLVM bytecode
files. Linker uses this symbol information to do symbol  resolution. Internally,
<a href="#lto">LLVMlto</a> maintains LLVM bytecode modules in memory. This 
function also provides list of external references used by bytecode file.<br>
</p>
</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="optimizemodules">optimizeModules()</a>
</div>

<div class="doc_text">
<p>
The linker invokes <tt>optimizeModules</tt> to optimize already read LLVM 
bytecode files by applying LLVM intermodular optimization techniques. This 
function runs LLVM intermodular optimizer and generates native object code 
as .o file at name and location provided by the linker.
</p>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
  <a name="debug">Debugging Information</a>
</div>
<!-- *********************************************************************** -->

<div