pytorch/caffe2/core/init_intrinsics_check.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

80 lines
2.3 KiB
C++

#include "caffe2/core/common.h"
#include "caffe2/core/flags.h"
#include "caffe2/core/init.h"
#include "caffe2/core/logging.h"
#include "caffe2/utils/cpuid.h"
C10_DEFINE_bool(
caffe2_quit_on_unsupported_cpu_feature,
false,
"If set, when Caffe2 is built with a CPU feature (like avx2) but the "
"current CPU does not support it, quit early. If not set (by default), "
"log this as an error message and continue execution.");
namespace caffe2 {
inline void QuitIfFeatureUnsupported(
const bool cpu_has_feature, const string& feature) {
VLOG(1) << "Caffe2 built with " << feature << ".";
if (!cpu_has_feature) {
string err_string =
"The Caffe2 binary is compiled with CPU feature " + feature +
", but your CPU does not support it. This will lead to segfaults "
"on your machine, such as SIGILL 'illegal instructions' on Linux. "
"As a result Caffe2 will preemptively quit. Please install or "
"build a Caffe2 binary with the feature turned off.";
if (FLAGS_caffe2_quit_on_unsupported_cpu_feature) {
LOG(FATAL) << err_string;
} else {
LOG(ERROR) << err_string;
}
}
}
static void WarnIfFeatureUnused(
const bool cpu_has_feature, const string& feature) {
VLOG(1) << "Caffe2 not built with " << feature << ".";
if (cpu_has_feature) {
#ifdef CAFFE2_NO_CROSS_ARCH_WARNING
// When cross-compiling single binary for multiple archs - turns off the
// annoying warning
VLOG(1)
#else
LOG(ERROR)
#endif
<< "CPU feature " << feature
<< " is present on your machine, "
"but the Caffe2 binary is not compiled with it. It means you "
"may not get the full speed of your CPU.";
}
}
bool Caffe2CheckIntrinsicsFeatures(int*, char***) {
#ifdef __AVX__
QuitIfFeatureUnsupported(GetCpuId().avx(), "avx");
#else
WarnIfFeatureUnused(GetCpuId().avx(), "avx");
#endif
#ifdef __AVX2__
QuitIfFeatureUnsupported(GetCpuId().avx2(), "avx2");
#else
WarnIfFeatureUnused(GetCpuId().avx2(), "avx2");
#endif
#ifdef __FMA__
QuitIfFeatureUnsupported(GetCpuId().fma(), "fma");
#else
WarnIfFeatureUnused(GetCpuId().fma(), "fma");
#endif
return true;
}
REGISTER_CAFFE2_INIT_FUNCTION(
Caffe2CheckIntrinsicsFeatures,
&Caffe2CheckIntrinsicsFeatures,
"Check intrinsics compatibility between the CPU feature and the binary.");
} // namespace caffe2