11.2. LTO Development Flow

11.2.1. How LTO Works

A key advantage to using LTO in an application build is that the linker is able to provide the compiler with the ability to optimize across C/C++ compilation unit boundaries. If LTO is not enabled during the build of an application, the compiler’s visibility into the application’s source code is limited to the C/C++ source file that is currently being compiled. Consequently, the compiler must make conservative assumptions about functions and variables that are referenced from the C/C++ source file, but are defined elsewhere. Hence, the compiler uses constraint with regards to what optimizations can be applied during the compilation of a given C/C++ source file.

When LTO is enabled, the linker will combine internal representation (IR) modules from the incoming object files that were compiler-generated from C/C++ source files into a single, merged IR module representing all of the functions and variables from all of the C/C++ source files in an application. The compiler then has visibility across all C/C++ source files via this merged IR module and is able to apply inter-module optimizations, such as aggressive inlining, constant merging, and aggressive machine outlining. For example, if multiple source files require access to the same string constant, with LTO enabled, they can all access a single instance of storage for the string constant as opposed to each source file requiring their own copy of the string constant.

11.2.2. LTO Development Flow Overview

LTO is easy to incorporate into an application build. An overview of the LTO development flow is shown in figure 1 below. The LTO development flow can be divided into two phases:

11.2.2.1. Compilation Phase

  • Compile C/C++ source files with the tiarmclang -flto compiler option

    The -flto option instructs the compiler to embed a bitcode encoding of the internal representation (IR) into each object file that is generated from a C/C++ source file by the compiler. A compiler-generated object that contains an bitcode IR encoding of a given C/C++ source module enables that module to participate in link-time optimization. A compiler-generated object file that does not contain a bitcode IR representation of a C/C++ source module will be treated as an ordinary object file at link time. It will not participate in link-time optimization, but will be incorporated into the traditional link step that occurs after link-time optimization and produces a linked ELF executable.

  • Compile as much C/C++ source as possible with the -flto option

    Every C/C++ source file that is compiled with the -flto option will be able to participate in link-time optimization. This includes C/C++ source files that are used to build libraries as well as an application’s C/C++ source files whose compiler-generated object files are input directly into the linker. An application will reap a greater benefit from link-time optimization if a higher percentage of the C/C++ source files that are incorporated into the build of the application are compiled with the -flto option.

    The C/C++ source files for all runtime libraries that are provided with the tiarmclang installation are compiled with the -flto option and will participate in link-time optimization when LTO is enabled during the link step of an application build.

Note

No additional compile-time is incurred when compiling with the -flto option

../../_images/lto_development_flow_fig1.jpg