CMAKE_IPO_ISSUE msvc (/LTCG:incremental)

I’ve create a public github to demonstrate this issue.

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)

The code above turns on LTO but not in an always desirable fashion.

It sets /LTCG to be INCREMENTAL. Which isn’t what I want for release configs.

I might want INCREMENTAL for debug, relwithdebinfo, etc. But not release. But it looks like I have no control over it.

1 Like

This issue is Windows MSVC specific.

The options CMake uses for IPO on MSVC can be seen here. It compiles with /GL and links with /INCREMENTAL:NO /LTCG. The Visual Studio generator then sends the flags through this mapping to convert them to the actual .vcxproj content. I think the order of the flags in that mapping may be wrong. You can try hacking that json file in your local installation to switch the two blocks seen in my link.

Please open an issue for this on the CMake Issue Tracker.

After looking at the implementation, the Visual Studio generator is not using those places I linked that list LTCG flags. It is directly implementing the INTERPROCEDURAL_OPTIMIZATION target property by generating .vcxproj settings.

Either way, please open an issue for this.

I’ve created the issue for this.

https://gitlab.kitware.com/cmake/cmake/issues/20484

I’ve also been having this issue.

Thanks hdf89shfdfs :slight_smile:

2 Likes

same issue here ASAN is incompatible with incremental LTCG :frowning:

here is my workaround for now

# Set these to ON when including geoproc as a sub CMake project
option(GEOPROC_DISABLE_ASAN "" OFF)
option(GEOPROC_DISABLE_IPO "" OFF)

if (NOT GEOPROC_DISABLE_ASAN)
    add_compile_definitions(GEOPROC_ASAN)
    if (MSVC)
        add_compile_options(/fsanitize=address)
        add_link_options(/INCREMENTAL:NO) # Diable incremental linking becauase MSVC ASAN is not compatible with it.
    else ()
        add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
        add_link_options(-fsanitize=address)
    endif ()
endif ()

if (NOT GEOPROC_DISABLE_IPO)
    # https://web.archive.org/web/20240419204531/https://cliutils.gitlab.io/modern-cmake/chapters/features/small.html#interprocedural-optimization
    include(CheckIPOSupported)
    check_ipo_supported(RESULT is_ipo_supported)
    if (is_ipo_supported)
        if (MSVC AND NOT GEOPROC_DISABLE_ASAN)
            # Default CMake interprocedural optimization MSVC flags are not compatible with ASAN
            # https://gitlab.kitware.com/cmake/cmake/-/issues/20484
            add_link_options(/LTCG) # Enable "Link Time Code Generation"
            add_compile_options(/GL) # LTCG is useless without "Whole Program Optimization"
            add_link_options(/INCREMENTAL:NO) # Disable incremental linking because /LTCG is not compatible with it.
        else ()
            set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
        endif ()
    endif ()
endif ()