Search code examples
compiler-constructioncode-generationllvmllvm-ir

Compiler output language - LLVM IR vs C


For writing a compiler, what are the advantages and disadvantages of using LLVM IR vs C for a target language? I know both are used, and I imagine that the final machine code would be similar if I were to use clang to compile the C. So what are other things to consider?


Solution

  • I've used LLVM IR for a few compiler back ends and have worked with compilers that use C as a back end. One thing that I found that gave the LLVM IR an advantage is that it is typed. It is hard to make completely ill-formed output without getting errors from the LLVM libraries.

    It is also easier to keep a close correlation between the source code and the IR for debugging, in my opinion.

    Plus, you get all the cool LLVM command line tools to analyse and process the IR your front end emits.