Skip to content

Commit

Permalink
[INFRA] Better LTO handling
Browse files Browse the repository at this point in the history
Force projects that include HIBF to enable CMAKE_INTERPROCEDURAL_OPTIMIZATION.
CMake does not only detect the compiler flags, but also chooses
programs that support the lto-plugin (ar, ranlib, ...).
This means that exporting the CXX_FLAGS is not enough, because linking
might fail due to the wrong programs being chosen.
  • Loading branch information
eseiler committed Oct 28, 2024
1 parent 7f601c1 commit c178907
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
37 changes: 23 additions & 14 deletions cmake/configuration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ else ()
endif ()

check_cxx_compiler_flag ("-march=native" HIBF_HAS_MARCH_NATIVE)
cmake_dependent_option (HIBF_NATIVE_BUILD "Optimize build for current architecture." ON
"HIBF_HAS_MARCH_NATIVE;NOT HIBF_IS_DEBUG" OFF)
cmake_dependent_option (HIBF_NATIVE_BUILD "Optimize build for current architecture." ON "HIBF_HAS_MARCH_NATIVE" OFF)

if (HIBF_NATIVE_BUILD)
list (APPEND HIBF_CXX_FLAGS "-march=native")
Expand All @@ -248,21 +247,31 @@ else ()
endif ()

check_ipo_supported (RESULT HIBF_HAS_LTO OUTPUT HIBF_HAS_LTO_OUTPUT)
cmake_dependent_option (HIBF_LTO_BUILD "Use Link Time Optimisation." ON "HIBF_HAS_LTO;NOT HIBF_IS_DEBUG" OFF)

if (HIBF_LTO_BUILD)
cmake_dependent_option (HIBF_LTO_BUILD "Use Link Time Optimisation." ON "HIBF_HAS_LTO" OFF)
cmake_dependent_option (HIBF_DEV_CHECK_LTO "LTO check." ON
"HIBF_LTO_BUILD;NOT HIBF_IS_TOP_LEVEL;NOT CMAKE_INTERPROCEDURAL_OPTIMIZATION" OFF)

if (HIBF_DEV_CHECK_LTO)
message (FATAL_ERROR " HIBF heavily benefits from Link Time Optimisation (LTO).\n"
" Your compiler supports LTO, but is has not been enabled for your project.\n \n"
" Add the following at the beginning of your CMakeLists.txt:\n"
" ```\n"
" include (CheckIPOSupported)\n"
" check_ipo_supported (RESULT result OUTPUT output)\n"
" if (result)\n"
" set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)\n"
" endif ()\n"
" ```"
" \n \n"
" For development purposes, this error can be disabled via `HIBF_DEV_CHECK_LTO=OFF`.")
elseif (HIBF_LTO_BUILD)
set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
list (APPEND HIBF_CXX_FLAGS ${CMAKE_CXX_COMPILE_OPTIONS_IPO})
hibf_config_print ("Link Time Optimisation: enabled")
else ()
if (HIBF_IS_DEBUG)
hibf_config_print ("Link Time Optimisation: disabled")
else ()
set (HIBF_LTO_LOG "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hibf.lto.log")
file (WRITE "${HIBF_LTO_LOG}" "${HIBF_HAS_LTO_OUTPUT}")
hibf_config_print ("Link Time Optimisation: not supported")
hibf_config_print (" See ${HIBF_LTO_LOG}")
endif ()
set (HIBF_LTO_LOG "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hibf.lto.log")
file (WRITE "${HIBF_LTO_LOG}" "${HIBF_HAS_LTO_OUTPUT}")
hibf_config_print ("Link Time Optimisation: not supported")
hibf_config_print (" See ${HIBF_LTO_LOG}")
endif ()

# ----------------------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions cmake/hibf-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

@PACKAGE_INIT@

include (CheckIPOSupported)
include (CMakeDependentOption)
include (CMakeFindDependencyMacro)

find_dependency (cereal @HIBF_CEREAL_VERSION@ REQUIRED)
Expand All @@ -26,4 +28,22 @@ else ()
find_dependency (simde @HIBF_SIMDE_VERSION@ REQUIRED)
endif ()

check_ipo_supported (RESULT HIBF_HAS_LTO OUTPUT HIBF_HAS_LTO_OUTPUT)
cmake_dependent_option (HIBF_DEV_CHECK_LTO "LTO check." ON "HIBF_HAS_LTO;NOT CMAKE_INTERPROCEDURAL_OPTIMIZATION" OFF)

if (HIBF_DEV_CHECK_LTO)
message (FATAL_ERROR " HIBF heavily benefits from Link Time Optimisation (LTO).\n"
" Your compiler supports LTO, but is has not been enabled for your project.\n \n"
" Add the following at the beginning of your CMakeLists.txt:\n"
" ```\n"
" include (CheckIPOSupported)\n"
" check_ipo_supported (RESULT result OUTPUT output)\n"
" if (result)\n"
" set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)\n"
" endif ()\n"
" ```"
" \n \n"
" For development purposes, this error can be disabled via `HIBF_DEV_CHECK_LTO=OFF`.")
endif ()

include ("${CMAKE_CURRENT_LIST_DIR}/hibf-targets.cmake")
2 changes: 1 addition & 1 deletion cmake/package-lock.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CPMDeclarePackage (simde
NAME simde
VERSION ${HIBF_SIMDE_VERSION}
GITHUB_REPOSITORY simd-everywhere/simde
DOWNLOAD_ONLY YES)
DOWNLOAD_ONLY YES QUIET YES)
# benchmark
set (HIBF_BENCHMARK_VERSION 1.9.0)
CPMDeclarePackage (benchmark
Expand Down
6 changes: 6 additions & 0 deletions test/hibf-test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ cmake_minimum_required (VERSION 3.10...3.30)
# have to be adapted or the option deactivated.
option (HIBF_BENCHMARK_ALIGN_LOOPS "Pass -falign-loops=32 to the benchmark builds." ON)

include (CheckIPOSupported)
check_ipo_supported (RESULT HIBF_TEST_HAS_LTO OUTPUT HIBF_TEST_HAS_LTO_OUTPUT)
if (HIBF_TEST_HAS_LTO)
set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()

get_filename_component (HIBF_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
add_subdirectory ("${HIBF_ROOT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/hibf_lib")
target_compile_options (hibf PUBLIC "-pedantic" "-Wall" "-Wextra" "-Werror")
Expand Down
6 changes: 6 additions & 0 deletions test/tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
cmake_minimum_required (VERSION 3.16...3.30)
project (hibf_test_tutorial CXX)

include (CheckIPOSupported)
check_ipo_supported (RESULT HIBF_TEST_HAS_LTO OUTPUT HIBF_TEST_HAS_LTO_OUTPUT)
if (HIBF_TEST_HAS_LTO)
set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
list (APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")

Expand Down
7 changes: 4 additions & 3 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
cmake_minimum_required (VERSION 3.10...3.30)
project (hibf_util CXX)

# https://cmake.org/cmake/help/latest/policy/CMP0135.html
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
cmake_policy (SET CMP0135 NEW)
include (CheckIPOSupported)
check_ipo_supported (RESULT HIBF_TEST_HAS_LTO OUTPUT HIBF_TEST_HAS_LTO_OUTPUT)
if (HIBF_TEST_HAS_LTO)
set (CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()

# Dependency: seqan::hibf
Expand Down

0 comments on commit c178907

Please sign in to comment.