1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM gold plugin
</title>
6 <link rel=
"stylesheet" href=
"llvm.css" type=
"text/css">
10 <div class=
"doc_title">LLVM gold plugin
</div>
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>
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>
19 <li><a href=
"#licensing">Licensing
</a></li>
21 <div class=
"doc_author">Written by Nick Lewycky
</div>
23 <!--=========================================================================-->
24 <div class=
"doc_section"><a name=
"introduction">Introduction
</a></div>
25 <!--=========================================================================-->
26 <div class=
"doc_text">
27 <p>Building with link time optimization requires cooperation from the
28 system linker. LTO support on Linux systems requires that you use
29 the
<a href=
"http://sourceware.org/binutils">gold linker
</a> which supports
30 LTO via plugins. This is the same mechanism used by the
31 <a href=
"http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO
</a>
33 <p>The LLVM gold plugin implements the
34 <a href=
"http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface
</a>
36 <a href=
"http://llvm.org/docs/LinkTimeOptimization.html#lto">libLTO
</a>.
37 The same plugin can also be used by other tools such as
<tt>ar
</tt> and
40 <!--=========================================================================-->
41 <div class=
"doc_section"><a name=
"build">How to build it
</a></div>
42 <!--=========================================================================-->
43 <div class=
"doc_text">
44 <p>You need to have gold with plugin support and build the LLVMgold
45 plugin. Check whether you have gold running
<tt>/usr/bin/ld -v
</tt>. It will
46 report
“GNU gold
” or else &#
8220GNU ld
” if not. If you have
47 gold, check for plugin support by running
<tt>/usr/bin/ld -plugin
</tt>. If it
48 complains &#
8220missing argument&#
8221 then you have plugin support. If not,
49 such as an
“unknown option
” error then you will either need to
50 build gold or install a version with plugin support.
</p>
52 <li>To build gold with plugin support:
53 <pre class=
"doc_code">
56 cvs -z
9 -d :pserver:anoncvs@sourceware.org:/cvs/src login
57 <em>{enter
"anoncvs" as the password}
</em>
58 cvs -z
9 -d :pserver:anoncvs@sourceware.org:/cvs/src co src
61 ../src/configure --enable-gold --enable-plugins
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
66 but don't have a visible -plugin option, instead relying on the gold plugin
67 being present in
<tt>../lib/bfd-plugins
</tt> relative to where the binaries are
69 <li>Build the LLVMgold plugin: Configure LLVM with
70 <tt>--with-binutils-include=/path/to/binutils/src/include
</tt> and run
74 <!--=========================================================================-->
75 <div class=
"doc_section"><a name=
"usage">Usage
</a></div>
76 <!--=========================================================================-->
77 <div class=
"doc_text">
78 <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
82 <tt>ld-new -plugin /path/to/libLLVMgold.so
</tt> to test it out. Once you're
83 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
86 <tt>-emit-llvm
</tt> or
<tt>-flto
</tt>, or the
<tt>-O4
</tt> flag which is
87 synonymous with
<tt>-O3 -flto
</tt>.
</p>
88 <p><tt>llvm-gcc
</tt> has a
<tt>-use-gold-plugin
</tt> option which looks
89 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
93 <p>If you want
<tt>ar
</tt> and
<tt>nm
</tt> to work seamlessly as well, install
94 <tt>libLLVMgold.so
</tt> to
<tt>/usr/lib/bfd-plugins
</tt>. If you built your
95 own gold, be sure to install the
<tt>ar
</tt> and
<tt>nm-new
</tt> you built to
100 <!-- ======================================================================= -->
101 <div class=
"doc_subsection">
102 <a name=
"example1">Example of link time optimization
</a>
105 <div class=
"doc_text">
106 <p>The following example shows a worked example of the gold plugin mixing
107 LLVM bitcode and native code.
108 <pre class=
"doc_code">
110 #include
<stdio.h
>
112 extern void foo1(void);
113 extern void foo4(void);
128 #include
<stdio.h
>
130 extern void foo2(void);
140 --- command lines ---
141 $ llvm-gcc -flto a.c -c -o a.o #
<-- a.o is LLVM bitcode file
142 $ ar q a.a a.o #
<-- a.a is an archive with LLVM bitcode
143 $ llvm-gcc b.c -c -o b.o #
<-- b.o is native object file
144 $ llvm-gcc -use-gold-plugin a.a b.o -o main #
<-- link with LLVMgold plugin
146 <p>Gold informs the plugin that foo3 is never referenced outside the IR,
147 leading LLVM to delete that function. However, unlike in the
148 <a href=
"http://llvm.org/docs/LinkTimeOptimization.html#example1">libLTO
149 example
</a> gold does not currently eliminate foo4.
</p>
152 <!--=========================================================================-->
153 <div class=
"doc_section"><a name=
"lto_autotools">Quickstart for using LTO with autotooled projects
</a></div>
154 <!--=========================================================================-->
155 <div class=
"doc_text">
156 <p>Once your system
<tt>ld
</tt>,
<tt>ar
</tt> and
<tt>nm
</tt> all support LLVM
157 bitcode, everything is in place for an easy to use LTO build of autotooled
160 <li>Follow the instructions
<a href=
"#build">on how to build libLLVMgold.so
</a>.
</li>
161 <li>Install the newly built binutils to
<tt>$PREFIX
</tt></li>
162 <li>Copy
<tt>Release/lib/libLLVMgold.so
</tt> to
163 <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/
4.2.1/
</tt> and
164 <tt>$PREFIX/lib/bfd-plugins/
</tt></li>
165 <li>Set environment variables (
<tt>$PREFIX
</tt> is where you installed llvm-gcc and
167 <pre class=
"doc_code">
168 export
CC=
"$PREFIX/bin/llvm-gcc -use-gold-plugin"
169 export
CXX=
"$PREFIX/bin/llvm-g++ -use-gold-plugin"
170 export
AR=
"$PREFIX/bin/ar"
171 export
NM=
"$PREFIX/bin/nm"
172 export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
176 <li>Or you can just set your path:
177 <pre class=
"doc_code">
178 export
PATH=
"$PREFIX/bin:$PATH"
179 export
CC=
"llvm-gcc -use-gold-plugin"
180 export
CXX=
"llvm-g++ -use-gold-plugin"
181 export RANLIB=/bin/true
185 <li>Configure
& build the project as usual:
<tt>./configure
&& make
&& make check
</tt> </li>
187 <p> The environment variable settings may work for non-autotooled projects
188 too, but you may need to set the
<tt>LD
</tt> environment variable as well.
</p>
191 <!--=========================================================================-->
192 <div class=
"doc_section"><a name=
"licensing">Licensing
</a></div>
193 <!--=========================================================================-->
194 <div class=
"doc_text">
195 <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file
196 <tt>plugin-api.h
</tt> from gold which means that the resulting LLVMgold.so
197 binary is also GPLv3. This can still be used to link non-GPLv3 programs just
198 as much as gold could without the plugin.
</p>
201 <!-- *********************************************************************** -->
204 <a href=
"http://jigsaw.w3.org/css-validator/check/referer"><img
205 src=
"http://jigsaw.w3.org/css-validator/images/vcss-blue" alt=
"Valid CSS"></a>
206 <a href=
"http://validator.w3.org/check/referer"><img
207 src=
"http://www.w3.org/Icons/valid-html401-blue" alt=
"Valid HTML 4.01"></a>
208 <a href=
"mailto:nicholas@metrix.on.ca">Nick Lewycky
</a><br>
209 <a href=
"http://llvm.org">The LLVM Compiler Infrastructure
</a><br>
210 Last modified: $Date:
2010-
04-
16 23:
58:
21 -
0800 (Fri,
16 Apr
2010) $