llvmwebassemblyllvm-irintermediate-code

How to convert LLVM IR to other Intermediate Representations?


I want to know how LLVM IR can be converted t other intermediate representations. I know that using llc -march=wasm32 -filetype=asm arithmetic.ll -o example.wat one can convert LLVM IR to the WASM IR but I want to know the behind the scene steps of it. I want to know the codebase maybe that does this conversion. If anyone can tell what are the general steps involved in such IR to IR conversion , it would be of great help.

I have parsed the LLVM IR using llvmlite but do not know what to do further.


Solution

  • An IR is basically a programming language. A little specialised, but it's a programming language. A program that produces an IR from another language is generally called "compiler", and the process is called "compilation".

    If you're lucky, the two IRs are closely related, and translation can be simpler/easier than compilation. But not in the case of IR→WASM.

    LLVM's WASM generator is a backend. (In LLVM parlance frontends produce IR from e.g. C and backends produce target languages from IR. WASM is a target language for IR.) Most backends are at least ten thousand lines long, some much more, which hints at the complexity of the task.