Enable LTO on Linux anc Mac Release#639
Enable LTO on Linux anc Mac Release#639apaniukov wants to merge 2 commits intoopenvinotoolkit:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enables Link Time Optimization (LTO) for Release builds to reduce the size of the produced shared library on non-Windows platforms, aligning Linux/macOS output more closely with Windows size.
Changes:
- Add
-fltocompile and link flags for Release builds insrc/CMakeLists.txt.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if(NOT MSVC) | ||
| include(CheckIPOSupported) | ||
| check_ipo_supported(RESULT ov_tokenizers_ipo_supported OUTPUT ov_tokenizers_ipo_error LANGUAGES C CXX) |
There was a problem hiding this comment.
The guard if(NOT MSVC) enables IPO/LTO for any non-MSVC toolchain, including Windows MinGW builds. This doesn’t match the PR’s stated intent (“Linux and Mac Release”) and may unexpectedly change Windows/non-MSVC builds. Consider tightening the condition to Linux/macOS only (e.g., using the LINUX flag set in cmake/platforms.cmake or APPLE).
| if(NOT MSVC) | ||
| include(CheckIPOSupported) | ||
| check_ipo_supported(RESULT ov_tokenizers_ipo_supported OUTPUT ov_tokenizers_ipo_error LANGUAGES C CXX) | ||
| if(ov_tokenizers_ipo_supported) | ||
| set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) | ||
| message(STATUS "IPO/LTO is enabled for Release builds") | ||
| else() | ||
| message(STATUS "IPO/LTO is not supported: ${ov_tokenizers_ipo_error}") |
There was a problem hiding this comment.
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) is set as a normal variable, which will shadow any user-provided cache setting (e.g., -DCMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE=OFF) and makes LTO hard to opt out from. Consider respecting an existing cache value or adding a dedicated option (e.g., ENABLE_LTO) so packagers/consumers can disable it when needed.
| if(NOT MSVC) | |
| include(CheckIPOSupported) | |
| check_ipo_supported(RESULT ov_tokenizers_ipo_supported OUTPUT ov_tokenizers_ipo_error LANGUAGES C CXX) | |
| if(ov_tokenizers_ipo_supported) | |
| set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) | |
| message(STATUS "IPO/LTO is enabled for Release builds") | |
| else() | |
| message(STATUS "IPO/LTO is not supported: ${ov_tokenizers_ipo_error}") | |
| option(ENABLE_LTO "Enable IPO/LTO for Release builds when supported" ON) | |
| if(NOT MSVC) | |
| if(ENABLE_LTO) | |
| include(CheckIPOSupported) | |
| check_ipo_supported(RESULT ov_tokenizers_ipo_supported OUTPUT ov_tokenizers_ipo_error LANGUAGES C CXX) | |
| if(ov_tokenizers_ipo_supported) | |
| set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) | |
| message(STATUS "IPO/LTO is enabled for Release builds") | |
| else() | |
| message(STATUS "IPO/LTO is not supported: ${ov_tokenizers_ipo_error}") | |
| endif() | |
| else() | |
| message(STATUS "IPO/LTO is disabled by ENABLE_LTO=OFF") |
9134160 to
0fea02d
Compare
| set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_OPTIONS "${extra_flags}") | ||
|
|
||
| # Enable LTO in Release build reduces the binary size by around 2 times | ||
| if(NOT MSVC AND CMAKE_BUILD_TYPE STREQUAL "Release") |
There was a problem hiding this comment.
i would make it as in genai:
via INTERPROCEDURAL_OPTIMIZATION_RELEASE property and ENABLE_LTO cmake flag
Window binary is 2 times smaller then on other platforms. Enable LTO to reduce binary size on Linux and Mac.
From 7.9 Mb to 3.6 Mb on Linux.
From 7.9 Mb to 3.9 Mb on Mac (same as Windows)