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

Supporting xsimd through xeus-cpp-lite #1084

Open
anutosh491 opened this issue Jan 27, 2025 · 10 comments
Open

Supporting xsimd through xeus-cpp-lite #1084

anutosh491 opened this issue Jan 27, 2025 · 10 comments

Comments

@anutosh491
Copy link
Contributor

Hey @serge-sans-paille

At this point, xeus-cpp-lite is able to support quite some header only libraries. For eg (nlohmann/json, boost-cpp etc)

Image

Image

And at this point we should also be able to support xsimd (we are able to do SIMD smoothly here)

Image

@anutosh491
Copy link
Contributor Author

But with xsimd we get some errors !

Image

This point to a failed assertion here (https://github.com/llvm/llvm-project/blob/main/clang/lib/Interpreter/Interpreter.cpp#L453) and suggests that we rebuild with assertions
Uncaught (in promise) RuntimeError: Aborted(Assertion failed: T.TheModule, at: /home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1737094283/work/clang/lib/Interpreter/Interpreter.cpp,453,Execute). Build with -sASSERTIONS for more info.

On Doing that, I see the following

Image

Which is a RuntimeError: memory access out of bounds error.

@anutosh491
Copy link
Contributor Author

anutosh491 commented Jan 27, 2025

So not sure what the error is as of now

  1. Maybe in llvm
  2. Maybe some flag or something missing while building xeus-cpp-lite (most flags come out of here and are defined here for us to look into)

But here are the build instructions for the replicate the above

  1. Currently xeus-cpp-lite doesn't have functionality to access the resource-dir headers where wasm_simd128.h lies (we can currently make use of the standard headers from emscripten's sysroot though and resource dir would be supported soon)

  2. So in the build instructions for building xeus-cpp-lite (https://github.com/compiler-research/xeus-cpp?tab=readme-ov-file#installation-within-a-mamba-environment-wasm-build-instructions)

Just copy the contents from emsdk/upstream/lib/clang/18/include/wasm_simd128.h to emsdk/upstream/emscripten/cache/sysroot/include/wasm_simd128.h

before the emcmake cmake ... step

  1. Add xsimd (recipe hosted on emscripten-forge) to your PREFIX)
    P.S : There might be something wrong with how xsimd is built too ... so we might need to check that too.

  2. Then simply follow the build instructions and we should be able to access xsimd headers through xeus-cpp-lite !

Overall I think we have all tools necessary to run xsimd with xeus-cpp-lite but something is being missed here.

@serge-sans-paille
Copy link
Contributor

Hey @anutosh491 Indeed based on the scenario you provide, everything should be working. I'll definitively have a look!

@anutosh491
Copy link
Contributor Author

Thanks Serge for the reply

  1. Doesn't look like an obvious llvm (clang-repl) error to me as we're able to load other libraries smoothly !
  2. I don't see an obvious reason for a "memory out of bounds error" and hence I am thinking its either some flag or the way we frame our recipe that might be not working as expected (https://github.com/emscripten-forge/recipes/tree/main/recipes/recipes_emscripten/xsimd)

@anutosh491
Copy link
Contributor Author

We now moved to a later version of emsdk. Everything was shifted from 3.1.45 to 3.1.73 !

@anutosh491
Copy link
Contributor Author

anutosh491 commented Feb 5, 2025

I was trying it with my toy project and using llvm built with assertions

I see this

Image

Need to see if this is exactly what's going wrong !

Here is the error message though

In file included from <<< inputs >>>:1:
In file included from input_line_2:1:
In file included from /include/xsimd/xsimd.hpp:62:
In file included from /include/xsimd/types/xsimd_batch.hpp:492:
In file included from /include/xsimd/types/../arch/xsimd_isa.hpp:124:
/include/xsimd/types/../arch/./xsimd_wasm.hpp:1339:20: error: always_inline function 'wasm_f64x2_make' requires target feature 'simd128', but would be inlined into function 'set' that is compiled without support for 'simd128'
 1339 |             return wasm_f64x2_make(values...);

@serge-sans-paille
Copy link
Contributor

This looks like your whole project should be compiled with simd128 activated, maybeincluding xeus-lite-cpp itself?

@anutosh491
Copy link
Contributor Author

