From 5e3450437711fbee24094971fa9ab4aaa5049f82 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Sat, 18 Sep 2021 17:13:58 +0800 Subject: [PATCH] fix(cmake/bazel/clang): remove finite-math-only opt from Ofast, for keep same build between gcc and clang more detail: ``` printf("%d\n", std::isnan(std::numeric_limits::quiet_NaN())); printf("%d\n", std::isnan(std::nan("1"))); ``` linux-clang and android-NDK clang have diff build logic with gcc and macos/windows clang/clangcl * clang++-12 -Ofast 1.cc output is: 0 0 * clang++-12 -Ofast 1.cc -fno-finite-math-only output is: 1 1 * g++ -Ofast 1.cc output is: 1 1 GitOrigin-RevId: f0622e2ca019b0072fd6798990a8aba156c8022f --- CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d03d3bed..e367ae1b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,7 +167,8 @@ if (NOT APPLE) endif() check_ipo_supported(RESULT IS_LTO_SUPPORT OUTPUT output_info) -if(IS_LTO_SUPPORT) +# LLVM on Windows report support LTO, but do not support -flto=full at link stage +if(IS_LTO_SUPPORT AND NOT WIN32) message(STATUS "lto is supported in this compiler") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=full") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto=full") @@ -363,6 +364,14 @@ else() else() set(OPTIMIZE_LEVEL "-g -O3 -DNDEBUG") endif() + #remove finite-math-only opt from Ofast, caused by clang have a different + #runtime finite math logic, this issue do not find at g++, but as a unity + #build flags, we force add -fno-finite-math-only when compiler support + CHECK_CXX_COMPILER_FLAG("-fno-finite-math-only" CXX_NO_FINITE_MATH_ONLY_SUPPORT) + if(CXX_NO_FINITE_MATH_ONLY_SUPPORT) + message(STATUS "force add -fno-finite-math-only for this compiler") + set(OPTIMIZE_LEVEL "${OPTIMIZE_LEVEL} -fno-finite-math-only") + endif() set(CMAKE_C_FLAGS_RELEASE "${OPTIMIZE_LEVEL}") set(CMAKE_CXX_FLAGS_RELEASE "${OPTIMIZE_LEVEL}") set(CMAKE_C_FLAGS_RELWITHDEBINFO "${OPTIMIZE_LEVEL}") -- GitLab