Skip to content

Commit cf3c8c5

Browse files
committed
CMakeLists.txt: add _avx512 file pattern detection, omitting on 32 bit and on older MSVCs
1 parent 7bbd0a6 commit cf3c8c5

1 file changed

Lines changed: 52 additions & 41 deletions

File tree

avs_core/CMakeLists.txt

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ endif()
1313
# Create library
1414
project("AvsCore" VERSION "${PROJECT_VERSION}" LANGUAGES CXX)
1515
Include("Files.cmake")
16+
17+
# Remove AVX512 specific files if it's a 32-bit build
18+
if(CMAKE_SIZEOF_VOID_P EQUAL 4) # Check for 32-bit
19+
list(FILTER AvsCore_Sources EXCLUDE REGEX ".*_avx512\\.(cpp|h)$")
20+
endif()
21+
22+
# Remove AVX512 specific files for older MSVC versions
23+
# Check MSVC version for lack of full AVX-512BW mask intrinsics (older than MSVC 19.22)
24+
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MSVC_IDE AND NOT CLANG_IN_VS AND NOT IntelLLVM_IN_VS)
25+
string(REGEX MATCH "^19\\.([0-9]+)\\.([0-9]+)" MSVC_VERSION_MATCH "${CMAKE_CXX_COMPILER_VERSION}")
26+
if(MSVC_VERSION_MATCH)
27+
set(MSVC_MAJOR 19)
28+
set(MSVC_MINOR ${CMAKE_MATCH_1})
29+
set(MSVC_PATCH ${CMAKE_MATCH_2})
30+
31+
if(MSVC_MAJOR LESS 19 OR (MSVC_MAJOR EQUAL 19 AND MSVC_MINOR LESS 22)) # Before MSVC 2019 16.2 (19.22)
32+
list(FILTER AvsCore_Sources EXCLUDE REGEX ".*_avx512\\.(cpp|h)$")
33+
message(WARNING "Detected older native 64-bit MSVC (version ${CMAKE_CXX_COMPILER_VERSION}). AVX-512 files are excluded due to missing AVX512BW mask intrinsics before Visual Studio 2019 16.2.")
34+
endif()
35+
else()
36+
# If version parsing fails, assume potential issues and warn
37+
message(WARNING "Could not parse native 64-bit MSVC version. AVX-512 support might be incomplete.")
38+
endif()
39+
endif()
40+
1641
add_library("AvsCore" ${AvsCore_Sources})
1742
set_target_properties("AvsCore" PROPERTIES "OUTPUT_NAME" "${CoreName}")
1843
if (NOT WIN32)
@@ -42,49 +67,35 @@ foreach(FILE ${AvsCore_Sources})
4267
source_group("${GROUP}" FILES "${FILE}")
4368
endforeach()
4469

