diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -225,12 +225,15 @@ # We may consider avoiding LTO altogether by using -fembed-bitcode # and teaching the linker to select machine code from .o files, see # https://lists.llvm.org/pipermail/llvm-dev/2021-April/149843.html - if((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld") + if((UNIX OR MINGW) AND LINKER_IS_LLD) set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--lto-O0") elseif(LINKER_IS_LLD_LINK) set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS " /opt:lldlto=0") + elseif(APPLE AND NOT LINKER_IS_LLD) + set_property(TARGET ${target_name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-mllvm,-O0") endif() endif() endif() @@ -1458,12 +1461,15 @@ # The runtime benefits of LTO don't outweight the compile time costs for tests. if(LLVM_ENABLE_LTO) - if((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld") + if((UNIX OR MINGW) AND LINKER_IS_LLD) set_property(TARGET ${test_name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--lto-O0") elseif(LINKER_IS_LLD_LINK) set_property(TARGET ${test_name} APPEND_STRING PROPERTY LINK_FLAGS " /opt:lldlto=0") + elseif(APPLE AND NOT LINKER_IS_LLD) + set_property(TARGET ${target_name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-mllvm,-O0") endif() endif() diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -14,6 +14,12 @@ include(CMakeDependentOption) include(LLVMProcessSources) +if(CMAKE_LINKER MATCHES ".*lld" OR (LLVM_USE_LINKER STREQUAL "lld" OR LLVM_ENABLE_LLD)) + set(LINKER_IS_LLD TRUE) +else() + set(LINKER_IS_LLD FALSE) +endif() + if(CMAKE_LINKER MATCHES "lld-link" OR (MSVC AND (LLVM_USE_LINKER STREQUAL "lld" OR LLVM_ENABLE_LLD))) set(LINKER_IS_LLD_LINK TRUE) else()