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> |
| 5 | <title>LLVM gold plugin</title> |
| 6 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
| 7 | </head> |
| 8 | <body> |
| 9 | |
| 10 | <div class="doc_title">LLVM gold plugin</div> |
| 11 | <ol> |
| 12 | <li><a href="#introduction">Introduction</a></li> |
| 13 | <li><a href="#build">How to build it</a></li> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 14 | <li><a href="#usage">Usage</a> |
| 15 | <ul> |
| 16 | <li><a href="#example1">Example of link time optimization</a></li> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 17 | <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] | 18 | </ul></li> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 19 | <li><a href="#licensing">Licensing</a></li> |
| 20 | </ol> |
| 21 | <div class="doc_author">Written by Nick Lewycky</div> |
| 22 | |
| 23 | <!--=========================================================================--> |
| 24 | <div class="doc_section"><a name="introduction">Introduction</a></div> |
| 25 | <!--=========================================================================--> |
| 26 | <div class="doc_text"> |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 27 | <p>Building with link time optimization requires cooperation from the |
Nick Lewycky | fd97aa2 | 2009-03-01 09:51:07 +0000 | [diff] [blame] | 28 | system linker. LTO support on Linux systems requires that you use |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 29 | the <a href="http://sourceware.org/binutils">gold linker</a> which supports |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 30 | LTO via plugins. This is the same system used by the upcoming |
| 31 | <a href="http://gcc.gnu.org/wiki/LinkTimeOptimization">GCC LTO</a> |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 32 | project.</p> |
| 33 | <p>The LLVM gold plugin implements the |
| 34 | <a href="http://gcc.gnu.org/wiki/whopr/driver">gold plugin interface</a> |
| 35 | on top of |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 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 |
| 38 | <tt>nm</tt>. |
| 39 | </div> |
| 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 build gold with plugin support and build the LLVMgold |
| 45 | plugin.</p> |
| 46 | <ul> |
| 47 | <li>Build gold with plugin support: |
| 48 | <pre class="doc_code"> |
| 49 | mkdir binutils |
| 50 | cd binutils |
| 51 | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src login |
| 52 | <em>{enter "anoncvs" as the password}</em> |
| 53 | cvs -z 9 -d :pserver:anoncvs@sourceware.org:/cvs/src co src |
| 54 | mkdir build |
| 55 | cd build |
| 56 | ../src/configure --enable-gold --enable-plugins |
| 57 | make all-gold |
| 58 | </pre> |
| 59 | That should leave you with binutils/build/gold/ld-new which supports the |
Nick Lewycky | 017730c | 2009-03-01 21:06:42 +0000 | [diff] [blame] | 60 | <tt>-plugin</tt> option. |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 61 | |
Duncan Sands | 10f0675 | 2009-03-01 15:19:03 +0000 | [diff] [blame] | 62 | <li>Build the LLVMgold plugin: Configure LLVM with |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 63 | <tt>--with-binutils-include=/path/to/binutils/src/include</tt> and run |
| 64 | <tt>make</tt>. |
| 65 | </ul> |
| 66 | </div> |
| 67 | <!--=========================================================================--> |
| 68 | <div class="doc_section"><a name="usage">Usage</a></div> |
| 69 | <!--=========================================================================--> |
| 70 | <div class="doc_text"> |
| 71 | <p>The linker takes a <tt>-plugin</tt> option that points to the path of |
| 72 | the plugin <tt>.so</tt> file. To find out what link command <tt>gcc</tt> |
| 73 | would run in a given situation, run <tt>gcc -v <em>[...]</em></tt> and look |
| 74 | for the line where it runs <tt>collect2</tt>. Replace that with |
| 75 | <tt>ld-new -plugin /path/to/LLVMgold.so</tt> to test it out. Once you're |
| 76 | ready to switch to using gold, backup your existing <tt>/usr/bin/ld</tt> |
| 77 | then replace it with <tt>ld-new</tt>.</p> |
| 78 | <p>You can produce bitcode files from <tt>llvm-gcc</tt> using |
Nick Lewycky | d66ff0f | 2009-03-01 20:58:07 +0000 | [diff] [blame] | 79 | <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is |
| 80 | synonymous with <tt>-O3 -flto</tt>.</p> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 81 | <p><tt>llvm-gcc</tt> has a <tt>-use-gold-plugin</tt> option which looks |
Nick Lewycky | 050147c | 2009-03-01 18:48:53 +0000 | [diff] [blame] | 82 | for the gold plugin in the same directories as it looks for <tt>cc1</tt> and |
| 83 | passes the <tt>-plugin</tt> option to ld. It will not look for an alternate |
| 84 | linker, which is why you need gold to be the installed system linker in your |
| 85 | path.</p> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 86 | </div> |
Nick Lewycky | f3888ba | 2009-03-01 21:55:10 +0000 | [diff] [blame] | 87 | |
| 88 | <!-- ======================================================================= --> |
| 89 | <div class="doc_subsection"> |
| 90 | <a name="example1">Example of link time optimization</a> |
| 91 | </div> |
| 92 | |
| 93 | <div class="doc_text"> |
| 94 | <p>The following example shows a worked example of the gold plugin mixing |
| 95 | LLVM bitcode and native code. |
| 96 | <pre class="doc_code"> |
| 97 | --- a.c --- |
| 98 | #include <stdio.h> |
| 99 | |
| 100 | extern void foo1(void); |
| 101 | extern void foo4(void); |
| 102 | |
| 103 | void foo2(void) { |
| 104 | printf("Foo2\n"); |
| 105 | } |
| 106 | |
| 107 | void foo3(void) { |
| 108 | foo4(); |
| 109 | } |
| 110 | |
| 111 | int main(void) { |
| 112 | foo1(); |
| 113 | } |
| 114 | |
| 115 | --- b.c --- |
| 116 | #include <stdio.h> |
| 117 | |
| 118 | extern void foo2(void); |
| 119 | |
| 120 | void foo1(void) { |
| 121 | foo2(); |
| 122 | } |
| 123 | |
| 124 | void foo4(void) { |
| 125 | printf("Foo4"); |
| 126 | } |
| 127 | |
| 128 | --- command lines --- |
| 129 | $ llvm-gcc -flto a.c -c -o a.o # <-- a.o is LLVM bitcode file |
| 130 | $ llvm-gcc b.c -c -o b.o # <-- b.o is native object file |
| 131 | $ llvm-gcc -use-gold-plugin a.o b.o -o main # <-- link with LLVMgold plugin |
| 132 | </pre> |
| 133 | <p>Gold informs the plugin that foo3 is never referenced outside the IR, |
| 134 | leading LLVM to delete that function. However, unlike in the |
| 135 | <a href="http://llvm.org/docs/LinkTimeOptimization.html#example1">libLTO |
| 136 | example</a> gold does not currently eliminate foo4.</p> |
| 137 | </div> |
| 138 | |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 139 | <!--=========================================================================--> |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 140 | <div class="doc_section"><a name="lto_autotools">Quickstart for using LTO with autotooled projects</a></div> |
| 141 | <!--=========================================================================--> |
| 142 | <div class="doc_text"> |
| 143 | <p><tt>gold</tt>, <tt>ar</tt> and <tt>nm</tt> all support plugins now, so everything should be |
| 144 | in place for an easy to use LTO build of autotooled projects:</p> |
| 145 | <ul> |
| 146 | <li>Follow the instructions <a href="#build">on how to build libLLVMgold.so</a>.</li> |
| 147 | <li>Install the newly built binutils to <tt>$PREFIX</tt></li> |
| 148 | <li>Copy <tt>Release/lib/libLLVMgold.so</tt> to |
| 149 | <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/4.2.1/</tt> and |
| 150 | <tt>$PREFIX/lib/bfd-plugins/</tt></li> |
| 151 | <li>Set environment variables (<tt>$PREFIX</tt> is where you installed llvm-gcc and |
| 152 | binutils): |
| 153 | <pre class="doc_code"> |
| 154 | export CC="$PREFIX/bin/llvm-gcc -use-gold-plugin" |
Torok Edwin | 4734ed8 | 2009-06-03 15:42:26 +0000 | [diff] [blame^] | 155 | export CXX="$PREFIX/bin/llvm-g++ -use-gold-plugin" |
Torok Edwin | 5641f8d | 2009-06-03 15:06:19 +0000 | [diff] [blame] | 156 | export AR="$PREFIX/bin/ar --plugin libLLVMgold.so" |
| 157 | export NM="$PREFIX/bin/nm --plugin libLLVMgold.so" |
| 158 | export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a |
| 159 | export CFLAGS="-O4" |
| 160 | </pre> |
| 161 | </li> |
| 162 | <li>Configure & build the project as usual: <tt>./configure && make && make check</tt> </li> |
| 163 | </ul> |
| 164 | <p> The environment variable settings may work for non-autotooled projects |
| 165 | too, but you may need to set the <tt>LD</tt> environment variable as well.</p> |
| 166 | </div> |
| 167 | |
| 168 | <!--=========================================================================--> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 169 | <div class="doc_section"><a name="licensing">Licensing</a></div> |
| 170 | <!--=========================================================================--> |
| 171 | <div class="doc_text"> |
Nick Lewycky | ef5facd | 2009-04-13 02:03:40 +0000 | [diff] [blame] | 172 | <p>Gold is licensed under the GPLv3. LLVMgold uses the interface file |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 173 | <tt>plugin-api.h</tt> from gold which means that the resulting LLVMgold.so |
| 174 | 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] | 175 | as much as gold could without the plugin.</p> |
Nick Lewycky | ebc6765 | 2009-03-01 09:38:29 +0000 | [diff] [blame] | 176 | </div> |
| 177 | |
| 178 | <!-- *********************************************************************** --> |
| 179 | <hr> |
| 180 | <address> |
| 181 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
| 182 | src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a> |
| 183 | <a href="http://validator.w3.org/check/referer"><img |
| 184 | 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] | 185 | <a href="mailto:nicholas@metrix.on.ca">Nick Lewycky</a><br> |
| 186 | <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br> |
| 187 | Last modified: $Date: 2009-01-01 23:10:51 -0800 (Thu, 01 Jan 2009) $ |
| 188 | </address> |
| 189 | </body> |
| 190 | </html> |