Compiler Output Language - LLVM IR vs C

To write a compiler, what are the advantages and disadvantages of using LLVM IR vs C for the target language? I know that both are used, and I think the final machine code would be similar if I used clang to compile C. So what else needs to be considered?

+8
compiler-construction code-generation llvm llvm-ir
source share
4 answers

I used LLVM IR for several compiler loops and worked with compilers that use C as the back end. One thing I discovered gave LLVM IR the advantage of being printed. It is difficult to draw completely poorly formed output without getting errors from the LLVM libraries.

In my opinion, it is also easier to maintain a close correlation between source code and IR for debugging.

In addition, you get all the cool LLVM command line tools for analyzing and processing IR, which emits your front end.

+7
source share

LLVM Benefits:

  • JIT - you can compile and run your code dynamically. Of course, this is possible using C (for example, using the built-in tcc ), but it is much less reliable and portable.
  • You can run your own optimization runs on the generated IR.
  • Reflection for free - checking LLVM with LLVM is much easier with checking generated code.
  • The LLVM library is not as large as most C compilers (apart from tcc , of course).

Disadvantages of LLVM:

  • The code is not portable, you need to modify it slightly depending on your purpose. There are several portable subsets of LLVMs, but this is still a tricky practice.
  • Runtime dependency on C ++ libraries can be a problem.
+2
source share

I doubt that you can implement proper debugging support for your language when targeting C.

+1
source share

Architectures and OSs for which there is no CLANG, obviously, or for which it is in an experimental state.

C is more widely accepted, but the LLVM IR allows you to load the LLVM engine. Not all paths to IR are equal.

0
source share

All Articles