blob: ee014101bc24065661aa1ad6ed6b28e38c3d5a25 [file] [log] [blame]
Nick Lewyckyebc67652009-03-01 09:38:29 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <title>LLVM gold plugin</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
9
10<div class="doc_title">LLVM gold plugin</div>
11<ol>
12 <li><a href="#introduction">Introduction</a></li>
13 <li><a href="#build">How to build it</a></li>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000014 <li><a href="#usage">Usage</a>
15 <ul>
16 <li><a href="#example1">Example of link time optimization</a></li>
17 </ul></li>
Nick Lewyckyebc67652009-03-01 09:38:29 +000018 <li><a href="#licensing">Licensing</a></li>
19</ol>
20<div class="doc_author">Written by Nick Lewycky</div>
21
22<!--=========================================================================-->
23<div class="doc_section"><a name="introduction">Introduction</a></div>
24<!--=========================================================================-->
25<div class="doc_text">
Duncan Sands10f06752009-03-01 15:19:03 +000026 <p>Building with link time optimization requires cooperation from the
Nick Lewyckyfd97aa22009-03-01 09:51:07 +000027system linker. LTO support on Linux systems requires that you use
Duncan Sands10f06752009-03-01 15:19:03 +000028the <a href="http://sourceware.org/binutils">gold linker</a> which supports
Nick Lewyckyebc67652009-03-01 09:38:29 +000029LTO via plugins. This is the same system used by the upcoming
30<a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a>
Duncan Sands10f06752009-03-01 15:19:03 +000031project.</p>
32 <p>The LLVM gold plugin implements the
33<a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a>
34on top of
Nick Lewyckyebc67652009-03-01 09:38:29 +000035<a href="http://llvm.org/docs/LinkTimeOptimization.html#lto">libLTO</a>.
36The same plugin can also be used by other tools such as <tt>ar</tt> and
37<tt>nm</tt>.
38</div>
39<!--=========================================================================-->
40<div class="doc_section"><a name="build">How to build it</a></div>
41<!--=========================================================================-->
42<div class="doc_text">
43 <p>You need to build gold with plugin support and build the LLVMgold
44plugin.</p>
45<ul>
46 <li>Build gold with plugin support:
47 <pre class="doc_code">
48mkdir binutils
49cd binutils
50cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
51<em>{enter "anoncvs" as the password}</em>
52cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co src
53mkdir build
54cd build
55../src/configure --enable-gold --enable-plugins
56make all-gold
57</pre>
58 That should leave you with binutils/build/gold/ld-new which supports the
Nick Lewycky017730c2009-03-01 21:06:42 +000059<tt>-plugin</tt> option.
Nick Lewyckyebc67652009-03-01 09:38:29 +000060
Duncan Sands10f06752009-03-01 15:19:03 +000061 <li>Build the LLVMgold plugin: Configure LLVM with
Nick Lewyckyebc67652009-03-01 09:38:29 +000062 <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run
63 <tt>make</tt>.
64</ul>
65</div>
66<!--=========================================================================-->
67<div class="doc_section"><a name="usage">Usage</a></div>
68<!--=========================================================================-->
69<div class="doc_text">
70 <p>The linker takes a <tt>-plugin</tt> option that points to the path of
71 the plugin <tt>.so</tt> file. To find out what link command <tt>gcc</tt>
72 would run in a given situation, run <tt>gcc -v <em>[...]</em></tt> and look
73 for the line where it runs <tt>collect2</tt>. Replace that with
74 <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're
75 ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt>
76 then replace it with <tt>ld-new</tt>.</p>
77 <p>You can produce bitcode files from <tt>llvm-gcc</tt> using
Nick Lewyckyd66ff0f2009-03-01 20:58:07 +000078 <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
79 synonymous with <tt>-O3 -flto</tt>.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +000080 <p><tt>llvm-gcc</tt> has a <tt>-use-gold-plugin</tt> option which looks
Nick Lewycky050147c2009-03-01 18:48:53 +000081 for the gold plugin in the same directories as it looks for <tt>cc1</tt> and
82 passes the <tt>-plugin</tt> option to ld. It will not look for an alternate
83 linker, which is why you need gold to be the installed system linker in your
84 path.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +000085</div>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000086
87<!-- ======================================================================= -->
88<div class="doc_subsection">
89 <a name="example1">Example of link time optimization</a>
90</div>
91
92<div class="doc_text">
93 <p>The following example shows a worked example of the gold plugin mixing
94 LLVM bitcode and native code.
95<pre class="doc_code">
96--- a.c ---
97#include &lt;stdio.h&gt;
98
99extern void foo1(void);
100extern void foo4(void);
101
102void foo2(void) {
103 printf("Foo2\n");
104}
105
106void foo3(void) {
107 foo4();
108}
109
110int main(void) {
111 foo1();
112}
113
114--- b.c ---
115#include &lt;stdio.h&gt;
116
117extern void foo2(void);
118
119void foo1(void) {
120 foo2();
121}
122
123void foo4(void) {
124 printf("Foo4");
125}
126
127--- command lines ---
128$ llvm-gcc -flto a.c -c -o a.o # &lt;-- a.o is LLVM bitcode file
129$ llvm-gcc b.c -c -o b.o # &lt;-- b.o is native object file
130$ llvm-gcc -use-gold-plugin a.o b.o -o main # &lt;-- link with LLVMgold plugin
131</pre>
132 <p>Gold informs the plugin that foo3 is never referenced outside the IR,
133 leading LLVM to delete that function. However, unlike in the
134 <a href="http://llvm.org/docs/LinkTimeOptimization.html#example1">libLTO
135 example</a> gold does not currently eliminate foo4.</p>
136</div>
137
Nick Lewyckyebc67652009-03-01 09:38:29 +0000138<!--=========================================================================-->
139<div class="doc_section"><a name="licensing">Licensing</a></div>
140<!--=========================================================================-->
141<div class="doc_text">
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000142 <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file
Nick Lewyckyebc67652009-03-01 09:38:29 +0000143<tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so
144binary is also GPLv3. This can still be used to link non-GPLv3 programs just
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000145as much as gold could without the plugin.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000146</div>
147
148<!-- *********************************************************************** -->
149<hr>
150<address>
151 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
152 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
153 <a href="http://validator.w3.org/check/referer"><img
154 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000155 <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br>
156 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
157 Last modified: $Date: 2009-01-01 23:10:51 -0800 (Thu, 01 Jan 2009) $
158</address>
159</body>
160</html>