From 325f0b7259eeef01ffdde3320a37dcad44739fa3 Mon Sep 17 00:00:00 2001 From: wanghenshui Date: Tue, 2 Aug 2022 16:03:43 +0800 Subject: [PATCH 1/3] Add LTO/IPO compile support --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6ffa0bcea1..1db752cd1f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,8 @@ option(ENABLE_TSAN "enable thread santinizer" OFF) option(ASAN_WITH_LSAN "enable leak santinizer while address santinizer is enabled" ON) option(ENABLE_STATIC_LIBSTDCXX "link kvrocks with static library of libstd++ instead of shared library" ON) option(USE_LUAJIT "use luaJIT instead of lua" ON) +option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" ON) + set(DEPS_FETCH_PROXY "" CACHE STRING "a template URL to proxy the traffic for fetching dependencies, e.g. with DEPS_FETCH_PROXY = https://some-proxy/, @@ -60,6 +62,30 @@ endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported(RESULT result OUTPUT output LANGUAGES CXX) + if(result) + message(STATUS "IPO / LTO enabled") + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + if(CMAKE_COMPILER_IS_GNUCXX) + set(CMAKE_CXX_COMPILE_OPTIONS_IPO -flto=auto -fno-fat-lto-objects) + set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=auto -fno-fat-lto-objects) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + find_package(LLVM REQUIRED) + if(NOT CLANG_LTO_CACHE) + set(CLANG_LTO_CACHE ${PROJECT_BINARY_DIR}/.clang-lto.cache) + endif() + + set(CMAKE_CXX_COMPILE_OPTIONS_IPO -flto=thin) + set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=thin) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Wl,--thinlto-jobs=all,--thinlto-cache-dir=${CLANG_LTO_CACHE}") + endif() + else() + message(WARNING "IPO is not supported: ${output}") + endif() +endif() + # GLIBC < 2.17 should explict specify the real time library when use clock_* find_library(REALTIME_LIB rt) if (REALTIME_LIB) From 6da7d28b698fae45e197189474c32bc14f5d8224 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 3 Aug 2022 16:15:42 +0800 Subject: [PATCH 2/3] fix lto --- CMakeLists.txt | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1db752cd1f0..e98f53239fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,8 +26,7 @@ option(ENABLE_TSAN "enable thread santinizer" OFF) option(ASAN_WITH_LSAN "enable leak santinizer while address santinizer is enabled" ON) option(ENABLE_STATIC_LIBSTDCXX "link kvrocks with static library of libstd++ instead of shared library" ON) option(USE_LUAJIT "use luaJIT instead of lua" ON) -option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" ON) - +option(ENABLE_IPO "enable interprocedural optimization" ON) set(DEPS_FETCH_PROXY "" CACHE STRING "a template URL to proxy the traffic for fetching dependencies, e.g. with DEPS_FETCH_PROXY = https://some-proxy/, @@ -62,30 +61,6 @@ endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -if(ENABLE_IPO) - include(CheckIPOSupported) - check_ipo_supported(RESULT result OUTPUT output LANGUAGES CXX) - if(result) - message(STATUS "IPO / LTO enabled") - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) - if(CMAKE_COMPILER_IS_GNUCXX) - set(CMAKE_CXX_COMPILE_OPTIONS_IPO -flto=auto -fno-fat-lto-objects) - set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=auto -fno-fat-lto-objects) - elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - find_package(LLVM REQUIRED) - if(NOT CLANG_LTO_CACHE) - set(CLANG_LTO_CACHE ${PROJECT_BINARY_DIR}/.clang-lto.cache) - endif() - - set(CMAKE_CXX_COMPILE_OPTIONS_IPO -flto=thin) - set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=thin) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Wl,--thinlto-jobs=all,--thinlto-cache-dir=${CLANG_LTO_CACHE}") - endif() - else() - message(WARNING "IPO is not supported: ${output}") - endif() -endif() - # GLIBC < 2.17 should explict specify the real time library when use clock_* find_library(REALTIME_LIB rt) if (REALTIME_LIB) @@ -175,6 +150,20 @@ if(FOUND_UNWIND_LIB) target_link_libraries(kvrocks_objs PUBLIC ${FOUND_UNWIND_LIB}) endif() +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported(RESULT ipo_result OUTPUT ipo_output LANGUAGES CXX) + + if(ipo_result) + set_property(TARGET kvrocks_objs PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + set_property(TARGET kvrocks_objs APPEND_STRING PROPERTY LINK_FLAGS " -fuse-ld=lld ") + endif() + else() + message(WARNING "IPO is not supported: ${ipo_output}") + endif() +endif() + # kvrocks main target add_executable(kvrocks src/main.cc) target_link_libraries(kvrocks PRIVATE kvrocks_objs ${EXTERNAL_LIBS}) From db725f51c2b2b2842ceacb68a04d7235afc71788 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 3 Aug 2022 16:58:11 +0800 Subject: [PATCH 3/3] fix --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e98f53239fb..0dbff81a428 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,7 +157,7 @@ if(ENABLE_IPO) if(ipo_result) set_property(TARGET kvrocks_objs PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set_property(TARGET kvrocks_objs APPEND_STRING PROPERTY LINK_FLAGS " -fuse-ld=lld ") + target_link_libraries(kvrocks_objs PUBLIC "-fuse-ld=lld") endif() else() message(WARNING "IPO is not supported: ${ipo_output}")