LTO with LLVM and CMake
Asked Answered
S

3

26

I am trying to apply Link Time Optimization with LLVM on a CMake Project, that creates a shared library. My question is pretty much the same as this one:

Switching between GCC and Clang/LLVM using CMake.

However, the answers do not seem to be applicable anymore, since llvm-ld is not present in the new versions. On the command line, I run the following commands to get LTO (Assuming there are only 2 .cpp files):

Compile to byte code:

clang++ -c FirstClass.cpp -O3 -flto -o FirstClass.bc
clang++ -c SecondClass.cpp -O3 -flto -o SecondClass.bc

Link byte code:

llvm-link FirstClass.bc SecondClass.bc -o unoptimized.bc

Optimize byte code:

opt -O3 unoptimized.bc -o optimized.bc

Convert byte code to shared object:

clang++ -shared optimized.bc -o libTest.so

Could somebody please tell me how to have CMake run the additional steps?

Semiprofessional answered 10/3, 2016 at 17:12 Comment(2)
It might be a job for add_custom_command of cmake... Something like add_custom_command(OUTPUT libTest.so COMMAND clang++ -shared optimized.bc -o libTest.so MAIN_DEPENDENCY optimized.bc) See #13470999George
Please also see stackoverflow.com/questions/31355692Realistic
D
24

The correct way to use Clang and enable LTO is using the -flto flag to the clang command line both at compile and link time.

In addition, you will need to be working on a platform with a linker that either directly supports LTO (Apple's platforms generally) or that have an LLVM linker plugin (Linux using the Gold linker, but I think some have gotten the BFD linker to support the linker plugin as well). If you're using the linker plugin, you'll need to make sure your install of LLVM built and installed the plugin. If it did, Clang will automatically add the necessary linker command line options to use the plugin when linking with -flto, even for shared objects.

Also, The LLVM project is working on a new linker (LLD) which will support LTO out of the box on all the platforms it supports, but it is still pretty early days. Currently I know of folks testing out its LTO support on Windows and Linux, and it seems to be working well but still misses many features.

Doorman answered 31/5, 2016 at 6:16 Comment(0)
R
3

check_ipo_supported() resulted for me in "Policy CMP0069 is not set" error on CMake 3.9.1.

Per its help, CMake up to 3.8 only supported Intel compiler's LTO. It didn't work on XCode 9's clang for me either.

What worked, in the end:

cmake_policy(SET CMP0069 NEW)
include(CheckIPOSupported)
check_ipo_supported()

add_executable(Foobar SOURCES)
set_target_properties(Foobar PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)

Looks like add_executable() needs to be after cmake_policy(SET CMP0069 NEW).

LTO cache

target_link_libraries(Foobar "-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache") did no harm.

Pick your command-line option depending on your linker.

More brutal option

According to @ChandlerCarruth's answer:

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
    target_link_libraries(Foobar -flto)
endif ()
Richter answered 27/10, 2017 at 0:35 Comment(0)
Y
2

Enabling (thin) lto on Cmake 3.9 and newer should be straightforward:

include(CheckIPOSupported)
check_ipo_supported()
set_target_properties(myProject PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)

Instead of set_target_properties per project, a single global setting of set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) can be done.

In order to speed up recompiles, a cache for LTO can be set:

function(append value)
    foreach(variable ${ARGN})
        set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
    endforeach(variable)
endfunction()

append("-fuse-ld=gold -Wl,--no-threads,--plugin-opt,cache-dir=${PROJECT_BINARY_DIR}/lto.cache" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)

This forces gold as linker, in order to use the right command line options. It might require a symlink of /usr/lib/LLVMgold.so to /usr/lib/llvm-4.0/lib/LLVMgold.so.

Yaker answered 31/7, 2017 at 19:38 Comment(2)
Be aware that when using the global setting, it has to be defined before adding the targets.Dominations
Is there a way to get cmake to pass -flto instead of -flto=thin?Yancey

© 2022 - 2024 — McMap. All rights reserved.