Skip to content

Commit 80c6813

Browse files
dpinophiln
authored andcommitted
[LibWebRTC] Enable AVX2 extensions only if host supports AVX2
https://bugs.webkit.org/show_bug.cgi?id=258708 Reviewed by Philippe Normand. Include specialized AVX2 files in LibWebRTC only if the host supports AVX2 extensions. Also, when AVX2 is enabled, avoid building using AVX512 extensions even if the host has support for it since it may result into compatibility issues (binary built on host with AVX512 support but executed in host without AVX512 support will result into a crash). * Source/ThirdParty/libwebrtc/CMakeLists.txt: * Source/cmake/DetectAVX2.cmake: Added. * Source/cmake/WebKitCommon.cmake: Canonical link: https://commits.webkit.org/265646@main
1 parent 1d55849 commit 80c6813

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

Source/ThirdParty/libwebrtc/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ if (WTF_CPU_X86_64 OR WTF_CPU_X86)
18551855
)
18561856
endif()
18571857

1858-
if (WTF_CPU_X86_64)
1858+
if (WTF_CPU_X86_64 AND WTF_CPU_HAS_AVX2)
18591859
list(APPEND webrtc_SOURCES
18601860
Source/webrtc/common_audio/fir_filter_avx2.cc
18611861
Source/webrtc/common_audio/resampler/sinc_resampler_avx2.cc
@@ -1866,7 +1866,8 @@ if (WTF_CPU_X86_64)
18661866
Source/webrtc/modules/audio_processing/aec3/vector_math_avx2.cc
18671867
Source/webrtc/modules/audio_processing/agc2/rnn_vad/vector_math_avx2.cc
18681868
)
1869-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
1869+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -mno-avx512f")
1870+
add_definitions(-DWEBRTC_ENABLE_AVX2)
18701871
endif()
18711872

18721873
if (WTF_CPU_ARM64)

Source/cmake/DetectAVX2.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#################################
2+
# Check for the presence of AVX2.
3+
#
4+
# Once done, this will define:
5+
# - AVX2_SUPPORT_FOUND - the system supports (at least) AVX2.
6+
#
7+
# Copyright (c) 2023, Igalia S.L.
8+
#
9+
# Redistribution and use in source and binary forms, with or without modification,
10+
# are permitted provided that the following conditions are met:
11+
#
12+
# * Redistributions of source code must retain the above copyright notice,
13+
# this list of conditions and the following disclaimer.
14+
#
15+
# * Redistributions in binary form must reproduce the above copyright notice,
16+
# this list of conditions and the following disclaimer in the documentation
17+
# and/or other materials provided with the distribution.
18+
#
19+
# * Neither the name of the copyright holders nor the names of its contributors
20+
# may be used to endorse or promote products derived from this software without
21+
# specific prior written permission.
22+
#
23+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
24+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26+
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29+
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
31+
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
set(AVX2_SUPPORT_FOUND FALSE)
34+
35+
macro(CHECK_FOR_AVX2)
36+
include(CheckCXXSourceRuns)
37+
38+
set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS})
39+
40+
if (COMPILER_IS_GCC_OR_CLANG)
41+
set(CMAKE_REQUIRED_FLAGS -mavx2)
42+
endif ()
43+
44+
if (MSVC AND CMAKE_CL_64)
45+
set(CMAKE_REQUIRED_FLAGS /arch:AVX2)
46+
endif ()
47+
48+
check_cxx_source_runs("
49+
#include <immintrin.h>
50+
int main()
51+
{
52+
volatile __m256i a, b;
53+
a = _mm256_set1_epi8 (1);
54+
b = _mm256_add_epi8 (a,a);
55+
return 0;
56+
}"
57+
HAVE_AVX2_EXTENSIONS)
58+
59+
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE})
60+
61+
if (COMPILER_IS_GCC_OR_CLANG OR (MSVC AND CMAKE_CL_64))
62+
if (HAVE_AVX2_EXTENSIONS)
63+
set(AVX2_SUPPORT_FOUND TRUE)
64+
endif ()
65+
endif ()
66+
67+
endmacro(CHECK_FOR_AVX2)
68+
69+
CHECK_FOR_AVX2()

Source/cmake/WebKitCommon.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ if (NOT HAS_RUN_WEBKIT_COMMON)
125125
set(WTF_CPU_UNKNOWN 1)
126126
endif ()
127127

128+
if (WTF_CPU_X86_64 AND NOT CMAKE_CROSSCOMPILING)
129+
include(DetectAVX2)
130+
if (AVX2_SUPPORT_FOUND)
131+
set(WTF_CPU_HAS_AVX2 1)
132+
endif ()
133+
endif ()
134+
128135
# -----------------------------------------------------------------------------
129136
# Determine the operating system
130137
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)