diff --git a/cmake/modules/HandleLLVMOptions.cmake b/cmake/modules/HandleLLVMOptions.cmake index 6db258ff66a..4b211e28557 100644 --- a/cmake/modules/HandleLLVMOptions.cmake +++ b/cmake/modules/HandleLLVMOptions.cmake @@ -635,6 +635,19 @@ append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate" 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(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") + append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") + append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +elseif(LLVM_ENABLE_LTO) + append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +endif() + # Plugin support # FIXME: Make this configurable. if(WIN32 OR CYGWIN) diff --git a/docs/CMake.rst b/docs/CMake.rst index 4e5feae9993..56d6867f602 100644 --- a/docs/CMake.rst +++ b/docs/CMake.rst @@ -347,6 +347,11 @@ LLVM-specific variables are ``Address``, ``Memory``, ``MemoryWithOrigins``, ``Undefined``, ``Thread``, and ``Address;Undefined``. Defaults to empty string. +**LLVM_ENABLE_LTO**:STRING + Add ``-flto`` or ``-flto=`` flags to the compile and link command + lines, enabling link-time optimization. Possible values are ``Off``, + ``On``, ``Thin`` and ``Full``. Defaults to OFF. + **LLVM_PARALLEL_COMPILE_JOBS**:STRING Define the maximum number of concurrent compilation jobs.