xref: /aosp_15_r20/external/llvm/docs/GoldPlugin.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker====================
2*9880d681SAndroid Build Coastguard WorkerThe LLVM gold plugin
3*9880d681SAndroid Build Coastguard Worker====================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard WorkerIntroduction
6*9880d681SAndroid Build Coastguard Worker============
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerBuilding with link time optimization requires cooperation from
9*9880d681SAndroid Build Coastguard Workerthe system linker. LTO support on Linux systems requires that you use the
10*9880d681SAndroid Build Coastguard Worker`gold linker`_ which supports LTO via plugins. This is the same mechanism
11*9880d681SAndroid Build Coastguard Workerused by the `GCC LTO`_ project.
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard WorkerThe LLVM gold plugin implements the gold plugin interface on top of
14*9880d681SAndroid Build Coastguard Worker:ref:`libLTO`.  The same plugin can also be used by other tools such as
15*9880d681SAndroid Build Coastguard Worker``ar`` and ``nm``.
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker.. _`gold linker`: http://sourceware.org/binutils
18*9880d681SAndroid Build Coastguard Worker.. _`GCC LTO`: http://gcc.gnu.org/wiki/LinkTimeOptimization
19*9880d681SAndroid Build Coastguard Worker.. _`gold plugin interface`: http://gcc.gnu.org/wiki/whopr/driver
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker.. _lto-how-to-build:
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard WorkerHow to build it
24*9880d681SAndroid Build Coastguard Worker===============
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard WorkerYou need to have gold with plugin support and build the LLVMgold plugin.
27*9880d681SAndroid Build Coastguard WorkerCheck whether you have gold running ``/usr/bin/ld -v``. It will report "GNU
28*9880d681SAndroid Build Coastguard Workergold" or else "GNU ld" if not. If you have gold, check for plugin support
29*9880d681SAndroid Build Coastguard Workerby running ``/usr/bin/ld -plugin``. If it complains "missing argument" then
30*9880d681SAndroid Build Coastguard Workeryou have plugin support. If not, such as an "unknown option" error then you
31*9880d681SAndroid Build Coastguard Workerwill either need to build gold or install a version with plugin support.
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker* Download, configure and build gold with plugin support:
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker  .. code-block:: bash
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker     $ git clone --depth 1 git://sourceware.org/git/binutils-gdb.git binutils
38*9880d681SAndroid Build Coastguard Worker     $ mkdir build
39*9880d681SAndroid Build Coastguard Worker     $ cd build
40*9880d681SAndroid Build Coastguard Worker     $ ../binutils/configure --enable-gold --enable-plugins --disable-werror
41*9880d681SAndroid Build Coastguard Worker     $ make all-gold
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker  That should leave you with ``build/gold/ld-new`` which supports
44*9880d681SAndroid Build Coastguard Worker  the ``-plugin`` option. Running ``make`` will additionally build
45*9880d681SAndroid Build Coastguard Worker  ``build/binutils/ar`` and ``nm-new`` binaries supporting plugins.
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker* Build the LLVMgold plugin. Run CMake with
48*9880d681SAndroid Build Coastguard Worker  ``-DLLVM_BINUTILS_INCDIR=/path/to/binutils/include``.  The correct include
49*9880d681SAndroid Build Coastguard Worker  path will contain the file ``plugin-api.h``.
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard WorkerUsage
52*9880d681SAndroid Build Coastguard Worker=====
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard WorkerThe linker takes a ``-plugin`` option that points to the path of
55*9880d681SAndroid Build Coastguard Workerthe plugin ``.so`` file. To find out what link command ``gcc``
56*9880d681SAndroid Build Coastguard Workerwould run in a given situation, run ``gcc -v [...]`` and
57*9880d681SAndroid Build Coastguard Workerlook for the line where it runs ``collect2``. Replace that with
58*9880d681SAndroid Build Coastguard Worker``ld-new -plugin /path/to/LLVMgold.so`` to test it out. Once you're
59*9880d681SAndroid Build Coastguard Workerready to switch to using gold, backup your existing ``/usr/bin/ld``
60*9880d681SAndroid Build Coastguard Workerthen replace it with ``ld-new``.
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard WorkerYou should produce bitcode files from ``clang`` with the option
63*9880d681SAndroid Build Coastguard Worker``-flto``. This flag will also cause ``clang`` to look for the gold plugin in
64*9880d681SAndroid Build Coastguard Workerthe ``lib`` directory under its prefix and pass the ``-plugin`` option to
65*9880d681SAndroid Build Coastguard Worker``ld``. It will not look for an alternate linker, which is why you need
66*9880d681SAndroid Build Coastguard Workergold to be the installed system linker in your path.
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker``ar`` and ``nm`` also accept the ``-plugin`` option and it's possible to
69*9880d681SAndroid Build Coastguard Workerto install ``LLVMgold.so`` to ``/usr/lib/bfd-plugins`` for a seamless setup.
70*9880d681SAndroid Build Coastguard WorkerIf you built your own gold, be sure to install the ``ar`` and ``nm-new`` you
71*9880d681SAndroid Build Coastguard Workerbuilt to ``/usr/bin``.
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard WorkerExample of link time optimization
75*9880d681SAndroid Build Coastguard Worker---------------------------------
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard WorkerThe following example shows a worked example of the gold plugin mixing LLVM
78*9880d681SAndroid Build Coastguard Workerbitcode and native code.
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker.. code-block:: c
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker   --- a.c ---
83*9880d681SAndroid Build Coastguard Worker   #include <stdio.h>
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker   extern void foo1(void);
86*9880d681SAndroid Build Coastguard Worker   extern void foo4(void);
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker   void foo2(void) {
89*9880d681SAndroid Build Coastguard Worker     printf("Foo2\n");
90*9880d681SAndroid Build Coastguard Worker   }
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker   void foo3(void) {
93*9880d681SAndroid Build Coastguard Worker     foo4();
94*9880d681SAndroid Build Coastguard Worker   }
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker   int main(void) {
97*9880d681SAndroid Build Coastguard Worker     foo1();
98*9880d681SAndroid Build Coastguard Worker   }
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker   --- b.c ---
101*9880d681SAndroid Build Coastguard Worker   #include <stdio.h>
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker   extern void foo2(void);
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker   void foo1(void) {
106*9880d681SAndroid Build Coastguard Worker     foo2();
107*9880d681SAndroid Build Coastguard Worker   }
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker   void foo4(void) {
110*9880d681SAndroid Build Coastguard Worker     printf("Foo4");
111*9880d681SAndroid Build Coastguard Worker   }
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker   --- command lines ---
116*9880d681SAndroid Build Coastguard Worker   $ clang -flto a.c -c -o a.o      # <-- a.o is LLVM bitcode file
117*9880d681SAndroid Build Coastguard Worker   $ ar q a.a a.o                   # <-- a.a is an archive with LLVM bitcode
118*9880d681SAndroid Build Coastguard Worker   $ clang b.c -c -o b.o            # <-- b.o is native object file
119*9880d681SAndroid Build Coastguard Worker   $ clang -flto a.a b.o -o main    # <-- link with LLVMgold plugin
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard WorkerGold informs the plugin that foo3 is never referenced outside the IR,
122*9880d681SAndroid Build Coastguard Workerleading LLVM to delete that function. However, unlike in the :ref:`libLTO
123*9880d681SAndroid Build Coastguard Workerexample <libLTO-example>` gold does not currently eliminate foo4.
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard WorkerQuickstart for using LTO with autotooled projects
126*9880d681SAndroid Build Coastguard Worker=================================================
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard WorkerOnce your system ``ld``, ``ar``, and ``nm`` all support LLVM bitcode,
129*9880d681SAndroid Build Coastguard Workereverything is in place for an easy to use LTO build of autotooled projects:
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker* Follow the instructions :ref:`on how to build LLVMgold.so
132*9880d681SAndroid Build Coastguard Worker  <lto-how-to-build>`.
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker* Install the newly built binutils to ``$PREFIX``
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker* Copy ``Release/lib/LLVMgold.so`` to ``$PREFIX/lib/bfd-plugins/``
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker* Set environment variables (``$PREFIX`` is where you installed clang and
139*9880d681SAndroid Build Coastguard Worker  binutils):
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker  .. code-block:: bash
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker     export CC="$PREFIX/bin/clang -flto"
144*9880d681SAndroid Build Coastguard Worker     export CXX="$PREFIX/bin/clang++ -flto"
145*9880d681SAndroid Build Coastguard Worker     export AR="$PREFIX/bin/ar"
146*9880d681SAndroid Build Coastguard Worker     export NM="$PREFIX/bin/nm"
147*9880d681SAndroid Build Coastguard Worker     export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker* Or you can just set your path:
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker  .. code-block:: bash
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker     export PATH="$PREFIX/bin:$PATH"
154*9880d681SAndroid Build Coastguard Worker     export CC="clang -flto"
155*9880d681SAndroid Build Coastguard Worker     export CXX="clang++ -flto"
156*9880d681SAndroid Build Coastguard Worker     export RANLIB=/bin/true
157*9880d681SAndroid Build Coastguard Worker* Configure and build the project as usual:
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard Worker  .. code-block:: bash
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker     % ./configure && make && make check
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard WorkerThe environment variable settings may work for non-autotooled projects too,
164*9880d681SAndroid Build Coastguard Workerbut you may need to set the ``LD`` environment variable as well.
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard WorkerLicensing
167*9880d681SAndroid Build Coastguard Worker=========
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard WorkerGold is licensed under the GPLv3. LLVMgold uses the interface file
170*9880d681SAndroid Build Coastguard Worker``plugin-api.h`` from gold which means that the resulting ``LLVMgold.so``
171*9880d681SAndroid Build Coastguard Workerbinary is also GPLv3. This can still be used to link non-GPLv3 programs
172*9880d681SAndroid Build Coastguard Workerjust as much as gold could without the plugin.
173