1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>LLVM gold plugin</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
11 <h1>LLVM gold plugin</h1>
13 <li><a href="#introduction">Introduction</a></li>
14 <li><a href="#build">How to build it</a></li>
15 <li><a href="#usage">Usage</a>
17 <li><a href="#example1">Example of link time optimization</a></li>
18 <li><a href="#lto_autotools">Quickstart for using LTO with autotooled projects</a></li>
20 <li><a href="#licensing">Licensing</a></li>
22 <div class="doc_author">Written by Nick Lewycky</div>
24 <!--=========================================================================-->
25 <h2><a name="introduction">Introduction</a></h2>
26 <!--=========================================================================-->
28 <p>Building with link time optimization requires cooperation from the
29 system linker. LTO support on Linux systems requires that you use
30 the <a href="http://sourceware.org/binutils">gold linker</a> which supports
31 LTO via plugins. This is the same mechanism used by the
32 <a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a>
34 <p>The LLVM gold plugin implements the
35 <a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a>
37 <a href="LinkTimeOptimization.html#lto">libLTO</a>.
38 The same plugin can also be used by other tools such as <tt>ar</tt> and
41 <!--=========================================================================-->
42 <h2><a name="build">How to build it</a></h2>
43 <!--=========================================================================-->
45 <p>You need to have gold with plugin support and build the LLVMgold
46 plugin. Check whether you have gold running <tt>/usr/bin/ld -v</tt>. It will
47 report “GNU gold” or else “GNU ld” if not. If you have
48 gold, check for plugin support by running <tt>/usr/bin/ld -plugin</tt>. If it
49 complains “missing argument” then you have plugin support. If not,
50 such as an “unknown option” error then you will either need to
51 build gold or install a version with plugin support.</p>
53 <li>To build gold with plugin support:
54 <pre class="doc_code">
57 cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
58 <em>{enter "anoncvs" as the password}</em>
59 cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
62 ../src/configure --enable-gold --enable-plugins
65 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
67 but don't have a visible -plugin option, instead relying on the gold plugin
68 being present in <tt>../lib/bfd-plugins</tt> relative to where the binaries are
70 <li>Build the LLVMgold plugin: Configure LLVM with
71 <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run
75 <!--=========================================================================-->
76 <h2><a name="usage">Usage</a></h2>
77 <!--=========================================================================-->
80 <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
84 <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're
85 ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt>
86 then replace it with <tt>ld-new</tt>.</p>
88 <p>You can produce bitcode files from <tt>clang</tt> using
89 <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
90 synonymous with <tt>-O3 -flto</tt>.</p>
92 <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
98 <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install
99 <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your
100 own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to
101 <tt>/usr/bin</tt>.<p>
103 <!-- ======================================================================= -->
105 <a name="example1">Example of link time optimization</a>
109 <p>The following example shows a worked example of the gold plugin mixing
110 LLVM bitcode and native code.
111 <pre class="doc_code">
113 #include <stdio.h>
115 extern void foo1(void);
116 extern void foo4(void);
131 #include <stdio.h>
133 extern void foo2(void);
143 --- command lines ---
144 $ clang -flto a.c -c -o a.o # <-- a.o is LLVM bitcode file
145 $ ar q a.a a.o # <-- a.a is an archive with LLVM bitcode
146 $ clang b.c -c -o b.o # <-- b.o is native object file
147 $ clang -flto a.a b.o -o main # <-- link with LLVMgold plugin
150 <p>Gold informs the plugin that foo3 is never referenced outside the IR,
151 leading LLVM to delete that function. However, unlike in the
152 <a href="LinkTimeOptimization.html#example1">libLTO
153 example</a> gold does not currently eliminate foo4.</p>
158 <!--=========================================================================-->
160 <a name="lto_autotools">
161 Quickstart for using LTO with autotooled projects
164 <!--=========================================================================-->
166 <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
171 <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li>
172 <li>Install the newly built binutils to <tt>$PREFIX</tt></li>
173 <li>Copy <tt>Release/lib/LLVMgold.so</tt> to
174 <tt>$PREFIX/lib/bfd-plugins/</tt></li>
175 <li>Set environment variables (<tt>$PREFIX</tt> is where you installed clang and
177 <pre class="doc_code">
178 export CC="$PREFIX/bin/clang -flto"
179 export CXX="$PREFIX/bin/clang++ -flto"
180 export AR="$PREFIX/bin/ar"
181 export NM="$PREFIX/bin/nm"
182 export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
186 <li>Or you can just set your path:
187 <pre class="doc_code">
188 export PATH="$PREFIX/bin:$PATH"
189 export CC="clang -flto"
190 export CXX="clang++ -flto"
191 export RANLIB=/bin/true
194 <li>Configure & build the project as usual:
195 <pre class="doc_code">
196 % ./configure && make && make check
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
205 <!--=========================================================================-->
206 <h2><a name="licensing">Licensing</a></h2>
207 <!--=========================================================================-->
209 <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file
210 <tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so
211 binary is also GPLv3. This can still be used to link non-GPLv3 programs just
212 as much as gold could without the plugin.</p>
215 <!-- *********************************************************************** -->
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>
222 <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br>
223 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
224 Last modified: $Date: 2010-04-16 23:58:21 -0800 (Fri, 16 Apr 2010) $