Skip to content

Commit 8360f87

Browse files
jessey-gitlgritz
authored andcommitted
build: Add appropriate compiler defines and flags for SIMD with MSVC (AcademySoftwareFoundation#4266)
This should address the following 2 issues when using MSVC: - MSVC does not set defines like `__SSE2__` or `__SSE4_2__` etc. we must do that ourselves - MSVC uses the `/arch` flag differently than other compilers and it should be set only once The ramifications of not having those defines set basically means that OIIO won't notice any SIMD is possible. For MSVC the /arch flag only controls which instruction set the compiler may use when generating code "on its own". For example, the x64 compiler by default will use SSE2 whenever possible when its generating code but when using `/arch:AVX` it will produce AVX code as it sees fit. NOTE: case matters here too! "avx" does not work but "AVX" does. However, if the source code uses intrinsics, MSVC will happily use those instead. In fact it's possible to use `avx512` intrinsics without specifying any /arch flag at all and it will happily generate corresponding avx512 code. This is much different than say GCC where, in order to use intrinsics at all, you must specify the corresponding flags allowing you to do so for the arch in question. Fixes AcademySoftwareFoundation#4265 ## Tests Existing simd_test.cpp should be sufficient I think. In addition to those, I've verified the below locally: Currently: when using `-DUSE_SIMD="sse4.2,avx2"` to build, we get the wrong `build:simd` flags: `sse2` After: using the same flags as above: `sse2,sse3,ssse3,sse41,sse42,avx,avx2` --------- Signed-off-by: Jesse Yurkovich <[email protected]>
1 parent e1397c8 commit 8360f87

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/cmake/compiler.cmake

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,29 @@ if (NOT USE_SIMD STREQUAL "")
327327
if (USE_SIMD STREQUAL "0")
328328
set (SIMD_COMPILE_FLAGS ${SIMD_COMPILE_FLAGS} "-DOIIO_NO_SIMD=1")
329329
else ()
330+
set(_highest_msvc_arch 0)
330331
string (REPLACE "," ";" SIMD_FEATURE_LIST ${USE_SIMD})
331332
foreach (feature ${SIMD_FEATURE_LIST})
332333
message (VERBOSE "SIMD feature: ${feature}")
333334
if (MSVC OR CMAKE_COMPILER_IS_INTEL)
334-
list (APPEND SIMD_COMPILE_FLAGS "/arch:${feature}")
335+
if (feature STREQUAL "sse2")
336+
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__")
337+
endif ()
338+
if (feature STREQUAL "sse4.1")
339+
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__" "/D__SSE4_1__")
340+
endif ()
341+
if (feature STREQUAL "sse4.2")
342+
list (APPEND SIMD_COMPILE_FLAGS "/D__SSE2__" "/D__SSE4_2__")
343+
endif ()
344+
if (feature STREQUAL "avx" AND _highest_msvc_arch LESS 1)
345+
set(_highest_msvc_arch 1)
346+
endif ()
347+
if (feature STREQUAL "avx2" AND _highest_msvc_arch LESS 2)
348+
set(_highest_msvc_arch 2)
349+
endif ()
350+
if (feature STREQUAL "avx512f" AND _highest_msvc_arch LESS 3)
351+
set(_highest_msvc_arch 3)
352+
endif ()
335353
else ()
336354
list (APPEND SIMD_COMPILE_FLAGS "-m${feature}")
337355
endif ()
@@ -343,6 +361,20 @@ if (NOT USE_SIMD STREQUAL "")
343361
proj_add_compile_options ("-ffp-contract=off")
344362
endif ()
345363
endforeach()
364+
365+
# Only add a single /arch flag representing the highest level of support.
366+
if (MSVC OR CMAKE_COMPILER_IS_INTEL)
367+
if (_highest_msvc_arch EQUAL 1)
368+
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX")
369+
endif ()
370+
if (_highest_msvc_arch EQUAL 2)
371+
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX2")
372+
endif ()
373+
if (_highest_msvc_arch EQUAL 3)
374+
list (APPEND SIMD_COMPILE_FLAGS "/arch:AVX512")
375+
endif ()
376+
endif ()
377+
unset(_highest_msvc_arch)
346378
endif ()
347379
proj_add_compile_options (${SIMD_COMPILE_FLAGS})
348380
endif ()

0 commit comments

Comments
 (0)