3

I'm using Gentoo Linux.

Here is my toolchain:

sys-kernel/linux-headers-3.9
sys-devel/binutils-2.23.1  USE="cxx nls zlib -multislot -multitarget -static-libs {-test} -vanilla"
sys-devel/gcc-4.7.3-r1:4.7  USE="cxx fortran gtk lto mudflap (multilib) nls nptl openmp (-altivec) -doc (-fixed-point) -gcj -go -graphite (-hardened) (-libssp) -multislot -nopie -nossp -objc -objc++ -objc-gc -regression-test -vanilla"
sys-libs/glibc-2.17:2.2  USE="(multilib) -debug -gd (-hardened) -nscd -profile (-selinux) -suid -systemtap -vanilla"

Here are my CFLAGS:

$ cat /etc/portage/make.conf

CFLAGS="-march=core-avx-i -mtune=core-avx-i -O2 -pipe -flto"
CXXFLAGS="${CFLAGS}"

CHOST="x86_64-pc-linux-gnu"
# etc...

The whole world is built with LTO, except for few packages:

$ cat /etc/portage/package.env

dev-lang/perl no-lto
dev-libs/elfutils no-lto
dev-lang/spidermonkey no-lto
dev-libs/glib no-lto
sys-devel/llvm no-lto
media-libs/mesa no-lto
media-libs/alsa-lib no-lto
sys-apps/preload no-lto
app-text/aspell no-lto
app-text/rarian no-lto
sys-power/upower no-lto
net-libs/farstream no-lto
dev-python/notify-python no-lto
x11-libs/wxGTK no-lto
media-video/avidemux no-lto
media-gfx/inkscape no-lto
x11-base/xorg-server no-lto
x11-drivers/xf86-video-intel no-lto
net-libs/webkit-gtk no-lto
mail-client/thunderbird no-lto

$ cat /etc/portage/env/no-lto

CFLAGS="${CFLAGS} -fno-lto"
CXXFLAGS="${CXXFLAGS} -fno-lto"
LDFLAGS="${LDFLAGS} -fno-lto"

On some blogs I noticed authors setting LDFLAGS in their make.conf file also, I didn't do that.

The operating system sets these LDFLAGS according to selected profile:

$ emerge --info | grep LDFLAGS

LDFLAGS="-Wl,-O1 -Wl,--as-needed"

Gentoo developers and maintainers do not recommend changing them

I would like to set these lines in my make.conf file, then rebuild toolchain and world:

CFLAGS="-march=core-avx-i -mtune=core-avx-i -O2 -pipe -flto -Wl,-flto"
LDFLAGS="-Wl,-flto -Wl,-O2"

Will there be any difference in performance/stability?

Are these differences worth the time needed to recompile entire world?

I'd like to hear suggestions, explanations, better practices from experienced Gentoo users/maintainers/programmers/administrators...

Thank you very much in advance!

3 Answers3

3

You don't need to change your CFLAGS there to add -Wl,-lfto; unless there are some packages that are mistakenly using CFLAGS for linking, it won't help (and those packages should be reported to bugzilla).

You do however need to add -flto to LDFLAGS to get full benefit of LTO. LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,-flto"

You will not actually get LTO performance until you have the addition in your LDFLAGS, so yes, you will need to recompile anything that did not explicitly enable/disable LTO itself.

robbat2
  • 1,294
2

When doing LTO builds, it is important to pass the same optimization flags to both the compiler and the LTO linker. This does not mean just -On, but actually all other optimization flags.

When you build the final binary and link using the compiler itself in a single pass, it all works. If you do it in two passes (libtool, I am looking at you!) then all hell breaks loose.

So, yes, it is exactly right to enforce that everything optimization-related in CFLAGS and CXXFLAGS is also present in LDFLAGS. And they must match for the results to be correct, the fact that the gcc documentation seems to imply otherwise is a reported bug against gcc.

Whether you need to enforce it by doing nothing special to LDFLAGS, or duplicating them in LDFLAGS depends on the build system used by the package, and whether late-linking by libtool and its ilk will happen.

anonymous
  • 116
1

This and this post of GentooLTO overlay, clearly states that adding LTO to LDFLAGS isn't done with the -Wl, flag but as -flto or ${CFLAGS} without a linker flag. Packages that need this linker flag, should be reported.

Example:

dev-lang/python-3.8.1::pg_overlay was built with the following:
USE="gdbm ncurses readline sqlite ssl xml -bluetooth -build -examples -hardened -ipv6 -libressl -test -tk -wininst"
CFLAGS="-march=native -mtune=native -O3 -pipe -flto=7 -fuse-linker-plugin -fomit-frame-pointer -fno-plt -fno-stack-protector -s -fwrapv"
CXXFLAGS="-march=native -mtune=native -O3 -pipe -flto=7 -fuse-linker-plugin -fomit-frame-pointer -fno-plt -fno-stack-protector -s -fwrapv"
FEATURES="preserve-libs usersandbox merge-sync ipc-sandbox unmerge-logs ebuild-locks distlocks binpkg-docompress multilib-strict xattr config-protect-if-modified news parallel-fetch sandbox usersync binpkg-dostrip unknown-features-warn strict unmerge-orphans assume-digests protect-owned pid-sandbox binpkg-logs fixlafiles network-sandbox parallel-install userpriv userfetch sfperms"
LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--sort-common -Wl,--strip-debug -flto=7 -fuse-linker-plugin -L."

Another post on Gentoo forums here.