Skip to content

Commit f59b1b5

Browse files
authored
Merge pull request #5088 from akyrtzi/pr/stable-picks-blake3
[Support/BLAKE3] Cherry-picks from upstream llvm.org related to BLAKE3
2 parents 828b75d + 573ff4e commit f59b1b5

File tree

6 files changed

+72
-59
lines changed

6 files changed

+72
-59
lines changed

llvm/lib/Support/BLAKE3/CMakeLists.txt

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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

78
if (LLVM_DISABLE_ASSEMBLY_FILES)
@@ -10,6 +11,10 @@ else()
1011
set(CAN_USE_ASSEMBLER TRUE)
1112
endif()
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,59 @@ 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(__x86_64__ "" IS_X64)
57+
if (IS_X64 OR CMAKE_OSX_ARCHITECTURES)
58+
# In a macOS Universal build (setting CMAKE_OSX_ARCHITECTURES to multiple
59+
# values), compilation of the source files will target multiple architectures
60+
# (each source file is internally compiled once for each architecture).
61+
# To accomodate this configuration we include these assembly files without a
62+
# CMake check but their source is guarded with architecture "#ifdef" checks.
63+
list(APPEND LLVM_BLAKE3_FILES
64+
blake3_sse2_x86-64_unix.S
65+
blake3_sse41_x86-64_unix.S
66+
blake3_avx2_x86-64_unix.S
67+
blake3_avx512_x86-64_unix.S
68+
)
69+
# Clang-6 needs this flag.
70+
set_source_files_properties(blake3_avx512_x86-64_unix.S
71+
PROPERTIES COMPILE_OPTIONS "-mavx512vl")
72+
else()
73+
disable_blake3_x86_simd()
74+
endif()
6075
endif()
6176
else()
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)
77+
# CAN_USE_ASSEMBLER == FALSE
78+
disable_blake3_x86_simd()
8579
endif()
8680

8781
add_library(LLVMSupportBlake3 OBJECT EXCLUDE_FROM_ALL ${LLVM_BLAKE3_FILES})

llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(__x86_64__)
2+
13
#if defined(__ELF__) && defined(__linux__)
24
.section .note.GNU-stack,"",%progbits
35
#endif
@@ -1821,3 +1823,4 @@ CMP_MSB_MASK:
18211823
BLAKE3_IV:
18221824
.long 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A
18231825

1826+
#endif

llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(__x86_64__)
2+
13
#if defined(__ELF__) && defined(__linux__)
24
.section .note.GNU-stack,"",%progbits
35
#endif
@@ -2595,3 +2597,5 @@ BLAKE3_IV_2:
25952597
.long 0x3C6EF372
25962598
BLAKE3_IV_3:
25972599
.long 0xA54FF53A
2600+
2601+
#endif

llvm/lib/Support/BLAKE3/blake3_neon.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "blake3_impl.h"
22

3+
#if BLAKE3_USE_NEON
4+
35
#include <arm_neon.h>
46

57
#ifdef __ARM_BIG_ENDIAN
@@ -350,3 +352,5 @@ void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,
350352
out = &out[BLAKE3_OUT_LEN];
351353
}
352354
}
355+
356+
#endif // BLAKE3_USE_NEON

llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(__x86_64__)
2+
13
#if defined(__ELF__) && defined(__linux__)
24
.section .note.GNU-stack,"",%progbits
35
#endif
@@ -2301,3 +2303,5 @@ PBLENDW_0x3F_MASK:
23012303
.long 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000
23022304
PBLENDW_0xC0_MASK:
23032305
.long 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFF
2306+
2307+
#endif

llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(__x86_64__)
2+
13
#if defined(__ELF__) && defined(__linux__)
24
.section .note.GNU-stack,"",%progbits
35
#endif
@@ -2038,3 +2040,5 @@ BLAKE3_BLOCK_LEN:
20382040
.long 64, 64, 64, 64
20392041
CMP_MSB_MASK:
20402042
.long 0x80000000, 0x80000000, 0x80000000, 0x80000000
2043+
2044+
#endif

0 commit comments

Comments
 (0)