From 2799fe4cdbe77e017544f2fe61ae9baef650dbe6 Mon Sep 17 00:00:00 2001 From: pennae Date: Tue, 21 Dec 2021 19:34:40 +0100 Subject: [PATCH] enable LTO in optimized builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gives 2-5% performance improvement across a board of tests. LTO is broken when using clang; some libs link fine while others crash the linker with a segfault in the llvm linker plugin. 🙁 --- Makefile | 3 ++- Makefile.config.in | 1 + configure.ac | 14 ++++++++++++++ doc/manual/src/release-notes/rl-next.md | 3 +++ mk/libraries.mk | 6 +++--- mk/programs.mk | 4 ++-- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5040d2884..903787e1a 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,8 @@ makefiles = \ OPTIMIZE = 1 ifeq ($(OPTIMIZE), 1) - GLOBAL_CXXFLAGS += -O3 + GLOBAL_CXXFLAGS += -O3 $(CXXLTO) + GLOBAL_LDFLAGS += $(CXXLTO) else GLOBAL_CXXFLAGS += -O0 -U_FORTIFY_SOURCE endif diff --git a/Makefile.config.in b/Makefile.config.in index 3505f337e..d724853fa 100644 --- a/Makefile.config.in +++ b/Makefile.config.in @@ -7,6 +7,7 @@ CC = @CC@ CFLAGS = @CFLAGS@ CXX = @CXX@ CXXFLAGS = @CXXFLAGS@ +CXXLTO = @CXXLTO@ EDITLINE_LIBS = @EDITLINE_LIBS@ ENABLE_S3 = @ENABLE_S3@ GTEST_LIBS = @GTEST_LIBS@ diff --git a/configure.ac b/configure.ac index 8a01c33ec..a86d88fad 100644 --- a/configure.ac +++ b/configure.ac @@ -147,6 +147,20 @@ if test "x$GCC_ATOMIC_BUILTINS_NEED_LIBATOMIC" = xyes; then LDFLAGS="-latomic $LDFLAGS" fi +# LTO is currently broken with clang for unknown reasons; ld segfaults in the llvm plugin +AC_ARG_ENABLE(lto, AS_HELP_STRING([--enable-lto],[Enable LTO (only supported with GCC) [default=no]]), + lto=$enableval, lto=no) +if test "$lto" = yes; then + if $CXX --version | grep -q GCC; then + AC_SUBST(CXXLTO, [-flto=jobserver]) + else + echo "error: LTO is only supported with GCC at the moment" >&2 + exit 1 + fi +else + AC_SUBST(CXXLTO, [""]) +fi + PKG_PROG_PKG_CONFIG AC_ARG_ENABLE(shared, AS_HELP_STRING([--enable-shared],[Build shared libraries for Nix [default=yes]]), diff --git a/doc/manual/src/release-notes/rl-next.md b/doc/manual/src/release-notes/rl-next.md index 7dd8387d8..d81e54e49 100644 --- a/doc/manual/src/release-notes/rl-next.md +++ b/doc/manual/src/release-notes/rl-next.md @@ -26,3 +26,6 @@ * Templates can now define a `welcomeText` attribute, which is printed out by `nix flake {init,new} --template