Wrap BMI2 instruction usage in support checks.
A previous version of this was submitted and rolled back due to breakage -- an attempt to accommodate Visual Studio resulted in compiler errors on GCC/Clang with -mavx2 but without -mbmi2. This version makes the BMI2 support check more strict, to avoid the errors. A previous CL introduced _bzhi_u32 (part of Intel's BMI2 instruction set, released in Haswell) gated by a check for the __BMI2__ preprocessor macro. This works for Clang and GCC, but does not work on Visual Studio, and may not work on other compilers. This CL plumbs the BMI2 support checks through the CMake configuration used by the open source build. It also replaces the <x86intrin.h> header, which does not exist on Visual Studio, with the more scoped headers <tmmintrin.h> (for SSSE3) and <immintrin.h> (for BMI2/AVX2). Asides from fixing the open source build, the more scoped headers make it slightly less likely that newer intrinsics will creep in without proper gating.
This commit is contained in:
@@ -14,6 +14,8 @@ option(SNAPPY_BUILD_TESTS "Build Snappy's own tests." ON)
|
||||
|
||||
option(SNAPPY_REQUIRE_AVX "Target processors with AVX support." OFF)
|
||||
|
||||
option(SNAPPY_REQUIRE_AVX2 "Target processors with AVX2 support." OFF)
|
||||
|
||||
include(TestBigEndian)
|
||||
test_big_endian(SNAPPY_IS_BIG_ENDIAN)
|
||||
|
||||
@@ -33,15 +35,27 @@ check_library_exists(lzo2 lzo1x_1_15_compress "" HAVE_LIBLZO2)
|
||||
|
||||
include(CheckCXXCompilerFlag)
|
||||
CHECK_CXX_COMPILER_FLAG("/arch:AVX" HAVE_VISUAL_STUDIO_ARCH_AVX)
|
||||
CHECK_CXX_COMPILER_FLAG("/arch:AVX2" HAVE_VISUAL_STUDIO_ARCH_AVX2)
|
||||
CHECK_CXX_COMPILER_FLAG("-mavx" HAVE_CLANG_MAVX)
|
||||
if (SNAPPY_REQUIRE_AVX)
|
||||
CHECK_CXX_COMPILER_FLAG("-mbmi2" HAVE_CLANG_MBMI2)
|
||||
if(SNAPPY_REQUIRE_AVX2)
|
||||
if(HAVE_VISUAL_STUDIO_ARCH_AVX2)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
|
||||
endif(HAVE_VISUAL_STUDIO_ARCH_AVX2)
|
||||
if(HAVE_CLANG_MAVX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
|
||||
endif(HAVE_CLANG_MAVX)
|
||||
if(HAVE_CLANG_MBMI2)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mbmi2")
|
||||
endif(HAVE_CLANG_MBMI2)
|
||||
elseif (SNAPPY_REQUIRE_AVX)
|
||||
if(HAVE_VISUAL_STUDIO_ARCH_AVX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
|
||||
endif(HAVE_VISUAL_STUDIO_ARCH_AVX)
|
||||
if(HAVE_CLANG_MAVX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
|
||||
endif(HAVE_CLANG_MAVX)
|
||||
endif(SNAPPY_REQUIRE_AVX)
|
||||
endif(SNAPPY_REQUIRE_AVX2)
|
||||
|
||||
include(CheckCXXSourceCompiles)
|
||||
check_cxx_source_compiles("
|
||||
@@ -66,6 +80,12 @@ int main() {
|
||||
return 0;
|
||||
}" SNAPPY_HAVE_SSSE3)
|
||||
|
||||
check_cxx_source_compiles("
|
||||
#include <immintrin.h>
|
||||
int main() {
|
||||
return _bzhi_u32(0, 1);
|
||||
}" SNAPPY_HAVE_BMI2)
|
||||
|
||||
include(CheckSymbolExists)
|
||||
check_symbol_exists("mmap" "sys/mman.h" HAVE_FUNC_MMAP)
|
||||
check_symbol_exists("sysconf" "unistd.h" HAVE_FUNC_SYSCONF)
|
||||
|
||||
Reference in New Issue
Block a user