Elffuss BraidHub

kikocis/ncnn public

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
Changeset3a0df369be07134921b24b07e8a5c2e59afb3ec32fa8e370b4c80e218571a816
Authorquink <zhilizhao@tencent.com> · 2024-07-02 12:44 +08:00
CommitterGitHub <noreply@github.com> · 2024-07-02 12:44 +08:00
Parents749938ca298c

Browse the files at this changeset

4 files changed

modified CMakeLists.txt +1 −1
@@ -559,7 +559,7 @@
559559 unset(CMAKE_REQUIRED_FLAGS)
560560 endif()
561561
562- if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NCNN_COMPILER_SUPPORT_X86_AVX)
562+ if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI" AND NCNN_COMPILER_SUPPORT_X86_AVX)
563563 option(NCNN_AVX "optimize x86 platform with avx extension" ON)
564564 if(NCNN_COMPILER_SUPPORT_X86_FMA)
565565 if(NCNN_AVX)
modified src/CMakeLists.txt +4 −2
@@ -395,8 +395,10 @@
395395 endif()
396396 target_compile_options(ncnn PRIVATE /D__SSE2__)
397397 else()
398- target_compile_options(ncnn PRIVATE -msse2 -msse)
399- if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
398+ if(NOT CMAKE_SYSTEM_NAME MATCHES "WASI")
399+ target_compile_options(ncnn PRIVATE -msse2 -msse)
400+ endif ()
401+ if(CMAKE_SYSTEM_NAME MATCHES "Emscripten|WASI")
400402 target_compile_options(ncnn PRIVATE -msimd128)
401403 endif()
402404 endif()
modified src/benchmark.cpp +12 −4
@@ -15,11 +15,20 @@
1515 #include "benchmark.h"
1616
1717 #if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
18+#define USE_CXX11_CLOCK 1
19+#else
20+#define USE_CXX11_CLOCK 0
21+#endif
22+
23+#if USE_CXX11_CLOCK
1824 #include <chrono>
25+#if NCNN_THREADS
1926 #include <thread>
27+#endif
2028 #include <numeric>
2129 #include <algorithm>
22-#else
30+#endif // USE_CXX11_CLOCK
31+
2332 #ifdef _WIN32
2433 #define WIN32_LEAN_AND_MEAN
2534 #include <windows.h>
@@ -27,7 +36,6 @@
2736 #include <sys/time.h> //gettimeofday()
2837 #include <unistd.h> // sleep()
2938 #endif // _WIN32
30-#endif
3139
3240 #if NCNN_BENCHMARK
3341 #include "layer/convolution.h"
@@ -46,7 +54,7 @@
4654
4755 double get_current_time()
4856 {
49-#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
57+#if USE_CXX11_CLOCK
5058 auto now = std::chrono::high_resolution_clock::now();
5159 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
5260 return usec.count() / 1000.0;
@@ -69,7 +77,7 @@
6977
7078 void sleep(unsigned long long int milliseconds)
7179 {
72-#if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL
80+#if USE_CXX11_CLOCK && NCNN_THREADS
7381 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
7482 #else
7583 #ifdef _WIN32
modified src/cpu.cpp +2 −0
@@ -17,8 +17,10 @@
1717 #include "platform.h"
1818
1919 #include <limits.h>
20+#ifndef __wasi__
2021 #include <setjmp.h>
2122 #include <signal.h>
23+#endif // __wasi__
2224 #include <stdio.h>
2325 #include <string.h>
2426