I'd like to play around with some ideas by attempting to target cranelift IR from wat/wasm. Is there a good cranelift runtime that I can use to confirm that my compilations are correct?
Hi @Daniel Macovei -- can you clarify what you mean by "Cranelift runtime"? There are a number of runtimes whose associated code generators use Cranelift, but Cranelift itself is not a runtime, and the IR is agnostic to a lot of the decisions that a runtime normally makes; so typically you will write software that generates CLIF, and tailor that CLIF to interface with your own runtime
there is Wasmtime, which is a WebAssembly runtime, and it uses Cranelift to compile Wasm to machine code by way of CLIF. But it only accepts Wasm as input, not CLIF
Well I guess I started writing my own compiler in js that converts wat to wasm. I was thinking that a next fun thing to do would be to write something that would execute the wasm too. I guess from my reading that I thought that I good first step might be to target CLIF, but wasn't sure what the best way would be to check that I was generating it correctly. So are you saying that typically people know that they're generating it correctly by then targeting a specific hardware from their CLIF?
(Not an expert but) I think the "normal" way is that people link to the Cranelift library and generate the IR in memory, right there—compiling from that to native code using the cranelift_jit
or cranelift_module
crates. As opposed to generating a text file with CLIF code and then, later, invoking a separate tool to translate that the rest of the way to native code.
Ah, OK, so I think there's a bit of X-Y problem going on here. At a top level, what you are trying to do is write a Wasm compiler+runtime. You're targeting CLIF, and you want to run that CLIF somehow to see if execution is correct. Is that right?
To answer the specific question first, folks typically test compilers that target Cranelift IR by compiling to native code, and running some test suite.
More generally, though, and I think where some of my confusion is coming from: CLIF isn't really intended to be a "target" in the same way that Wasm bytecode is. The only real use of CLIF currently is to drive Cranelift to produce machine code for one of our supported targets (x86_64, aarch64, s390x). There is a nascent "CLIF interpreter" that supports a few opcodes, but nothing that would really be useful for true testing; it doesn't even really support memory (and it's not clear what it would mean to do so, since CLIF operates at a machine-code-semantics level).
Moreover, CLIF doesn't define any of the ways to interact with the outside world, etc. A user of Cranelift generates CLIF that specifically accesses data structures, and calls functions, provided by their runtime, whatever that is, in the context of a process running native code.
Onto the topic of writing a Wasm compiler using Cranelift: it sounds like you are wanting to do it by generating CLIF. That's totally workable; but what you'll need to do is invoke Cranelift to produce machine code, then write a runtime that the machine code interfaces with (in C/C++/Rust or some other natively-compiled language). Or you could use cranelift-wasm
, a frontend to the core cranelift-codegen
that does the Wasm-to-CLIF translation for you, but with runtime-specific details provided by you. Or ultimately use Wasmtime to run the Wasm. If the goal is learning, the first two do sound like interesting projects, and you have the benefit of the existing Wasmtime+Cranelift stack to compare and test against.
Hopefully that helps? Happy to explain any of the above more if needed!
Ok cool I guess I have some thinking to do... I'm working on an M1 so I guess I might have to use llvm instead of cranelift then?
@Daniel Macovei no, Cranelift works great on an M1 (I do a lot of my development on one) -- that's aarch64
in the target list above
we support: macOS, Windows, Linux on x86-64; macOS, Linux on aarch64; and Linux on s390x
So I'm thinking then that I'm more interested in the first one (generating CLIF from wasm) and then using cranelift to produce machine code
having difficulty building or installing in wasmtime or cranelift. getting the following. any tips? Screen-Shot-2022-06-01-at-6.20.38-PM.png
idk if this is the right place to be asking for this kindof help... sorry if not
You're missing the git submodules -- git submodule update --init --recursive
https://docs.wasmtime.dev/contributing-building.html might be helpful (includes the above and more details)
Thanks I'll check that out
Last updated: Nov 22 2024 at 16:03 UTC