diff options
author | Ruslan Baratov <ruslan_baratov@yahoo.com> | 2017-03-28 06:47:42 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-03-30 18:56:52 (GMT) |
commit | dfa8263f4be4f1413b73c81649fdc4567a71e56a (patch) | |
tree | 23e540d2dcb39793a0f9ef503e392fb854beb985 | |
parent | 1588a577d16cfb1a689a444b1db1df3ccff2cc3d (diff) | |
download | CMake-dfa8263f4be4f1413b73c81649fdc4567a71e56a.zip CMake-dfa8263f4be4f1413b73c81649fdc4567a71e56a.tar.gz CMake-dfa8263f4be4f1413b73c81649fdc4567a71e56a.tar.bz2 |
Implement interprocedural optimization for GNU compilers
Honor the `INTERPROCEDURAL_OPTIMIZATION` target property for GNU
compilers by activating their link-time-optimization (LTO) flags.
-rw-r--r-- | Help/release/dev/gcc-ipo.rst | 7 | ||||
-rw-r--r-- | Modules/Compiler/Clang.cmake | 8 | ||||
-rw-r--r-- | Modules/Compiler/GNU.cmake | 39 | ||||
-rw-r--r-- | Modules/Compiler/QCC.cmake | 8 | ||||
-rw-r--r-- | Source/cmGeneratorTarget.cxx | 23 | ||||
-rw-r--r-- | Source/cmGeneratorTarget.h | 3 | ||||
-rw-r--r-- | Source/cmMakefileLibraryTargetGenerator.cxx | 21 | ||||
-rw-r--r-- | Source/cmNinjaNormalTargetGenerator.cxx | 8 |
8 files changed, 103 insertions, 14 deletions
diff --git a/Help/release/dev/gcc-ipo.rst b/Help/release/dev/gcc-ipo.rst new file mode 100644 index 0000000..ebc5c0d --- /dev/null +++ b/Help/release/dev/gcc-ipo.rst @@ -0,0 +1,7 @@ +GCC IPO +------- + +* Interprocedural optimization (IPO) is now supported for GNU + compilers using link time optimization (LTO) flags. See the + :prop_tgt:`INTERPROCEDURAL_OPTIMIZATION` target property and + :module:`CheckIPOSupported` module. diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake index 96263fc..6b99a08 100644 --- a/Modules/Compiler/Clang.cmake +++ b/Modules/Compiler/Clang.cmake @@ -27,5 +27,13 @@ else() set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=") set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "--gcc-toolchain=") endif() + + set(_CMAKE_IPO_SUPPORTED_BY_CMAKE NO) + set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO) + + unset(CMAKE_${lang}_COMPILE_OPTIONS_IPO) + unset(CMAKE_${lang}_ARCHIVE_CREATE_IPO) + unset(CMAKE_${lang}_ARCHIVE_APPEND_IPO) + unset(CMAKE_${lang}_ARCHIVE_FINISH_IPO) endmacro() endif() diff --git a/Modules/Compiler/GNU.cmake b/Modules/Compiler/GNU.cmake index b67002c..4b1c598 100644 --- a/Modules/Compiler/GNU.cmake +++ b/Modules/Compiler/GNU.cmake @@ -45,4 +45,43 @@ macro(__compiler_gnu lang) if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462 set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ") endif() + + set(_CMAKE_IPO_SUPPORTED_BY_CMAKE YES) + set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO) + + # '-flto' introduced since GCC 4.5: + # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no) + # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes) + if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5) + set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES) + set(__lto_flags -flto) + + if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7) + # '-ffat-lto-objects' introduced since GCC 4.7: + # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no) + # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes) + list(APPEND __lto_flags -fno-fat-lto-objects) + endif() + + set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags}) + + # Need to use version of 'ar'/'ranlib' with plugin support. + # Quote from [documentation][1]: + # + # To create static libraries suitable for LTO, + # use gcc-ar and gcc-ranlib instead of ar and ranlib + # + # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html + set(CMAKE_${lang}_ARCHIVE_CREATE_IPO + "${CMAKE_GCC_AR} cr <TARGET> <LINK_FLAGS> <OBJECTS>" + ) + + set(CMAKE_${lang}_ARCHIVE_APPEND_IPO + "${CMAKE_GCC_AR} r <TARGET> <LINK_FLAGS> <OBJECTS>" + ) + + set(CMAKE_${lang}_ARCHIVE_FINISH_IPO + "${CMAKE_GCC_RANLIB} <TARGET>" + ) + endif() endmacro() diff --git a/Modules/Compiler/QCC.cmake b/Modules/Compiler/QCC.cmake index 2d7e881..695a138 100644 --- a/Modules/Compiler/QCC.cmake +++ b/Modules/Compiler/QCC.cmake @@ -12,4 +12,12 @@ macro(__compiler_qcc lang) set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-Wp,-isystem,") set(CMAKE_DEPFILE_FLAGS_${lang} "-Wc,-MD,<DEPFILE>,-MT,<OBJECT>,-MF,<DEPFILE>") + + set(_CMAKE_IPO_SUPPORTED_BY_CMAKE NO) + set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO) + + unset(CMAKE_${lang}_COMPILE_OPTIONS_IPO) + unset(CMAKE_${lang}_ARCHIVE_CREATE_IPO) + unset(CMAKE_${lang}_ARCHIVE_APPEND_IPO) + unset(CMAKE_${lang}_ARCHIVE_FINISH_IPO) endmacro() diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index bb79050..c8422d4 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -2466,19 +2466,28 @@ void cmGeneratorTarget::GetAppleArchs(const std::string& config, } } +//---------------------------------------------------------------------------- +std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable( + std::string const& var, std::string const& config) const +{ + if (this->IsIPOEnabled(config)) { + std::string varIPO = var + "_IPO"; + if (this->Makefile->IsDefinitionSet(varIPO)) { + return varIPO; + } + } + + return var; +} + +//---------------------------------------------------------------------------- std::string cmGeneratorTarget::GetCreateRuleVariable( std::string const& lang, std::string const& config) const { switch (this->GetType()) { case cmStateEnums::STATIC_LIBRARY: { std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY"; - if (this->IsIPOEnabled(config)) { - std::string varIPO = var + "_IPO"; - if (this->Makefile->GetDefinition(varIPO)) { - return varIPO; - } - } - return var; + return this->GetFeatureSpecificLinkRuleVariable(var, config); } case cmStateEnums::SHARED_LIBRARY: return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY"; diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 80bccd5..2510407 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -309,6 +309,9 @@ public: void GetAppleArchs(const std::string& config, std::vector<std::string>& archVec) const; + std::string GetFeatureSpecificLinkRuleVariable( + std::string const& var, std::string const& config) const; + /** Return the rule variable used to create this type of target. */ std::string GetCreateRuleVariable(std::string const& lang, std::string const& config) const; diff --git a/Source/cmMakefileLibraryTargetGenerator.cxx b/Source/cmMakefileLibraryTargetGenerator.cxx index 9ce13ec..cc8a6b3 100644 --- a/Source/cmMakefileLibraryTargetGenerator.cxx +++ b/Source/cmMakefileLibraryTargetGenerator.cxx @@ -129,14 +129,9 @@ void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules() { std::string linkLanguage = this->GeneratorTarget->GetLinkerLanguage(this->ConfigName); - std::string linkRuleVar = "CMAKE_"; - linkRuleVar += linkLanguage; - linkRuleVar += "_CREATE_STATIC_LIBRARY"; - if (this->GeneratorTarget->IsIPOEnabled(this->ConfigName) && - this->Makefile->GetDefinition(linkRuleVar + "_IPO")) { - linkRuleVar += "_IPO"; - } + std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable( + linkLanguage, this->ConfigName); std::string extraFlags; this->LocalGenerator->GetStaticLibraryFlags( @@ -676,18 +671,30 @@ void cmMakefileLibraryTargetGenerator::WriteLibraryRules( std::string arCreateVar = "CMAKE_"; arCreateVar += linkLanguage; arCreateVar += "_ARCHIVE_CREATE"; + + arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( + arCreateVar, this->ConfigName); + if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) { cmSystemTools::ExpandListArgument(rule, archiveCreateCommands); } std::string arAppendVar = "CMAKE_"; arAppendVar += linkLanguage; arAppendVar += "_ARCHIVE_APPEND"; + + arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( + arAppendVar, this->ConfigName); + if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) { cmSystemTools::ExpandListArgument(rule, archiveAppendCommands); } std::string arFinishVar = "CMAKE_"; arFinishVar += linkLanguage; arFinishVar += "_ARCHIVE_FINISH"; + + arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( + arFinishVar, this->ConfigName); + if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) { cmSystemTools::ExpandListArgument(rule, archiveFinishCommands); } diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 5552fa9..eac3ecf 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -516,6 +516,10 @@ std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd() std::string linkCmdVar = "CMAKE_"; linkCmdVar += this->TargetLinkLanguage; linkCmdVar += "_ARCHIVE_CREATE"; + + linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( + linkCmdVar, this->GetConfigName()); + const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar); cmSystemTools::ExpandListArgument(linkCmd, linkCmds); } @@ -523,6 +527,10 @@ std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd() std::string linkCmdVar = "CMAKE_"; linkCmdVar += this->TargetLinkLanguage; linkCmdVar += "_ARCHIVE_FINISH"; + + linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( + linkCmdVar, this->GetConfigName()); + const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar); cmSystemTools::ExpandListArgument(linkCmd, linkCmds); } |