DukeofStars opened issue #5320:
I wish to create a custom backend (similar to risc-v) for Cranelift, as a sort of personal project. However the documentation is lacking to say the least, and I have found no way to create one other than to hard code it in. So I have some questions:
- Is this even possible?
- How does Cranelift handle backends, ie. are they all embedded into the binary at compile time, or are they loaded at runtime
- How would you go about creating a backend? How do you tell cranelift that your backend exists? How do you use and test said backend?
- How would you distribute your backend so it can be used by compilers?
bjorn3 commented on issue #5320:
In principle it is possible as the TargetIsa trait is public. In practice the majority of the backend framework (
cranelift_codegen::machinst
) is private so you would either have to copy it, work entirely from scratch or fork cranelift.Cranelift doesn't load backends at runtime. You can select which backends you want using the cargo features of cranelift-codegen and it will build them as part of cranelift-codegen itself.
If you create an external backend you did have to implement TargetIsa for it and then modify whichever compiler you want to use to use this TargetIsa instead of looking up one of cranelift's builtin backends.
Rust doesn't really like distributing crates in any way other than as source code because every rustc version is abi incompatible with every other rustc version and as such every crate has to be compiled using the same rustc version.
DukeofStars commented on issue #5320:
Thanks for the help! But this solution doesn't really fit my needs, and I have no wish to modify the rust compiler to target my backend. I might just try out LLVM
DukeofStars closed issue #5320:
I wish to create a custom backend (similar to risc-v) for Cranelift, as a sort of personal project. However the documentation is lacking to say the least, and I have found no way to create one other than to hard code it in. So I have some questions:
- Is this even possible?
- How does Cranelift handle backends, ie. are they all embedded into the binary at compile time, or are they loaded at runtime
- How would you go about creating a backend? How do you tell cranelift that your backend exists? How do you use and test said backend?
- How would you distribute your backend so it can be used by compilers?
bjorn3 commented on issue #5320:
What has the rust compiler to do with this? Did you want to add your own target to the cranelift backend for rustc?
jameysharp commented on issue #5320:
LLVM has a much larger developer community than we do, which may make it a better choice for you. I do want to note a couple of things though.
First: We have a goal of making Cranelift backend development easier, so thank you for your feedback on which points you found most challenging.
Second, to clarify: You don't need to modify rustc to do anything with Cranelift. bjorn3's point was just that the way the Rust ecosystem works today, there is no useful unit of distribution for a Rust component except in source code form. So if your question about distributing a backend was whether there's some kind of binary plugin system, that isn't a good option for Rust projects like Cranelift today.
Finally: It isn't clear to me which compilers you were hoping to use with your new backend. The main users of Cranelift today are Wasmtime, which uses it to compile WebAssembly to native code, and cg-clif, which is a Rust compiler backend. In both cases there isn't currently any support for using out-of-tree Cranelift backends, so the easiest option is to fork Cranelift and develop new backends in-tree.
cfallin commented on issue #5320:
I want to add a little more to the excellent answers above re:
However the documentation is lacking to say the least
and say that we are very open to contributions for documentation as well! Unfortunately with so much to do and with so few people, we haven't had time to write a "How to Build a Backend" tutorial. But that's on my long-term list for docs that would be useful to have. Much of Cranelift's momentum comes from volunteer contributions, so if anyone wants to try to put together notes or an outline for such a thing, and start the process, that would be a really valuable effort I think.
DukeofStars commented on issue #5320:
What has the rust compiler to do with this? Did you want to add your own target to the cranelift backend for rustc?
Yes, the idea was to compile rust mainly, but potentially other languages, using the cranelift backend, then into my custom backend.
ie. frontend (rustc...) > cranelift > optimise > custom backend.First: We have a goal of making Cranelift backend development easier, so thank you for your feedback on which points you found most challenging.
This sounds great, and I look forward to working with Cranelift in the future, as LLVM, while powerful, can be difficult to work with too.
Last updated: Nov 22 2024 at 17:03 UTC