Skip to content

CMake: Fix illegal instruction on MSVC - #261

Open
khuiqel wants to merge 1 commit into
google:mainfrom
khuiqel:bandaid-msvc-arch
Open

CMake: Fix illegal instruction on MSVC#261
khuiqel wants to merge 1 commit into
google:mainfrom
khuiqel:bandaid-msvc-arch

Conversation

@khuiqel

@khuiqel khuiqel commented Sep 11, 2026

Copy link
Copy Markdown

Problem

CMakeLists.txt checks SNAPPY_HAVE_BMI2 by compiling a function with _bzhi_u32. On Clang and GCC, SNAPPY_HAVE_BMI2 will be 0 if BMI2 is not enabled. On MSVC, the result will always be 1 on x86 regardless of the microarchitecture level. On CPUs without AVX2/BMI2, this will result in an illegal instruction (in snappy::ExtractLowBytes()). Presumably this also occurs with SNAPPY_HAVE_SSSE3 and SNAPPY_HAVE_X86_CRC32, as well as SNAPPY_HAVE_NEON_CRC32 and SNAPPY_HAVE_NEON on ARM, but I couldn't verify that. Can be reproduced with the BM_UFlat benchmarks.

The BMI2 check was added in 4f0adca, which noted the __BMI2__ preprocessor macro doesn't exist on MSVC. Did not test MSVC+Clang.

Solution

It's very much a band-aid, but wrap the instruction checks in if(NOT MSVC). This solves the illegal instruction issue, but SSSE3/CRC32/BMI2 intrinsics will no longer generate.

+if(NOT MSVC)
+
/* ARM and other x86 intrinsics checks */

check_cxx_source_compiles("
#include <immintrin.h>
int main() {
  return _bzhi_u32(0, 1);
}" SNAPPY_HAVE_BMI2)
 
+endif(NOT MSVC)
+

All 25 test pass with this patch. The following tests fail without this patch (on a CPU without BMI2):

[  FAILED  ] 14 tests, listed below:
[  FAILED  ] CorruptedTest.VerifyCorrupted
[  FAILED  ] Snappy.SimpleTests
[  FAILED  ] Snappy.AppendSelfPatternExtensionEdgeCases
[  FAILED  ] Snappy.AppendSelfPatternExtensionEdgeCasesExhaustive
[  FAILED  ] Snappy.MaxBlowup
[  FAILED  ] Snappy.RandomData
[  FAILED  ] Snappy.CompressionContext
[  FAILED  ] Snappy.CompressionContextStaticWorkspace
[  FAILED  ] Snappy.IOVecSinkEdgeCases
[  FAILED  ] Snappy.IOVecCopyOverflow
[  FAILED  ] Snappy.ZeroOffsetCopy
[  FAILED  ] Snappy.ZeroOffsetCopyValidation
[  FAILED  ] Snappy.LiteralLengthU32Overflow
[  FAILED  ] Snappy.TestBenchmarkFiles

Checking for SSSE3 and SSE4.2 (x86 CRC32) support on MSVC is impossible because MSVC only defines __AVX__, __AVX2__, and AVX-512 macros with no plan to change. So a proper solution for MSVC would involve adding SNAPPY_HAVE_XXXX options like AVX/AVX2 for SSSE3 and SSE4.2, like Highway does.

Somewhat related to #254

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant