From 7036facf10e5591ea93bb53d7e97097f82613729 Mon Sep 17 00:00:00 2001 From: dpasukhi Date: Thu, 30 Nov 2023 10:29:11 +0000 Subject: [PATCH] 0033399: Configuration - Research into opportunities to increase performance Implement CMake profiles to modify compiler and linker flags --- CMakeLists.txt | 4 ++++ adm/cmake/occt_defs_flags.cmake | 38 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ef2cb161b..86905287dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,10 @@ endmacro() set (USE_MMGR_TYPE "NATIVE" CACHE STRING "Select using memory manager tool.") set_property(CACHE USE_MMGR_TYPE PROPERTY STRINGS "NATIVE" "FLEXIBLE" "TBB" "JEMALLOC") +# set profile for C++ compiler and linker +set (BUILD_OPT_PROFILE "Default" CACHE STRING "Select profile for compiler and linker.") +set_property(CACHE BUILD_OPT_PROFILE PROPERTY STRINGS "Default" "Production") + # include variable description OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/vardescr") diff --git a/adm/cmake/occt_defs_flags.cmake b/adm/cmake/occt_defs_flags.cmake index 33b50e8ce7..3d989abcd4 100644 --- a/adm/cmake/occt_defs_flags.cmake +++ b/adm/cmake/occt_defs_flags.cmake @@ -44,9 +44,25 @@ elseif (MSVC) endif() if (MSVC) - # string pooling (GF), function-level linking (Gy) - set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GF /Gy") - set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /GF /Gy") + if ("${BUILD_OPT_PROFILE}" STREQUAL "Production") + # string pooling (GF), function-level linking (Gy) + set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GF /Gy") + set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /GF /Gy") + + # Favor fast code (Ot), Omit frame pointers (Oy) + set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ot /Oy") + set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ot /Oy") + + # Whole Program Optimisation (GL), Enable intrinsic functions (Oi) + set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL /Oi") + set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /GL /Oi") + + # Link-Time Code Generation(LTCG) is requared for Whole Program Optimisation(GL) + set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") + set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG") + set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") + set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} /LTCG") + endif() if (BUILD_FORCE_RelWithDebInfo) # generate debug info (Zi), inline expansion level (Ob1) set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi /Ob1") @@ -118,6 +134,22 @@ if (MSVC) endif() elseif (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "[Cc][Ll][Aa][Nn][Gg]")) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + + if ("${BUILD_OPT_PROFILE}" STREQUAL "Production") + # /Ot (favor speed over size) is similar to -O2 or -O3 in GCC/Clang. + # /Oy (omit frame pointers) is similar to -fomit-frame-pointer in GCC/Clang. + # /GL (whole program optimization) is similar to -flto (Link Time Optimization) in GCC/Clang. + # /GF (eliminate duplicate strings) doesn't have a direct equivalent in GCC/Clang, but the compilers do string pooling automatically. + # /Gy (enable function-level linking) is similar to -ffunction-sections in GCC/Clang. + # /fp:precise (improve floating-point consistency) is similar to -ffloat-store in GCC, but there isn't a perfect equivalent in Clang. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fomit-frame-pointer -flto -ffunction-sections -ffloat-store") + + # Link-Time Code Generation(LTCG) is requared for Whole Program Optimisation(GL) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto") + set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -flto") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -flto") + endif() if (CMAKE_CXX_COMPILER_ID MATCHES "[Cc][Ll][Aa][Nn][Gg]") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshorten-64-to-32") endif()