bytecodealliance / cargo-wasi

A lightweight Cargo subcommand to build Rust code for the `wasm32-wasi` target
https://bytecodealliance.github.io/cargo-wasi/
Apache License 2.0
445 stars 29 forks source link

Running tests with some wasmtime features enabled #108

Open marmeladema opened 3 years ago

marmeladema commented 3 years ago

Hello!

I am very new to the wasm ecosystem and trying a bit all the tools/framework/runtime out there. I am working on porting https://github.com/cloudflare/sliceslice-rs to wasm. That project requires simd instructions to work.

I have a wip branch working but to run the tests, I cannot simple do:

$ cargo wasi test
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests (target/wasm32-wasi/debug/deps/sliceslice-c22d039e805d5bb0.wasm)
     Running tests/i386.rs (target/wasm32-wasi/debug/deps/i386-310c1b29d531f32f.wasm)
     Running `/home/adema/code/sliceslice-rs/target/wasm32-wasi/debug/deps/sliceslice-c22d039e805d5bb0.wasm`
Error: failed to run main module `/home/adema/code/sliceslice-rs/target/wasm32-wasi/debug/deps/sliceslice-c22d039e805d5bb0.wasm`

Caused by:
    0: WebAssembly translation error
    1: Invalid input WebAssembly code at offset 337919: SIMD support is not enabled

because the simd feature of wasmtime is not enabled by default.

My problem is that I haven't found a way to enable simd from cargo-wasi. To run the tests, I have to manually run:

$ wasmtime --enable-simd /home/adema/code/sliceslice-rs/target/wasm32-wasi/debug/deps/sliceslice-c22d039e805d5bb0.wasm

running 14 tests
test tests::memchr_search_different ... ok
test tests::memchr_search_middle ... ok
test tests::memchr_search_mutiple ... ok
test tests::memchr_search_prefix ... ok
test tests::memchr_search_same ... ok
test tests::memchr_search_suffix ... ok
test tests::needle_array_size ... ok
test tests::needle_slice_size ... ok
test wasm32::tests::search_different ... ok
test wasm32::tests::search_middle ... ok
test wasm32::tests::search_multiple ... ok
test wasm32::tests::search_prefix ... ok
test wasm32::tests::search_same ... ok
test wasm32::tests::search_suffix ... ok

test result: ok. 14 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

but that requires finding the wasm binary path which is not easily predictable.

My question is then: is there a way to pass command line options to wasmtime? I can see different options if that's not already implemented:

Thank you in advance for taking the time to read this :+1:

alexcrichton commented 3 years ago

Thanks for the report! This isn't supported well today unfortunately. Some initial work was done at https://github.com/bytecodealliance/cargo-wasi/pull/19 but it stalled out because I don't think there's an idea of what the best design for this is right now.

marmeladema commented 3 years ago

From a user perspective, I think I would prefer the cargo metadata option the most because I think people should not have to know which wasmtime features to enable or disable. If you agree with this, and if we restrict ourselves to specifying only wasmtime features, how much work do you think it would be?

What I had in mind was something like:

[package.metadata.wasi.wasmtime.features]
simd = true

or if we want to have per profile configuration:

[package.metadata.wasi.wasmtime.profile.release]
features = { simd = true }
marmeladema commented 2 years ago

@alexcrichton I don't know exactly what changed but I am now able to run cargo wasi without problem! Maybe the simd feature is enabled by default now? Anyway, that's really neat, I was able to port sliceslice to wasm32 :+1: