LTO: what it is and how to use it on Gentoo

What is LTO?

LTO It is the acronym for Link Time Optimization. It is an operation in which the compiler delays optimizations at the moment of linking the source files, allowing you to see all the files that make up a single executable as a single file, and in this way apply optimizations in a more effective way.

For more information: the wiki of GCC.

To see the benefits and drawbacks of LTO: Benchmarks of Phoronix

Things to keep in mind

  • It is unstable, it can cause problems in some packages.
  • Some packages will fail to compile (more on how to fix this later).
  • It is recommended to use the linker Gold.
  • Use always the latest version of GCC.

Gold, in addition to supporting advanced features that make it more attractive to use LTO, it's faster than gnu ldespecially when it comes to large programs, where it can become 5 times faster. To use it, run:

binutils-config --linker ld.gold


Using LTO: recommended method

Instead of activating LTO globally (which can cause problems) it is better to activate it in the packages we want. This way, only the packages you want to benefit from LTO they are compiled with this optimization, or which avoids slower compilation times in programs that do not benefit from it. This can be achieved in the following way:

In the / etc / portage / env directory, we create the file LTO.conf and we add the following lines:

CFLAGS="${CFLAGS} -flto=5" #pon en -flto los hilos  de tu CPU + 1
CXXFLAGS="${CXXFLAGS} -flto=5" #igual que arriba
LDFLAGS="${LDFLAGS} -fuse-linker-plugin" #solo si usas Gold, es mejor.

So, to use LTO in a package, we just have to put its name (the full name, www-client / firefox, instead of firefox) in the file package.env, Together with LTO.conf to your right. Below is an example:

app-emulation/wine LTO.conf
www-client/firefox LTO.conf
sys-devel/gcc LTO.conf
kde-base/kdelibs LTO.conf

Now we just have to compile the packages we want them to use LTO.


Using LTO globally (not recommended)

Instead of applying LTO package by package, we can also apply it globally (which is what I use). To apply it, the steps to follow are:

We edit the /etc/portage/make.conf file and add the following (they are the same lines from the file LTO.conf):

CFLAGS="${CFLAGS} -flto=5" #pon en -flto los hilos  de tu CPU + 1
CXXFLAGS="${CXXFLAGS} -flto=5" #igual que arriba
LDFLAGS="${LDFLAGS} -fuse-linker-plugin" #solo si usas Gold, es mejor.

In turn, we create the file /etc/portage/env/no-LTO.conf and add the following lines:

CFLAGS="${CFLAGS} -fno-lto -fno-use-linker-plugin"
CXXFLAGS="${CXXFLAGS} -fno-lto -fno-use-linker-plugin"
LDFLAGS="${LDFLAGS} -fno-lto -fno-use-linker-plugin"

And in the file package.env we will put the packages whose compilation fails with LTO. Hey here my package.env (note that I use nolto.conf instead of non-LTO.conf).

We must also add LTO to USE variable of make.conf, this is necessary because the developers of Gentoo they are (slowly) adding optional patches for the use of this optimization in some packages.

Once this is done, let's proceed to recompile all the system packages:

emerge -e @world @system --keep-going &> errores

By using –keep-going, we are telling Portage to ignore errors, while &> redirect the output of all bugs to a file called errors, use this file to see which packages fail to compile and add them to the list of package.env.

The &> will deprive us of all output, if we want to see the output of the process, we must use (as root) this command:

tail -f /var/log/emerge.log

And that's all, I hope I have not left any doubts, if that I will cheer up and write an article Openmp I Graphite.


3 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   msx said

    Gentoo, meh ...
    Let's go to the important thing: Graphite!

    1.    x11tete11x said

      The stupid and ssensual Graphite xD, that subnormal thing that bursts your xD system, I remember that the last time I used it, it was enough to make a for that iterates 50 times doing "notify-send KDE <3" so that in iteration number 50 , the environment will burst to hell hahahahaha, yes, I was going to the re-farts xD

    2.    roader said

      I already said that I was still encouraged and made one of Graphite. But anyway, Graphite (and any other optimization) works best with LTO. Also, unlike LTO, Graphite and OpenMP have a problem. Not all programs benefit from it, and in those that do not benefit, performance decreases, so in this case it is better to use it only for a few packages.