From 054464dce2f0763a6d4027b70ff5c5f4abb3d7c8 Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:34:15 -0700 Subject: [PATCH] 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`. --- cmake/CMakeLists.txt | 26 ++++++++++--------- cmake/external/xnnpack.cmake | 7 ----- .../azure-pipelines/templates/web-ci.yml | 2 +- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 32e51db1ca..522b3e550a 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -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() diff --git a/cmake/external/xnnpack.cmake b/cmake/external/xnnpack.cmake index 7f7307b079..4df57b5ed1 100644 --- a/cmake/external/xnnpack.cmake +++ b/cmake/external/xnnpack.cmake @@ -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() diff --git a/tools/ci_build/github/azure-pipelines/templates/web-ci.yml b/tools/ci_build/github/azure-pipelines/templates/web-ci.yml index 6d31ac3e6a..df53f23a26 100644 --- a/tools/ci_build/github/azure-pipelines/templates/web-ci.yml +++ b/tools/ci_build/github/azure-pipelines/templates/web-ci.yml @@ -85,7 +85,7 @@ stages: ExtraBuildArgs: '$(ExtraBuildArgs)' PoolName: ${{ parameters.PoolName }} SkipPublish: true - TimeoutInMinutes: 150 + TimeoutInMinutes: 180 - stage: Build_web_Release dependsOn: Build_wasm_Release