45-
if (MSVC_IDE)
46-
IF(CLANG_IN_VS STREQUAL "1" OR
47-
IntelLLVM_IN_VS STREQUAL "1")
48-
# special SSSE3 option for source files with *_ssse3.cpp pattern
49-
file(GLOB_RECURSE SRCS_SSSE3 "*_ssse3.cpp")
50-
set_source_files_properties(${SRCS_SSSE3} PROPERTIES COMPILE_FLAGS " -mssse3 ")
51-
52-
# special SSE4.1 option for source files with *_sse41.cpp pattern
53-
file(GLOB_RECURSE SRCS_SSE41 "*_sse41.cpp")
54-
set_source_files_properties(${SRCS_SSE41} PROPERTIES COMPILE_FLAGS " -msse4.1 ")
55-
56-
# special AVX option for source files with *_avx.cpp pattern
57-
file(GLOB_RECURSE SRCS_AVX "*_avx.cpp")
58-
set_source_files_properties(${SRCS_AVX} PROPERTIES COMPILE_FLAGS " -mavx ")
59-
60-
# special AVX2 option for source files with *_avx2.cpp pattern
61-
file(GLOB_RECURSE SRCS_AVX2 "*_avx2.cpp")
62-
set_source_files_properties(${SRCS_AVX2} PROPERTIES COMPILE_FLAGS " -mavx2 -mfma ")
63-
ELSE()
64-
# special AVX option for source files with *_avx.cpp pattern
65-
file(GLOB_RECURSE SRCS_AVX "*_avx.cpp")
66-
set_source_files_properties(${SRCS_AVX} PROPERTIES COMPILE_FLAGS " /arch:AVX ")
67-
68-
# special AVX2 option for source files with *_avx2.cpp pattern
69-
file(GLOB_RECURSE SRCS_AVX2 "*_avx2.cpp")
70-
set_source_files_properties(${SRCS_AVX2} PROPERTIES COMPILE_FLAGS " /arch:AVX2 ")
71-
ENDIF()
72-
else()
73-
# special SSSE3 option for source files with *_ssse3.cpp pattern
74-
file(GLOB_RECURSE SRCS_SSSE3 "*_ssse3.cpp")
75-
set_source_files_properties(${SRCS_SSSE3} PROPERTIES COMPILE_FLAGS " -mssse3 ")
76-
77-
# special SSE4.1 option for source files with *_sse41.cpp pattern
78-
file(GLOB_RECURSE SRCS_SSE41 "*_sse41.cpp")
79-
set_source_files_properties(${SRCS_SSE41} PROPERTIES COMPILE_FLAGS " -msse4.1 ")
70+
function(handle_arch_flags ARCH_SUFFIX GCC_FLAGS MSVC_FLAGS)
71+
string(TOLOWER "${ARCH_SUFFIX}" ARCH_SUFFIX_LOWER)
72+
file(GLOB_RECURSE SRCS_${ARCH_SUFFIX} "*_${ARCH_SUFFIX_LOWER}.cpp")
73+
if(SRCS_${ARCH_SUFFIX})
74+
if (MSVC_IDE)
75+
IF(CLANG_IN_VS STREQUAL "1" OR IntelLLVM_IN_VS STREQUAL "1")
76+
set_source_files_properties(${SRCS_${ARCH_SUFFIX}} PROPERTIES COMPILE_FLAGS "${GCC_FLAGS}")
77+
ELSE()
78+
# MSVC gives warning in x64 when SSE2 flag is added, it's just required as a minimum
79+
if(NOT (CMAKE_SIZEOF_VOID_P EQUAL 8 AND "${MSVC_FLAGS}" STREQUAL " /arch:SSE2 "))
80+
set_source_files_properties(${SRCS_${ARCH_SUFFIX}} PROPERTIES COMPILE_FLAGS "${MSVC_FLAGS}")
81+
endif()
82+
ENDIF()
83+
else()
84+
set_source_files_properties(${SRCS_${ARCH_SUFFIX}} PROPERTIES COMPILE_FLAGS "${GCC_FLAGS}")
85+
endif()
86+
list(APPEND AvsCore_Sources ${SRCS_${ARCH_SUFFIX}})
87+
endif()
88+
endfunction()
8089

81-
# special AVX option for source files with *_avx.cpp pattern
82-
file(GLOB_RECURSE SRCS_AVX "*_avx.cpp")
83-
set_source_files_properties(${SRCS_AVX} PROPERTIES COMPILE_FLAGS -mavx )
90+
# gcc/llvm/clang-like flags, MSVC flags
91+
handle_arch_flags(SSSE3 " -mssse3 " " /arch:SSE2 ") # no special SSSE3 option in MSVC
92+
handle_arch_flags(SSE41 " -msse4.1 " " /arch:SSE2 ") # no special SSE4.1 option in MSVC
93+
handle_arch_flags(AVX " -mavx " " /arch:AVX ")
94+
handle_arch_flags(AVX2 " -mavx2 -mfma " " /arch:AVX2 ")
8495

85-
# special AVX2 option for source files with *_avx2.cpp pattern
86-
file(GLOB_RECURSE SRCS_AVX2 "*_avx2.cpp")
87-
set_source_files_properties(${SRCS_AVX2} PROPERTIES COMPILE_FLAGS "-mavx2 -mfma")
96+
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # AVX512 exists only on 64-bit
97+
handle_arch_flags(AVX512 " -mavx512f -mavx512bw " " /arch:AVX512 ")
98+
# though such source patterns were already removed from source list
8899
endif()
89100

90101
# Specify include directories

0 commit comments

Comments
 (0)