• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!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<h1>LLVM gold plugin</h1>
11<ol>
12  <li><a href="#introduction">Introduction</a></li>
13  <li><a href="#build">How to build it</a></li>
14  <li><a href="#usage">Usage</a>
15  <ul>
16    <li><a href="#example1">Example of link time optimization</a></li>
17    <li><a href="#lto_autotools">Quickstart for using LTO with autotooled projects</a></li>
18  </ul></li>
19  <li><a href="#licensing">Licensing</a></li>
20</ol>
21<div class="doc_author">Written by Nick Lewycky</div>
22
23<!--=========================================================================-->
24<h2><a name="introduction">Introduction</a></h2>
25<!--=========================================================================-->
26<div>
27  <p>Building with link time optimization requires cooperation from the
28system linker. LTO support on Linux systems requires that you use
29the <a href="http://sourceware.org/binutils">gold linker</a> which supports
30LTO via plugins. This is the same mechanism used by the
31<a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a>
32project.</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
36<a href="LinkTimeOptimization.html#lto">libLTO</a>.
37The same plugin can also be used by other tools such as <tt>ar</tt> and
38<tt>nm</tt>.
39</div>
40<!--=========================================================================-->
41<h2><a name="build">How to build it</a></h2>
42<!--=========================================================================-->
43<div>
44  <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>
51<ul>
52  <li>To build gold with plugin support:
53    <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>
58cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils
59mkdir build
60cd build
61../src/configure --enable-gold --enable-plugins
62make all-gold
63</pre>
64    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.
69    <li>Build the LLVMgold plugin: Configure LLVM with
70    <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run
71    <tt>make</tt>.
72</ul>
73</div>
74<!--=========================================================================-->
75<h2><a name="usage">Usage</a></h2>
76<!--=========================================================================-->
77<div>
78
79  <p>The linker takes a <tt>-plugin</tt> option that points to the path of
80  the plugin <tt>.so</tt> file. To find out what link command <tt>gcc</tt>
81  would run in a given situation, run <tt>gcc -v <em>[...]</em></tt> and look
82  for the line where it runs <tt>collect2</tt>. Replace that with
83  <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're
84  ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt>
85  then replace it with <tt>ld-new</tt>.</p>
86
87  <p>You can produce bitcode files from <tt>clang</tt> using
88  <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
89  synonymous with <tt>-O3 -flto</tt>.</p>
90
91  <p><tt>Clang</tt> has a <tt>-use-gold-plugin</tt> option which looks for the
92  gold plugin in the same directories as it looks for <tt>cc1</tt> and passes
93  the <tt>-plugin</tt> option to <tt>ld</tt>. It will not look for an alternate
94  linker, which is why you need gold to be the installed system linker in your
95  path.</p>
96
97  <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install
98  <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your
99  own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to
100  <tt>/usr/bin</tt>.<p>
101
102<!-- ======================================================================= -->
103<h3>
104  <a name="example1">Example of link time optimization</a>
105</h3>
106
107<div>
108  <p>The following example shows a worked example of the gold plugin mixing
109  LLVM bitcode and native code.
110<pre class="doc_code">
111--- a.c ---
112#include &lt;stdio.h&gt;
113
114extern void foo1(void);
115extern void foo4(void);
116
117void foo2(void) {
118  printf("Foo2\n");
119}
120
121void foo3(void) {
122  foo4();
123}
124
125int main(void) {
126  foo1();
127}
128
129--- b.c ---
130#include &lt;stdio.h&gt;
131
132extern void foo2(void);
133
134void foo1(void) {
135  foo2();
136}
137
138void foo4(void) {
139  printf("Foo4");
140}
141
142--- command lines ---
143$ clang -flto a.c -c -o a.o                 # &lt;-- a.o is LLVM bitcode file
144$ ar q a.a a.o                              # &lt;-- a.a is an archive with LLVM bitcode
145$ clang b.c -c -o b.o                       # &lt;-- b.o is native object file
146$ clang -use-gold-plugin a.a b.o -o main    # &lt;-- link with LLVMgold plugin
147</pre>
148
149  <p>Gold informs the plugin that foo3 is never referenced outside the IR,
150  leading LLVM to delete that function. However, unlike in the
151  <a href="LinkTimeOptimization.html#example1">libLTO
152  example</a> gold does not currently eliminate foo4.</p>
153</div>
154
155</div>
156
157<!--=========================================================================-->
158<h2>
159  <a name="lto_autotools">
160    Quickstart for using LTO with autotooled projects
161  </a>
162</h2>
163<!--=========================================================================-->
164<div>
165  <p>Once your system <tt>ld</tt>, <tt>ar</tt>, and <tt>nm</tt> all support LLVM
166     bitcode, everything is in place for an easy to use LTO build of autotooled
167     projects:</p>
168
169  <ul>
170    <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li>
171    <li>Install the newly built binutils to <tt>$PREFIX</tt></li>
172    <li>Copy <tt>Release/lib/LLVMgold.so</tt> to
173        <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/4.2.1/</tt> and
174        <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">
178export CC="$PREFIX/bin/clang -use-gold-plugin"
179export CXX="$PREFIX/bin/clang++ -use-gold-plugin"
180export AR="$PREFIX/bin/ar"
181export NM="$PREFIX/bin/nm"
182export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
183export CFLAGS="-O4"
184</pre>
185     </li>
186     <li>Or you can just set your path:
187<pre class="doc_code">
188export PATH="$PREFIX/bin:$PATH"
189export CC="clang -use-gold-plugin"
190export CXX="clang++ -use-gold-plugin"
191export RANLIB=/bin/true
192export CFLAGS="-O4"
193</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>
198   </ul>
199
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>
203</div>
204
205<!--=========================================================================-->
206<h2><a name="licensing">Licensing</a></h2>
207<!--=========================================================================-->
208<div>
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
211binary is also GPLv3. This can still be used to link non-GPLv3 programs just
212as much as gold could without the plugin.</p>
213</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>
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) $
225</address>
226</body>
227</html>
228