https://github.com/oneapi-src/oneTBB/pull/608

From 6feeba8035ea2bdf652d473a35730b19427752db Mon Sep 17 00:00:00 2001
From: Ivan Kochin <kochin.ivan@intel.com>
Date: Wed, 27 Oct 2021 17:23:32 +0300
Subject: [PATCH] Use native CMake way to detect the IPO support (#608)

* Use native CMake way to detect the IPO support

Signed-off-by: Kochin Ivan <kochin.ivan@intel.com>
---
 CMakeLists.txt               | 17 +++++++++++++++++
 cmake/README.md              |  1 +
 cmake/compilers/Clang.cmake  |  8 ++------
 cmake/compilers/GNU.cmake    |  8 +++-----
 cmake/compilers/MSVC.cmake   |  1 +
 cmake/utils.cmake            | 13 +++++++++++++
 src/tbb/CMakeLists.txt       |  5 ++---
 src/tbbbind/CMakeLists.txt   |  6 +++---
 src/tbbmalloc/CMakeLists.txt |  5 ++---
 9 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 13b1dbc2c..4dbdadb97 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,6 +103,7 @@ option(TBBMALLOC_BUILD "Enable tbbmalloc build" ON)
 option(TBB_CPF "Enable preview features of the library" OFF)
 option(TBB_FIND_PACKAGE "Enable search for external oneTBB using find_package instead of build from sources" OFF)
 option(TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH "Disable HWLOC automatic search by pkg-config tool" OFF)
+option(TBB_ENABLE_IPO "Enable Interprocedural Optimization (IPO) during the compilation" ON)
 
 if (NOT DEFINED BUILD_SHARED_LIBS)
     set(BUILD_SHARED_LIBS ON)
@@ -181,6 +182,22 @@ foreach(FILE_WITH_EXTRA_TARGETS ${FILES_WITH_EXTRA_TARGETS})
     include(${FILE_WITH_EXTRA_TARGETS})
 endforeach()
 
+# - Enabling LTO on Android causes the NDK bug.
+#   NDK throws the warning: "argument unused during compilation: '-Wa,--noexecstack'"
+# - For some reason GCC does not instrument code with Thread Sanitizer when lto is enabled and C linker is used.
+if (TBB_ENABLE_IPO AND BUILD_SHARED_LIBS AND NOT ANDROID_PLATFORM AND NOT TBB_SANITIZE MATCHES "thread")
+    if (NOT CMAKE_VERSION VERSION_LESS 3.9)
+        cmake_policy(SET CMP0069 NEW)
+        include(CheckIPOSupported)
+        check_ipo_supported(RESULT TBB_IPO_PROPERTY)
+    else()
+        set(TBB_IPO_FLAGS TRUE)
+    endif()
+    if (TBB_IPO_PROPERTY OR TBB_IPO_FLAGS)
+        message(STATUS "IPO enabled")
+    endif()
+endif()
+
 set(TBB_COMPILER_SETTINGS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/compilers/${CMAKE_CXX_COMPILER_ID}.cmake)
 if (EXISTS ${TBB_COMPILER_SETTINGS_FILE})
     include(${TBB_COMPILER_SETTINGS_FILE})
diff --git a/cmake/README.md b/cmake/README.md
index ec56e423c..ed1631de3 100644
--- a/cmake/README.md
+++ b/cmake/README.md
@@ -16,6 +16,7 @@ TBB_CPF:BOOL - Enable preview features of the library (OFF by default)
 TBB_INSTALL_VARS:BOOL - Enable auto-generated vars installation(packages generated by `cpack` and `make install` will also include the vars script)(OFF by default)
 TBB_VALGRIND_MEMCHECK:BOOL - Enable scan for memory leaks using Valgrind (OFF by default)
 TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH - Disable HWLOC automatic search by pkg-config tool (OFF by default)
+TBB_ENABLE_IPO - Enable Interprocedural Optimization (IPO) during the compilation (ON by default)
 ```
 
 ## Configure, build and test
diff --git a/cmake/compilers/Clang.cmake b/cmake/compilers/Clang.cmake
index 183341fcc..69aa51932 100644
--- a/cmake/compilers/Clang.cmake
+++ b/cmake/compilers/Clang.cmake
@@ -58,12 +58,8 @@ if (MINGW)
     list(APPEND TBB_COMMON_COMPILE_FLAGS -U__STRICT_ANSI__)
 endif()
 
-# Enabling LTO on Android causes the NDK bug.
-# NDK throws the warning: "argument unused during compilation: '-Wa,--noexecstack'"
-if (NOT ANDROID_PLATFORM AND BUILD_SHARED_LIBS)
-    set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
-    set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
-endif()
+set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
+set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
 
 # TBB malloc settings
 set(TBBMALLOC_LIB_COMPILE_FLAGS -fno-rtti -fno-exceptions)
diff --git a/cmake/compilers/GNU.cmake b/cmake/compilers/GNU.cmake
index fa14c869e..a9cfa8927 100644
--- a/cmake/compilers/GNU.cmake
+++ b/cmake/compilers/GNU.cmake
@@ -63,11 +63,9 @@ if (MINGW)
     list(APPEND TBB_COMMON_COMPILE_FLAGS -U__STRICT_ANSI__)
 endif()
 
-# For some reason GCC does not instrument code with Thread Sanitizer when lto is enabled and C linker is used.
-if (NOT TBB_SANITIZE MATCHES "thread")
-    set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
-    set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
-endif()
+set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
+set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
+
 
 # TBB malloc settings
 set(TBBMALLOC_LIB_COMPILE_FLAGS -fno-rtti -fno-exceptions)
diff --git a/cmake/compilers/MSVC.cmake b/cmake/compilers/MSVC.cmake
index 3447418cc..5767235a0 100644
--- a/cmake/compilers/MSVC.cmake
+++ b/cmake/compilers/MSVC.cmake
@@ -77,6 +77,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "(Clang|IntelLLVM)")
     endif()
     set(TBB_OPENMP_NO_LINK_FLAG TRUE)
     set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
+    set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
 else()
     set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:/GL>)
     set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-LTCG> $<$<NOT:$<CONFIG:Debug>>:-INCREMENTAL:NO>)
diff --git a/cmake/utils.cmake b/cmake/utils.cmake
index 06d3a9aee..f74abfcf9 100644
--- a/cmake/utils.cmake
+++ b/cmake/utils.cmake
@@ -44,3 +44,16 @@ macro(tbb_install_target target)
                 COMPONENT devel)
     endif()
 endmacro()
+
+macro(tbb_handle_ipo target)
+    if (TBB_IPO_PROPERTY)
+        set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
+    elseif (TBB_IPO_FLAGS)
+        target_compile_options(${target} PRIVATE ${TBB_IPO_COMPILE_FLAGS})
+        if (COMMAND target_link_options)
+            target_link_options(${target} PRIVATE ${TBB_IPO_LINK_FLAGS})
+        else()
+            target_link_libraries(${target} PRIVATE ${TBB_IPO_LINK_FLAGS})
+        endif()
+    endif()
+endmacro()
diff --git a/src/tbb/CMakeLists.txt b/src/tbb/CMakeLists.txt
index 80fdcdcde..021392d89 100644
--- a/src/tbb/CMakeLists.txt
+++ b/src/tbb/CMakeLists.txt
@@ -79,7 +79,6 @@ target_compile_options(tbb
     ${TBB_WARNING_SUPPRESS}
     ${TBB_LIB_COMPILE_FLAGS}
     ${TBB_COMMON_COMPILE_FLAGS}
-    ${TBB_IPO_COMPILE_FLAGS}
 )
 
 # Avoid use of target_link_libraries here as it changes /DEF option to \DEF on Windows.
@@ -89,6 +88,8 @@ set_target_properties(tbb PROPERTIES
     SOVERSION ${TBB_BINARY_VERSION}
 )
 
+tbb_handle_ipo(tbb)
+
 if (TBB_DEF_FILE_PREFIX) # If there's no prefix, assume we're using export directives
     set_target_properties(tbb PROPERTIES
         LINK_FLAGS ${TBB_LINK_DEF_FILE_FLAG}${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbb.def
@@ -103,14 +104,12 @@ if (COMMAND target_link_options)
         PRIVATE
         ${TBB_LIB_LINK_FLAGS}
         ${TBB_COMMON_LINK_FLAGS}
-        ${TBB_IPO_LINK_FLAGS}
     )
 else()
     target_link_libraries(tbb
         PRIVATE
         ${TBB_LIB_LINK_FLAGS}
         ${TBB_COMMON_LINK_FLAGS}
-        ${TBB_IPO_LINK_FLAGS}
     )
 endif()
 
diff --git a/src/tbbbind/CMakeLists.txt b/src/tbbbind/CMakeLists.txt
index 99b7ccaac..3233ec718 100644
--- a/src/tbbbind/CMakeLists.txt
+++ b/src/tbbbind/CMakeLists.txt
@@ -46,7 +46,6 @@ function(tbbbind_build TBBBIND_NAME REQUIRED_HWLOC_TARGET)
         ${TBB_WARNING_LEVEL}
         ${TBB_LIB_COMPILE_FLAGS}
         ${TBB_COMMON_COMPILE_FLAGS}
-        ${TBB_IPO_COMPILE_FLAGS}
     )
 
     # Avoid use of target_link_libraries here as it changes /DEF option to \DEF on Windows.
@@ -55,6 +54,9 @@ function(tbbbind_build TBBBIND_NAME REQUIRED_HWLOC_TARGET)
         VERSION ${TBBBIND_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
         SOVERSION ${TBBBIND_BINARY_VERSION}
     )
+
+    tbb_handle_ipo(${TBBBIND_NAME})
+
     if (TBB_DEF_FILE_PREFIX) # If there's no prefix, assume we're using export directives
         set_target_properties(${TBBBIND_NAME} PROPERTIES
             LINK_FLAGS ${TBB_LINK_DEF_FILE_FLAG}${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbbind.def
@@ -69,14 +71,12 @@ function(tbbbind_build TBBBIND_NAME REQUIRED_HWLOC_TARGET)
             PRIVATE
             ${TBB_LIB_LINK_FLAGS}
             ${TBB_COMMON_LINK_FLAGS}
-            ${TBB_IPO_LINK_FLAGS}
         )
     else()
         target_link_libraries(${TBBBIND_NAME}
             PRIVATE
             ${TBB_LIB_LINK_FLAGS}
             ${TBB_COMMON_LINK_FLAGS}
-            ${TBB_IPO_LINK_FLAGS}
         )
     endif()
 
diff --git a/src/tbbmalloc/CMakeLists.txt b/src/tbbmalloc/CMakeLists.txt
index f77bc8f10..5a851851f 100644
--- a/src/tbbmalloc/CMakeLists.txt
+++ b/src/tbbmalloc/CMakeLists.txt
@@ -59,7 +59,6 @@ target_compile_options(tbbmalloc
     ${TBB_LIB_COMPILE_FLAGS}
     ${TBBMALLOC_LIB_COMPILE_FLAGS}
     ${TBB_COMMON_COMPILE_FLAGS}
-    ${TBB_IPO_COMPILE_FLAGS}
 )
 
 enable_language(C)
@@ -72,6 +71,8 @@ set_target_properties(tbbmalloc PROPERTIES
     LINKER_LANGUAGE C
 )
 
+tbb_handle_ipo(tbbmalloc)
+
 if (TBB_DEF_FILE_PREFIX) # If there's no prefix, assume we're using export directives
     set_target_properties(tbbmalloc PROPERTIES
         LINK_FLAGS ${TBB_LINK_DEF_FILE_FLAG}${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbmalloc.def
@@ -88,14 +89,12 @@ if (COMMAND target_link_options)
         PRIVATE
         ${TBB_LIB_LINK_FLAGS}
         ${TBB_COMMON_LINK_FLAGS}
-        ${TBB_IPO_LINK_FLAGS}
     )
 else()
     target_link_libraries(tbbmalloc
         PRIVATE
         ${TBB_LIB_LINK_FLAGS}
         ${TBB_COMMON_LINK_FLAGS}
-        ${TBB_IPO_LINK_FLAGS}
     )
 endif()
 
