Skip to contentSkip to conversation

Config.in: add new option BR2_ENABLE_LTO to globally enable LTO

PATCH In review Arnout Vandecappelle (Essensium/Mind) 2 participants 6 messages Jul 25, 2022 – Jul 26, 2022 Arm STM32

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 Config.in: add new option BR2_ENABLE_LTO to globally enable LTO

Some packages have a configure option to enable LTO. Add a global option to enable/disable this in packages.

Note that we could instead (or in addition) pass -flto directly to the compiler in the toolchain wrapper, but that has a *much* bigger impact, so for the time being let's not do that.

No toolchain dependency is added for this, because all toolchains now support LTO.
- LTO support was merged in GCC years before 4.6 (our minimal version) was released.
- Our oldest external toolchain, Sourcery ARM 2014.05, has LTO support.
- For our internal toolchains, it is supposedly regulated by BR2_GCC_ENABLE_LTO. However, that option only *enables* it - it doesn't disable LTO support when it's not provided. Since LTO support defaults to enabled for a long time already in GCC, in practice all Buildroot-generated toolchains do support LTO.

---
 Config.in | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
Config.in +19 -0
index af9052d32b..8fc431a5da 100644
@@ -551,6 +551,25 @@ config BR2_OPTIMIZE_FAST
 
 endchoice
 
+config BR2_ENABLE_LTO
+	bool "build packages with link-time optimisation"
+	help
+	  Enable the link-time optimisation (LTO) option when building
+	  packages. Link-time optimisation re-runs optimisations at link
+	  time, which allows the compiler to do interprocedural analysis
+	  across compilation units and thus come with better results:
+	  smaller size and better performance.
+
+	  Note that this analysis is limited to statically linked object
+	  files and libraries.
+
+	  This option may significantly increase build times, sometimes
+	  5 times longer, with only limited gains.
+
+	  At this time, this option only enables LTO in packages that
+	  have an explicit configuration option for it. Other packages
+	  always enable LTO, but most packages never enable LTO.
+
 config BR2_GOOGLE_BREAKPAD_ENABLE
 	bool "Enable google-breakpad support"
 	depends on BR2_INSTALL_LIBSTDCPP
Signature · 6 lines
2.35.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

