In CMake, how do you enable interprocedural optimization only for a target's Release configuration, and why call check_ipo_supported first?
answer
- INTERPROCEDURAL_OPTIMIZATION property
- the _<CONFIG> variant
- CheckIPOSupported module
- CMP0069 makes it an error
- flags differ per compiler
basics
~20 sInclude CheckIPOSupported, call check_ipo_supported(RESULT ok), and if ok set the INTERPROCEDURAL_OPTIMIZATION_RELEASE property on the target. Since policy CMP0069, enabling IPO on a compiler CMake has no IPO flags for is an error, so the check keeps unsupported toolchains building.
solid answer
~40 sInterprocedural optimization (IPO, usually link-time optimization) is a target property: `INTERPROCEDURAL_OPTIMIZATION`, with a per-configuration variant `INTERPROCEDURAL_OPTIMIZATION_<CONFIG>` that overrides it, so `INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE` limits it to Release. `CMAKE_INTERPROCEDURAL_OPTIMIZATION` initialises the property for targets created later. CMake picks the compiler's flags: `-flto=thin` for Clang, `-flto=auto -fno-fat-lto-objects` for GCC 11 and later on non-Apple platforms, `/GL` with `/LTCG` for MSVC. Under policy CMP0069 (NEW for projects declaring 3.9 or later), enabling the property where CMake knows no IPO flags is an error, so `include(CheckIPOSupported)` and `check_ipo_supported(RESULT ok OUTPUT msg)` test it first; without `RESULT`, an unsupported compiler is a fatal error. Because it is a property, it works with single- and multi-config generators alike.
code
cmake · 12 linescmake_minimum_required(VERSION 3.25)
project(render CXX)
add_executable(render main.cpp core.cpp)
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_ok OUTPUT ipo_msg LANGUAGES CXX)
message(STATUS "IPO supported: ${ipo_ok}")
if(ipo_ok)
set_property(TARGET render PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
else()
message(WARNING "IPO not supported: ${ipo_msg}")
endif()go deeper
Know that CMake turns LTO on with the INTERPROCEDURAL_OPTIMIZATION property instead of hand-written flags.
Explain the per-configuration property, the variable that initialises it, and what check_ipo_supported reports with and without RESULT.
Guard IPO with the support check, limit it to release configurations, and read compile and link lines to confirm the flags each compiler receives.
Decide where LTO's link-time cost is worth paying, such as release pipelines only, and keep that choice switchable through the CMake variables.
## What IPO means in CMake **Interprocedural optimization** lets the compiler optimize across translation units instead of one `.cpp` file at a time, enabling inlining and dead-code removal across files. When it happens at link time it is called **link-time optimization (LTO)**, the common form. CMake exposes it as one portable switch, so projects do not hard-code `-flto` and its relatives. ## The property and its per-configuration form - `INTERPROCEDURAL_OPTIMIZATION` on a target turns IPO on for every configuration. - `INTERPROCEDURAL_OPTIMIZATION_<CONFIG>`, for example `INTERPROCEDURAL_OPTIMIZATION_RELEASE`, applies to one configuration and overrides the general property when set. - `CMAKE_INTERPROCEDURAL_OPTIMIZATION` and `CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG>` initialise those properties for targets created afterwards, which is how a user or a CI job can turn IPO on without editing the project. Release-only LTO keeps Debug builds and incremental links fast while the shipped binary gets the cross-file optimization. ## Why check first Policy `CMP0069`, introduced in CMake 3.9, made the property **enforced**: with the `NEW` behaviour, CMake adds IPO flags for the current compiler or **produces an error** if it does not know any. A project that sets the property unconditionally therefore fails to configure on a toolchain without IPO support. The `CheckIPOSupported` module avoids this: ```cmake include(CheckIPOSupported) check_ipo_supported(RESULT ipo_ok OUTPUT ipo_msg LANGUAGES CXX) if(ipo_ok) set_property(TARGET render PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) else() message(WARNING "IPO not supported: ${ipo_msg}") endif() ``` 1. `RESULT` receives `YES` or `NO`. Without it, an unsupported compiler is a **fatal error**, which suits projects that require LTO. 2. `OUTPUT` receives the reason, worth printing in a warning. 3. `LANGUAGES` limits the check; by default it covers every enabled language. 4. Since CMake 3.24 (policy `CMP0138`), the check uses the caller's `CMAKE_<LANG>_FLAGS` and `CMAKE_<LANG>_FLAGS_<CONFIG>`, so it tests the flags the real build will use. `check_ipo_supported` itself requires `CMP0069` to be `NEW`, which any project declaring `cmake_minimum_required(VERSION 3.9)` or later has. ## What CMake passes to each compiler CMake's compiler modules choose the flags, so the project never names them: | Compiler | IPO compile flags CMake adds | |---|---| | Clang 3.9+, AppleClang 8+ | `-flto=thin` | | GCC 11+ (non-Apple platforms) | `-flto=auto -fno-fat-lto-objects` | | MSVC | `/GL`, with `/INCREMENTAL:NO` and `/LTCG` at link time | For GCC, CMake also switches static-library creation to the plugin-aware `gcc-ar` and `gcc-ranlib`, because a plain `ar` cannot index LTO objects. In a CMake 4.4.4 run with Apple clang and Ninja Multi-Config, the Release configuration compiled and linked with `-O3 -DNDEBUG -flto=thin`, while Debug compiled with `-g` only. ## Working with every generator Because IPO is a target property with a per-configuration form, the same code works for single-config generators, where only the configured build type's property applies, and multi-config generators, where each configuration gets its own flags. Adding `-flto` to `CMAKE_CXX_FLAGS_RELEASE` by hand also reaches only Release, but it is compiler-specific, skips the archiver change GCC needs, and is invisible to `check_ipo_supported`. ## Turning it on from outside and checking it A project does not have to decide alone. The variables let the person running the build choose: - `-DCMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE=ON` initialises the Release property for targets created afterwards, so a release pipeline can enable LTO even if the project never mentions it. - A project that wants to stay in control can wrap its own setting in an `option()`, as the `CheckIPOSupported` documentation's example does, and call the check only when the option is on. To confirm the result, read the commands: `cmake --build build --config Release -v` should show the IPO flag on both the compile and the link lines of the target, and the Debug build should not. If the flag is missing in Release, look for a later `set_property` that overrides it, or a target created before the `CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE` variable was set. ## Practical cautions - LTO raises link time and memory; teams often enable it only for release pipelines. - GCC objects built with `-fno-fat-lto-objects` carry only intermediate code, and GCC's manual notes that this requires the complete toolchain, linker and archiver included, to be LTO-aware. - Mixing compilers, or compiler versions, in one LTO link generally fails, since the intermediate representation is compiler-specific. What the optimizer actually does with LTO belongs to the material on GCC linking and optimization.
- How can a CI job turn on LTO for a project that never sets the IPO property?Configure with `-DCMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE=ON` (or the unsuffixed variable for all configurations). The variable initialises the target property for every target created afterwards, which is what policy CMP0069's documentation describes as letting a user enable IPO in an environment known to support it.
- Why does a GCC LTO build of a static library need gcc-ar instead of ar, and who handles that?With LTO the objects hold GCC's intermediate representation, and the archive index must be built through the LTO plugin, which `gcc-ar` and `gcc-ranlib` load. When the IPO property is on, CMake's GNU compiler module switches the archive commands for that target itself, which is one reason to use the property rather than adding `-flto` by hand.
- Setting INTERPROCEDURAL_OPTIMIZATION on an unsupported compiler is silently ignored
- IPO in CMake means adding -flto to CMAKE_CXX_FLAGS yourself
- check_ipo_supported without RESULT just returns NO when IPO is unsupported
- INTERPROCEDURAL_OPTIMIZATION_RELEASE needs a single-config generator to work
- CMake passes the same LTO flag to GCC, Clang and MSVC