blob: e25c45751929f037d33be3a514edbff6e84b78f9 [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
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000010<h1>LLVM gold plugin</h1>
Nick Lewyckyebc67652009-03-01 09:38:29 +000011<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>
Torok Edwin5641f8d2009-06-03 15:06:19 +000017 <li><a href="#lto_autotools">Quickstart for using LTO with autotooled projects</a></li>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000018 </ul></li>
Nick Lewyckyebc67652009-03-01 09:38:29 +000019 <li><a href="#licensing">Licensing</a></li>
20</ol>
21<div class="doc_author">Written by Nick Lewycky</div>
22
23<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000024<h2><a name="introduction">Introduction</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000025<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000026<div>
Duncan Sands10f06752009-03-01 15:19:03 +000027 <p>Building with link time optimization requires cooperation from the
Nick Lewyckyfd97aa22009-03-01 09:51:07 +000028system linker. LTO support on Linux systems requires that you use
Duncan Sands10f06752009-03-01 15:19:03 +000029the <a href="http://sourceware.org/binutils">gold linker</a> which supports
Nick Lewycky5c212802010-04-17 07:00:24 +000030LTO via plugins. This is the same mechanism used by the
Nick Lewyckyebc67652009-03-01 09:38:29 +000031<a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a>
Duncan Sands10f06752009-03-01 15:19:03 +000032project.</p>
33 <p>The LLVM gold plugin implements the
34<a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a>
35on top of
NAKAMURA Takumi31c18062011-04-09 02:13:48 +000036<a href="LinkTimeOptimization.html#lto">libLTO</a>.
Nick Lewyckyebc67652009-03-01 09:38:29 +000037The same plugin can also be used by other tools such as <tt>ar</tt> and
38<tt>nm</tt>.
39</div>
40<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000041<h2><a name="build">How to build it</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000042<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000043<div>
Nick Lewycky5c212802010-04-17 07:00:24 +000044 <p>You need to have gold with plugin support and build the LLVMgold
45plugin. Check whether you have gold running <tt>/usr/bin/ld -v</tt>. It will
46report &#8220;GNU gold&#8221; or else &#8220GNU ld&#8221; if not. If you have
47gold, check for plugin support by running <tt>/usr/bin/ld -plugin</tt>. If it
48complains &#8220missing argument&#8221 then you have plugin support. If not,
49such as an &#8220;unknown option&#8221; error then you will either need to
50build gold or install a version with plugin support.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +000051<ul>
Nick Lewycky5c212802010-04-17 07:00:24 +000052 <li>To build gold with plugin support:
Nick Lewyckyebc67652009-03-01 09:38:29 +000053 <pre class="doc_code">
54mkdir binutils
55cd binutils
56cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
57<em>{enter "anoncvs" as the password}</em>
Duncan Sandsed7713d2010-10-06 06:45:11 +000058cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
Nick Lewyckyebc67652009-03-01 09:38:29 +000059mkdir build
60cd build
61../src/configure --enable-gold --enable-plugins
62make all-gold
63</pre>
Nick Lewycky5c212802010-04-17 07:00:24 +000064 That should leave you with <tt>binutils/build/gold/ld-new</tt> which supports the <tt>-plugin</tt> option. It also built would have
65<tt>binutils/build/binutils/ar</tt> and <tt>nm-new</tt> which support plugins
66but don't have a visible -plugin option, instead relying on the gold plugin
67being present in <tt>../lib/bfd-plugins</tt> relative to where the binaries are
68placed.
Duncan Sands10f06752009-03-01 15:19:03 +000069 <li>Build the LLVMgold plugin: Configure LLVM with
Nick Lewyckyebc67652009-03-01 09:38:29 +000070 <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run
71 <tt>make</tt>.
72</ul>
73</div>
74<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000075<h2><a name="usage">Usage</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000076<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000077<div>
Nick Lewyckyebc67652009-03-01 09:38:29 +000078 <p>The linker takes a <tt>-plugin</tt> option that points to the path of
79 the plugin <tt>.so</tt> file. To find out what link command <tt>gcc</tt>
80 would run in a given situation, run <tt>gcc -v <em>[...]</em></tt> and look
81 for the line where it runs <tt>collect2</tt>. Replace that with
Rafael Espindola53843f82010-08-08 21:14:26 +000082 <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're
Nick Lewyckyebc67652009-03-01 09:38:29 +000083 ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt>
84 then replace it with <tt>ld-new</tt>.</p>
85 <p>You can produce bitcode files from <tt>llvm-gcc</tt> using
Nick Lewyckyd66ff0f2009-03-01 20:58:07 +000086 <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
87 synonymous with <tt>-O3 -flto</tt>.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +000088 <p><tt>llvm-gcc</tt> has a <tt>-use-gold-plugin</tt> option which looks
Nick Lewycky050147c2009-03-01 18:48:53 +000089 for the gold plugin in the same directories as it looks for <tt>cc1</tt> and
90 passes the <tt>-plugin</tt> option to ld. It will not look for an alternate
91 linker, which is why you need gold to be the installed system linker in your
92 path.</p>
Nick Lewycky5c212802010-04-17 07:00:24 +000093 <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install
Rafael Espindola53843f82010-08-08 21:14:26 +000094 <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your
Nick Lewycky5c212802010-04-17 07:00:24 +000095 own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to
96 <tt>/usr/bin</tt>.
97 <p>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000098
99<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000100<h3>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000101 <a name="example1">Example of link time optimization</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000102</h3>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000103
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000104<div>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000105 <p>The following example shows a worked example of the gold plugin mixing
106 LLVM bitcode and native code.
107<pre class="doc_code">
108--- a.c ---
109#include &lt;stdio.h&gt;
110
111extern void foo1(void);
112extern void foo4(void);
113
114void foo2(void) {
115 printf("Foo2\n");
116}
117
118void foo3(void) {
119 foo4();
120}
121
122int main(void) {
123 foo1();
124}
125
126--- b.c ---
127#include &lt;stdio.h&gt;
128
129extern void foo2(void);
130
131void foo1(void) {
132 foo2();
133}
134
135void foo4(void) {
136 printf("Foo4");
137}
138
139--- command lines ---
140$ llvm-gcc -flto a.c -c -o a.o # &lt;-- a.o is LLVM bitcode file
Rafael Espindola91f935e2009-07-08 11:13:34 +0000141$ ar q a.a a.o # &lt;-- a.a is an archive with LLVM bitcode
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000142$ llvm-gcc b.c -c -o b.o # &lt;-- b.o is native object file
Rafael Espindola91f935e2009-07-08 11:13:34 +0000143$ llvm-gcc -use-gold-plugin a.a b.o -o main # &lt;-- link with LLVMgold plugin
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000144</pre>
145 <p>Gold informs the plugin that foo3 is never referenced outside the IR,
146 leading LLVM to delete that function. However, unlike in the
NAKAMURA Takumi31c18062011-04-09 02:13:48 +0000147 <a href="LinkTimeOptimization.html#example1">libLTO
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000148 example</a> gold does not currently eliminate foo4.</p>
149</div>
150
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000151</div>
152
Nick Lewyckyebc67652009-03-01 09:38:29 +0000153<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000154<h2>
155 <a name="lto_autotools">
156 Quickstart for using LTO with autotooled projects
157 </a>
158</h2>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000159<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000160<div>
Nick Lewycky5c212802010-04-17 07:00:24 +0000161 <p>Once your system <tt>ld</tt>, <tt>ar</tt> and <tt>nm</tt> all support LLVM
162 bitcode, everything is in place for an easy to use LTO build of autotooled
163 projects:</p>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000164 <ul>
Rafael Espindola53843f82010-08-08 21:14:26 +0000165 <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000166 <li>Install the newly built binutils to <tt>$PREFIX</tt></li>
Rafael Espindola53843f82010-08-08 21:14:26 +0000167 <li>Copy <tt>Release/lib/LLVMgold.so</tt> to
Torok Edwin5641f8d2009-06-03 15:06:19 +0000168 <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/4.2.1/</tt> and
169 <tt>$PREFIX/lib/bfd-plugins/</tt></li>
170 <li>Set environment variables (<tt>$PREFIX</tt> is where you installed llvm-gcc and
171 binutils):
172 <pre class="doc_code">
173export CC="$PREFIX/bin/llvm-gcc -use-gold-plugin"
Torok Edwin4734ed82009-06-03 15:42:26 +0000174export CXX="$PREFIX/bin/llvm-g++ -use-gold-plugin"
Torok Edwin347c7bb2009-06-04 16:08:10 +0000175export AR="$PREFIX/bin/ar"
176export NM="$PREFIX/bin/nm"
Torok Edwin5641f8d2009-06-03 15:06:19 +0000177export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
178export CFLAGS="-O4"
Nick Lewycky9d813df2009-06-06 18:14:04 +0000179</pre>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000180 </li>
Torok Edwin347c7bb2009-06-04 16:08:10 +0000181 <li>Or you can just set your path:
182 <pre class="doc_code">
183export PATH="$PREFIX/bin:$PATH"
184export CC="llvm-gcc -use-gold-plugin"
185export CXX="llvm-g++ -use-gold-plugin"
186export RANLIB=/bin/true
187export CFLAGS="-O4"
Nick Lewycky9d813df2009-06-06 18:14:04 +0000188</pre>
Torok Edwin347c7bb2009-06-04 16:08:10 +0000189 </li>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000190 <li>Configure &amp; build the project as usual: <tt>./configure &amp;&amp; make &amp;&amp; make check</tt> </li>
191 </ul>
192 <p> The environment variable settings may work for non-autotooled projects
193 too, but you may need to set the <tt>LD</tt> environment variable as well.</p>
194</div>
195
196<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000197<h2><a name="licensing">Licensing</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000198<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000199<div>
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000200 <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file
Nick Lewyckyebc67652009-03-01 09:38:29 +0000201<tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so
202binary is also GPLv3. This can still be used to link non-GPLv3 programs just
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000203as much as gold could without the plugin.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000204</div>
205
206<!-- *********************************************************************** -->
207<hr>
208<address>
209 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
210 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
211 <a href="http://validator.w3.org/check/referer"><img
212 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000213 <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000214 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
Nick Lewycky5c212802010-04-17 07:00:24 +0000215 Last modified: $Date: 2010-04-16 23:58:21 -0800 (Fri, 16 Apr 2010) $
Nick Lewyckyebc67652009-03-01 09:38:29 +0000216</address>
217</body>
218</html>