Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When writing code compiled by LLVM backend, does architecture matter?

My question is actually more general than the title:

At what point does the architecture matter when writing code that will eventually be compiled to LLVM intermediary code, and then from there to the machine language?

  1. Let's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?

  2. Or could it be that the Rust standard library hasn't been made "ARM compatible" yet, so I couldn't compile to ARM even if the LLVM targets it?

  3. What if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?

  4. If all the above examples compile just fine, what do I have to do to get my code to break on one architecture not compile to a certain architecture?

Bonus question of the same variety:

  1. Let's say the standard library makes use of OS system calls (which is surely does). Do you have to care about architecture when making system calls? Or does the OS (Linux, for example) abstract away architecture as well?

Thanks.

like image 772
bob Avatar asked Dec 16 '15 07:12

bob


1 Answers

TL;DR

From my understanding you can compile to any target LLVM supports (there may still be a few caveats here with frontends using inline assembler or module level inline assembly), however, you are not guaranteed it will actually execute correctly. The frontend is responsible for doing the work to be portable across the platforms the author supports.

Note also that as a frontend developer you are responsible for providing the data layout and target triple.

see also:

Your Questions:

Let's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?

This is dependent on the authors of the Rust frontend.

Or could it be that the Rust standard library hasn't been made "ARM compatible" yet, so I couldn't compile to ARM even if the LLVM targets it?

I'm pretty sure LLVM would be able to emit the instructions, but it may not be correct in terms of addressing.

I have not used the inline assembler facilities mentioned above myself, but I assume if it allows platform specific assembly then this would break platform agnostic compilation as well.

What if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?

This again depends on what the Rust frontend emits. There may be some boilerplate setup logic it emits even before it emits instructions for your logic.

I'm writing my own language in LLVM that does this in the case of a special function called "main". I am targeting the C ABI so it will wrap this main with a proper C style main and invoke it with a stricter set of parameters.

If all the above examples compile just fine, what do I have to do to get my code to break on one architecture not compile to a certain architecture?

Consider C/C++ with Clang as mentioned in the llvm FAQ. Clang is a frontend, probably the most popular, for LLVM and the users writing C/C++ are responsible for #include-ing the appropriate platform specific functionality.

Some languages may be designed more platform independent and the frontend could then handle the work for you.

Let's say the standard library makes use of OS system calls (which is surely does). Do you have to care about architecture when making system calls? Or does the OS (Linux, for example) abstract away architecture as well?

I'm assuming you are talking about the case where the frontend targets the C standard library in which case LLVM has standard C library intrinsics which could be used by the frontend. This is not the only way, however, as you can use the call instruction to invoke C functions directly if targeting the C ABI as in the Kaleidoscope example.

In the end the standard library can be a portability issue and must be addressed by the frontend developers.

like image 142
Matthew Sanders Avatar answered Sep 28 '22 07:09

Matthew Sanders