From a05c113f800b5b7417b1c878454b933ce016d620 Mon Sep 17 00:00:00 2001 From: quink Date: Tue, 2 Jul 2024 12:44:26 +0800 Subject: [PATCH] Add wasi support (#5534) * benchmark: Don't use std::thread when NCNN_THREADS is OFF * Add wasi support cmake -B build -DCMAKE_BUILD_TYPE=Release \ --toolchain ${wasi_sdk}/share/cmake/wasi-sdk.cmake \ -DNCNN_RUNTIME_CPU=OFF \ -DNCNN_DISABLE_EXCEPTION=ON \ -DNCNN_THREADS=OFF cmake --build build After build, you can run benchncnn on cmdline with wasmtime: wasmtime --dir . benchncnn --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 6 ++++-- src/benchmark.cpp | 16 ++++++++++++---- src/cpu.cpp | 2 ++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0b6cfc8b58..309e3b8fbd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -559,7 +559,7 @@ else() unset(CMAKE_REQUIRED_FLAGS) endif() - if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_COMPILER_SUPPORT_X86_AVX) + if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI" AND NCNN_COMPILER_SUPPORT_X86_AVX) option(NCNN_AVX "optimize x86 platform with avx extension" ON) if(NCNN_COMPILER_SUPPORT_X86_FMA) if(NCNN_AVX) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 35e46535a3b..d3f55ce7790 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -395,8 +395,10 @@ if(NCNN_TARGET_ARCH STREQUAL "x86") endif() target_compile_options(ncnn PRIVATE /D__SSE2__) else() - target_compile_options(ncnn PRIVATE -msse2 -msse) - if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") + if(NOT CMAKE_SYSTEM_NAME MATCHES "WASI") + target_compile_options(ncnn PRIVATE -msse2 -msse) + endif () + if(CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI") target_compile_options(ncnn PRIVATE -msimd128) endif() endif() diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 40fa4615800..49538faade4 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -15,11 +15,20 @@ #include "benchmark.h" #if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL +#define USE_CXX11_CLOCK 1 +#else +#define USE_CXX11_CLOCK 0 +#endif + +#if USE_CXX11_CLOCK #include +#if NCNN_THREADS #include +#endif #include #include -#else +#endif // USE_CXX11_CLOCK + #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -27,7 +36,6 @@ #include //gettimeofday() #include // sleep() #endif // _WIN32 -#endif #if NCNN_BENCHMARK #include "layer/convolution.h" @@ -46,7 +54,7 @@ namespace ncnn { double get_current_time() { -#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL +#if USE_CXX11_CLOCK auto now = std::chrono::high_resolution_clock::now(); auto usec = std::chrono::duration_cast(now.time_since_epoch()); return usec.count() / 1000.0; @@ -69,7 +77,7 @@ double get_current_time() void sleep(unsigned long long int milliseconds) { -#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL +#if USE_CXX11_CLOCK && NCNN_THREADS std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); #else #ifdef _WIN32 diff --git a/src/cpu.cpp b/src/cpu.cpp index 370aeeb610c..b1afbba3f65 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -17,8 +17,10 @@ #include "platform.h" #include +#ifndef __wasi__ #include #include +#endif // __wasi__ #include #include