From c0c33a4854587e412742841f199edd4616be6c00 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Tue, 28 Jul 2020 13:09:12 -0700 Subject: [PATCH] Makefile support for link-time optimization (#7181) Summary: `USE_LTO=1` in `make` commands now enables LTO. The archiver (`ar`) needed to change in this PR to use a wrapper that enables the LTO plugin. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7181 Test Plan: build a few ways ``` $ make clean && USE_LTO=1 make -j48 db_bench $ make clean && USE_CLANG=1 USE_LTO=1 make -j48 db_bench $ make clean && ROCKSDB_NO_FBCODE=1 USE_LTO=1 make -j48 db_bench ``` Reviewed By: cheng-chang Differential Revision: D22784994 Pulled By: ajkr fbshipit-source-id: 9c45333bd49bf4615aa04c85b7c6fd3925421152 --- Makefile | 11 +++++++++++ build_tools/build_detect_platform | 11 +++++++++++ build_tools/fbcode_config.sh | 2 ++ build_tools/fbcode_config4.8.1.sh | 2 ++ build_tools/fbcode_config_platform007.sh | 2 ++ 5 files changed, 28 insertions(+) diff --git a/Makefile b/Makefile index b36e0438f0..847e7b47df 100644 --- a/Makefile +++ b/Makefile @@ -195,6 +195,17 @@ endif $(warning Warning: Compiling in debug mode. Don't use the resulting binary in production) endif +# `USE_LTO=1` enables link-time optimizations. Among other things, this enables +# more devirtualization opportunities and inlining across translation units. +# This can save significant overhead introduced by RocksDB's pluggable +# interfaces/internal abstractions, like in the iterator hierarchy. It works +# better when combined with profile-guided optimizations (not currently +# supported natively in Makefile). +ifeq ($(USE_LTO), 1) + CXXFLAGS += -flto + LDFLAGS += -flto -fuse-linker-plugin +endif + #----------------------------------------------- include src.mk diff --git a/build_tools/build_detect_platform b/build_tools/build_detect_platform index b93b029ea7..66f83b2b08 100755 --- a/build_tools/build_detect_platform +++ b/build_tools/build_detect_platform @@ -89,6 +89,16 @@ if test -z "$CXX"; then fi fi +if test -z "$AR"; then + if [ -x "$(command -v gcc-ar)" ]; then + AR=gcc-ar + elif [ -x "$(command -v llvm-ar)" ]; then + AR=llvm-ar + else + AR=ar + fi +fi + # Detect OS if test -z "$TARGET_OS"; then TARGET_OS=`uname -s` @@ -756,6 +766,7 @@ ROCKSDB_PATCH=`build_tools/version.sh patch` echo "CC=$CC" >> "$OUTPUT" echo "CXX=$CXX" >> "$OUTPUT" +echo "AR=$AR" >> "$OUTPUT" echo "PLATFORM=$PLATFORM" >> "$OUTPUT" echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> "$OUTPUT" echo "JAVA_LDFLAGS=$JAVA_LDFLAGS" >> "$OUTPUT" diff --git a/build_tools/fbcode_config.sh b/build_tools/fbcode_config.sh index 4834be5f4c..2f56daa08b 100644 --- a/build_tools/fbcode_config.sh +++ b/build_tools/fbcode_config.sh @@ -109,6 +109,7 @@ if [ -z "$USE_CLANG" ]; then # gcc CC="$GCC_BASE/bin/gcc" CXX="$GCC_BASE/bin/g++" + AR="$GCC_BASE/bin/gcc-ar" CFLAGS+=" -B$BINUTILS/gold" CFLAGS+=" -isystem $GLIBC_INCLUDE" @@ -119,6 +120,7 @@ else CLANG_INCLUDE="$CLANG_LIB/clang/stable/include" CC="$CLANG_BIN/clang" CXX="$CLANG_BIN/clang++" + AR="$CLANG_BIN/llvm-ar" KERNEL_HEADERS_INCLUDE="$KERNEL_HEADERS_BASE/include" diff --git a/build_tools/fbcode_config4.8.1.sh b/build_tools/fbcode_config4.8.1.sh index 5f0813a041..c5ba8ddf4b 100644 --- a/build_tools/fbcode_config4.8.1.sh +++ b/build_tools/fbcode_config4.8.1.sh @@ -69,6 +69,7 @@ if [ -z "$USE_CLANG" ]; then # gcc CC="$GCC_BASE/bin/gcc" CXX="$GCC_BASE/bin/g++" + CXX="$GCC_BASE/bin/gcc-ar" CFLAGS="-B$BINUTILS/gold -m64 -mtune=generic" CFLAGS+=" -isystem $GLIBC_INCLUDE" @@ -81,6 +82,7 @@ else CLANG_INCLUDE="$CLANG_LIB/clang/*/include" CC="$CLANG_BIN/clang" CXX="$CLANG_BIN/clang++" + AR="$CLANG_BIN/llvm-ar" KERNEL_HEADERS_INCLUDE="$KERNEL_HEADERS_BASE/include/" diff --git a/build_tools/fbcode_config_platform007.sh b/build_tools/fbcode_config_platform007.sh index 60b51800ad..f7de7fcca0 100644 --- a/build_tools/fbcode_config_platform007.sh +++ b/build_tools/fbcode_config_platform007.sh @@ -118,6 +118,7 @@ if [ -z "$USE_CLANG" ]; then # gcc CC="$GCC_BASE/bin/gcc" CXX="$GCC_BASE/bin/g++" + AR="$GCC_BASE/bin/gcc-ar" CFLAGS+=" -B$BINUTILS/gold" CFLAGS+=" -isystem $LIBGCC_INCLUDE" @@ -128,6 +129,7 @@ else CLANG_INCLUDE="$CLANG_LIB/clang/stable/include" CC="$CLANG_BIN/clang" CXX="$CLANG_BIN/clang++" + AR="$CLANG_BIN/llvm-ar" KERNEL_HEADERS_INCLUDE="$KERNEL_HEADERS_BASE/include"