Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
NAKAMURA Takumi | 5c6e4df | 2011-10-31 11:21:59 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 6 | <title>LLVM gold plugin</title> |
| 7 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
| 8 | </head> |
| 9 | <body> |
| 10 | |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 11 | <h1>LLVM gold plugin</h1> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 12 | <ol> |
| 13 | <li><a href="#introduction">Introduction</a></li> |
| 14 | <li><a href="#build">How to build it</a></li> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 15 | <li><a href="#usage">Usage</a> |
| 16 | <ul> |
| 17 | <li><a href="#example1">Example of link time optimization</a></li> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 18 | <li><a href="#lto_autotools">Quickstart for using LTO with autotooled projects</a></li> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 19 | </ul></li> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 20 | <li><a href="#licensing">Licensing</a></li> |
| 21 | </ol> |
| 22 | <div class="doc_author">Written by Nick Lewycky</div> |
| 23 | |
| 24 | <!--=========================================================================--> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 25 | <h2><a name="introduction">Introduction</a></h2> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 26 | <!--=========================================================================--> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 27 | <div> |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 28 | <p>Building with link time optimization requires cooperation from the |
Nick Lewycky | fd97aa2 | 2009-03-01 09:51:07 +0000 | [diff] [blame] | 29 | system linker. LTO support on Linux systems requires that you use |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 30 | the <a href="http://sourceware.org/binutils">gold linker</a> which supports |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 31 | LTO via plugins. This is the same mechanism used by the |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 32 | <a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a> |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 33 | project.</p> |
| 34 | <p>The LLVM gold plugin implements the |
| 35 | <a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a> |
| 36 | on top of |
NAKAMURA Takumi | 31c1806 | 2011-04-09 02:13:48 +0000 | [diff] [blame] | 37 | <a href="LinkTimeOptimization.html#lto">libLTO</a>. |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 38 | The same plugin can also be used by other tools such as <tt>ar</tt> and |
| 39 | <tt>nm</tt>. |
| 40 | </div> |
| 41 | <!--=========================================================================--> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 42 | <h2><a name="build">How to build it</a></h2> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 43 | <!--=========================================================================--> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 44 | <div> |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 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> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 52 | <ul> |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 53 | <li>To build gold with plugin support: |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 54 | <pre class="doc_code"> |
| 55 | mkdir binutils |
| 56 | cd binutils |
| 57 | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login |
| 58 | <em>{enter "anoncvs" as the password}</em> |
Duncan Sands | ed7713d | 2010-10-06 06:45:11 +0000 | [diff] [blame] | 59 | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 60 | mkdir build |
| 61 | cd build |
| 62 | ../src/configure --enable-gold --enable-plugins |
| 63 | make all-gold |
| 64 | </pre> |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 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 |
| 69 | placed. |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 70 | <li>Build the LLVMgold plugin: Configure LLVM with |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 71 | <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run |
| 72 | <tt>make</tt>. |
| 73 | </ul> |
| 74 | </div> |
| 75 | <!--=========================================================================--> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 76 | <h2><a name="usage">Usage</a></h2> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 77 | <!--=========================================================================--> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 78 | <div> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 79 | |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 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 |
Rafael Espindola | 53843f8 | 2010-08-08 21:14:26 +0000 | [diff] [blame] | 84 | <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 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> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 87 | |
| 88 | <p>You can produce bitcode files from <tt>clang</tt> using |
Nick Lewycky | d66ff0f | 2009-03-01 20:58:07 +0000 | [diff] [blame] | 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> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 91 | |
| 92 | <p><tt>Clang</tt> has a <tt>-use-gold-plugin</tt> option which looks for the |
| 93 | gold plugin in the same directories as it looks for <tt>cc1</tt> and passes |
| 94 | the <tt>-plugin</tt> option to <tt>ld</tt>. It will not look for an alternate |
Nick Lewycky | 050147c | 2009-03-01 18:48:53 +0000 | [diff] [blame] | 95 | linker, which is why you need gold to be the installed system linker in your |
| 96 | path.</p> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 97 | |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 98 | <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install |
Rafael Espindola | 53843f8 | 2010-08-08 21:14:26 +0000 | [diff] [blame] | 99 | <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 100 | own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 101 | <tt>/usr/bin</tt>.<p> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 102 | |
| 103 | <!-- ======================================================================= --> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 104 | <h3> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 105 | <a name="example1">Example of link time optimization</a> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 106 | </h3> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 107 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 108 | <div> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 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"> |
| 112 | --- a.c --- |
| 113 | #include <stdio.h> |
| 114 | |
| 115 | extern void foo1(void); |
| 116 | extern void foo4(void); |
| 117 | |
| 118 | void foo2(void) { |
| 119 | printf("Foo2\n"); |
| 120 | } |
| 121 | |
| 122 | void foo3(void) { |
| 123 | foo4(); |
| 124 | } |
| 125 | |
| 126 | int main(void) { |
| 127 | foo1(); |
| 128 | } |
| 129 | |
| 130 | --- b.c --- |
| 131 | #include <stdio.h> |
| 132 | |
| 133 | extern void foo2(void); |
| 134 | |
| 135 | void foo1(void) { |
| 136 | foo2(); |
| 137 | } |
| 138 | |
| 139 | void foo4(void) { |
| 140 | printf("Foo4"); |
| 141 | } |
| 142 | |
| 143 | --- command lines --- |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 144 | $ clang -flto a.c -c -o a.o # <-- a.o is LLVM bitcode file |
Rafael Espindola | 91f935e | 2009-07-08 11:13:34 +0000 | [diff] [blame] | 145 | $ ar q a.a a.o # <-- a.a is an archive with LLVM bitcode |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 146 | $ clang b.c -c -o b.o # <-- b.o is native object file |
| 147 | $ clang -use-gold-plugin a.a b.o -o main # <-- link with LLVMgold plugin |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 148 | </pre> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 149 | |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 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 |
NAKAMURA Takumi | 31c1806 | 2011-04-09 02:13:48 +0000 | [diff] [blame] | 152 | <a href="LinkTimeOptimization.html#example1">libLTO |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 153 | example</a> gold does not currently eliminate foo4.</p> |
| 154 | </div> |
| 155 | |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 156 | </div> |
| 157 | |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 158 | <!--=========================================================================--> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 159 | <h2> |
| 160 | <a name="lto_autotools"> |
| 161 | Quickstart for using LTO with autotooled projects |
| 162 | </a> |
| 163 | </h2> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 164 | <!--=========================================================================--> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 165 | <div> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 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 |
| 168 | projects:</p> |
| 169 | |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 170 | <ul> |
Rafael Espindola | 53843f8 | 2010-08-08 21:14:26 +0000 | [diff] [blame] | 171 | <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 172 | <li>Install the newly built binutils to <tt>$PREFIX</tt></li> |
Rafael Espindola | 53843f8 | 2010-08-08 21:14:26 +0000 | [diff] [blame] | 173 | <li>Copy <tt>Release/lib/LLVMgold.so</tt> to |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 174 | <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/4.2.1/</tt> and |
| 175 | <tt>$PREFIX/lib/bfd-plugins/</tt></li> |
| 176 | <li>Set environment variables (<tt>$PREFIX</tt> is where you installed clang and |
| 177 | binutils): |
| 178 | <pre class="doc_code"> |
| 179 | export CC="$PREFIX/bin/clang -use-gold-plugin" |
| 180 | export CXX="$PREFIX/bin/clang++ -use-gold-plugin" |
Torok Edwin | 347c7bb | 2009-06-04 16:08:10 +0000 | [diff] [blame] | 181 | export AR="$PREFIX/bin/ar" |
| 182 | export NM="$PREFIX/bin/nm" |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 183 | export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a |
| 184 | export CFLAGS="-O4" |
Nick Lewycky | 9d813df | 2009-06-06 18:14:04 +0000 | [diff] [blame] | 185 | </pre> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 186 | </li> |
Torok Edwin | 347c7bb | 2009-06-04 16:08:10 +0000 | [diff] [blame] | 187 | <li>Or you can just set your path: |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 188 | <pre class="doc_code"> |
Torok Edwin | 347c7bb | 2009-06-04 16:08:10 +0000 | [diff] [blame] | 189 | export PATH="$PREFIX/bin:$PATH" |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 190 | export CC="clang -use-gold-plugin" |
| 191 | export CXX="clang++ -use-gold-plugin" |
Torok Edwin | 347c7bb | 2009-06-04 16:08:10 +0000 | [diff] [blame] | 192 | export RANLIB=/bin/true |
| 193 | export CFLAGS="-O4" |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 194 | </pre></li> |
| 195 | <li>Configure & build the project as usual: |
| 196 | <pre class="doc_code"> |
| 197 | % ./configure && make && make check |
| 198 | </pre></li> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 199 | </ul> |
Bill Wendling | ea55c83 | 2011-09-18 12:37:20 +0000 | [diff] [blame] | 200 | |
| 201 | <p>The environment variable settings may work for non-autotooled projects |
| 202 | too, but you may need to set the <tt>LD</tt> environment variable as |
| 203 | well.</p> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 204 | </div> |
| 205 | |
| 206 | <!--=========================================================================--> |
NAKAMURA Takumi | 05d0265 | 2011-04-18 23:59:50 +0000 | [diff] [blame] | 207 | <h2><a name="licensing">Licensing</a></h2> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 208 | <!--=========================================================================--> |
NAKAMURA Takumi | f5af6ad | 2011-04-23 00:30:22 +0000 | [diff] [blame] | 209 | <div> |
Nick Lewycky | ef5facd | 2009-04-13 02:03:40 +0000 | [diff] [blame] | 210 | <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 211 | <tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so |
| 212 | binary is also GPLv3. This can still be used to link non-GPLv3 programs just |
Nick Lewycky | ef5facd | 2009-04-13 02:03:40 +0000 | [diff] [blame] | 213 | as much as gold could without the plugin.</p> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 214 | </div> |
| 215 | |
| 216 | <!-- *********************************************************************** --> |
| 217 | <hr> |
| 218 | <address> |
| 219 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
| 220 | src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a> |
| 221 | <a href="http://validator.w3.org/check/referer"><img |
| 222 | src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 223 | <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br> |
NAKAMURA Takumi | b9a3363 | 2011-04-09 02:13:37 +0000 | [diff] [blame] | 224 | <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br> |
Nick Lewycky | 5c21280 | 2010-04-17 07:00:24 +0000 | [diff] [blame] | 225 | Last modified: $Date: 2010-04-16 23:58:21 -0800 (Fri, 16 Apr 2010) $ |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 226 | </address> |
| 227 | </body> |
| 228 | </html> |