Skip to content

Commit 342b329

Browse files
kouclaude
andcommitted
GH-51690: [C++] Don't define __SSE2__ globally with MSVC
simdjson >= 5 uses GCC builtins such as __builtin_ctz when __SSE2__ is defined. So we can't build Apache Arrow C++ with simdjson >= 5 and MSVC because we define __SSE2__ globally. We don't need to define __SSE2__ because xsimd detects SSE2 by _M_X64 with MSVC. We still need to define __SSE4_1__/__SSE4_2__ for xsimd because MSVC doesn't define them even with /arch:SSE4.2. But we define them only where SSE4.2 is enabled. So they are no longer defined with ARROW_SIMD_LEVEL=NONE. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 50c2fc2 commit 342b329

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

‎cpp/cmake_modules/SetupCxxFlags.cmake‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ endif()
4949
if(ARROW_CPU_FLAG STREQUAL "x86")
5050
# x86/amd64 compiler flags, msvc/gcc/clang
5151
if(MSVC)
52-
set(ARROW_SSE4_2_FLAG "/arch:SSE4.2")
53-
# These definitions are needed for xsimd to consider the corresponding instruction
54-
# sets available, but they are not set by MSVC (unlike other compilers).
55-
# See https://github.com/AcademySoftwareFoundation/OpenImageIO/issues/4265
56-
add_definitions(-D__SSE2__ -D__SSE4_1__ -D__SSE4_2__)
52+
# MSVC doesn't define __SSE4_1__/__SSE4_2__ even with /arch:SSE4.2 (unlike
53+
# other compilers) but xsimd needs them to consider SSE4.x available. We
54+
# define them only where SSE4.2 is enabled. We don't define __SSE2__
55+
# because xsimd detects SSE2 by _M_X64 and simdjson >= 5 uses GCC builtins
56+
# when __SSE2__ is defined.
57+
# See also:
58+
# * https://learn.microsoft.com/en-us/cpp/build/reference/arch-x64
59+
# * https://github.com/xtensor-stack/xsimd/issues/878
60+
# * https://github.com/apache/arrow/issues/51690
61+
set(ARROW_SSE4_2_FLAG "/arch:SSE4.2" "-D__SSE4_1__" "-D__SSE4_2__")
5762
set(ARROW_AVX2_FLAGS "/arch:AVX2")
5863
# MSVC has no specific flag for BMI2, it seems to be enabled with AVX2
5964
set(ARROW_BMI2_FLAGS "/arch:AVX2")
@@ -502,7 +507,8 @@ if(ARROW_CPU_FLAG STREQUAL "x86")
502507
if(NOT CXX_SUPPORTS_SSE4_2)
503508
message(FATAL_ERROR "SSE4.2 required but compiler doesn't support it.")
504509
endif()
505-
set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} ${ARROW_SSE4_2_FLAG}")
510+
list(JOIN ARROW_SSE4_2_FLAG " " ARROW_SSE4_2_FLAG_COMMAND_LINE)
511+
string(APPEND CXX_COMMON_FLAGS " ${ARROW_SSE4_2_FLAG_COMMAND_LINE}")
506512
add_definitions(-DARROW_HAVE_SSE4_2)
507513
elseif(NOT ARROW_SIMD_LEVEL STREQUAL "NONE")
508514
message(WARNING "ARROW_SIMD_LEVEL=${ARROW_SIMD_LEVEL} not supported by x86.")

0 commit comments

Comments
 (0)