Stream: general

Topic: Running cranelift JITed code directly with wasmtime?


view this post on Zulip John Barker (Oct 30 2023 at 01:45):

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?

view this post on Zulip bjorn3 (Oct 30 2023 at 15:10):

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?

view this post on Zulip John Barker (Oct 30 2023 at 23:21):

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

view this post on Zulip Chris Fallin (Oct 30 2023 at 23:30):

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

view this post on Zulip John Barker (Oct 30 2023 at 23:37):

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

view this post on Zulip John Barker (Oct 30 2023 at 23:38):

i.e. I want to generate cranelift IR, and have wasmtime run it

view this post on Zulip Chris Fallin (Oct 30 2023 at 23:38):

ah, unfortunately Cranelift cannot generate Wasm

view this post on Zulip Chris Fallin (Oct 30 2023 at 23:39):

however there are a number of other Wasm compiler backends you could use if you want to generate it in-process

view this post on Zulip Chris Fallin (Oct 30 2023 at 23:40):

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)

view this post on Zulip John Barker (Oct 30 2023 at 23:43):

walrus looks like what I need, presumably it doesn't do any optimization?

view this post on Zulip Chris Fallin (Oct 30 2023 at 23:44):

no, I don't think so; waffle does, but it could still use some work :-)

view this post on Zulip John Barker (Oct 30 2023 at 23:50):

interesting, is waffle/walrus considered a backend even tho it generates wasm which is not host native?

view this post on Zulip John Barker (Oct 30 2023 at 23:52):

oh and waffle is SSA IR, perfect

view this post on Zulip Chris Fallin (Oct 31 2023 at 00:54):

yep, both are "backends" in the sense that they lower an IR to some target

view this post on Zulip Chris Fallin (Oct 31 2023 at 00:55):

Wasm is a virtual ISA but still an ISA

view this post on Zulip Chris Fallin (Oct 31 2023 at 00:55):

(and e.g. LLVM considers Wasm an ordinary target architecture and uses its usual backend framework for it, too)

view this post on Zulip John Barker (Oct 31 2023 at 00:59):

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?

Contribute to cfallin/waffle development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Oct 31 2023 at 02:59):

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

Contribute to cfallin/weval development by creating an account on GitHub.

view this post on Zulip Chris Fallin (Oct 31 2023 at 03:00):

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 :-)

view this post on Zulip John Barker (Oct 31 2023 at 05:28):

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?

view this post on Zulip Chris Fallin (Oct 31 2023 at 15:59):

no, it's not intended to be unobvious -- it's just a minimal API put together for the use of another project; improvements welcome :-)

view this post on Zulip John Barker (Nov 01 2023 at 00:22):

not quite sure where I'd start tbqh, thanks anyway


Last updated: Dec 23 2024 at 12:05 UTC