From 01fe5aaeea74cc25d4052fec0be6d7d03e0faec0 Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Fri, 16 Apr 2021 15:34:57 +1000 Subject: [PATCH] wasm: add simd support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Emscripten only supports SSE1, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, and 128-bit AVX instruction sets at this time. https://emscripten.org/docs/porting/simd.html Browsers might need to enable simd support in the advanced configurations about: config or chrome:flags Enable by configuring Qt with -sse2 Fixes: QTBUG-63924 Change-Id: Ifeafae20e199dee0d19689802ad20fd0bd424ca7 Reviewed-by: Morten Johan Sørvig (cherry picked from commit 0e100a4d892cbdcdb7f326213ec1e4d0d1ae6d89) Reviewed-by: Qt Cherry-pick Bot --- cmake/QtCompilerOptimization.cmake | 3 +++ cmake/QtInternalTargets.cmake | 4 ++++ cmake/QtWasmHelpers.cmake | 5 +++++ config.tests/arch/CMakeLists.txt | 4 ++++ configure.cmake | 6 ++++-- mkspecs/features/wasm/wasm.prf | 8 ++++++++ src/corelib/global/qprocessordetection.h | 4 ++++ src/corelib/global/qsimd_p.h | 5 ++++- 8 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmake/QtCompilerOptimization.cmake b/cmake/QtCompilerOptimization.cmake index 952ace66528..69029a59de6 100644 --- a/cmake/QtCompilerOptimization.cmake +++ b/cmake/QtCompilerOptimization.cmake @@ -199,4 +199,7 @@ if(WASM) set(QT_CFLAGS_OPTIMIZE_FULL "-O3") set(QT_CFLAGS_OPTIMIZE_SIZE "-Os") set(QT_CFLAGS_OPTIMIZE_DEBUG "-g2") + + set(QT_CFLAGS_SSE2 -O2 -msimd128 -msse -msse2) + endif() diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index 22080b4d536..02be218ebee 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -186,6 +186,10 @@ if(UIKIT) qt_internal_apply_bitcode_flags(PlatformCommonInternal) endif() +if(WASM AND QT_FEATURE_sse2) + target_compile_definitions(PlatformCommonInternal INTERFACE QT_COMPILER_SUPPORTS_SSE2) +endif() + # Taken from mkspecs/common/msvc-version.conf and mkspecs/common/msvc-desktop.conf if (MSVC) if (MSVC_VERSION GREATER_EQUAL 1799) diff --git a/cmake/QtWasmHelpers.cmake b/cmake/QtWasmHelpers.cmake index bc5e455fcd1..cc26341ca83 100644 --- a/cmake/QtWasmHelpers.cmake +++ b/cmake/QtWasmHelpers.cmake @@ -9,6 +9,11 @@ function (qt_internal_setup_wasm_target_properties wasmTarget) "SHELL:-s FETCH=1") target_compile_options("${wasmTarget}" INTERFACE --bind) + #simd + if (QT_FEATURE_sse2) + target_compile_options("${wasmTarget}" INTERFACE -O2 -msimd128 -msse -msse2) + endif() + # Hardcode wasm memory size. Emscripten does not currently support memory growth # (ALLOW_MEMORY_GROWTH) in pthreads mode, and requires specifying the memory size # at build time. Further, browsers limit the maximum initial memory size to 1GB. diff --git a/config.tests/arch/CMakeLists.txt b/config.tests/arch/CMakeLists.txt index 2e11f20674c..9ef6e37b3bb 100644 --- a/config.tests/arch/CMakeLists.txt +++ b/config.tests/arch/CMakeLists.txt @@ -4,3 +4,7 @@ project(arch LANGUAGES CXX) add_executable(architecture_test) set_property(TARGET architecture_test PROPERTY MACOSX_BUNDLE FALSE) target_sources(architecture_test PRIVATE arch.cpp) + +if(EMSCRIPTEN) + target_compile_options(architecture_test PRIVATE -O2 -msimd128 -msse -msse2) +endif() diff --git a/configure.cmake b/configure.cmake index 5705e376118..4d21afe0cb0 100644 --- a/configure.cmake +++ b/configure.cmake @@ -646,7 +646,9 @@ qt_feature("signaling_nan" PUBLIC ) qt_feature("sse2" PRIVATE LABEL "SSE2" - CONDITION ( ( ( TEST_architecture_arch STREQUAL i386 ) OR ( TEST_architecture_arch STREQUAL x86_64 ) ) AND TEST_subarch_sse2 ) OR QT_FORCE_FEATURE_sse2 # special case + CONDITION ( ( ( TEST_architecture_arch STREQUAL i386 ) + OR ( TEST_architecture_arch STREQUAL x86_64 ) ) AND TEST_subarch_sse2 ) OR QT_FORCE_FEATURE_sse2 OR WASM + AUTODETECT NOT WASM ) qt_feature_definition("sse2" "QT_COMPILER_SUPPORTS_SSE2" VALUE "1") qt_feature_config("sse2" QMAKE_PRIVATE_CONFIG) @@ -1004,7 +1006,7 @@ qt_configure_add_summary_entry( TYPE "featureList" ARGS "sse2 sse3 ssse3 sse4_1 sse4_2" MESSAGE "SSE" - CONDITION ( ( TEST_architecture_arch STREQUAL i386 ) OR ( TEST_architecture_arch STREQUAL x86_64 ) ) + CONDITION ( ( TEST_architecture_arch STREQUAL i386 ) OR ( TEST_architecture_arch STREQUAL x86_64 ) OR ( TEST_architecture_arch STREQUAL wasm ) ) ) qt_configure_add_summary_entry( TYPE "featureList" diff --git a/mkspecs/features/wasm/wasm.prf b/mkspecs/features/wasm/wasm.prf index 5ffc647135d..e1b10cb23a9 100644 --- a/mkspecs/features/wasm/wasm.prf +++ b/mkspecs/features/wasm/wasm.prf @@ -36,6 +36,14 @@ exists($$QMAKE_QT_CONFIG) { message("Setting INITIAL_MEMORY to" $$INITIAL_MEMORY) EMCC_THREAD_LFLAGS += -s INITIAL_MEMORY=$$INITIAL_MEMORY } + + qtConfig(sse2) { + QMAKE_CFLAGS += -O2 -msimd128 -msse -msse2 + QMAKE_CXXFLAGS += -O2 -msimd128 -msse -msse2 + QMAKE_LFLAGS += -msimd128 -msse -msse2 + QMAKE_LFLAGS_DEBUG += -msimd128 -msse -msse2 + } + QMAKE_LFLAGS += $$EMCC_THREAD_LFLAGS QMAKE_LFLAGS_DEBUG += $$EMCC_THREAD_LFLAGS QMAKE_CFLAGS += $$EMCC_THREAD_LFLAGS diff --git a/src/corelib/global/qprocessordetection.h b/src/corelib/global/qprocessordetection.h index f7bca433458..72cc0b4ad41 100644 --- a/src/corelib/global/qprocessordetection.h +++ b/src/corelib/global/qprocessordetection.h @@ -340,6 +340,10 @@ # define Q_PROCESSOR_WASM # define Q_BYTE_ORDER Q_LITTLE_ENDIAN # define Q_PROCESSOR_WORDSIZE 8 +#ifdef QT_COMPILER_SUPPORTS_SSE2 +# define Q_PROCESSOR_X86 6 // enables SIMD support +#endif + #endif /* diff --git a/src/corelib/global/qsimd_p.h b/src/corelib/global/qsimd_p.h index 35979176fa4..118a62e6acb 100644 --- a/src/corelib/global/qsimd_p.h +++ b/src/corelib/global/qsimd_p.h @@ -190,10 +190,13 @@ # define __SSE__ 1 # endif -# if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) +# if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && !defined(Q_OS_WASM) // GCC 4.4 and Clang 2.8 added a few more intrinsics there # include # endif +#ifdef Q_OS_WASM +# include +# endif # if defined(__SSE4_2__) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS) && (defined(Q_CC_INTEL) || defined(Q_CC_MSVC)) // POPCNT instructions: