CM支持GCC的链接时间优化(LTO)

时间:2015-07-11 09:37:12

标签: cmake lto

我有最新的cmake构建并尝试所有构建配置(Debug,MinSizeRel,RelWithDebugInfo,Release,General)我在生成的makefile中看不到(文本搜索)字符串-lto,所以或者功能仍然是不存在,或者它需要手动翻转(在这种情况下,文本搜索LTO或链接时间优化)文档没有结果,所以我在官方文档中看不到启用LTO的方法。

是否有任何方法可以“全局”(对于所有编译器)启用LTO,而无需指定仅由GCC支持的手动标志?

4 个答案:

答案 0 :(得分:37)

好消息! CMake v3.9 最后 支持LTO

实施例

以下是展示其运作方式的示例代码:

cmake_minimum_required(VERSION 3.9.4)

include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)

add_executable(example Example.cpp)

if( supported )
    message(STATUS "IPO / LTO enabled")
    set_property(TARGET example PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
    message(STATUS "IPO / LTO not supported: <${error}>")
endif()

对于GCC,这会将-flto -fno-fat-lto-objects添加到目标编译命令。

检查编译器支持

模块CheckIPOSupported提供检查编译器是否支持过程间优化(IPO / LTO)

check_ipo_supported([RESULT <result>] [OUTPUT <output>]
                    [LANGUAGES <lang>...])

如果没有传递参数(= check_ipo_supported()),则会引发错误以指示它不受支持,否则result变量将设置为YES或{{ 1}}。有关详细信息,请参阅模块文档。

启用LTO

LTO针对所有目标的 单一目标 默认 启用。

单个目标的LTO

为目标集INTERPROCEDURAL_OPTIMIZATION启用LTO至NO。这是由set_property()命令完成的:

TRUE

LTO默认

通过将CMAKE_INTERPROCEDURAL_OPTIMIZATION设置为set_property(TARGET name-target-here PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) ,可以默认启用LTO:

TRUE

这将为此行之后创建的所有目标启用set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) 。之前创建的那些不受影响。

另见

答案 1 :(得分:10)

编辑:截至2015年10月28日,在CMake IRC上

jcelerier | I have a question about INTERPROCEDURAL_OPTIMIZATION
jcelerier | it sounds like it should enable -flto on gcc
+ngladitz | jcelerier: its only implemented for the intel compiler
jcelerier | ngladitz: ah, okay
jcelerier | are there other switches for pgo / lto ?
jcelerier | or must it be done by hand ?
+ngladitz | there currently is no first class support otherwise     

相关的CMake问题:https://gitlab.kitware.com/cmake/cmake/issues/15245

CMake在目标上具有INTERPROCEDURAL_OPTIMIZATION属性,这听起来可能会在某些平台上启用LTO。

链接:http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html

在目标MyLib上启用它:

add_library(MyLib ...)
...
set_property(TARGET MyLib PROPERTY INTERPROCEDURAL_OPTIMIZATION True)

答案 2 :(得分:2)

是的,根据源代码,gcc / clang编译器不支持LTO。他们只支持英特尔编译器。

我为cmake 3.4.3提供了一些grep输出:

grep -HiR 'INTERPROCEDURAL_OPTIMIZATION' *
Help/manual/cmake-properties.7.rst:  
/prop_dir/INTERPROCEDURAL_OPTIMIZATION_CONFIG
Help/manual/cmake-properties.7.rst:  
/prop_dir/INTERPROCEDURAL_OPTIMIZATION
Help/manual/cmake-properties.7.rst:  
/prop_tgt/INTERPROCEDURAL_OPTIMIZATION_CONFIG
Help/manual/cmake-properties.7.rst:  
/prop_tgt/INTERPROCEDURAL_OPTIMIZATION
Help/prop_dir/INTERPROCEDURAL_OPTIMIZATION.rst:INTERPROCEDURAL_OPTIMIZATION
Help/prop_dir/INTERPROCEDURAL_OPTIMIZATION_CONFIG.rst:INTERPROCEDURAL_OPTIMIZATION_<CONFIG    
Help/prop_dir/INTERPROCEDURAL_OPTIMIZATION_CONFIG.rst:This is a
per-configuration version of INTERPROCEDURAL_OPTIMIZATION.
Help/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.rst:INTERPROCEDURAL_OPTIMIZATION
Help/prop_tgt/INTERPROCEDURAL_OPTIMIZATION_CONFIG.rst:INTERPROCEDURAL_OPTIMIZATION_<CONFIG    
Help/prop_tgt/INTERPROCEDURAL_OPTIMIZATION_CONFIG.rst:This is a
per-configuration version of INTERPROCEDURAL_OPTIMIZATION.
Modules/Platform/Linux-Intel.cmake:    # INTERPROCEDURAL_OPTIMIZATION
Source/cmCommonTargetGenerator.cxx: 
if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
Source/cmGeneratorTarget.cxx:          
"INTERPROCEDURAL_OPTIMIZATION", config))
Source/cmMakefileLibraryTargetGenerator.cxx: 
if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
Tests/IPO/CMakeLists.txt:set_property(DIRECTORY PROPERTY
INTERPROCEDURAL_OPTIMIZATION 1)

如您所见,我们只有英特尔平台模块。

所以我为这个非常有用的功能创建了功能请求:https://cmake.org/Bug/view.php?id=15939

答案 3 :(得分:0)

目前,gcc和clang都支持lto。 (请参阅:https://clang.llvm.org/docs/CommandGuide/clang.html https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html ) 您可以通过cmake启用它: 设置(CMAKE_INTERPROCEDURAL_OPTIMIZATION True) 但是,您需要版本的cmake> = 3.9,并且我认为可能会设置一些策略,并检查编译器/工具链是否支持LTO(请检查CMAKE_IPO_AVAILABLE并抛出错误,否则将引发错误)

并为gcc / clang设置标志(对于fcc> 4.7设置-flto和-ffat-lto-files,对于clang设置-flto = full / thin)