From: Wolfgang Bangerth Date: Fri, 18 Oct 2024 23:31:26 +0000 (-0600) Subject: Enable the use of interprocedural/link-time optimization. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=898f51baec42e2cdcfb9b10629262c1cd1b106b4;p=dealii.git Enable the use of interprocedural/link-time optimization. --- diff --git a/cmake/checks/check_02_compiler_features.cmake b/cmake/checks/check_02_compiler_features.cmake index 59251dca98..cab88ab5bb 100644 --- a/cmake/checks/check_02_compiler_features.cmake +++ b/cmake/checks/check_02_compiler_features.cmake @@ -407,3 +407,25 @@ if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") reset_cmake_required() endif() + + +# +# Check whether the compiler supports interprocedural and link-time +# optimization. If so, we can set the corresponding property on the +# compile and link targets later on. +# +if(DEAL_II_USE_LTO) + include(CheckIPOSupported) + check_ipo_supported(RESULT _res OUTPUT _out LANGUAGES CXX) + if (_res) + message(STATUS "Compiler supports interprocedural/link-time optimizations") + set(DEAL_II_USE_LTO "YES") + else() + message(FATAL_ERROR "You asked for interprocedural/link-time optimizations, but the compiler does not support these optimizations: ${_out}") + set(DEAL_II_USE_LTO "NO") + endif() +else() + # User did not set DEAL_II_USE_LTO on the command line. Set the + # variable to a specific (negative) value. + set(DEAL_II_USE_LTO "NO") +endif() diff --git a/cmake/macros/macro_populate_target_properties.cmake b/cmake/macros/macro_populate_target_properties.cmake index b85a803ccd..7223089c5b 100644 --- a/cmake/macros/macro_populate_target_properties.cmake +++ b/cmake/macros/macro_populate_target_properties.cmake @@ -15,7 +15,7 @@ # # populate_target_properties( ) # -# This function populate target properties according to (globally) defined +# This function populates target properties according to (globally) defined # DEAL_II_* variables. Specifically: # # DEAL_II_LIBRARIES DEAL_II_LIBRARIES_ @@ -126,4 +126,13 @@ function(populate_target_properties _target _build) ${DEAL_II_LIBRARIES} ${DEAL_II_LIBRARIES_${_build}} ${DEAL_II_TARGETS} ${DEAL_II_TARGETS_${_build}} ) + + + # For release builds (and their corresponding object files), + # use interprocedural optimizations if possible + if (DEAL_II_USE_LTO AND ("${_build}" STREQUAL "RELEASE")) + set_property(TARGET ${_target} + PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + endfunction() diff --git a/cmake/setup_cached_variables.cmake b/cmake/setup_cached_variables.cmake index b3fc8b9e5d..eb2d5957bf 100644 --- a/cmake/setup_cached_variables.cmake +++ b/cmake/setup_cached_variables.cmake @@ -31,6 +31,7 @@ # # CMAKE_BUILD_TYPE # DEAL_II_ALLOW_PLATFORM_INTROSPECTION +# DEAL_II_USE_LTO # DEAL_II_SETUP_COVERAGE # DEAL_II_UNITY_BUILD # DEAL_II_EARLY_DEPRECATIONS @@ -159,6 +160,12 @@ option(DEAL_II_ALLOW_PLATFORM_INTROSPECTION ) mark_as_advanced(DEAL_II_ALLOW_PLATFORM_INTROSPECTION) +option(DEAL_II_USE_LTO + "Allow the compiler to use interprocedural and link-time optimization (LTO)." + OFF + ) +mark_as_advanced(DEAL_II_USE_LTO) + option(DEAL_II_SETUP_COVERAGE "Setup debug compiler flags to provide additional test coverage information. Currently only gprof is supported." OFF diff --git a/doc/users/cmake_dealii.html b/doc/users/cmake_dealii.html index ddc2f12b8e..26305f34dd 100644 --- a/doc/users/cmake_dealii.html +++ b/doc/users/cmake_dealii.html @@ -781,6 +781,14 @@ cmake -DLAPACK_FOUND=true \ (default), deal.II will perform platform introspection for the given CPU. +
  • + DEAL_II_USE_LTO: If set to "ON" (the default is + "OFF"), allow the compiler to use interprocedural and + link-time optimization for release builds. These + optimizations often substantially improve performance, but + take both a substantial amount of memory and CPU time when + compiling the library. +
  • BUILD_SHARED_LIBS: If set (default), deal.II will be linked as a shared library diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 5d4f55ba06..502e7e921d 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -94,6 +94,7 @@ foreach(build ${DEAL_II_BUILD_TYPES}) set(build_camelcase "Release") endif() + # # Combine all ${build} OBJECT targets to a ${build} library: #