From 24241e407c5994829c74c169255491905fb0fa10 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Wed, 5 Mar 2025 14:45:45 +0100 Subject: [PATCH 1/4] cmake: use -flto=auto compiler flag when supported Use -flto=auto compiler flag when supported, this silence this GCC warning: > lto-wrapper: warning: using serial compilation of # LTRANS jobs This also greatly speeds-up the linkage time as it enables LTO multithreading in GCC (either by using Make jobserver if detected, either by detecting CPU cores). Also always set LTO if enabled. --- CMakeLists.txt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ed79c99..8d845db9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.5) set(CMAKE_CXX_STANDARD 11) +include(CheckCXXCompilerFlag) + set(CRUNCH_PROJECT_NAME crunch) set(CRUNCH_LIBRARY_NAME crn) set(CRUNCH_EXE_NAME crunch) @@ -50,6 +52,14 @@ macro(set_linker_flag FLAG) endif() endmacro() +macro(try_cxx_flag PROP FLAG) + check_CXX_compiler_flag(${FLAG} FLAG_${PROP}) + + if (FLAG_${PROP}) + set_cxx_flag(${FLAG}) + endif() +endmacro() + # This option decides if crunch is dynamically linked against libcrn.so # statically linked against libcrn.o, enabling it always build libcrn.so. # This option is a builtin CMake one, the name means “build executables @@ -125,9 +135,12 @@ else() # It should be done at the very end because it copies all compiler flags # to the linker flags. if (USE_LTO) - set_cxx_flag("-flto" RELEASE) - set_cxx_flag("-flto" RELWITHDEBINFO) - set_cxx_flag("-flto" MINSIZEREL) + try_cxx_flag(FLTO_AUTO "-flto=auto") + + if (NOT FLAG_FLTO_AUTO) + try_cxx_flag(FLTO "-flto") + endif() + set_linker_flag("${CMAKE_CXX_FLAGS}" RELEASE) set_linker_flag("${CMAKE_CXX_FLAGS}" RELWITHDEBINFO) set_linker_flag("${CMAKE_CXX_FLAGS}" MINSIZEREL) From 59c53e9ee934c5159828fdde5b414c9a1d00316c Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Wed, 5 Mar 2025 15:11:18 +0100 Subject: [PATCH 2/4] cmake: rework the fast-math enablement and force the disablement Some compilers may enable fast-math by default (example: ICC). Some contractions are still safe and can be enabled. --- CMakeLists.txt | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d845db9..eb450017 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,9 @@ if (MSVC) # and https://devblogs.microsoft.com/cppblog/the-fpcontract-flag-and-changes-to-fp-modes-in-vs2022/ # By default, MSVC doesn't enable the /fp:fast option. set_cxx_flag("/fp:fast") + else() + # Precise model (/fp:precise) should do safe contractions, but we should not trust that (see below). + set_cxx_flag("/fp:strict") endif() if (USE_LTO) @@ -122,14 +125,34 @@ else() set_cxx_flag("-O3" RELWITHDEBINFO) endif() + try_cxx_flag(FNO_MATH_ERRNO "-fno-math-errno") + if (USE_FAST_MATH) - # By default, GCC uses -ffp-contract=fast with -std=gnu* and uses -ffp-contract=off with -std=c*. - # See https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html - # By default, GCC doesn't enable the -ffast-math option. - set_cxx_flag("-ffast-math -fno-math-errno -ffp-contract=fast") + try_cxx_flag(FFAST_MATH "-ffast-math") + + # GCC. + try_cxx_flag(FFP_CONTRACT_FAST "-ffp-contract=fast") + # Clang. + try_cxx_flag(FFP_MODEL_FAST "-ffp-model=agressive") + # ICC. + try_cxx_flag(FP_MODEL_FAST_2 "-fp-model=fast=2") + try_cxx_flag(QSIMD_HONOR_FP_MODEL "-qsimd-honor-fp-model") else() + try_cxx_flag(FNO_FAST_MATH "-fno-fast-math") + + # By default, GCC uses -ffp-contract=fast with -std=gnu* and uses -ffp-contract=off with -std=c*. # By default, GCC uses -std=gnu* and then enables -ffp-contract=fast even if -ffast-math is not enabled. - set_cxx_flag("-ffp-contract=off") + # See https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html + # GCC fast contractions (-ffp-contract=fast) should be safe, but aren't on arm64 with GCC 12. + # Clang precise contractions (-ffp-contract=precise) should be safe, but aren't on arm64 with Clang 14. + + # GCC. + try_cxx_flag(FFP_CONTRACT_OFF "-ffp-contract=off") + # Clang + try_cxx_flag(FFP_MODEL_STRICT "-ffp-model=strict") + # ICC. + try_cxx_flag(FP_MODEL_STRICT "-fp-model=strict") + try_cxx_flag(QSIMD_HONOR_FP_MODEL "-qsimd-honor-fp-model") endif() # It should be done at the very end because it copies all compiler flags From 1baede82088096a40893ee26dec5c5d4d2b8e24e Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Wed, 5 Mar 2025 15:50:17 +0100 Subject: [PATCH 3/4] cmake: fix typo --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb450017..4ba54dd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ endmacro() # This option decides if crunch is dynamically linked against libcrn.so # statically linked against libcrn.o, enabling it always build libcrn.so. # This option is a builtin CMake one, the name means “build executables -# against shader libraries”, not “build the shared libraries”. +# against shared libraries”, not “build the shared libraries”. option(BUILD_SHARED_LIBS "Link executables against shared library" OFF) # Always build libcrn.so even if crunch is linked to libcrn statically. option(BUILD_SHARED_LIBCRN "Build shared libcrn" OFF) From 9174b64cc3f12eeef57b31da8c3e52eebe5b1381 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 17 Mar 2025 18:02:41 +0100 Subject: [PATCH 4/4] ci: fast-math is now disabled by default --- .appveyor.yml | 1 - .azure-pipelines.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index d55826f4..d44067de 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -53,7 +53,6 @@ build_script: cmake -Wdev -G"%generator%" -A"%platform%" -S. -Bbuild -DCMAKE_CONFIGURATION_TYPES=Release -DBUILD_CRUNCH=ON -DBUILD_EXAMPLES=ON - -DUSE_FAST_MATH=OFF cmake --build build --config Release diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index 9b4a9a98..c22b05ef 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -125,7 +125,6 @@ steps: if [ -z "${SOURCE_DIR:-}" ]; then cmake_args+=(-DBUILD_CRUNCH=ON -DBUILD_EXAMPLES=ON -DBUILD_SHARED_LIBS=ON) fi - cmake_args+=(-DUSE_FAST_MATH=OFF) cmake -S"${SOURCE_DIR:-.}" -Bbuild "${cmake_args[@]}" cmake --build build --config Release displayName: 'Build'