[BE] Add append_cxx_flag_if_supported macro (#82883)

And use it throughout the CMakeLists and rectify `IF(APPLE)`/`IF(GNU_CXX_VERSION VERSION_GREATER A.B)` and so on

Also, add `target_compile_options_if_supported` and use it in `Dependencies.cmake` as well as in test's `CMakeListst.txt`

Delete `-Wno-unknown-warning-option` to test that conditions indeed working as expected
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82883
Approved by: https://github.com/seemethere
This commit is contained in:
Nikita Shulga 2022-08-10 14:32:25 +00:00 committed by PyTorch MergeBot
parent b3dea3e413
commit 62c8d30f9f
7 changed files with 90 additions and 84 deletions

View File

@ -43,7 +43,7 @@ set(CMAKE_C_STANDARD 11 CACHE STRING "The C standard whose features are reques
if(DEFINED GLIBCXX_USE_CXX11_ABI)
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1")
string(APPEND CMAKE_CXX_FLAGS " -D_GLIBCXX_USE_CXX11_ABI=1")
else()
# Please note this is required in order to ensure compatibility between gcc 9 and gcc 7
# This could be removed when all Linux PyTorch binary builds are compiled by the same toolchain again
@ -799,22 +799,22 @@ if(NOT MSVC)
# Details at http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1459
string(APPEND CMAKE_CXX_FLAGS " -Wall")
string(APPEND CMAKE_CXX_FLAGS " -Wextra")
string(APPEND CMAKE_CXX_FLAGS " -Werror=return-type")
append_cxx_flag_if_supported("-Werror=return-type" CMAKE_CXX_FLAGS)
if(NOT USE_CUDNN)
# Temporary fix to ignore non virtual dtor error if cudnn is used. A
# separate PR to cudnn_frontend is needed to address this later on
string(APPEND CMAKE_CXX_FLAGS " -Werror=non-virtual-dtor")
append_cxx_flag_if_supported("-Werror=non-virtual-dtor" CMAKE_CXX_FLAGS)
endif()
string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-field-initializers")
string(APPEND CMAKE_CXX_FLAGS " -Wno-type-limits")
string(APPEND CMAKE_CXX_FLAGS " -Wno-array-bounds")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-pragmas")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-parameter")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-function")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-result")
string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-overflow")
string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-aliasing")
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=deprecated-declarations")
append_cxx_flag_if_supported("-Wno-missing-field-initializers" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-type-limits" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-array-bounds" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unknown-pragmas" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unused-parameter" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unused-function" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unused-result" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-strict-overflow" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-strict-aliasing" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-error=deprecated-declarations" CMAKE_CXX_FLAGS)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_CXX_FLAGS " -Wno-range-loop-analysis")
string(APPEND CMAKE_CXX_FLAGS " -Wno-pass-failed")
@ -855,32 +855,31 @@ if(NOT MSVC)
endif()
endif()
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=pedantic")
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=redundant-decls")
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=old-style-cast")
append_cxx_flag_if_supported("-Wno-error=pedantic" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-error=redundant-decls" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-error=old-style-cast" CMAKE_CXX_FLAGS)
# These flags are not available in GCC-4.8.5. Set only when using clang.
# Compared against https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Option-Summary.html
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_CXX_FLAGS " -Wconstant-conversion")
string(APPEND CMAKE_CXX_FLAGS " -Wno-invalid-partial-specialization")
string(APPEND CMAKE_CXX_FLAGS " -Wno-typedef-redefinition")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-warning-option")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field")
string(APPEND CMAKE_CXX_FLAGS " -Wno-inconsistent-missing-override")
string(APPEND CMAKE_CXX_FLAGS " -Wno-aligned-allocation-unavailable")
string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions")
string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const")
string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces")
string(APPEND CMAKE_CXX_FLAGS " -Qunused-arguments")
append_cxx_flag_if_supported("-Wconstant-conversion" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-invalid-partial-specialization" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-typedef-redefinition" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unused-private-field" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-inconsistent-missing-override" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-aligned-allocation-unavailable" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-c++14-extensions" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-constexpr-not-const" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-missing-braces" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Qunused-arguments" CMAKE_CXX_FLAGS)
if(${COLORIZE_OUTPUT})
string(APPEND CMAKE_CXX_FLAGS " -fcolor-diagnostics")
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
if(${COLORIZE_OUTPUT})
string(APPEND CMAKE_CXX_FLAGS " -fdiagnostics-color=always")
endif()
if(${COLORIZE_OUTPUT})
append_cxx_flag_if_supported("-fcolor-diagnostics" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-fdiagnostics-color=always" CMAKE_CXX_FLAGS)
endif()
if((APPLE AND (NOT ("${CLANG_VERSION_STRING}" VERSION_LESS "9.0")))
OR(CMAKE_COMPILER_IS_GNUCXX
AND(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 AND NOT APPLE)))
@ -895,21 +894,15 @@ if(NOT MSVC)
endif()
endif(WERROR)
if(NOT APPLE)
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-but-set-variable")
string(APPEND CMAKE_CXX_FLAGS " -Wno-maybe-uninitialized")
append_cxx_flag_if_supported("-Wno-unused-but-set-variable" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
endif()
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
string(APPEND CMAKE_CXX_FLAGS " -fno-math-errno")
string(APPEND CMAKE_CXX_FLAGS " -fno-trapping-math")
check_cxx_compiler_flag("-Werror=format" HAS_WERROR_FORMAT)
if(HAS_WERROR_FORMAT)
string(APPEND CMAKE_CXX_FLAGS " -Werror=format")
endif()
check_cxx_compiler_flag("-Werror=cast-function-type" HAS_WERROR_CAST_FUNCTION_TYPE)
if(HAS_WERROR_CAST_FUNCTION_TYPE)
string(APPEND CMAKE_CXX_FLAGS " -Werror=cast-function-type")
endif()
append_cxx_flag_if_supported("-fno-math-errno" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-fno-trapping-math" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=format" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=cast-function-type" CMAKE_CXX_FLAGS)
check_cxx_compiler_flag("-Werror=sign-compare" HAS_WERROR_SIGN_COMPARE)
# This doesn't work globally so we use the test on specific
# target_compile_options
@ -971,19 +964,17 @@ if(APPLE)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_MPS -fno-objc-arc")
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -weak_framework Foundation -weak_framework MetalPerformanceShaders -weak_framework MetalPerformanceShadersGraph -weak_framework Metal")
endif()
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field")
string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces")
string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions")
string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const")
append_cxx_flag_if_supported("-Wno-unused-private-field" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-missing-braces" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-c++14-extensions" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-constexpr-not-const" CMAKE_CXX_FLAGS)
endif()
if(EMSCRIPTEN)
string(APPEND CMAKE_CXX_FLAGS " -Wno-implicit-function-declaration -DEMSCRIPTEN -s DISABLE_EXCEPTION_CATCHING=0")
endif()
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0.0)
string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow")
endif()
append_cxx_flag_if_supported("-Wno-stringop-overflow" CMAKE_CXX_FLAGS)
if(ANDROID AND (NOT ANDROID_DEBUG_SYMBOLS))
if(CMAKE_COMPILER_IS_GNUCXX)

View File

@ -417,10 +417,12 @@ struct Vectorized<c10::qint8> : public Vectorizedqi {
// This is needed because the compiler emits awful code for the default
// constructor for moving the enum
// NOLINTNEXTLINE(clang-diagnostic-deprecated-copy)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-copy"
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wdeprecated-copy")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wdeprecated-copy")
#endif
Vectorized(const Vectorized<c10::qint8>& other) : Vectorizedqi(other.vals) { }
#pragma clang diagnostic pop
C10_CLANG_DIAGNOSTIC_POP()
void store(void* ptr, int count = size()) const {
if (count != size()) {
@ -580,10 +582,12 @@ struct Vectorized<c10::quint8> : public Vectorizedqi {
}
// NOLINTNEXTLINE(clang-diagnostic-deprecated-copy)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-copy"
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wdeprecated-copy")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wdeprecated-copy")
#endif
Vectorized(const Vectorized<c10::quint8>& other) : Vectorizedqi(other.vals) { }
#pragma clang diagnostic pop
C10_CLANG_DIAGNOSTIC_POP()
void store(void* ptr, int count = size()) const {
if (count != size()) {

View File

@ -36,11 +36,6 @@
#include <ATen/native/TensorIteratorDynamicCasting.h>
#include <ATen/cpu/vec/vec.h>
#ifndef _MSC_VER
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#endif
namespace at { namespace native { inline namespace CPU_CAPABILITY {
using namespace vec;

View File

@ -26,7 +26,9 @@ if(USE_CUDA)
target_link_libraries(nvfuser_bench PRIVATE torch_library benchmark)
if(NOT MSVC)
target_compile_options(nvfuser_bench PRIVATE -Wno-unused-variable -Wno-deprecated-copy -Werror)
target_compile_options_if_supported(nvfuser_bench -Werror)
target_compile_options_if_supported(nvfuser_bench -Wno-unused-variable)
target_compile_options_if_supported(nvfuser_bench -Wno-deprecated-copy)
endif()
endif()

View File

@ -823,12 +823,8 @@ if(USE_FBGEMM)
set_property(TARGET fbgemm PROPERTY POSITION_INDEPENDENT_CODE ON)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
# See https://github.com/pytorch/pytorch/issues/74352
target_compile_options(asmjit PRIVATE -Wno-deprecated-copy)
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.1.6)
OR("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0.0))
# -Wno-unused-but-set-variable doesn't exist in Apple clang version 13.0.0 (clang-1300.0.29.30)
target_compile_options(asmjit PRIVATE -Wno-unused-but-set-variable)
endif()
target_compile_options_if_supported(asmjit -Wno-deprecated-copy)
target_compile_options_if_supported(asmjit -Wno-unused-but-set-variable)
endif()
endif()

View File

@ -570,3 +570,26 @@ function(torch_update_find_cuda_flags)
" CUDA_NVCC_FLAGS_MINSIZEREL = ${FLAGS_MINSIZEREL}")
endif()
endfunction()
##############################################################################
# CHeck if given flag is supported and append it to provided outputvar
# Also define HAS_UPPER_CASE_FLAG_NAME variable
# Usage:
# append_cxx_flag_if_supported("-Werror" CMAKE_CXX_FLAGS)
function(append_cxx_flag_if_supported flag outputvar)
string(TOUPPER "HAS${flag}" _FLAG_NAME)
string(REGEX REPLACE "[=-]" "_" _FLAG_NAME "${_FLAG_NAME}")
check_cxx_compiler_flag("${flag}" ${_FLAG_NAME})
if(${_FLAG_NAME})
string(APPEND ${outputvar} " ${flag}")
set(${outputvar} "${${outputvar}}" PARENT_SCOPE)
endif()
endfunction()
function(target_compile_options_if_supported target flag)
set(_compile_options "")
append_cxx_flag_if_supported("${flag}" _compile_options)
if(NOT "${_compile_options}" STREQUAL "")
target_compile_options(${target} PRIVATE ${flag})
endif()
endfunction()

View File

@ -54,8 +54,8 @@ add_executable(test_api ${TORCH_API_TEST_SOURCES})
target_include_directories(test_api PRIVATE ${ATen_CPU_INCLUDE})
target_link_libraries(test_api PRIVATE torch gtest)
if(NOT MSVC)
target_compile_options(test_api PRIVATE -Wno-unused-variable)
target_compile_options(test_api PRIVATE -Wno-unused-local-typedefs)
target_compile_options_if_supported(test_api -Wno-unused-variable)
target_compile_options_if_supported(test_api -Wno-unused-local-typedefs)
endif()
if(USE_CUDA)
@ -81,19 +81,14 @@ if(USE_OPENMP AND CMAKE_COMPILER_IS_GNUCXX AND (CMAKE_CXX_COMPILER_VERSION VERSI
endif()
if(NOT MSVC)
if(APPLE)
target_compile_options(test_api PRIVATE
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
-Wno-missing-braces)
else()
target_compile_options(test_api PRIVATE
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
-Wno-maybe-uninitialized
# gcc gives nonsensical warnings about variadic.h
-Wno-unused-but-set-parameter)
endif()
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
target_compile_options_if_supported(test_api "-Wno-missing-braces")
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
target_compile_options_if_supported(test_api "-Wno-maybe-uninitialized")
# gcc gives nonsensical warnings about variadic.h
target_compile_options_if_supported(test_api "-Wno-unused-but-set-parameter")
endif()
if(INSTALL_TEST)