diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/cpuid_check | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/cpuid_check')
-rw-r--r-- | library/cpp/cpuid_check/README.md | 13 | ||||
-rw-r--r-- | library/cpp/cpuid_check/cpu_id_check.cpp | 64 | ||||
-rw-r--r-- | library/cpp/cpuid_check/ya.make | 7 |
3 files changed, 84 insertions, 0 deletions
diff --git a/library/cpp/cpuid_check/README.md b/library/cpp/cpuid_check/README.md new file mode 100644 index 0000000000..9c0e8dfa59 --- /dev/null +++ b/library/cpp/cpuid_check/README.md @@ -0,0 +1,13 @@ +Simple utility to check base target x86 SIMD exensions at startup. + +Program may be built with some SIMD extension enabled (e.g. `-msse4.2`). `PEERDIR` to this library adds statrup check that machine where the program is running supports SIMD extension the program is built for. + +Currently supported check are: sse4.2, pclmul, aes, avx, avx2 and fma. + +**Note:** the library depends on `util`. +**Note:** the library adds stratup code and so if `PEERDIR`-ed from `LIBRARY` will do so for all `PROGRAM`-s that (transitively) use the `LIBRARY`. Don't do this! + +You normally don't need to `PEERDIR` this library at all. Since making sse4 in Arcadia default this library is used implicitly. It is `PEERDIR`-ed from all `PROGRAM`-s and derived modules (e.g. `PY2_PROGRAM`, but not `GO_PROGRAM` or `JAVA_PROGRAM`). +It is also not applied to `PROGRAM`-s where `NO_UTIL()`, `NO_PLATFORM()` or `ALLOCATOR(FAKE)` set to avoid undesired dependencied. To disable this implicit check use `NO_CPU_CHECK()` macro or `-DCPU_CHECK=no` ya make flag. + + diff --git a/library/cpp/cpuid_check/cpu_id_check.cpp b/library/cpp/cpuid_check/cpu_id_check.cpp new file mode 100644 index 0000000000..f19a573567 --- /dev/null +++ b/library/cpp/cpuid_check/cpu_id_check.cpp @@ -0,0 +1,64 @@ +#include <util/system/compat.h> +#include <util/system/compiler.h> +#include <util/system/cpu_id.h> +#include <util/system/platform.h> + +#define Y_CPU_ID_ENUMERATE_STARTUP_CHECKS(F) \ + F(SSE42) \ + F(PCLMUL) \ + F(AES) \ + F(AVX) \ + F(AVX2) \ + F(FMA) + +namespace { + [[noreturn]] void ReportISAError(const char* isa) { + err(-1, "This program was compiled for %s which is not supported on your system, exiting...", isa); + } + +#define Y_DEF_NAME(X) \ + void Assert##X() noexcept { \ + if (!NX86::Have##X()) { \ + ReportISAError(#X); \ + } \ + } + + Y_CPU_ID_ENUMERATE_STARTUP_CHECKS(Y_DEF_NAME) +#undef Y_DEF_NAME + + class TBuildCpuChecker { + public: + TBuildCpuChecker() { + Check(); + } + + private: + void Check() const noexcept { +#if defined(_fma_) + AssertFMA(); +#elif defined(_avx2_) + AssertAVX2(); +#elif defined(_avx_) + AssertAVX(); +#elif defined(_aes_) + AssertAES(); +#elif defined(_pclmul_) + AssertPCLMUL(); +#elif defined(_sse4_2_) + AssertSSE42(); +#endif + +#define Y_DEF_NAME(X) Y_UNUSED(Assert##X); + Y_CPU_ID_ENUMERATE_STARTUP_CHECKS(Y_DEF_NAME) +#undef Y_DEF_NAME + } + }; +} + +#if defined(_x86_) && !defined(_MSC_VER) +#define INIT_PRIORITY(x) __attribute__((init_priority(x))) +#else +#define INIT_PRIORITY(x) +#endif + +const static TBuildCpuChecker CheckCpuWeAreRunningOn INIT_PRIORITY(101) ; diff --git a/library/cpp/cpuid_check/ya.make b/library/cpp/cpuid_check/ya.make new file mode 100644 index 0000000000..9d794f3edc --- /dev/null +++ b/library/cpp/cpuid_check/ya.make @@ -0,0 +1,7 @@ +LIBRARY() + +OWNER(g:util g:ymake) + +SRCS(GLOBAL cpu_id_check.cpp) + +END() |