1*9880d681SAndroid Build Coastguard Worker====================================================== 2*9880d681SAndroid Build Coastguard WorkerLLVM Link Time Optimization: Design and Implementation 3*9880d681SAndroid Build Coastguard Worker====================================================== 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Worker.. contents:: 6*9880d681SAndroid Build Coastguard Worker :local: 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard WorkerDescription 9*9880d681SAndroid Build Coastguard Worker=========== 10*9880d681SAndroid Build Coastguard Worker 11*9880d681SAndroid Build Coastguard WorkerLLVM features powerful intermodular optimizations which can be used at link 12*9880d681SAndroid Build Coastguard Workertime. Link Time Optimization (LTO) is another name for intermodular 13*9880d681SAndroid Build Coastguard Workeroptimization when performed during the link stage. This document describes the 14*9880d681SAndroid Build Coastguard Workerinterface and design between the LTO optimizer and the linker. 15*9880d681SAndroid Build Coastguard Worker 16*9880d681SAndroid Build Coastguard WorkerDesign Philosophy 17*9880d681SAndroid Build Coastguard Worker================= 18*9880d681SAndroid Build Coastguard Worker 19*9880d681SAndroid Build Coastguard WorkerThe LLVM Link Time Optimizer provides complete transparency, while doing 20*9880d681SAndroid Build Coastguard Workerintermodular optimization, in the compiler tool chain. Its main goal is to let 21*9880d681SAndroid Build Coastguard Workerthe developer take advantage of intermodular optimizations without making any 22*9880d681SAndroid Build Coastguard Workersignificant changes to the developer's makefiles or build system. This is 23*9880d681SAndroid Build Coastguard Workerachieved through tight integration with the linker. In this model, the linker 24*9880d681SAndroid Build Coastguard Workertreates LLVM bitcode files like native object files and allows mixing and 25*9880d681SAndroid Build Coastguard Workermatching among them. The linker uses `libLTO`_, a shared object, to handle LLVM 26*9880d681SAndroid Build Coastguard Workerbitcode files. This tight integration between the linker and LLVM optimizer 27*9880d681SAndroid Build Coastguard Workerhelps to do optimizations that are not possible in other models. The linker 28*9880d681SAndroid Build Coastguard Workerinput allows the optimizer to avoid relying on conservative escape analysis. 29*9880d681SAndroid Build Coastguard Worker 30*9880d681SAndroid Build Coastguard Worker.. _libLTO-example: 31*9880d681SAndroid Build Coastguard Worker 32*9880d681SAndroid Build Coastguard WorkerExample of link time optimization 33*9880d681SAndroid Build Coastguard Worker--------------------------------- 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard WorkerThe following example illustrates the advantages of LTO's integrated approach 36*9880d681SAndroid Build Coastguard Workerand clean interface. This example requires a system linker which supports LTO 37*9880d681SAndroid Build Coastguard Workerthrough the interface described in this document. Here, clang transparently 38*9880d681SAndroid Build Coastguard Workerinvokes system linker. 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard Worker* Input source file ``a.c`` is compiled into LLVM bitcode form. 41*9880d681SAndroid Build Coastguard Worker* Input source file ``main.c`` is compiled into native object code. 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 44*9880d681SAndroid Build Coastguard Worker 45*9880d681SAndroid Build Coastguard Worker --- a.h --- 46*9880d681SAndroid Build Coastguard Worker extern int foo1(void); 47*9880d681SAndroid Build Coastguard Worker extern void foo2(void); 48*9880d681SAndroid Build Coastguard Worker extern void foo4(void); 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard Worker --- a.c --- 51*9880d681SAndroid Build Coastguard Worker #include "a.h" 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard Worker static signed int i = 0; 54*9880d681SAndroid Build Coastguard Worker 55*9880d681SAndroid Build Coastguard Worker void foo2(void) { 56*9880d681SAndroid Build Coastguard Worker i = -1; 57*9880d681SAndroid Build Coastguard Worker } 58*9880d681SAndroid Build Coastguard Worker 59*9880d681SAndroid Build Coastguard Worker static int foo3() { 60*9880d681SAndroid Build Coastguard Worker foo4(); 61*9880d681SAndroid Build Coastguard Worker return 10; 62*9880d681SAndroid Build Coastguard Worker } 63*9880d681SAndroid Build Coastguard Worker 64*9880d681SAndroid Build Coastguard Worker int foo1(void) { 65*9880d681SAndroid Build Coastguard Worker int data = 0; 66*9880d681SAndroid Build Coastguard Worker 67*9880d681SAndroid Build Coastguard Worker if (i < 0) 68*9880d681SAndroid Build Coastguard Worker data = foo3(); 69*9880d681SAndroid Build Coastguard Worker 70*9880d681SAndroid Build Coastguard Worker data = data + 42; 71*9880d681SAndroid Build Coastguard Worker return data; 72*9880d681SAndroid Build Coastguard Worker } 73*9880d681SAndroid Build Coastguard Worker 74*9880d681SAndroid Build Coastguard Worker --- main.c --- 75*9880d681SAndroid Build Coastguard Worker #include <stdio.h> 76*9880d681SAndroid Build Coastguard Worker #include "a.h" 77*9880d681SAndroid Build Coastguard Worker 78*9880d681SAndroid Build Coastguard Worker void foo4(void) { 79*9880d681SAndroid Build Coastguard Worker printf("Hi\n"); 80*9880d681SAndroid Build Coastguard Worker } 81*9880d681SAndroid Build Coastguard Worker 82*9880d681SAndroid Build Coastguard Worker int main() { 83*9880d681SAndroid Build Coastguard Worker return foo1(); 84*9880d681SAndroid Build Coastguard Worker } 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard WorkerTo compile, run: 87*9880d681SAndroid Build Coastguard Worker 88*9880d681SAndroid Build Coastguard Worker.. code-block:: console 89*9880d681SAndroid Build Coastguard Worker 90*9880d681SAndroid Build Coastguard Worker % clang -flto -c a.c -o a.o # <-- a.o is LLVM bitcode file 91*9880d681SAndroid Build Coastguard Worker % clang -c main.c -o main.o # <-- main.o is native object file 92*9880d681SAndroid Build Coastguard Worker % clang -flto a.o main.o -o main # <-- standard link command with -flto 93*9880d681SAndroid Build Coastguard Worker 94*9880d681SAndroid Build Coastguard Worker* In this example, the linker recognizes that ``foo2()`` is an externally 95*9880d681SAndroid Build Coastguard Worker visible symbol defined in LLVM bitcode file. The linker completes its usual 96*9880d681SAndroid Build Coastguard Worker symbol resolution pass and finds that ``foo2()`` is not used 97*9880d681SAndroid Build Coastguard Worker anywhere. This information is used by the LLVM optimizer and it 98*9880d681SAndroid Build Coastguard Worker removes ``foo2()``. 99*9880d681SAndroid Build Coastguard Worker 100*9880d681SAndroid Build Coastguard Worker* As soon as ``foo2()`` is removed, the optimizer recognizes that condition ``i 101*9880d681SAndroid Build Coastguard Worker < 0`` is always false, which means ``foo3()`` is never used. Hence, the 102*9880d681SAndroid Build Coastguard Worker optimizer also removes ``foo3()``. 103*9880d681SAndroid Build Coastguard Worker 104*9880d681SAndroid Build Coastguard Worker* And this in turn, enables linker to remove ``foo4()``. 105*9880d681SAndroid Build Coastguard Worker 106*9880d681SAndroid Build Coastguard WorkerThis example illustrates the advantage of tight integration with the 107*9880d681SAndroid Build Coastguard Workerlinker. Here, the optimizer can not remove ``foo3()`` without the linker's 108*9880d681SAndroid Build Coastguard Workerinput. 109*9880d681SAndroid Build Coastguard Worker 110*9880d681SAndroid Build Coastguard WorkerAlternative Approaches 111*9880d681SAndroid Build Coastguard Worker---------------------- 112*9880d681SAndroid Build Coastguard Worker 113*9880d681SAndroid Build Coastguard Worker**Compiler driver invokes link time optimizer separately.** 114*9880d681SAndroid Build Coastguard Worker In this model the link time optimizer is not able to take advantage of 115*9880d681SAndroid Build Coastguard Worker information collected during the linker's normal symbol resolution phase. 116*9880d681SAndroid Build Coastguard Worker In the above example, the optimizer can not remove ``foo2()`` without the 117*9880d681SAndroid Build Coastguard Worker linker's input because it is externally visible. This in turn prohibits the 118*9880d681SAndroid Build Coastguard Worker optimizer from removing ``foo3()``. 119*9880d681SAndroid Build Coastguard Worker 120*9880d681SAndroid Build Coastguard Worker**Use separate tool to collect symbol information from all object files.** 121*9880d681SAndroid Build Coastguard Worker In this model, a new, separate, tool or library replicates the linker's 122*9880d681SAndroid Build Coastguard Worker capability to collect information for link time optimization. Not only is 123*9880d681SAndroid Build Coastguard Worker this code duplication difficult to justify, but it also has several other 124*9880d681SAndroid Build Coastguard Worker disadvantages. For example, the linking semantics and the features provided 125*9880d681SAndroid Build Coastguard Worker by the linker on various platform are not unique. This means, this new tool 126*9880d681SAndroid Build Coastguard Worker needs to support all such features and platforms in one super tool or a 127*9880d681SAndroid Build Coastguard Worker separate tool per platform is required. This increases maintenance cost for 128*9880d681SAndroid Build Coastguard Worker link time optimizer significantly, which is not necessary. This approach 129*9880d681SAndroid Build Coastguard Worker also requires staying synchronized with linker developements on various 130*9880d681SAndroid Build Coastguard Worker platforms, which is not the main focus of the link time optimizer. Finally, 131*9880d681SAndroid Build Coastguard Worker this approach increases end user's build time due to the duplication of work 132*9880d681SAndroid Build Coastguard Worker done by this separate tool and the linker itself. 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard WorkerMulti-phase communication between ``libLTO`` and linker 135*9880d681SAndroid Build Coastguard Worker======================================================= 136*9880d681SAndroid Build Coastguard Worker 137*9880d681SAndroid Build Coastguard WorkerThe linker collects information about symbol definitions and uses in various 138*9880d681SAndroid Build Coastguard Workerlink objects which is more accurate than any information collected by other 139*9880d681SAndroid Build Coastguard Workertools during typical build cycles. The linker collects this information by 140*9880d681SAndroid Build Coastguard Workerlooking at the definitions and uses of symbols in native .o files and using 141*9880d681SAndroid Build Coastguard Workersymbol visibility information. The linker also uses user-supplied information, 142*9880d681SAndroid Build Coastguard Workersuch as a list of exported symbols. LLVM optimizer collects control flow 143*9880d681SAndroid Build Coastguard Workerinformation, data flow information and knows much more about program structure 144*9880d681SAndroid Build Coastguard Workerfrom the optimizer's point of view. Our goal is to take advantage of tight 145*9880d681SAndroid Build Coastguard Workerintegration between the linker and the optimizer by sharing this information 146*9880d681SAndroid Build Coastguard Workerduring various linking phases. 147*9880d681SAndroid Build Coastguard Worker 148*9880d681SAndroid Build Coastguard WorkerPhase 1 : Read LLVM Bitcode Files 149*9880d681SAndroid Build Coastguard Worker--------------------------------- 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard WorkerThe linker first reads all object files in natural order and collects symbol 152*9880d681SAndroid Build Coastguard Workerinformation. This includes native object files as well as LLVM bitcode files. 153*9880d681SAndroid Build Coastguard WorkerTo minimize the cost to the linker in the case that all .o files are native 154*9880d681SAndroid Build Coastguard Workerobject files, the linker only calls ``lto_module_create()`` when a supplied 155*9880d681SAndroid Build Coastguard Workerobject file is found to not be a native object file. If ``lto_module_create()`` 156*9880d681SAndroid Build Coastguard Workerreturns that the file is an LLVM bitcode file, the linker then iterates over the 157*9880d681SAndroid Build Coastguard Workermodule using ``lto_module_get_symbol_name()`` and 158*9880d681SAndroid Build Coastguard Worker``lto_module_get_symbol_attribute()`` to get all symbols defined and referenced. 159*9880d681SAndroid Build Coastguard WorkerThis information is added to the linker's global symbol table. 160*9880d681SAndroid Build Coastguard Worker 161*9880d681SAndroid Build Coastguard Worker 162*9880d681SAndroid Build Coastguard WorkerThe lto* functions are all implemented in a shared object libLTO. This allows 163*9880d681SAndroid Build Coastguard Workerthe LLVM LTO code to be updated independently of the linker tool. On platforms 164*9880d681SAndroid Build Coastguard Workerthat support it, the shared object is lazily loaded. 165*9880d681SAndroid Build Coastguard Worker 166*9880d681SAndroid Build Coastguard WorkerPhase 2 : Symbol Resolution 167*9880d681SAndroid Build Coastguard Worker--------------------------- 168*9880d681SAndroid Build Coastguard Worker 169*9880d681SAndroid Build Coastguard WorkerIn this stage, the linker resolves symbols using global symbol table. It may 170*9880d681SAndroid Build Coastguard Workerreport undefined symbol errors, read archive members, replace weak symbols, etc. 171*9880d681SAndroid Build Coastguard WorkerThe linker is able to do this seamlessly even though it does not know the exact 172*9880d681SAndroid Build Coastguard Workercontent of input LLVM bitcode files. If dead code stripping is enabled then the 173*9880d681SAndroid Build Coastguard Workerlinker collects the list of live symbols. 174*9880d681SAndroid Build Coastguard Worker 175*9880d681SAndroid Build Coastguard WorkerPhase 3 : Optimize Bitcode Files 176*9880d681SAndroid Build Coastguard Worker-------------------------------- 177*9880d681SAndroid Build Coastguard Worker 178*9880d681SAndroid Build Coastguard WorkerAfter symbol resolution, the linker tells the LTO shared object which symbols 179*9880d681SAndroid Build Coastguard Workerare needed by native object files. In the example above, the linker reports 180*9880d681SAndroid Build Coastguard Workerthat only ``foo1()`` is used by native object files using 181*9880d681SAndroid Build Coastguard Worker``lto_codegen_add_must_preserve_symbol()``. Next the linker invokes the LLVM 182*9880d681SAndroid Build Coastguard Workeroptimizer and code generators using ``lto_codegen_compile()`` which returns a 183*9880d681SAndroid Build Coastguard Workernative object file creating by merging the LLVM bitcode files and applying 184*9880d681SAndroid Build Coastguard Workervarious optimization passes. 185*9880d681SAndroid Build Coastguard Worker 186*9880d681SAndroid Build Coastguard WorkerPhase 4 : Symbol Resolution after optimization 187*9880d681SAndroid Build Coastguard Worker---------------------------------------------- 188*9880d681SAndroid Build Coastguard Worker 189*9880d681SAndroid Build Coastguard WorkerIn this phase, the linker reads optimized a native object file and updates the 190*9880d681SAndroid Build Coastguard Workerinternal global symbol table to reflect any changes. The linker also collects 191*9880d681SAndroid Build Coastguard Workerinformation about any changes in use of external symbols by LLVM bitcode 192*9880d681SAndroid Build Coastguard Workerfiles. In the example above, the linker notes that ``foo4()`` is not used any 193*9880d681SAndroid Build Coastguard Workermore. If dead code stripping is enabled then the linker refreshes the live 194*9880d681SAndroid Build Coastguard Workersymbol information appropriately and performs dead code stripping. 195*9880d681SAndroid Build Coastguard Worker 196*9880d681SAndroid Build Coastguard WorkerAfter this phase, the linker continues linking as if it never saw LLVM bitcode 197*9880d681SAndroid Build Coastguard Workerfiles. 198*9880d681SAndroid Build Coastguard Worker 199*9880d681SAndroid Build Coastguard Worker.. _libLTO: 200*9880d681SAndroid Build Coastguard Worker 201*9880d681SAndroid Build Coastguard Worker``libLTO`` 202*9880d681SAndroid Build Coastguard Worker========== 203*9880d681SAndroid Build Coastguard Worker 204*9880d681SAndroid Build Coastguard Worker``libLTO`` is a shared object that is part of the LLVM tools, and is intended 205*9880d681SAndroid Build Coastguard Workerfor use by a linker. ``libLTO`` provides an abstract C interface to use the LLVM 206*9880d681SAndroid Build Coastguard Workerinterprocedural optimizer without exposing details of LLVM's internals. The 207*9880d681SAndroid Build Coastguard Workerintention is to keep the interface as stable as possible even when the LLVM 208*9880d681SAndroid Build Coastguard Workeroptimizer continues to evolve. It should even be possible for a completely 209*9880d681SAndroid Build Coastguard Workerdifferent compilation technology to provide a different libLTO that works with 210*9880d681SAndroid Build Coastguard Workertheir object files and the standard linker tool. 211*9880d681SAndroid Build Coastguard Worker 212*9880d681SAndroid Build Coastguard Worker``lto_module_t`` 213*9880d681SAndroid Build Coastguard Worker---------------- 214*9880d681SAndroid Build Coastguard Worker 215*9880d681SAndroid Build Coastguard WorkerA non-native object file is handled via an ``lto_module_t``. The following 216*9880d681SAndroid Build Coastguard Workerfunctions allow the linker to check if a file (on disk or in a memory buffer) is 217*9880d681SAndroid Build Coastguard Workera file which libLTO can process: 218*9880d681SAndroid Build Coastguard Worker 219*9880d681SAndroid Build Coastguard Worker.. code-block:: c 220*9880d681SAndroid Build Coastguard Worker 221*9880d681SAndroid Build Coastguard Worker lto_module_is_object_file(const char*) 222*9880d681SAndroid Build Coastguard Worker lto_module_is_object_file_for_target(const char*, const char*) 223*9880d681SAndroid Build Coastguard Worker lto_module_is_object_file_in_memory(const void*, size_t) 224*9880d681SAndroid Build Coastguard Worker lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*) 225*9880d681SAndroid Build Coastguard Worker 226*9880d681SAndroid Build Coastguard WorkerIf the object file can be processed by ``libLTO``, the linker creates a 227*9880d681SAndroid Build Coastguard Worker``lto_module_t`` by using one of: 228*9880d681SAndroid Build Coastguard Worker 229*9880d681SAndroid Build Coastguard Worker.. code-block:: c 230*9880d681SAndroid Build Coastguard Worker 231*9880d681SAndroid Build Coastguard Worker lto_module_create(const char*) 232*9880d681SAndroid Build Coastguard Worker lto_module_create_from_memory(const void*, size_t) 233*9880d681SAndroid Build Coastguard Worker 234*9880d681SAndroid Build Coastguard Workerand when done, the handle is released via 235*9880d681SAndroid Build Coastguard Worker 236*9880d681SAndroid Build Coastguard Worker.. code-block:: c 237*9880d681SAndroid Build Coastguard Worker 238*9880d681SAndroid Build Coastguard Worker lto_module_dispose(lto_module_t) 239*9880d681SAndroid Build Coastguard Worker 240*9880d681SAndroid Build Coastguard Worker 241*9880d681SAndroid Build Coastguard WorkerThe linker can introspect the non-native object file by getting the number of 242*9880d681SAndroid Build Coastguard Workersymbols and getting the name and attributes of each symbol via: 243*9880d681SAndroid Build Coastguard Worker 244*9880d681SAndroid Build Coastguard Worker.. code-block:: c 245*9880d681SAndroid Build Coastguard Worker 246*9880d681SAndroid Build Coastguard Worker lto_module_get_num_symbols(lto_module_t) 247*9880d681SAndroid Build Coastguard Worker lto_module_get_symbol_name(lto_module_t, unsigned int) 248*9880d681SAndroid Build Coastguard Worker lto_module_get_symbol_attribute(lto_module_t, unsigned int) 249*9880d681SAndroid Build Coastguard Worker 250*9880d681SAndroid Build Coastguard WorkerThe attributes of a symbol include the alignment, visibility, and kind. 251*9880d681SAndroid Build Coastguard Worker 252*9880d681SAndroid Build Coastguard Worker``lto_code_gen_t`` 253*9880d681SAndroid Build Coastguard Worker------------------ 254*9880d681SAndroid Build Coastguard Worker 255*9880d681SAndroid Build Coastguard WorkerOnce the linker has loaded each non-native object files into an 256*9880d681SAndroid Build Coastguard Worker``lto_module_t``, it can request ``libLTO`` to process them all and generate a 257*9880d681SAndroid Build Coastguard Workernative object file. This is done in a couple of steps. First, a code generator 258*9880d681SAndroid Build Coastguard Workeris created with: 259*9880d681SAndroid Build Coastguard Worker 260*9880d681SAndroid Build Coastguard Worker.. code-block:: c 261*9880d681SAndroid Build Coastguard Worker 262*9880d681SAndroid Build Coastguard Worker lto_codegen_create() 263*9880d681SAndroid Build Coastguard Worker 264*9880d681SAndroid Build Coastguard WorkerThen, each non-native object file is added to the code generator with: 265*9880d681SAndroid Build Coastguard Worker 266*9880d681SAndroid Build Coastguard Worker.. code-block:: c 267*9880d681SAndroid Build Coastguard Worker 268*9880d681SAndroid Build Coastguard Worker lto_codegen_add_module(lto_code_gen_t, lto_module_t) 269*9880d681SAndroid Build Coastguard Worker 270*9880d681SAndroid Build Coastguard WorkerThe linker then has the option of setting some codegen options. Whether or not 271*9880d681SAndroid Build Coastguard Workerto generate DWARF debug info is set with: 272*9880d681SAndroid Build Coastguard Worker 273*9880d681SAndroid Build Coastguard Worker.. code-block:: c 274*9880d681SAndroid Build Coastguard Worker 275*9880d681SAndroid Build Coastguard Worker lto_codegen_set_debug_model(lto_code_gen_t) 276*9880d681SAndroid Build Coastguard Worker 277*9880d681SAndroid Build Coastguard WorkerWhich kind of position independence is set with: 278*9880d681SAndroid Build Coastguard Worker 279*9880d681SAndroid Build Coastguard Worker.. code-block:: c 280*9880d681SAndroid Build Coastguard Worker 281*9880d681SAndroid Build Coastguard Worker lto_codegen_set_pic_model(lto_code_gen_t) 282*9880d681SAndroid Build Coastguard Worker 283*9880d681SAndroid Build Coastguard WorkerAnd each symbol that is referenced by a native object file or otherwise must not 284*9880d681SAndroid Build Coastguard Workerbe optimized away is set with: 285*9880d681SAndroid Build Coastguard Worker 286*9880d681SAndroid Build Coastguard Worker.. code-block:: c 287*9880d681SAndroid Build Coastguard Worker 288*9880d681SAndroid Build Coastguard Worker lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*) 289*9880d681SAndroid Build Coastguard Worker 290*9880d681SAndroid Build Coastguard WorkerAfter all these settings are done, the linker requests that a native object file 291*9880d681SAndroid Build Coastguard Workerbe created from the modules with the settings using: 292*9880d681SAndroid Build Coastguard Worker 293*9880d681SAndroid Build Coastguard Worker.. code-block:: c 294*9880d681SAndroid Build Coastguard Worker 295*9880d681SAndroid Build Coastguard Worker lto_codegen_compile(lto_code_gen_t, size*) 296*9880d681SAndroid Build Coastguard Worker 297*9880d681SAndroid Build Coastguard Workerwhich returns a pointer to a buffer containing the generated native object file. 298*9880d681SAndroid Build Coastguard WorkerThe linker then parses that and links it with the rest of the native object 299*9880d681SAndroid Build Coastguard Workerfiles. 300