From: Matthew Fernandez Date: Sun, 8 Jan 2023 20:16:41 +0000 (-0800) Subject: CMake: enable LTO in release mode X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11c2897cc52385aa0bb03b022088a875814488b3;p=graphviz CMake: enable LTO in release mode Link-Time Optimization (LTO) is a mechanism that enables the compiler to optimize across translation unit boundaries. In particular, it enables cross-file function inlining. This is present and mature in the majority of contemporary compilers and switching it on has few downsides. Some performance numbers: ┌───────┬──────────────────┬──────────────────┬──────┐ │ │ before │ after │ diff │ ╞═══════╪══════════════════╪══════════════════╪══════╡ │ 1652¹ │ 17.88s │ 17.85s │ -0% │ │ │ 50.4MB peak RSS │ 50.5MB peak RSS │ +0% │ ├───────┼──────────────────┼──────────────────┼──────┤ │ 1718² │ 2m21s │ 2m13s │ -6% │ │ │ 15.8MB peak RSS │ 15.9 MB peak RSS │ +1% │ ├───────┼──────────────────┼──────────────────┼──────┤ │ 1864³ │ 13.26s │ 13.07s │ -1% │ │ │ 462.0MB peak RSS │ 461.8MB peak RSS │ -0% │ ├───────┼──────────────────┼──────────────────┼──────┤ │ 2064⁴ │ 11m42s │ 11m30s │ -2% │ │ │ 1.26GB peak RSS │ 1.26GB peak RSS │ -0% │ ├───────┼──────────────────┼──────────────────┼──────┤ │ 2095⁵ │ 2m18s │ 2m19s │ +1% │ │ │ 92.3MB peak RSS │ 92.1MB peak RSS │ -0% │ └───────┴──────────────────┴──────────────────┴──────┘ ¹ The test case from https://gitlab.com/graphviz/graphviz/-/issues/1652 run as `neato -Tsvg -o /dev/null 1652.dot`. ² swedish-flat.dot Magnus attached to https://gitlab.com/graphviz/graphviz/-/issues/1718 run as `circo -Tsvg -o /dev/null swedish-flag.dot`. ³ The test case from https://gitlab.com/graphviz/graphviz/-/issues/1864 run as `twopi -Tsvg -o /dev/null 1864.dot`. ⁴ The test case from https://gitlab.com/graphviz/graphviz/-/issues/2064 run as `dot -Gnslimit=2 -Gnslimit1=2 -Gmaxiter=5000 -Tsvg -o /dev/null 2064.dot`. ⁵ The tests/2095.dot test case from prior to minimization (3819821ea70fae730dd224936628ed3929b03531). Run as `dot -Tsvg -o /dev/null 2095.dot`. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index fc3a377b2..e425ae38c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,6 +220,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) +# enable LTO in release builds +include(CheckIPOSupported) +check_ipo_supported(RESULT HAVE_IPO) +if(CMAKE_BUILD_TYPE STREQUAL "Release" OR + CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") + if(HAVE_IPO) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) + endif() +endif() + if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) # MSVC warning C4996 mostly fires on completely valid code. The changes # proposed in the warning text often seriously compromise the code