anutosh491 commented Feb 5, 2025

Hey Serge,

Yeah I was thinking the same.

Even before we go to xeus-cpp-lite, I am trying to get stuff working with a smaller toy project (as involves lesser things and makes debugging easier)

So I see just 3 factors here

  1. xsimd
  2. llvm (clang & lld) - We only depend on libclangInterpreter.a as such that we need to link against the final target. Currently being built using
emcmake cmake -DCMAKE_BUILD_TYPE=MinSizeRel         \
    -DCMAKE_PREFIX_PATH=$PREFIX                     \
    -DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten    \
    -DLLVM_TARGETS_TO_BUILD="WebAssembly"           \
    -DLLVM_ENABLE_ASSERTIONS=ON                     \
    -DLLVM_INCLUDE_BENCHMARKS=OFF                   \
    -DLLVM_INCLUDE_EXAMPLES=OFF                     \
    -DLLVM_INCLUDE_TESTS=OFF                        \
    -DLLVM_ENABLE_LIBEDIT=OFF                       \
    -DLLVM_ENABLE_PROJECTS="clang;lld"              \
    -DCMAKE_VERBOSE_MAKEFILE=ON                     \
    -DLLVM_ENABLE_THREADS=OFF                       \
    -DLLVM_ENABLE_ZSTD=OFF                          \
    -DLLVM_ENABLE_LIBXML2=OFF                       \
    -DCLANG_ENABLE_STATIC_ANALYZER=OFF              \
    -DCLANG_ENABLE_ARCMT=OFF                        \
    -DCLANG_ENABLE_BOOTSTRAP=OFF                    \
    -DCMAKE_CXX_FLAGS="-Dwait4=__syscall_wait4 -fexceptions" \
    ../llvm
  1. clang-repl-wasm which is the toy project ... that uses the following
find_package(LLVM REQUIRED CONFIG)
find_package(LLD REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)

set (CMAKE_CXX_STANDARD 17)
add_compile_options(-Wall -pedantic -fPIC)

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})

add_executable(Compiler CompilerModule.cpp)

# Link against LLVM libraries
target_link_libraries(Compiler embind)
target_link_libraries(Compiler clangInterpreter)

target_link_options(Compiler PRIVATE
    -O1
    -fexceptions
    -msimd128
    -sMODULARIZE
    -sEXPORT_ES6=1
    -sASSERTIONS=1
    -sNO_DISABLE_EXCEPTION_CATCHING
    -sALLOW_MEMORY_GROWTH=1
    -sINITIAL_MEMORY=128MB
    -sTOTAL_STACK=32MB
    -sMAIN_MODULE=1
    -sUSE_SDL=2
    -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,stringToNewUTF8,getValue,setValue
    -sEXPORTED_FUNCTIONS=_malloc,_free,__ZTIN10emscripten3valE
    "-Wl,--export=__clang_Interpreter_SetValueNoAlloc"
    --preload-file /Users/anutosh491/micromamba/envs/xeus-cpp-wasm-build/opt/emsdk/upstream/emscripten/cache/sysroot/@/
    --profiling-funcs
)

Now as you can see I did add -msimd128 as a link option above but that still gets me to

Image

So the only thing I am guessing is left here is llvm. Do I need to built that with the -msimd128 flag too ?
Not sure if any changes are required on xsimd (I see for running tests we build using this flag)

xsimd/test/CMakeLists.txt

Lines 221 to 227 in 6d6067a

if(EMSCRIPTEN)
set_target_properties(test_xsimd PROPERTIES LINK_FLAGS "-s MODULARIZE=1 -s EXPORT_NAME=test_xsimd_wasm -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -lembind")
target_compile_options(test_xsimd
PUBLIC --std=c++14
PUBLIC "SHELL: -msimd128"
PUBLIC "SHELL: -msse2"
)

The recipe is present here if we want to look into any changes as to how xsimd is built (https://github.com/emscripten-forge/recipes/tree/main/recipes/recipes_emscripten/xsimd)

@serge-sans-paille
Copy link
Contributor

serge-sans-paille commented Feb 5, 2025 via email

@serge-sans-paille
Copy link
Contributor

Hey @anutosh491, any other way I can help?

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

2 participants