LLVM

Table of Contents

Used by clang, Rust, Swift, Julia, Ruby, CUDA, and others.

1. Structure

1.1. Frontend

  • It provides tools for converting custom abstract syntax tree(AST) into intermediate representation(IR)
  • codegen()

1.2. Middleend

  • It analyzes IR and optimize it.

1.3. Backend

  • It convert IR into the machine code.

2. Intermediate Representation

  • A statement must contain one of the op codes.
  • No semicolons!
  • Variables are immutable
  • The inline dereferencing is not possible.
  • No "unsigned" type exists, because there's no distintion.
  • A basic block must end either with a terminating operation: br or ret.

2.1. Types

  • i1 boolean, flag
  • iN integer, uN unsigned integer. N can be any number between 1 and 232 - 1.
  • <ty>* pointer, ptr can be used to automatically infer the pointer type

2.2. Instructions

Global variables and functions start with @. Local variables and unnamed variables start with %.

  • <ty> *alloca <ty>
  • define <ty> <fn>(<ty> <var>, ...) { ... } define function
  • declare <ty> <fn>(<ty>, ...) external functions
  • alloca <ty>[, <ty> <NumElements>][, align <alignment>]
  • load <ty>, <ty*> <val> load from memory
  • store <ty> <val>, <ty*> <ptr>
  • add <wrap> <ty> <val1>, <val2>
    • sub mul
  • icmp <cmp>, <ty> <val1>, <val2>
  • br i1 <cond>, label <val1>, label <val2>, br label <val>
  • call <ty> <fn>(...)
  • ret <ty> <val>
  • getelementptr <ty>, <ty> *<ptr>, <ty> <pos>, <ty> <offset>

Error handling using libunwind

  • resume <ty> <val>
  • landingpad <ty> <val> [cleanup] [catch <ty> <val>] [filter <ty> <val>]

2.3. Tokens

  • If the name is a number they must be in order starting from 0 which representing the entry point.
  • @var global collapsed:: true
    • Function name must start with @?
  • %var local variable
  • label: label
  • #attr attribute of a function
    • The attributes are specified at the end of the .ll file, and it is assigned to a function by adding it at the end of the function declaration: define <ty> <fn>(...) #attr { ... }
  • !flag flag

3. libunwind

Unwind (stack tracing) library developed as part of LLVM project.

In general error is handled in three main pathways:

  • explicit error invocation within the source code -> unwind stack
  • runtime error (if runtime exists) triggers error -> unwind stack
  • os error -> signal received by the process -> either the signal handler triggers unwind, or OS perform default operation (core dump)

4. Reference

Author: Jeemin Kim

Created: 2026-07-16 Thu 21:34