From ecd00f59d5120085e9e58a7aae342f04a9d4cf86 Mon Sep 17 00:00:00 2001 From: GAECHTER TOYA Stefan Date: Fri, 23 Sep 2022 12:52:56 +0200 Subject: [PATCH 1/3] Check for native architecture and set GTSAM_COMPILE_OPTIONS_PUBLIC accordingly --- cmake/GtsamBuildTypes.cmake | 39 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/cmake/GtsamBuildTypes.cmake b/cmake/GtsamBuildTypes.cmake index e63fbf1dd4..d2918ad048 100644 --- a/cmake/GtsamBuildTypes.cmake +++ b/cmake/GtsamBuildTypes.cmake @@ -1,6 +1,6 @@ include(CheckCXXCompilerFlag) # for check_cxx_compiler_flag() -# Set cmake policy to recognize the AppleClang compiler +# Set cmake policy to recognize the Apple Clang compiler # independently from the Clang compiler. if(POLICY CMP0025) cmake_policy(SET CMP0025 NEW) @@ -87,10 +87,10 @@ if(MSVC) list_append_cache(GTSAM_COMPILE_DEFINITIONS_PRIVATE WINDOWS_LEAN_AND_MEAN NOMINMAX - ) + ) # Avoid literally hundreds to thousands of warnings: list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC - /wd4267 # warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data + /wd4267 # warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data ) add_compile_options(/wd4005) @@ -183,18 +183,33 @@ set(CMAKE_EXE_LINKER_FLAGS_PROFILING ${GTSAM_CMAKE_EXE_LINKER_FLAGS_PROFILING}) # Clang uses a template depth that is less than standard and is too small if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") - # Apple Clang before 5.0 does not support -ftemplate-depth. - if(NOT (APPLE AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "5.0")) - list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-ftemplate-depth=1024") - endif() + # Apple Clang before 5.0 does not support -ftemplate-depth. + if(NOT (APPLE AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "5.0")) + list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-ftemplate-depth=1024") + endif() endif() if (NOT MSVC) - option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF) - if(GTSAM_BUILD_WITH_MARCH_NATIVE AND (APPLE AND NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")) - # Add as public flag so all dependant projects also use it, as required - # by Eigen to avid crashes due to SIMD vectorization: - list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") + # Add as public flag so all dependant projects also use it, as required + # by Eigen to avid crashes due to SIMD vectorization: option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF) + if(APPLE AND (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") AND ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "15.0")) + if(GTSAM_BUILD_WITH_MARCH_NATIVE) + if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") + else() + message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") + endif() + endif() + else() + include(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) + if(GTSAM_BUILD_WITH_MARCH_NATIVE) + if(COMPILER_SUPPORTS_MARCH_NATIVE) + list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") + else() + message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") + endif() + endif() endif() endif() From d1fa38457afc9c0d0e921b3a1c3e6fdc81144c87 Mon Sep 17 00:00:00 2001 From: GAECHTER TOYA Stefan Date: Wed, 19 Oct 2022 14:54:24 +0200 Subject: [PATCH 2/3] Improve handling of GTSAM_BUILD_WITH_MARCH_NATIVE flag --- cmake/GtsamBuildTypes.cmake | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/cmake/GtsamBuildTypes.cmake b/cmake/GtsamBuildTypes.cmake index d2918ad048..8be4ce76df 100644 --- a/cmake/GtsamBuildTypes.cmake +++ b/cmake/GtsamBuildTypes.cmake @@ -190,27 +190,28 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") endif() if (NOT MSVC) - # Add as public flag so all dependant projects also use it, as required - # by Eigen to avid crashes due to SIMD vectorization: option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF) - if(APPLE AND (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") AND ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "15.0")) - if(GTSAM_BUILD_WITH_MARCH_NATIVE) + option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF) + if(GTSAM_BUILD_WITH_MARCH_NATIVE) + if(APPLE AND (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") AND ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "15.0")) if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + # Add as public flag so all dependant projects also use it, as required + # by Eigen to avoid crashes due to SIMD vectorization: list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") else() message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") - endif() - endif() - else() - include(CheckCXXCompilerFlag) - CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) - if(GTSAM_BUILD_WITH_MARCH_NATIVE) + endif() # CMAKE_SYSTEM_PROCESSOR + else() + include(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) if(COMPILER_SUPPORTS_MARCH_NATIVE) - list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") + # Add as public flag so all dependant projects also use it, as required + # by Eigen to avoid crashes due to SIMD vectorization: + list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") else() message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") - endif() - endif() - endif() + endif() # COMPILER_SUPPORTS_MARCH_NATIVE + endif() # APPLE + endif() # GTSAM_BUILD_WITH_MARCH_NATIVE endif() # Set up build type library postfixes From b34a8775bc00180719cc377dfe1a8eec4747a51b Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 23 Dec 2022 22:32:03 +0530 Subject: [PATCH 3/3] clean up cmake and add comments --- cmake/GtsamBuildTypes.cmake | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cmake/GtsamBuildTypes.cmake b/cmake/GtsamBuildTypes.cmake index 8be4ce76df..b6107e6d82 100644 --- a/cmake/GtsamBuildTypes.cmake +++ b/cmake/GtsamBuildTypes.cmake @@ -192,23 +192,31 @@ endif() if (NOT MSVC) option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF) if(GTSAM_BUILD_WITH_MARCH_NATIVE) - if(APPLE AND (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") AND ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "15.0")) - if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") - # Add as public flag so all dependant projects also use it, as required + # Check if Apple OS and compiler is [Apple]Clang + if(APPLE AND (${CMAKE_CXX_COMPILER_ID} MATCHES "^(Apple)?Clang$")) + # Check Clang version since march=native is only supported for version 15.0+. + if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "15.0") + if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + # Add as public flag so all dependent projects also use it, as required + # by Eigen to avoid crashes due to SIMD vectorization: + list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") + else() + message(WARNING "Option GTSAM_BUILD_WITH_MARCH_NATIVE ignored, because native architecture is not supported for Apple silicon and AppleClang version < 15.0.") + endif() # CMAKE_SYSTEM_PROCESSOR + else() + # Add as public flag so all dependent projects also use it, as required # by Eigen to avoid crashes due to SIMD vectorization: list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") - else() - message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") - endif() # CMAKE_SYSTEM_PROCESSOR + endif() # CMAKE_CXX_COMPILER_VERSION else() include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) if(COMPILER_SUPPORTS_MARCH_NATIVE) - # Add as public flag so all dependant projects also use it, as required + # Add as public flag so all dependent projects also use it, as required # by Eigen to avoid crashes due to SIMD vectorization: list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native") else() - message(WARNING "The option GTSAM_BUILD_WITH_MARCH_NATIVE is ignored, because native architecture is not supported.") + message(WARNING "Option GTSAM_BUILD_WITH_MARCH_NATIVE ignored, because native architecture is not supported.") endif() # COMPILER_SUPPORTS_MARCH_NATIVE endif() # APPLE endif() # GTSAM_BUILD_WITH_MARCH_NATIVE