Cross Platform: Rust to LLVM IR and is LLVM IR is portable?

Hi

As Rust Compiler is based on LLVM (correct me if i am wrong), and i can convert a library/binary project into LLVM IR using "rustc .... --emit=llvm-ir"

(Cross Platform): Is this LLVM IR portable to any platform that is supported by rust link (windows/Linux/Mac) and compile final executable ?

if not What is MIR, is this MIR cross platform ?

Thanks in advance
B

2 Likes

Yes, LLVM's intermediate language is cross platform. That's one of the main reasons for it existing! Rust doesn't have to handle the specifics of compiling your code down to the target architecture/OS; it just has to turn it into IR, and then LLVM can take it from there.

That said, you definitely don't have to have Rust emit IR and then manually compile it - cross compilation is really easy, especially if you use rustup to manage the different toolchains.

For an explanation of what exactly MIR is, I'd recommend this blog post - Niko Matsakis has a golden gift for making complicated topics easy to understand, and I can guarantee I wouldn't do a better job trying to explain it here :stuck_out_tongue:

1 Like

This is not my understanding. See Frequently Asked Questions (FAQ) — LLVM 18.0.0git documentation for example, or c++ - LLVM bitcode cross-platform - Stack Overflow

3 Likes

Whoops, sorry, apparently my understanding of it was a bit oversimplified. Thanks for the clarification!

The way I understand it is that the IR itself is platform independent, but the represented code will almost certainly have a lot of platform assumptions baked in.

1 Like

To add problems, LLVM IR an implementation detail of LLVM and not stable. Different versions of LLVM might or might not understand IR generated by another version.

This is an issue as Apple now supports sending them code as bitcode: https://github.com/rust-lang/rust/issues/35968

It needs you to use the correct version of LLVM.

To my understanding, there's other platforms where that approach is used.