Stream: cranelift

Topic: Bootstrapping. Difficulties. Questions.


view this post on Zulip Chris Clark (Dec 25 2024 at 17:35):

I hadn't really thought about this, but using cranelift to bootstrap is going to be quite difficult. I just started the process of focusing on being able to make my language in my own language (no where near there). But I will have a problem I'm sure others have ran into. I will need to conform to rust implementations if I wanted to link rust built cranelift crates to my language.

I could choose a high level point where I pass variables in to a point where it is fully rust, rather than use all cranelift components independently. But then, my language will have a significant surface area still in rust.

I was thinking it might be nice to build my objects following rust standards so I could link any rust library, but I'm almost certain the rust community will view this as undefined behavior, even if I mimicked lifetimes, alignment, and everything perfectly.

Has anyone done this for their language?

view this post on Zulip Jacob Lifshay (Dec 26 2024 at 02:05):

Chris Clark said:

I'm almost certain the rust community will view this as undefined behavior

no, that's implementation-defined behavior (where it works as intended but the exact behavior isn't guaranteed to be stable). undefined behavior is worse, where you're violating assumptions the compiler makes (it operates based on the assumption that X isn't possible, so if you do X anyway it breaks) so it's possible to do any of: do exactly what you want, delete all your code, do something completely unexpected (stereotypically that's formatting your hard drive), crash (even at compile-time), etc.

view this post on Zulip Jacob Lifshay (Dec 26 2024 at 02:12):

to be clear i'm not trying to imply rustc's behavior wil be easy to match or well specified, but it is possible to do things where your output will always work (ignoring bugs), unlike UB

view this post on Zulip Chris Clark (Dec 26 2024 at 17:35):

@bjorn3 I just found this github repo, rustc_codegen_cranelift and I'm now very scared of trying to mimic rustc. Is this a good place for me to start?

You are probably looking for the upstream version of rustc_codegen_cranelift - bjorn3/rustc_codegen_cranelift

view this post on Zulip bjorn3 (Dec 26 2024 at 19:41):

rustc_codegen_cranelift and the default LLVM backend are ABI compatible with each other (minus a couple of bugs), so I don't think you will get much benefit from using rustc_codegen_cranelift in terms of interoperability.

view this post on Zulip Chris Clark (Dec 26 2024 at 20:17):

Ahh, I found base.rs and it does look like this will help me generate structures and implementation that should match rustc though. Or am I misunderstanding?

Cranelift based backend for rustc. Contribute to rust-lang/rustc_codegen_cranelift development by creating an account on GitHub.

view this post on Zulip bjorn3 (Dec 26 2024 at 20:38):

base.rs translates MIR to cranelift ir. Rustc is not designed to generate arbitrary MIR however. Each MIR body has to correspond with an item and items are normally created when lowering rust AST to HIR. You can probably get it to work, but it would not be trivial.


Last updated: Jan 24 2025 at 00:11 UTC