fix XNNPACK on WebAssembly SIMD (#13161)

### Description

fix XNNPACK on WebAssembly SIMD.

Flag "-msimd128" need to be applied to every source file when compiling
WASM SIMD. Currently only a part of the source files are compiled with
this flag so we get inconsistent result for
`sizeof(xnn_f32_minmax_params)` because the type definition include a
`#ifdef` for `__wasm_simd128__`. The inconsistency causes writing
garbage data to a stack variable and eventually cause the crash.

XNNPACK libraries are C libraries so need to apply the build flags not
only to `CMAKE_CXX_FLAGS` but also to `CMAKE_C_FLAGS`.
This commit is contained in:
Yulong Wang 2022-09-30 16:34:15 -07:00 committed by GitHub
parent 0bf0991fa2
commit 054464dce2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 20 deletions

View file

@ -356,6 +356,7 @@ if (onnxruntime_BUILD_WEBASSEMBLY)
# (2) "-flto=thin" does not work correctly for wasm-ld.
# we don't set onnxruntime_ENABLE_LTO because it appends flag "-flto=thin"
# instead, we manually set CMAKE_CXX_FLAGS "-flto"
string(APPEND CMAKE_C_FLAGS " -flto")
string(APPEND CMAKE_CXX_FLAGS " -flto")
endif()
@ -368,9 +369,22 @@ if (onnxruntime_BUILD_WEBASSEMBLY)
set(CMAKE_CXX_FLAGS_DEBUG "-g0")
endif()
if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
string(APPEND CMAKE_C_FLAGS " -msimd128")
string(APPEND CMAKE_CXX_FLAGS " -msimd128")
endif()
if (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING)
string(APPEND CMAKE_C_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
endif()
# Build WebAssembly with multi-threads support.
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
string(APPEND CMAKE_C_FLAGS " -pthread")
string(APPEND CMAKE_CXX_FLAGS " -pthread")
string(APPEND CMAKE_C_FLAGS " -s USE_PTHREADS=1 -Wno-pthreads-mem-growth")
string(APPEND CMAKE_CXX_FLAGS " -s USE_PTHREADS=1 -Wno-pthreads-mem-growth")
endif()
endif()
@ -2130,18 +2144,6 @@ if (onnxruntime_BUILD_CSHARP)
endif()
if (onnxruntime_BUILD_WEBASSEMBLY)
if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
string(APPEND CMAKE_CXX_FLAGS " -msimd128")
endif()
if (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING)
string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
endif()
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
string(APPEND CMAKE_CXX_FLAGS " -s USE_PTHREADS=1 -Wno-pthreads-mem-growth")
endif()
message(STATUS "WebAssembly Build is enabled")
include(onnxruntime_webassembly.cmake)
endif()

View file

@ -59,10 +59,6 @@ if(onnxruntime_BUILD_WEBASSEMBLY)
set(${target_srcs} ${bazel_srcs} PARENT_SCOPE)
endfunction()
if(onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
target_compile_options(XNNPACK PRIVATE "-pthread")
endif()
GetSrcListFromBazel("PROD_SCALAR_WASM_MICROKERNEL_SRCS" prod_scalar_wasm_srcs)
GetSrcListFromBazel("ALL_WASM_MICROKERNEL_SRCS" all_wasm_srcs)
GetSrcListFromBazel("WASM32_ASM_MICROKERNEL_SRCS" wasm32_asm_srcs)
@ -82,9 +78,6 @@ if(onnxruntime_BUILD_WEBASSEMBLY)
if(onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
GetSrcListFromBazel("ALL_WASMSIMD_MICROKERNEL_SRCS" all_wasmsimd_srcs)
message(DEBUG "all_wasmsimd_srcs: ${all_wasmsimd_srcs}")
target_compile_options(params_init PRIVATE "-msimd128")
target_compile_options(XNNPACK PRIVATE "-msimd128")
target_sources(XNNPACK PRIVATE ${all_wasmsimd_srcs})
endif()
endif()

View file

@ -85,7 +85,7 @@ stages:
ExtraBuildArgs: '$(ExtraBuildArgs)'
PoolName: ${{ parameters.PoolName }}
SkipPublish: true
TimeoutInMinutes: 150
TimeoutInMinutes: 180
- stage: Build_web_Release
dependsOn: Build_wasm_Release