adambratschikaye opened issue #7652:
At DFINITY we've been looking into running the Wasmtime compilation of Wasm modules within Wasm itself (for sandboxing and determinism). It looks like we won't be able to use it at the moment because the performance hit is too large given that we lose the option to compile functions in parallel. But I have a rough POC with the changes needed to make this work and we were wondering if you'd be interested in merging them anyway, since it would be a pretty cool property of Wasmtime. There's still some cleanup needed and I'd be happy to help with that.
There's a more detailed description of the changes at the link provided, but most of it was just refactoring so that the functions
Config::build_compilerandModule::build_artifactscan be compiled towasm32targets.
ulan commented on issue #7652:
@alexcrichton, @tschneidereit: ^ this is the result of exploring the idea you mentioned here: https://github.com/bytecodealliance/rfcs/pull/32#issuecomment-1740930345
alexcrichton commented on issue #7652:
Thanks for working forward on this, and it's great to see something already up and running!
I've skimmed over the changes you've made and they're all looking reasonable to me. I'd personally prefer though to have a different trajectory for integrating a change like this in the sense that I already feel like we have too many internal crates and I'd prefer to move towards consolidating them instead of proliferating them. In that sense I'd prefer to not add new
wasmtime-jit-runtimeorwasmtime-compilecrates but instead control this all through Cargo features of thewasmtimecrate if possible.Cargo features provides a good way in my mind to think about this and continue development of it into the future. It means that the Wasmtime CLI, for example, could be itself compiled to WebAssembly so long as everything except the
compilecommand were disabled. The output ofcompilewould be usable by Wasmtime of the same version though. Reducing the number of crates additionally helps keep things a bit more maintainable internally I think since there's less of a question of what-goes-where.Organizationally what I'd recommend is to move all target-agnostic things, such as
wasmtime_jit::ObjectBuilder, intowasmtime-environ. Thewasmtime-environcrate already actually compiles to WebAssembly and is used by jco. In that sense thewasmtime-environcrate is a suitable location for "stuff things here that compile everywhere and have no system dependencies". I think that thewasmtime-compileandwasmtime-jit-runtimecrates could probably be merged intowasmtime-environ. One day I'd like to merge the{wasmtime-runtime, wasmtime-jit, wasmtime}crates if possible since I don't think we're getting much mileage from the distinction right now.
I've also been wondering a bit about the threading issue you mention since I think that's important to solve. One initial way I was thinking it might be solved was explicit threading on the host, for example no shared state and a
Store-per-thread where the embedder orchestrates the threads and calls cranelift once-per-function. Wiring this all up I don't think is that feasible though since it would be exposing a lot of internals of Wasmtime which wouldn't be great.Alternatively though one option would be the
wasm32-wasi-threadstarget. While I haven't tested it myself I think that therayoncrate should work there so in theorywasmtime run -S threads wasmtime.wasm compile ...should "just work" and have internal parallelism. That would then also work from an embedder point of view assuming you wouldn't use the literal CLI itself.
bjorn3 commented on issue #7652:
wasm32-wasi-threads doesn't work with the wasi-preview2 based wasi-preview1 implementation in wasmtime, right?
alexcrichton commented on issue #7652:
Correct, yeah, that's tracked by https://github.com/bytecodealliance/wasmtime/issues/7551.
adambratschikaye commented on issue #7652:
Regarding merging the crates and using features, what would you think about the following steps?:
- Move everything target agnostic from
wasmtime-jittowasmtime-environ.- Move the remaining parts of
wasmtime-jitintowasmtimeand removewasmtime-jit.- Put everything target specific in
wasmtimeunder a feature, such that we can compile it towasm32if that feature isn't enabled.I'm not sure how messy step 3 would end up being. Maybe it would be easier to move the parts of
wasmtimerequired for compilation fromwasmtimetowasmtime-environinstead of 3?
alexcrichton commented on issue #7652:
Step 1/2 sound good to me.
For step 3 I agree things may get a bit messy. One possibility though is we could have:
crates/wasmtime/src/runtime/*.rs- all runtime bits, only one cfg to turn off the entire thingscrates/wasmtime/src/compile/*.rs- all compiling bits, only one cfg to turn off as wellThe final crate structure of types like
wasmtime::Instancewould be the same but everything would be defined undercrates/wasmtime/src/runtime/store.rsfor example. I'd personally prefer to slurp things into thewasmtimecrate rather than the other way around, but not at the cost of large contortions so if you find it's too difficult to go that direction I think it's reasonable to move agnostic things intowasmtime-environ.
adambratschikaye commented on issue #7652:
Sounds good, I'll give it a try.
adambratschikaye closed issue #7652:
At DFINITY we've been looking into running the Wasmtime compilation of Wasm modules within Wasm itself (for sandboxing and determinism). It looks like we won't be able to use it at the moment because the performance hit is too large given that we lose the option to compile functions in parallel. But I have a rough POC with the changes needed to make this work and we were wondering if you'd be interested in merging them anyway, since it would be a pretty cool property of Wasmtime. There's still some cleanup needed and I'd be happy to help with that.
There's a more detailed description of the changes at the link provided, but most of it was just refactoring so that the functions
Config::build_compilerandModule::build_artifactscan be compiled towasm32targets.
adambratschikaye commented on issue #7652:
I think we can close this as https://github.com/bytecodealliance/wasmtime/pull/7766 is merged.
Last updated: Dec 13 2025 at 19:03 UTC