summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authorcostan <costan@google.com>2019-01-08 06:06:34 -0800
committerVictor Costan <pwnall@chromium.org>2019-01-08 06:44:11 -0800
commit4f0adca400ed519c463f8bbc3031296e38dd746d (patch)
tree596709c820040ee910fca48dc5c7eb35ab0048ee /CMakeLists.txt
parent46768e335d6454855aed17ba3162b5903395bfba (diff)
downloadsnappy-git-4f0adca400ed519c463f8bbc3031296e38dd746d.tar.gz
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.
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt24
1 files changed, 22 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 41a2124..6d3a157 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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)