PATCH 2/5 package/*: use BR2_ENABLE_LTO to enable LTO instead of BR2_GCC_ENABLE_LTO

The BR2_GCC_ENABLE_LTO controls whether or not LTO is available in the (internal) toolchain - it shouldn't be used to control whether packages are built with LTO. Use the new BR2_ENABLE_LTO option instead.

---
 package/fastd/fastd.mk         | 2 +-
 package/log4cplus/log4cplus.mk | 2 +-
 package/netdata/netdata.mk     | 2 +-
 package/rocksdb/rocksdb.mk     | 2 +-
 package/unbound/unbound.mk     | 2 +-
 package/valgrind/valgrind.mk   | 2 +-
 package/wireshark/wireshark.mk | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)
package/fastd/fastd.mk +1 -1
index 5cd6a909c3..6c496cf9ef 100644
@@ -46,7 +46,7 @@ else
 FASTD_CONF_OPTS += -Dsystemd=disabled
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 FASTD_CONF_OPTS += -Db_lto=true
 else
 FASTD_CONF_OPTS += -Db_lto=false
package/log4cplus/log4cplus.mk +1 -1
index b2e9ac3ed1..2dab661654 100644
@@ -11,7 +11,7 @@ LOG4CPLUS_LICENSE = Apache-2.0, BSD-2-Clause, BSD-like (threadpool)
 LOG4CPLUS_LICENSE_FILES = LICENSE
 LOG4CPLUS_INSTALL_STAGING = YES
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 LOG4CPLUS_CONF_OPTS += --enable-lto
 else
 LOG4CPLUS_CONF_OPTS += --disable-lto
package/netdata/netdata.mk +1 -1
index 04b146591f..9e661f4943 100644
@@ -23,7 +23,7 @@ NETDATA_DEPENDENCIES = libuv util-linux zlib
 # provided by autoconf relies on wchar_t.
 NETDATA_CONF_ENV = ac_cv_prog_cc_c99=-std=gnu99
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 NETDATA_CONF_OPTS += --enable-lto
 else
 NETDATA_CONF_OPTS += --disable-lto
package/rocksdb/rocksdb.mk +1 -1
index c355d0012d..bed0634134 100644
@@ -69,7 +69,7 @@ else
 ROCKSDB_MAKE_OPTS += ROCKSDB_DISABLE_ZSTD=1
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 ROCKSDB_MAKE_OPTS += USE_LTO=1
 else
 ROCKSDB_MAKE_OPTS += USE_LTO=0
package/unbound/unbound.mk +1 -1
index 988508576b..8863d039bb 100644
@@ -35,7 +35,7 @@ else
 UNBOUND_CONF_OPTS += --without-pthreads
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 UNBOUND_CONF_OPTS += --enable-flto
 else
 UNBOUND_CONF_OPTS += --disable-flto
package/valgrind/valgrind.mk +1 -1
index b4c4b8b5a2..17b131c3ec 100644
@@ -52,7 +52,7 @@ VALGRIND_CONF_OPTS += \
 	--host=$(patsubst arm-%,armv7-%,$(GNU_TARGET_NAME))
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 VALGRIND_CONF_OPTS += --enable-lto
 else
 VALGRIND_CONF_OPTS += --disable-lto
package/wireshark/wireshark.mk +1 -1
index 1a549f7037..e5d5e6f677 100644
@@ -31,7 +31,7 @@ ifeq ($(BR2_TOOLCHAIN_HAS_LIBATOMIC),y)
 WIRESHARK_CONF_OPTS += -DCMAKE_EXE_LINKER_FLAGS=-latomic
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
+ifeq ($(BR2_ENABLE_LTO),y)
 WIRESHARK_CONF_OPTS += -DENABLE_LTO=ON
 else
 WIRESHARK_CONF_OPTS += -DENABLE_LTO=OFF
Signature · 6 lines
2.35.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw

PATCH 3/5 configs/*: use BR2_ENABLE_LTO to enable LTO instead of BR2_GCC_ENABLE_LTO

In reply to Arnout Vandecappelle (Essensium/Mind)

BR2_GCC_ENABLE_LTO only enables LTO in the toolchain, not in packages. Use BR2_ENABLE_LTO instead to enable it in packages as well.

Note that BR2_GCC_ENABLE_LTO is currently non-functional (LTO is still enabled in the toolchain even if it's not set), so we *do* have LTO available in the toolchain.

Note that the option has no effect at all on these defconfigs anyway, since they don't contain any package that is affected by the BR2_ENABLE_LTO option. Still, it makes sense to enable it for these really memory-constrained targets for when packages are added.

---
 configs/stm32f429_disco_xip_defconfig | 2 +-
 configs/stm32f469_disco_xip_defconfig | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
configs/stm32f429_disco_xip_defconfig +1 -1
index 01b54c02cc..f9b3270ecd 100644
@@ -2,7 +2,7 @@ BR2_arm=y
 BR2_cortex_m4=y
 BR2_KERNEL_HEADERS_5_15=y
 # BR2_UCLIBC_INSTALL_UTILS is not set
-BR2_GCC_ENABLE_LTO=y
+BR2_ENABLE_LTO=y
 BR2_ROOTFS_POST_BUILD_SCRIPT="board/stmicroelectronics/common/stm32f4xx/stm32-post-build.sh"
 BR2_LINUX_KERNEL=y
 BR2_LINUX_KERNEL_CUSTOM_VERSION=y
configs/stm32f469_disco_xip_defconfig +1 -1
index d95b7759f3..fc3b3f9213 100644
@@ -2,7 +2,7 @@ BR2_arm=y
 BR2_cortex_m4=y
 BR2_KERNEL_HEADERS_5_15=y
 # BR2_UCLIBC_INSTALL_UTILS is not set
-BR2_GCC_ENABLE_LTO=y
+BR2_ENABLE_LTO=y
 BR2_ROOTFS_POST_BUILD_SCRIPT="board/stmicroelectronics/common/stm32f4xx/stm32-post-build.sh"
 BR2_LINUX_KERNEL=y
 BR2_LINUX_KERNEL_CUSTOM_VERSION=y
Signature · 6 lines
2.35.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#3 lore raw

PATCH 4/5 package/binutils: remove BR2_BINUTILS_ENABLE_LTO

In reply to Arnout Vandecappelle (Essensium/Mind)

Currently, this option doesn't do anything. It only adds --enable-plugins --enable-lto to the configure flags, but doesn't disable them if it is not set. Since both of these default to enabled, plugins and lto are effectively always enabled.

There really is no need to make this configurable: it adds a bit of size and build time to host-binutils, but we don't care about that for host tools. It's still up to individual builds to enable the LTO options.

Therefore, remove the option entirely. For clarity, explicitly pass --enable-plugins --enable-lto to configure.

---
 package/binutils/Config.in.host | 3 ---
 package/binutils/binutils.mk    | 6 ++----
 package/gcc/Config.in.host      | 1 -
 3 files changed, 2 insertions(+), 8 deletions(-)
package/binutils/Config.in.host +0 -3
index ef1b2a94db..5bdda25faf 100644
@@ -35,9 +35,6 @@ config BR2_BINUTILS_VERSION
 	default "2.37"		if BR2_BINUTILS_VERSION_2_37_X
 	default "2.38"		if BR2_BINUTILS_VERSION_2_38_X
 
-config BR2_BINUTILS_ENABLE_LTO
-	bool
-
 config BR2_BINUTILS_EXTRA_CONFIG_OPTIONS
 	string "Additional binutils options"
 	default ""
package/binutils/binutils.mk +2 -4
index c94ebe0995..6362cb1641 100644
@@ -86,6 +86,8 @@ HOST_BINUTILS_CONF_OPTS = \
 	--with-sysroot=$(STAGING_DIR) \
 	--enable-poison-system-directories \
 	--without-debuginfod \
+	--enable-plugins \
+	--enable-lto \
 	$(BINUTILS_DISABLE_GDB_CONF_OPTS) \
 	$(BINUTILS_EXTRA_CONFIG_OPTIONS)
 
@@ -120,10 +122,6 @@ HOST_BINUTILS_POST_EXTRACT_HOOKS += BINUTILS_XTENSA_OVERLAY_EXTRACT
 HOST_BINUTILS_EXTRA_DOWNLOADS += $(ARCH_XTENSA_OVERLAY_URL)
 endif
 
-ifeq ($(BR2_BINUTILS_ENABLE_LTO),y)
-HOST_BINUTILS_CONF_OPTS += --enable-plugins --enable-lto
-endif
-
 # Hardlinks between binaries in different directories cause a problem
 # with rpath fixup, so we de-hardlink those binaries, and replace them
 # with copies instead.
package/gcc/Config.in.host +0 -1
index a7e237c3f7..20187db6fd 100644
@@ -132,7 +132,6 @@ config BR2_TOOLCHAIN_BUILDROOT_DLANG
 
 config BR2_GCC_ENABLE_LTO
 	bool "Enable compiler link-time-optimization support"
-	select BR2_BINUTILS_ENABLE_LTO
 	help
 	  This option enables link-time optimization (LTO) support in
 	  gcc.
Signature · 6 lines
2.35.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#4 lore raw

PATCH 5/5 package/gcc: remove BR2_GCC_ENABLE_LTO

In reply to Arnout Vandecappelle (Essensium/Mind)

Currently, this option doesn't do anything. It only adds --enable-plugins --enable-lto to the configure flags, but doesn't disable them if it is not set. Since both of these default to enabled, plugins and lto are effectively always enabled.

There really is no need to make this configurable: it adds a bit of size and build time to host-gcc, but we don't care about that for host tools. It's still up to individual builds to enable the LTO options.

Therefore, remove the option entirely. For clarity, explicitly pass --enable-plugins --enable-lto to configure.

No legacy handling is added for the removed option. Since the behaviour hasn't actually changed (independently of whether the option was enabled or not), there's no point bothering the user with a legacy option.

elf2flt was linking with libdl depending on this option. Since the option doesn't do anything, this is probably not needed. Still, to avoid breaking things, and because linking with libdl doesn't cost us anything anyway, always link with libdl.

---
 package/elf2flt/elf2flt.mk | 6 +-----
 package/gcc/Config.in.host | 6 ------
 package/gcc/gcc.mk         | 6 ++----
 3 files changed, 3 insertions(+), 15 deletions(-)
package/elf2flt/elf2flt.mk +1 -5
index 9230e3ce1a..6cd0786c3d 100644
@@ -24,11 +24,7 @@ HOST_ELF2FLT_CONF_OPTS = \
 	--target=$(GNU_TARGET_NAME) \
 	--disable-werror
 
-HOST_ELF2FLT_LIBS = -lz
-
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
-HOST_ELF2FLT_LIBS += -ldl
-endif
+HOST_ELF2FLT_LIBS = -lz -ldl
 
 HOST_ELF2FLT_CONF_ENV = LIBS="$(HOST_ELF2FLT_LIBS)"
 
package/gcc/Config.in.host +0 -6
index 20187db6fd..9833ae1336 100644
@@ -130,12 +130,6 @@ config BR2_TOOLCHAIN_BUILDROOT_DLANG
 	  D language and you want D libraries to be installed on your
 	  target system.
 
-config BR2_GCC_ENABLE_LTO
-	bool "Enable compiler link-time-optimization support"
-	help
-	  This option enables link-time optimization (LTO) support in
-	  gcc.
-
 config BR2_GCC_ENABLE_OPENMP
 	bool "Enable compiler OpenMP support"
 	depends on !BR2_PTHREADS_NONE && !BR2_arc && !BR2_microblaze
package/gcc/gcc.mk +2 -4
index 114e774503..ec6f3b550a 100644
@@ -75,6 +75,8 @@ HOST_GCC_COMMON_CONF_OPTS = \
 	--disable-libssp \
 	--disable-multilib \
 	--disable-decimal-float \
+	--enable-plugins \
+	--enable-lto \
 	--with-gmp=$(HOST_DIR) \
 	--with-mpc=$(HOST_DIR) \
 	--with-mpfr=$(HOST_DIR) \
@@ -165,10 +167,6 @@ else
 HOST_GCC_COMMON_CONF_OPTS += --enable-tls
 endif
 
-ifeq ($(BR2_GCC_ENABLE_LTO),y)
-HOST_GCC_COMMON_CONF_OPTS += --enable-plugins --enable-lto
-endif
-
 ifeq ($(BR2_PTHREADS_NONE),y)
 HOST_GCC_COMMON_CONF_OPTS += \
 	--disable-threads \
Signature · 6 lines
2.35.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#5 lore raw
In reply to Arnout Vandecappelle (Essensium/Mind)
Quoted 18 lines · On Mon, 25 Jul 2022 17:22:25 +0200 "Arnout Vandecappelle (Essensium/Mind)"…

Some packages have a configure option to enable LTO. Add a global option to enable/disable this in packages.

Note that we could instead (or in addition) pass -flto directly to the compiler in the toolchain wrapper, but that has a *much* bigger impact, so for the time being let's not do that.

No toolchain dependency is added for this, because all toolchains now support LTO.
- LTO support was merged in GCC years before 4.6 (our minimal version) was released.
- Our oldest external toolchain, Sourcery ARM 2014.05, has LTO support.
- For our internal toolchains, it is supposedly regulated by BR2_GCC_ENABLE_LTO. However, that option only *enables* it - it doesn't disable LTO support when it's not provided. Since LTO support defaults to enabled for a long time already in GCC, in practice all Buildroot-generated toolchains do support LTO.

Quoted 4 lines
---
 Config.in | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Series applied, thanks!

Thomas

Signature · 7 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot