Hi, looking at the jit example with cranelift, it does an unsafe jump into the pointer returned by get_finalized_function
but I want to return to the launching code (host?) and would like to use wasmtime to run, what's the recommended way to do so?
it does an unsafe jump into the pointer returned by get_finalized_function
It does a regular call from which you can return: https://github.com/bytecodealliance/cranelift-jit-demo/blob/e435835efbd7636bca230a3434d1d586587b378b/src/bin/toy.rs#L53
and would like to use wasmtime to run
Wasmtime can only run wasm modules it compiled itself. What do you want to achieve with adding Wasmtime in the mix?
wasmtime has a lot of control over constraints and run time which i'd like to leverage, like running with limited fuel or epoch interruption
Unfortunately you can't really have one and not the other: wasmtime's codegen works by plugging in runtime-specific behavior to Cranelift's Wasm frontend; there's no way out of the requirement to run Wasm.
Another way of looking at this is: what is the platform you wish to have? Native code but with ...? If e.g. epoch interruption or memory sandboxing, that only is possible because Wasmtime compiles Wasm down to machine code, and so has the opportunity to inject its own instrumentation, interruption points, sandboxing, etc. If you already have native code, produced on your own, then wasmtime's runtime jumping to that code doesn't add anything
oh I think I miscommunicated, I'd like to use cranelift's JIT to generate the wasm, and have wasmtime run it, not one but not the other
i.e. I want to generate cranelift IR, and have wasmtime run it
ah, unfortunately Cranelift cannot generate Wasm
however there are a number of other Wasm compiler backends you could use if you want to generate it in-process
e.g. the wasm-encoder
crate lets you emit it manually instruction-by-instruction; or there are a few higher-level IRs, like the walrus
crate (mostly 1-to-1 with Wasm's structured control flow) or my waffle
crate (an SSA IR in a CFG)
walrus looks like what I need, presumably it doesn't do any optimization?
no, I don't think so; waffle
does, but it could still use some work :-)
interesting, is waffle/walrus considered a backend even tho it generates wasm which is not host native?
oh and waffle is SSA IR, perfect
yep, both are "backends" in the sense that they lower an IR to some target
Wasm is a virtual ISA but still an ISA
(and e.g. LLVM considers Wasm an ordinary target architecture and uses its usual backend framework for it, too)
thanks, is there any chance you could make a link to the github repo in the crates.io listing? i.e. https://github.com/cfallin/waffle and is there an example anywhere? a simple hello world?
unfortunately there aren't too many examples of its use beyond github.com/cfallin/weval, which is the use-case that drove its design/implementation
i.e. it's more or less in a "wrote it for myself, others are free to use and contribute" state of early-open-source-project lifecycle -- PRs welcome :-)
it's not obvious how you'd generate IR by hand like say the example in here: https://crates.io/crates/wasm-encoder , is that intended?
no, it's not intended to be unobvious -- it's just a minimal API put together for the use of another project; improvements welcome :-)
not quite sure where I'd start tbqh, thanks anyway
Last updated: Nov 22 2024 at 16:03 UTC