[llvm-dev] What was the IR made for precisely?

Renato Golin via llvm-dev llvm-dev at lists.llvm.org
Thu Oct 27 08:30:42 PDT 2016


On 27 October 2016 at 16:18, David Chisnall via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> LLVM IR is intended as a compiler intermediate representation.  It aims to be able to express anything that arbitrary source languages might want and anything that target architectures may implement, in a way that is not closely tied to either specific source languages or target architectures.  It doesn’t entirely succeed in this, but it comes a lot closer than C to this goal (not surprising, as this was never a goal of C).

Yeah, "LLVM IR is a compiler IR, not a target-independent language".

Lowering your language to LLVM IR is a great way to reduce the cost of
target-specific knowledge in your front-end 10-fold. But there's still
the remaining 1/10th that you need to do.

Exception handling is one obviously complicated one, but there's also:

* Procedure-call standard differences: most arguments will marshal
correctly in LLVM IR, but complex structures sometimes need to be
lowered to arrays, or moved around.
* Type sizes: The LLVM lowering is smart enough (in most cases) to
split too-large values (64-bit int on 32-bit machine), but that's
error prone and inefficient. You should emit the best types per
architecture.
* Your language's ABI: C++ has an ABI which defines the layout of
classes and objects that can be different between targets. You may
want to make special concessions due to speed and code-size concerns.

Pretty much everything else should be dealt with by the IR passes and lowering.

But the biggest benefit of lowering to LLVM IR is safety.

Lowering to C will actually be lowering to "strings", not proper C.
Unless you write your own C validation engine (ie. a compiler), you
can't know if you have lowered to valid C or not.

The IR libraries (IRBuilder et al) give you that for free. This
immensely simplifies the task of lowering your language, and you can
focus on what really matters: the semantics of your language.

Hope that helps.

--renato


More information about the llvm-dev mailing list