@@ -2,6 +2,7 @@ set(LLVM_BLAKE3_FILES
22  blake3.c
33  blake3_dispatch.c
44  blake3_portable.c
5+   blake3_neon.c
56)
67
78if  (LLVM_DISABLE_ASSEMBLY_FILES)
@@ -10,6 +11,10 @@ else()
1011  set (CAN_USE_ASSEMBLER TRUE )
1112endif ()
1213
14+ macro (disable_blake3_x86_simd)
15+   add_definitions (-DBLAKE3_NO_AVX512 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_SSE2)
16+ endmacro ()
17+ 
1318# The BLAKE3 team recommends using the assembly versions, from the README: 
1419# 
1520# "For each of the x86 SIMD instruction sets, four versions are available: 
@@ -18,70 +23,62 @@ endif()
1823# preferred. They perform better, they perform more consistently across 
1924# different compilers, and they build more quickly." 
2025
21- if  (MSVC )
22-   check_symbol_exists(_M_X64 ""  IS_X64)
23-   check_symbol_exists(_M_ARM64 ""  IS_ARM64)
24- else ()
25-   check_symbol_exists(__x86_64__ ""  IS_X64)
26-   check_symbol_exists(__aarch64__ ""  IS_ARM64)
27- 
28-   if  (IS_X64)
29-     # Clang-6 needs this flag. 
30-     set_source_files_properties (blake3_avx512_x86-64_windows_gnu.S
31-       PROPERTIES COMPILE_OPTIONS "-mavx512vl" )
32-     set_source_files_properties (blake3_avx512_x86-64_unix.S
33-       PROPERTIES COMPILE_OPTIONS "-mavx512vl" )
34-   endif ()
35- endif ()
36- 
37- if  (IS_X64 AND  CAN_USE_ASSEMBLER)
26+ if  (CAN_USE_ASSEMBLER)
3827  if  (MSVC )
39-     enable_language (ASM_MASM)
40-     list (APPEND  LLVM_BLAKE3_FILES
41-       blake3_sse2_x86-64_windows_msvc.asm
42-       blake3_sse41_x86-64_windows_msvc.asm
43-       blake3_avx2_x86-64_windows_msvc.asm
44-       blake3_avx512_x86-64_windows_msvc.asm
45-     )
28+     check_symbol_exists(_M_X64 ""  IS_X64)
29+     if  (IS_X64)
30+       enable_language (ASM_MASM)
31+       list (APPEND  LLVM_BLAKE3_FILES
32+         blake3_sse2_x86-64_windows_msvc.asm
33+         blake3_sse41_x86-64_windows_msvc.asm
34+         blake3_avx2_x86-64_windows_msvc.asm
35+         blake3_avx512_x86-64_windows_msvc.asm
36+       )
37+     else ()
38+       disable_blake3_x86_simd()
39+     endif ()
4640  elseif (WIN32  OR  CYGWIN )
47-     list (APPEND  LLVM_BLAKE3_FILES
48-       blake3_sse2_x86-64_windows_gnu.S
49-       blake3_sse41_x86-64_windows_gnu.S
50-       blake3_avx2_x86-64_windows_gnu.S
51-       blake3_avx512_x86-64_windows_gnu.S
52-     )
41+     check_symbol_exists(__x86_64__ ""  IS_X64)
42+     if  (IS_X64)
43+       list (APPEND  LLVM_BLAKE3_FILES
44+         blake3_sse2_x86-64_windows_gnu.S
45+         blake3_sse41_x86-64_windows_gnu.S
46+         blake3_avx2_x86-64_windows_gnu.S
47+         blake3_avx512_x86-64_windows_gnu.S
48+       )
49+       # Clang-6 needs this flag. 
50+       set_source_files_properties (blake3_avx512_x86-64_windows_gnu.S
51+         PROPERTIES COMPILE_OPTIONS "-mavx512vl" )
52+     else ()
53+       disable_blake3_x86_simd()
54+     endif ()
5355  else ()
54-     list (APPEND  LLVM_BLAKE3_FILES
55-       blake3_sse2_x86-64_unix.S
56-       blake3_sse41_x86-64_unix.S
57-       blake3_avx2_x86-64_unix.S
58-       blake3_avx512_x86-64_unix.S
59-     )
56+     check_symbol_exists(__i386__ ""  IS_X32)
57+     if  (IS_X32)
58+       # blake3 C code autoenables SIMD for i386, but those implementations are 
59+       # only available via the intrinsics sources which we don't enable here. 
60+       disable_blake3_x86_simd()
61+     else ()
62+       # In a macOS Universal build (setting CMAKE_OSX_ARCHITECTURES to multiple 
63+       # values), compilation of the source files will target multiple architectures 
64+       # (each source file is internally compiled once for each architecture). 
65+       # To accomodate this configuration we include these assembly files without a 
66+       # CMake check but their source is guarded with architecture "#ifdef" checks. 
67+       list (APPEND  LLVM_BLAKE3_FILES
68+         blake3_sse2_x86-64_unix.S
69+         blake3_sse41_x86-64_unix.S
70+         blake3_avx2_x86-64_unix.S
71+         blake3_avx512_x86-64_unix.S
72+       )
73+       # Clang-6 needs this flag. We also suppress '-Wunused-command-line-argument' 
74+       # in case the file is included with arm architecture. 
75+       set_source_files_properties (blake3_avx512_x86-64_unix.S
76+         PROPERTIES COMPILE_OPTIONS "-mavx512vl;-Wno-unused-command-line-argument" )
77+     endif ()
6078  endif ()
6179else ()
62-   # In a macOS Universal build (setting CMAKE_OSX_ARCHITECTURES to multiple 
63-   # values), IS_X64 and IS_ARM64 won't be set, but compilation of the source 
64-   # files will consider targeting either of them (each source file is 
65-   # internally compiled once for each architecture). Thus, if we on the CMake 
66-   # level decide not to include the assembly files, tell the source to not 
67-   # expect it to be present either. 
68-   # 
69-   # Also, if targeting i386, then the blake3 source code automatically enables 
70-   # the SIMD implementations, but we don't provide those sources. 
71-   # 
72-   # FIXME: We could improve the CMAKE_OSX_ARCHITECTURES configuration by 
73-   # including all SIMD implementation files that might be relevant, and 
74-   # wrapping them in ifdefs like "#ifdef __x86_64__", to allow them to be 
75-   # included in a build for any architecture. 
76-   add_definitions (-DBLAKE3_NO_AVX512 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_SSE2)
77- endif ()
78- 
79- if  (IS_ARM64)
80-   list (APPEND  LLVM_BLAKE3_FILES
81-     blake3_neon.c
82-   )
83- else ()
84-   add_definitions (-DBLAKE3_USE_NEON=0)
80+   # CAN_USE_ASSEMBLER == FALSE 
81+   disable_blake3_x86_simd()
8582endif ()
8683
8784add_library (LLVMSupportBlake3 OBJECT EXCLUDE_FROM_ALL  ${LLVM_BLAKE3_FILES} )
0 commit comments