Index: cmake/modules/HandleLLVMOptions.cmake =================================================================== --- cmake/modules/HandleLLVMOptions.cmake +++ cmake/modules/HandleLLVMOptions.cmake @@ -17,6 +17,42 @@ set(LINKER_IS_LLD_LINK FALSE) endif() +set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO") +string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO) +if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK) + message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)") +endif() +if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") + append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) + if(NOT LINKER_IS_LLD_LINK) + append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + endif() + # If the linker supports it, enable the lto cache. This improves initial build + # time a little since we re-link a lot of the same objects, and significantly + # improves incremental build time. + # FIXME: We should move all this logic into the clang driver. + if(APPLE) + append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache" + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + elseif(UNIX AND LLVM_USE_LINKER STREQUAL "lld") + append("-Wl,--thinlto-cache-dir=${PROJECT_BINARY_DIR}/lto.cache" + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + elseif(LLVM_USE_LINKER STREQUAL "gold") + append("-Wl,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache" + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + endif() +elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") + append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) + if(NOT LINKER_IS_LLD_LINK) + append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + endif() +elseif(LLVM_ENABLE_LTO) + append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) + if(NOT LINKER_IS_LLD_LINK) + append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) + endif() +endif() + # Ninja Job Pool support # The following only works with the Ninja generator in CMake >= 3.0. set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING @@ -32,6 +68,11 @@ set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING "Define the maximum number of concurrent link jobs.") +if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN" AND NOT LLVM_PARALLEL_LINK_JOBS) + # ThinLTO does its own parallelism. Limit concurrent link jobs to 2 + # by default to avoid spawning too many threads. + set(LLVM_PARALLEL_LINK_JOBS "2") +endif() if(LLVM_PARALLEL_LINK_JOBS) if(NOT CMAKE_MAKE_PROGRAM MATCHES "ninja") message(WARNING "Job pooling is only available with Ninja generators.") @@ -713,42 +754,6 @@ CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) -set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO") -string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO) -if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK) - message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)") -endif() -if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") - append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) - if(NOT LINKER_IS_LLD_LINK) - append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - endif() - # If the linker supports it, enable the lto cache. This improves initial build - # time a little since we re-link a lot of the same objects, and significantly - # improves incremental build time. - # FIXME: We should move all this logic into the clang driver. - if(APPLE) - append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache" - CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - elseif(UNIX AND LLVM_USE_LINKER STREQUAL "lld") - append("-Wl,--thinlto-cache-dir=${PROJECT_BINARY_DIR}/lto.cache" - CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - elseif(LLVM_USE_LINKER STREQUAL "gold") - append("-Wl,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache" - CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - endif() -elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") - append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) - if(NOT LINKER_IS_LLD_LINK) - append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - endif() -elseif(LLVM_ENABLE_LTO) - append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS) - if(NOT LINKER_IS_LLD_LINK) - append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) - endif() -endif() - # This option makes utils/extract_symbols.py be used to determine the list of # symbols to export from LLVM tools. This is necessary when using MSVC if you # want to allow plugins, though note that the plugin has to explicitly link