Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Couple of fixes to allow SIMD on Emscripten
Browse files Browse the repository at this point in the history
 - Switch from `-msse` to `-msse4.2`. In theory, those should be identical, but Emscripten currently has a bug where only latter form is supported: emscripten-core/emscripten#12746. Since we're using only SSE 4.2, an explicit form seems to be preferable anyway.
 - Add `-msimd128` to flags to actually enable WebAssembly SIMD when performing SIMD detection. It's currently required in addition to `-msse*` / `-mfpu=neon` flags which only perform translation of corresponding intrinsics to Wasm SIMD ones. See a discussion at emscripten-core/emscripten#12714 for automating this and making easier in the future.
 - Remove compilation branch that prevented definitions of `WP2_USE_SSE` and `WP2_USE_NEON` on Emscripten even when SIMD support was detected at compile-time.
 - Add an implementation of `WP2GetCPUInfo` for Emscripten which uses static `WP2_USE_*` flags to determine if a corresponding SIMD instruction is supported. This is because Wasm doesn't have proper feature detection (yet) and requires making separate build for SIMD version anyway.
  • Loading branch information
RReverser committed Nov 10, 2020
1 parent c90b5b4 commit 2b263fe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ if(POLICY CMP0048)
endif(POLICY CMP0048)

# Options for coder / decoder executables.
option(WP2_ENABLE_SIMD "Enable any SIMD optimization." ON)
if(NOT EMSCRIPTEN)
# Disable SIMD on Emscripten by default, as it's a new unstable Wasm feature.
# Users can still explicitly opt-in to make a SIMD-enabled build.
set(WP2_ENABLE_SIMD_DEFAULT ON)
endif()
option(WP2_ENABLE_SIMD "Enable any SIMD optimization." ${WP2_ENABLE_SIMD_DEFAULT})
set(WP2_ENABLE_CONTEXT_SWITCH
AUTO
CACHE STRING "Use context switch optimization for incremental decoding.")
Expand Down Expand Up @@ -96,7 +101,6 @@ endif()

# disable some configurations for WP2_JS
if(WP2_BUILD_WP2_JS)
set(WP2_ENABLE_SIMD OFF)
set(WP2_ENABLE_TESTS OFF)
endif()

Expand Down
5 changes: 4 additions & 1 deletion cmake/cpu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if(MSVC)
set(SIMD_ENABLE_FLAGS "/arch:AVX;")
set(SIMD_DISABLE_FLAGS)
else()
set(SIMD_ENABLE_FLAGS "-msse4;-mfpu=neon")
set(SIMD_ENABLE_FLAGS "-msse4.2;-mfpu=neon")
set(SIMD_DISABLE_FLAGS "-mno-sse4;")
endif()

Expand All @@ -83,6 +83,9 @@ foreach(I_SIMD RANGE ${WP2_SIMD_DEFINES_RANGE})
wp2_check_compiler_flag(${WP2_SIMD_DEFINE} ${WP2_ENABLE_SIMD})
if(NOT WP2_HAVE_${WP2_SIMD_DEFINE})
list(GET SIMD_ENABLE_FLAGS ${I_SIMD} SIMD_COMPILE_FLAG)
if(EMSCRIPTEN)
set(SIMD_COMPILE_FLAG "-msimd128 ${SIMD_COMPILE_FLAG}")
endif()
set(CMAKE_REQUIRED_FLAGS ${SIMD_COMPILE_FLAG})
wp2_check_compiler_flag(${WP2_SIMD_DEFINE} ${WP2_ENABLE_SIMD})
else()
Expand Down
30 changes: 30 additions & 0 deletions src/dsp/cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,36 @@ static bool AndroidCPUInfo(WP2CPUFeature feature) {
return false;
}
WP2CPUInfo WP2GetCPUInfo = AndroidCPUInfo;
#elif defined(EMSCRIPTEN) // also needs to be before generic NEON test
// Use compile flags as an indicator of SIMD support instead of a runtime check.
static bool wasmCPUInfo(WP2CPUFeature feature) {
switch (feature) {
case kSSE2:
case kSSE3:
case kSlowSSSE3:
case kSSE4_1:
case kSSE4_2:
case kSSE:
#ifdef WP2_USE_SSE
return true;
#endif
return false;
case kAVX:
case kAVX2:
#ifdef WP2_USE_AVX2:
return true;
#endif
return false;
case kNEON:
#ifdef WP2_USE_NEON
return true;
#endif
return false;
default:
return false;
}
}
WP2CPUInfo WP2GetCPUInfo = wasmCPUInfo;
#elif defined(WP2_USE_NEON)
// define a dummy function to enable turning off NEON at runtime by setting
// WP2DecGetCPUInfo = NULL
Expand Down
5 changes: 0 additions & 5 deletions src/dsp/dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ static_assert(_MSC_VER >= 1900, "Visual Studio needs to be at version >= 2015");
# define __has_builtin(x) 0
#endif

// for now, none of the optimizations below are available in emscripten
#if !defined(EMSCRIPTEN)

#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
#define WP2_MSC_SSE // Visual C++ SSE4.2 targets
#endif
Expand Down Expand Up @@ -113,8 +110,6 @@ static_assert(_MSC_VER >= 1900, "Visual Studio needs to be at version >= 2015");
#define WP2_USE_MSA
#endif

#endif /* EMSCRIPTEN */

// This macro prevents thread_sanitizer from reporting known concurrent writes.
#define WP2_TSAN_IGNORE_FUNCTION
#if defined(__has_feature)
Expand Down

0 comments on commit 2b263fe

Please sign in to comment.