|
| 1 | +include(CheckIPOSupported) |
| 2 | +include(TestCXXAcceptsFlag) |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# ifem_lto |
| 6 | +# |
| 7 | +# Configure link-time optimization (IPO) for a target. |
| 8 | +# |
| 9 | +# This function enables LTO (Link Time Optimization) on the specified target. |
| 10 | +# It has extended interfaces for GCC and Clang settings. |
| 11 | +# |
| 12 | +# Enablement is controlled using the IFEM_USE_LTO option. |
| 13 | +# |
| 14 | +# Incremental cache directories are automatically managed under: |
| 15 | +# ${CMAKE_BINARY_DIR}/LTOCache/<config> |
| 16 | +# |
| 17 | +# Usage: |
| 18 | +# |
| 19 | +# ifem_ipo( |
| 20 | +# TARGET <target> |
| 21 | +# [CONFIGURATION_TYPES <config1;config2;...>] |
| 22 | +# ) |
| 23 | +# |
| 24 | +# Arguments: |
| 25 | +# TARGET (required) Target to which optimization will be applied. |
| 26 | +# CONFIGURATION_TYPES (optional) List of configuration names (e.g. Release). |
| 27 | +# If omitted, the default list from: |
| 28 | +# IFEM_LTO_CONFIGURATION_TYPES |
| 29 | +# is used. |
| 30 | +# |
| 31 | +# Cache Variables: |
| 32 | +# IFEM_LTO_JOBS (e.g. 1, 8) |
| 33 | +# IFEM_LTO_CACHE_POLICY (e.g. "prune_after=604800") |
| 34 | +# IFEM_LTO_CONFIGURATION_TYPES (e.g. "Release;RelWithDebInfo") |
| 35 | +# |
| 36 | +# ------------------------------------------------------------------------------ |
| 37 | + |
| 38 | +set(IFEM_LTO_CONFIGURATION_TYPES "Release;RelWithDebInfo" CACHE |
| 39 | + STRING "Default configuration types to apply interprocedural optimization.") |
| 40 | +set(IFEM_LTO_JOBS 1 CACHE STRING "Default interprocedural optimization jobs. Set to 'auto' to use all cores") |
| 41 | +set(IFEM_LTO_CACHE_POLICY "" CACHE STRING "Default interprocedural optimization cache policy.") |
| 42 | +mark_as_advanced(IFEM_LTO_CACHE_POLICY) |
| 43 | +mark_as_advanced(IFEM_LTO_CONFIGURATION_TYPES) |
| 44 | + |
| 45 | +# define possible values for LTO (order matters: first one in the list is used as default type) |
| 46 | +set(ifem_ipo_types) |
| 47 | +check_ipo_supported(LANGUAGES C CXX RESULT ipo_supported) |
| 48 | + |
| 49 | +if(IFEM_USE_LTO AND NOT ipo_supported) |
| 50 | + message(STATUS "Link-time optimization not supported") |
| 51 | + set(IFEM_USE_LTO OFF) |
| 52 | +endif() |
| 53 | + |
| 54 | +if(IFEM_USE_LTO) |
| 55 | + message(STATUS "Link-time optimization enabled") |
| 56 | +endif() |
| 57 | + |
| 58 | +function(ifem_lto) |
| 59 | + if(NOT IFEM_USE_LTO) |
| 60 | + return() |
| 61 | + endif() |
| 62 | + |
| 63 | + cmake_parse_arguments(PARAM "" "TARGET" "CONFIGURATION_TYPES" ${ARGN}) |
| 64 | + |
| 65 | + # if not set, use the default configuration types |
| 66 | + if(NOT PARAM_CONFIGURATION_TYPES) |
| 67 | + set(PARAM_CONFIGURATION_TYPES ${IFEM_LTO_CONFIGURATION_TYPES}) |
| 68 | + endif() |
| 69 | + |
| 70 | + foreach(config ${PARAM_CONFIGURATION_TYPES}) |
| 71 | + if(CMAKE_CONFIGURATION_TYPES MATCHES ${config} OR CMAKE_BUILD_TYPE MATCHES ${config}) |
| 72 | + set(LTO_CACHE_PATH ${CMAKE_BINARY_DIR}/LTOCache/${config}) |
| 73 | + |
| 74 | + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 75 | + # https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Optimize-Options.html |
| 76 | + target_compile_options(${PARAM_TARGET} PRIVATE $<$<CONFIG:${config}>:-flto=${IFEM_LTO_JOBS}>) |
| 77 | + target_link_options(${PARAM_TARGET} INTERFACE $<$<CONFIG:${config}>:-flto=${IFEM_LTO_JOBS}>) |
| 78 | + |
| 79 | + if(CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 15.0.0) |
| 80 | + # use incremental LTO https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/Optimize-Options.html |
| 81 | + target_compile_options(${PARAM_TARGET} PRIVATE $<$<CONFIG:${config}>:-flto-incremental=${LTO_CACHE_PATH}>) |
| 82 | + target_link_options(${PARAM_TARGET} INTERFACE $<$<CONFIG:${config}>:-flto-incremental=${LTO_CACHE_PATH}>) |
| 83 | + |
| 84 | + # Configure cache directory |
| 85 | + file(MAKE_DIRECTORY ${LTO_CACHE_PATH}) |
| 86 | + set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${LTO_CACHE_PATH}) |
| 87 | + endif() |
| 88 | + elseif(CMAKE_CXX_COMPILER_ID STREQUAL Clang) |
| 89 | + # Use ThinLTO and reuse cache (see https://releases.llvm.org/20.1.0/tools/clang/docs/ThinLTO.html) |
| 90 | + |
| 91 | + if(${CMAKE_CXX_COMPILER_LINKER_ID} MATCHES "GNU") |
| 92 | + message(FATAL_ERROR "Cannot use ThinLTO with BFD linker") |
| 93 | + endif() |
| 94 | + |
| 95 | + target_compile_options(${PARAM_TARGET} PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${config}>>:-flto=thin>) |
| 96 | + target_link_options(${PARAM_TARGET} INTERFACE $<$<CONFIG:${config}>:-flto=thin>) |
| 97 | + |
| 98 | + # Configure cache directory |
| 99 | + file(MAKE_DIRECTORY ${LTO_CACHE_PATH}) |
| 100 | + set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${LTO_CACHE_PATH}) |
| 101 | + |
| 102 | + # Parallel and cache options are linker dependent |
| 103 | + if(${CMAKE_CXX_COMPILER_LINKER_ID} MATCHES "LLD") |
| 104 | + target_link_options(${PARAM_TARGET} INTERFACE |
| 105 | + $<$<CONFIG:${config}>:LINKER:--thinlto-jobs=${IFEM_LTO_JOBS}> |
| 106 | + $<$<CONFIG:${config}>:LINKER:--thinlto-cache-dir=${LTO_CACHE_PATH}> |
| 107 | + ) |
| 108 | + if(IFEM_LTO_CACHE_POLICY) |
| 109 | + target_link_options(${PARAM_TARGET} INTERFACE |
| 110 | + $<$<CONFIG:${config}>:LINKER:--thinlto-cache-policy=${IFEM_LTO_CACHE_POLICY}> |
| 111 | + ) |
| 112 | + endif() |
| 113 | + elseif(${CMAKE_CXX_COMPILER_LINKER_ID} MATCHES "GNU|GNUgold|MOLD") |
| 114 | + target_link_options(${PARAM_TARGET} INTERFACE |
| 115 | + $<$<CONFIG:${config}>:LINKER:-plugin-opt,jobs=${IFEM_LTO_JOBS}> |
| 116 | + $<$<CONFIG:${config}>:LINKER:-plugin-opt,cache-dir=${LTO_CACHE_PATH}> |
| 117 | + ) |
| 118 | + if(IFEM_LTO_CACHE_POLICY) |
| 119 | + target_link_options(${PARAM_TARGET} INTERFACE |
| 120 | + $<$<CONFIG:${config}>:LINKER:-plugin-opt,cache-policy=${IFEM_LTO_CACHE_POLICY}> |
| 121 | + ) |
| 122 | + endif() |
| 123 | + elseif(${CMAKE_CXX_COMPILER_LINKER_ID} MATCHES "AppleClang") |
| 124 | + target_link_options(${PARAM_TARGET} INTERFACE |
| 125 | + $<$<CONFIG:${config}>:LINKER:-mllvm,-threads=${IFEM_LTO_JOBS}> |
| 126 | + $<$<CONFIG:${config}>:LINKER:-cache_path_lto,${LTO_CACHE_PATH}> |
| 127 | + ) |
| 128 | + else() |
| 129 | + message(DEBUG "LTO for Clang is supported, but linker type '${CMAKE_CXX_COMPILER_LINKER_ID}' is unrecognized. Skip adding parallelism or cache flags.") |
| 130 | + endif() |
| 131 | + endif() |
| 132 | + endif() |
| 133 | + endforeach() |
| 134 | +endfunction() |
0 commit comments