Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] webassembly simd as new dedicated target #5902

Open
nihui opened this issue Feb 12, 2025 · 2 comments
Open

[RFC] webassembly simd as new dedicated target #5902

nihui opened this issue Feb 12, 2025 · 2 comments

Comments

@nihui
Copy link
Member

nihui commented Feb 12, 2025

pros

  • wasm as a first-class citizen
  • avoid any slow emulation of sse2 intrinsics
  • brings native i8 mul long and i16-dp
  • brings fmadd and i8-u7-dp with relaxed-simd, which is crucial for neural network inference
  • v8 implement fp16 functions

cons

  • new target with LOTS OF optimization code

to be investigated

resources

llvm wasm simd header
https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/wasm_simd128.h

simd
https://github.com/WebAssembly/simd/blob/main/proposals/simd/SIMD.md

relaxed-simd
https://github.com/WebAssembly/relaxed-simd/blob/main/proposals/relaxed-simd/Overview.md

half-precision
https://github.com/WebAssembly/half-precision/blob/main/proposals/half-precision/Overview.md

@nihui
Copy link
Member Author

nihui commented Feb 12, 2025

runtime simd and relaxed-simd discovery

#include <stdio.h>
#include <emscripten.h>

EM_JS(int, check_simd_support, (), {
    return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11]));
});

EM_JS(int, check_relaxed_simd_support, (), {
    return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,15,1,13,0,65,1,253,15,65,2,253,15,253,128,2,11]));
});

int main()
{
    printf("simd = %d\n", check_simd_support());
    printf("relaxed-simd = %d\n", check_relaxed_simd_support());

    return 0;
}
app simd relaxed-simd
linux firefox 1 0
linux chrome 1 1
android browser 1 1
android wechat webview 1 1
android wechat work webview 1 0
macos safari 1 0
ios safari 1 0
ios wechat safari 1 0

@nihui
Copy link
Member Author

nihui commented Feb 13, 2025

wasm code coverage

# compile with --coverage -fprofile-arcs -ftest-coverage -g
emcc test.cpp -c -o test.o --coverage -fprofile-arcs -ftest-coverage -g
emcc simplegcov.cpp -c -o simplegcov.o --coverage -fprofile-arcs -ftest-coverage -g

# link with --coverage -g -s FORCE_FILESYSTEM=1 -sEXIT_RUNTIME=1
# link with -lnodefs.js -lnoderawfs.js
emcc test.o simplegcov.o -o test.js -s EXPORTED_FUNCTIONS='["_main"]' --coverage -g -s FORCE_FILESYSTEM=1 -sEXIT_RUNTIME=1 -lnodefs.js -lnoderawfs.js

# run wasm
node test.js

# collect gcda and generate output html
lcov -d ./ -c --gcov-tool `pwd`/llvm-gcov -o lcov.info --ignore-errors inconsistent
genhtml lcov.info -o ./output --ignore-errors inconsistent

#5903

the llvm-gcov wrapper

#!/bin/sh
exec llvm-cov gcov "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant