Skip to content

Fix #4265: Add appropriate compiler defines and flags for SIMD with MSVC #4266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/cmake/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,29 @@ if (NOT USE_SIMD STREQUAL "")
if (USE_SIMD STREQUAL "0")
set (SIMD_COMPILE_FLAGS ${SIMD_COMPILE_FLAGS} "-DOIIO_NO_SIMD=1")
else ()
set(_highest_msvc_arch 0)
string (REPLACE "," ";" SIMD_FEATURE_LIST ${USE_SIMD})
foreach (feature ${SIMD_FEATURE_LIST})
message (VERBOSE "SIMD feature: ${feature}")
if (MSVC OR CMAKE_COMPILER_IS_INTEL)
list (APPEND SIMD_COMPILE_FLAGS "/arch:${feature}")
if (feature STREQUAL "sse2")
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__")
endif ()
if (feature STREQUAL "sse4.1")
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__" "/D__SSE4_1__")
endif ()
if (feature STREQUAL "sse4.2")
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__" "/D__SSE4_2__")
endif ()
if (feature STREQUAL "avx" AND _highest_msvc_arch LESS 1)
set(_highest_msvc_arch 1)
endif ()
if (feature STREQUAL "avx2" AND _highest_msvc_arch LESS 2)
set(_highest_msvc_arch 2)
endif ()
if (feature STREQUAL "avx512f" AND _highest_msvc_arch LESS 3)
set(_highest_msvc_arch 3)
endif ()
else ()
list (APPEND SIMD_COMPILE_FLAGS "-m${feature}")
endif ()
Expand All @@ -343,6 +361,20 @@ if (NOT USE_SIMD STREQUAL "")
proj_add_compile_options ("-ffp-contract=off")
endif ()
endforeach()

# Only add a single /arch flag representing the highest level of support.
if (MSVC OR CMAKE_COMPILER_IS_INTEL)
if (_highest_msvc_arch EQUAL 1)
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX")
endif ()
if (_highest_msvc_arch EQUAL 2)
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX2")
endif ()
if (_highest_msvc_arch EQUAL 3)
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX512")
endif ()
endif ()
unset(_highest_msvc_arch)
endif ()
proj_add_compile_options (${SIMD_COMPILE_FLAGS})
endif ()
Expand Down
Loading