blob: 2c08bd031b7ffdc8938fe2ceacf1840dacd8c78f [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>
NAKAMURA Takumi5c6e4df2011-10-31 11:21:59 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Nick Lewyckyebc67652009-03-01 09:38:29 +00006 <title>LLVM gold plugin</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8</head>
9<body>
10
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000011<h1>LLVM gold plugin</h1>
Nick Lewyckyebc67652009-03-01 09:38:29 +000012<ol>
13 <li><a href="#introduction">Introduction</a></li>
14 <li><a href="#build">How to build it</a></li>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000015 <li><a href="#usage">Usage</a>
16 <ul>
17 <li><a href="#example1">Example of link time optimization</a></li>
Torok Edwin5641f8d2009-06-03 15:06:19 +000018 <li><a href="#lto_autotools">Quickstart for using LTO with autotooled projects</a></li>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +000019 </ul></li>
Nick Lewyckyebc67652009-03-01 09:38:29 +000020 <li><a href="#licensing">Licensing</a></li>
21</ol>
22<div class="doc_author">Written by Nick Lewycky</div>
23
24<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000025<h2><a name="introduction">Introduction</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000026<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000027<div>
Duncan Sands10f06752009-03-01 15:19:03 +000028 <p>Building with link time optimization requires cooperation from the
Nick Lewyckyfd97aa22009-03-01 09:51:07 +000029system linker. LTO support on Linux systems requires that you use
Duncan Sands10f06752009-03-01 15:19:03 +000030the <a href="http://sourceware.org/binutils">gold linker</a> which supports
Nick Lewycky5c212802010-04-17 07:00:24 +000031LTO via plugins. This is the same mechanism used by the
Nick Lewyckyebc67652009-03-01 09:38:29 +000032<a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a>
Duncan Sands10f06752009-03-01 15:19:03 +000033project.</p>
34 <p>The LLVM gold plugin implements the
35<a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a>
36on top of
NAKAMURA Takumi31c18062011-04-09 02:13:48 +000037<a href="LinkTimeOptimization.html#lto">libLTO</a>.
Nick Lewyckyebc67652009-03-01 09:38:29 +000038The same plugin can also be used by other tools such as <tt>ar</tt> and
39<tt>nm</tt>.
40</div>
41<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000042<h2><a name="build">How to build it</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000043<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000044<div>
Nick Lewycky5c212802010-04-17 07:00:24 +000045 <p>You need to have gold with plugin support and build the LLVMgold
46plugin. Check whether you have gold running <tt>/usr/bin/ld -v</tt>. It will
47report &#8220;GNU gold&#8221; or else &#8220GNU ld&#8221; if not. If you have
48gold, check for plugin support by running <tt>/usr/bin/ld -plugin</tt>. If it
49complains &#8220missing argument&#8221 then you have plugin support. If not,
50such as an &#8220;unknown option&#8221; error then you will either need to
51build gold or install a version with plugin support.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +000052<ul>
Nick Lewycky5c212802010-04-17 07:00:24 +000053 <li>To build gold with plugin support:
Nick Lewyckyebc67652009-03-01 09:38:29 +000054 <pre class="doc_code">
55mkdir binutils
56cd binutils
57cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
58<em>{enter "anoncvs" as the password}</em>
Duncan Sandsed7713d2010-10-06 06:45:11 +000059cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
Nick Lewyckyebc67652009-03-01 09:38:29 +000060mkdir build
61cd build
62../src/configure --enable-gold --enable-plugins
63make all-gold
64</pre>
Nick Lewycky5c212802010-04-17 07:00:24 +000065 That should leave you with <tt>binutils/build/gold/ld-new</tt> which supports the <tt>-plugin</tt> option. It also built would have
66<tt>binutils/build/binutils/ar</tt> and <tt>nm-new</tt> which support plugins
67but don't have a visible -plugin option, instead relying on the gold plugin
68being present in <tt>../lib/bfd-plugins</tt> relative to where the binaries are
69placed.
Duncan Sands10f06752009-03-01 15:19:03 +000070 <li>Build the LLVMgold plugin: Configure LLVM with
Nick Lewyckyebc67652009-03-01 09:38:29 +000071 <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run
72 <tt>make</tt>.
73</ul>
74</div>
75<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000076<h2><a name="usage">Usage</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +000077<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000078<div>
Bill Wendlingea55c832011-09-18 12:37:20 +000079
Nick Lewyckyebc67652009-03-01 09:38:29 +000080 <p>The linker takes a <tt>-plugin</tt> option that points to the path of
81 the plugin <tt>.so</tt> file. To find out what link command <tt>gcc</tt>
82 would run in a given situation, run <tt>gcc -v <em>[...]</em></tt> and look
83 for the line where it runs <tt>collect2</tt>. Replace that with
Rafael Espindola53843f82010-08-08 21:14:26 +000084 <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're
Nick Lewyckyebc67652009-03-01 09:38:29 +000085 ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt>
86 then replace it with <tt>ld-new</tt>.</p>
Bill Wendlingea55c832011-09-18 12:37:20 +000087
88 <p>You can produce bitcode files from <tt>clang</tt> using
Nick Lewyckyd66ff0f2009-03-01 20:58:07 +000089 <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
90 synonymous with <tt>-O3 -flto</tt>.</p>
Bill Wendlingea55c832011-09-18 12:37:20 +000091
Peter Collingbourne5f73f682011-11-05 04:17:28 +000092 <p>Any of these flags will also cause <tt>clang</tt> to look for the
93 gold plugin in the <tt>lib</tt> directory under its prefix and pass the
94 <tt>-plugin</tt> option to <tt>ld</tt>. It will not look for an alternate
95 linker, which is why you need gold to be the installed system linker in
96 your path.</p>
Bill Wendlingea55c832011-09-18 12:37:20 +000097
Nick Lewycky5c212802010-04-17 07:00:24 +000098 <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install
Rafael Espindola53843f82010-08-08 21:14:26 +000099 <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your
Nick Lewycky5c212802010-04-17 07:00:24 +0000100 own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to
Bill Wendlingea55c832011-09-18 12:37:20 +0000101 <tt>/usr/bin</tt>.<p>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000102
103<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000104<h3>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000105 <a name="example1">Example of link time optimization</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000106</h3>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000107
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000108<div>
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000109 <p>The following example shows a worked example of the gold plugin mixing
110 LLVM bitcode and native code.
111<pre class="doc_code">
112--- a.c ---
113#include &lt;stdio.h&gt;
114
115extern void foo1(void);
116extern void foo4(void);
117
118void foo2(void) {
119 printf("Foo2\n");
120}
121
122void foo3(void) {
123 foo4();
124}
125
126int main(void) {
127 foo1();
128}
129
130--- b.c ---
131#include &lt;stdio.h&gt;
132
133extern void foo2(void);
134
135void foo1(void) {
136 foo2();
137}
138
139void foo4(void) {
140 printf("Foo4");
141}
142
143--- command lines ---
Peter Collingbourne5f73f682011-11-05 04:17:28 +0000144$ clang -flto a.c -c -o a.o # &lt;-- a.o is LLVM bitcode file
145$ ar q a.a a.o # &lt;-- a.a is an archive with LLVM bitcode
146$ clang b.c -c -o b.o # &lt;-- b.o is native object file
147$ clang -flto a.a b.o -o main # &lt;-- link with LLVMgold plugin
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000148</pre>
Bill Wendlingea55c832011-09-18 12:37:20 +0000149
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000150 <p>Gold informs the plugin that foo3 is never referenced outside the IR,
151 leading LLVM to delete that function. However, unlike in the
NAKAMURA Takumi31c18062011-04-09 02:13:48 +0000152 <a href="LinkTimeOptimization.html#example1">libLTO
Nick Lewyckyf3888ba2009-03-01 21:55:10 +0000153 example</a> gold does not currently eliminate foo4.</p>
154</div>
155
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000156</div>
157
Nick Lewyckyebc67652009-03-01 09:38:29 +0000158<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000159<h2>
160 <a name="lto_autotools">
161 Quickstart for using LTO with autotooled projects
162 </a>
163</h2>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000164<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000165<div>
Bill Wendlingea55c832011-09-18 12:37:20 +0000166 <p>Once your system <tt>ld</tt>, <tt>ar</tt>, and <tt>nm</tt> all support LLVM
167 bitcode, everything is in place for an easy to use LTO build of autotooled
168 projects:</p>
169
Torok Edwin5641f8d2009-06-03 15:06:19 +0000170 <ul>
Rafael Espindola53843f82010-08-08 21:14:26 +0000171 <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000172 <li>Install the newly built binutils to <tt>$PREFIX</tt></li>
Rafael Espindola53843f82010-08-08 21:14:26 +0000173 <li>Copy <tt>Release/lib/LLVMgold.so</tt> to
Bill Wendlingea55c832011-09-18 12:37:20 +0000174 <tt>$PREFIX/lib/bfd-plugins/</tt></li>
175 <li>Set environment variables (<tt>$PREFIX</tt> is where you installed clang and
176 binutils):
177<pre class="doc_code">
Peter Collingbourne5f73f682011-11-05 04:17:28 +0000178export CC="$PREFIX/bin/clang -flto"
179export CXX="$PREFIX/bin/clang++ -flto"
Torok Edwin347c7bb2009-06-04 16:08:10 +0000180export AR="$PREFIX/bin/ar"
181export NM="$PREFIX/bin/nm"
Torok Edwin5641f8d2009-06-03 15:06:19 +0000182export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
183export CFLAGS="-O4"
Nick Lewycky9d813df2009-06-06 18:14:04 +0000184</pre>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000185 </li>
Torok Edwin347c7bb2009-06-04 16:08:10 +0000186 <li>Or you can just set your path:
Bill Wendlingea55c832011-09-18 12:37:20 +0000187<pre class="doc_code">
Torok Edwin347c7bb2009-06-04 16:08:10 +0000188export PATH="$PREFIX/bin:$PATH"
Peter Collingbourne5f73f682011-11-05 04:17:28 +0000189export CC="clang -flto"
190export CXX="clang++ -flto"
Torok Edwin347c7bb2009-06-04 16:08:10 +0000191export RANLIB=/bin/true
192export CFLAGS="-O4"
Bill Wendlingea55c832011-09-18 12:37:20 +0000193</pre></li>
194 <li>Configure &amp; build the project as usual:
195<pre class="doc_code">
196% ./configure &amp;&amp; make &amp;&amp; make check
197</pre></li>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000198 </ul>
Bill Wendlingea55c832011-09-18 12:37:20 +0000199
200 <p>The environment variable settings may work for non-autotooled projects
201 too, but you may need to set the <tt>LD</tt> environment variable as
202 well.</p>
Torok Edwin5641f8d2009-06-03 15:06:19 +0000203</div>
204
205<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000206<h2><a name="licensing">Licensing</a></h2>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000207<!--=========================================================================-->
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000208<div>
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000209 <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file
Nick Lewyckyebc67652009-03-01 09:38:29 +0000210<tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so
211binary is also GPLv3. This can still be used to link non-GPLv3 programs just
Nick Lewyckyef5facd2009-04-13 02:03:40 +0000212as much as gold could without the plugin.</p>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000213</div>
214
215<!-- *********************************************************************** -->
216<hr>
217<address>
218 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
219 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
220 <a href="http://validator.w3.org/check/referer"><img
221 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Nick Lewyckyebc67652009-03-01 09:38:29 +0000222 <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000223 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
Nick Lewycky5c212802010-04-17 07:00:24 +0000224 Last modified: $Date: 2010-04-16 23:58:21 -0800 (Fri, 16 Apr 2010) $
Nick Lewyckyebc67652009-03-01 09:38:29 +0000225</address>
226</body>
227</html>