espoal opened issue #12598:
I will start to admit that I'm a noob in the compiler topics, so I'm probably misunderstanding something fundamental.
Goal: I would like to implement a custom target using cranelift, which is a virtual machine very similar to WASM. Let's say for now that the source is Rust code.
My understanding: I think that to achieve this I would need to implement a custom backend / codegen module, while reusing the existing Rust frontend. I believe that the Rust-to-WASM pipeline is implemented this way, but this issue seems to contradict what I know.
Questions:
- Is my understanding right? If not, how is the Rust-to-WASM pipeline implemented?
- Are there better/simpler approaches?
- Where could I find some resources on implementing a custom target/backend? Or is this very novel territory that is not yet covered by tutorials?
espoal edited issue #12598:
I will start to admit that I'm a noob in the compiler topic, so I'm probably misunderstanding something fundamental.
Goal: I would like to implement a custom target using cranelift, which is a virtual machine very similar to WASM. Let's say for now that the source is Rust code.
My understanding: I think that to achieve this I would need to implement a custom backend / codegen module, while reusing the existing Rust frontend. I believe that the Rust-to-WASM pipeline is implemented this way, but this issue seems to contradict what I know.
Questions:
- Is my understanding right? If not, how is the Rust-to-WASM pipeline implemented?
- Are there better/simpler approaches?
- Where could I find some resources on implementing a custom target/backend? Or is this very novel territory that is not yet covered by tutorials?
espoal edited issue #12598:
I will start to admit that I'm a noob in the compiler topic, so I'm probably misunderstanding something fundamental.
Goal: I would like to implement a custom target using cranelift, which is a virtual machine very similar to WASM. Let's say for now that the source is Rust code.
My understanding: I think that to achieve this I would need to implement a custom backend / codegen module, while reusing the existing Rust frontend. I believe that the Rust-to-WASM pipeline is implemented this way, but this issue seems to contradict what I know.
Questions:
- Is my understanding right? If not, how is the Rust-to-WASM pipeline implemented?
- Are there better/simpler approaches?
- Where could I find some resources on implementing a custom target/backend? Or is this very novel territory that is not yet covered by tutorials?
Thank you!
bjorn3 commented on issue #12598:
Rustc uses LLVM for compiling to wasm. No Cranelift or Wasmtime is involved with that at all. Only compiling the wasm to machine code uses Cranelift as compiler when using Wasmtime as wasm runtime. There is also rustc_codegen_cranelift for directly compiling rust code with Cranelift, but that doesn't allow producing wasm, rather it directly produces machine code. Only wasm -> cranelift ir is supported through Wasmtime, not cranelift ir -> wasm. Wasm is different enough from machine code that you can't reuse Cranelift backend framework at all. Cranelift expects to compile to a register machine (like almost all real hardware), while wasm is a stack machine.
espoal commented on issue #12598:
Thanks @bjorn3 for the explanation. So from what I understood:
- Rust get compiled to WASM bytecode using the LLVM backend
- The WASM file is then distributed
- To execute it, wasmtime uses cranelift to lower it to actual machine code
- Lowering can be done either JIT, or AOT
Makes sense now.
espoal edited a comment on issue #12598:
Thanks @bjorn3 for the explanation. So from what I understood:
- Rust get compiled to WASM bytecode using the LLVM backend
- The WASM file is then distributed
- To execute it, wasmtime uses cranelift to lower it to actual machine code
- Lowering can be done either JIT, or AOT
Makes sense now. If that's correct please answer with a thumb up, and I will consider this issue closed.
espoal edited a comment on issue #12598:
Thanks @bjorn3 for the explanation. So from what I understood:
- Rust get compiled to WASM bytecode using the LLVM backend
- The WASM file is then distributed
- To execute it, wasmtime uses cranelift to lower it to actual machine code
- Lowering can be done either JIT, or AOT
Makes sense now. If that's correct please react with a thumb up, and I will consider this issue closed.
espoal closed issue #12598:
I will start to admit that I'm a noob in the compiler topic, so I'm probably misunderstanding something fundamental.
Goal: I would like to implement a custom target using cranelift, which is a virtual machine very similar to WASM. Let's say for now that the source is Rust code.
My understanding: I think that to achieve this I would need to implement a custom backend / codegen module, while reusing the existing Rust frontend. I believe that the Rust-to-WASM pipeline is implemented this way, but this issue seems to contradict what I know.
Questions:
- Is my understanding right? If not, how is the Rust-to-WASM pipeline implemented?
- Are there better/simpler approaches?
- Where could I find some resources on implementing a custom target/backend? Or is this very novel territory that is not yet covered by tutorials?
Thank you!
Last updated: Feb 24 2026 at 04:36 UTC