Fireship · @fireship
LLVM in 100 Seconds
Uploaded May 23, 2022 · Length 2:36
Summary
llvm explained in 100 seconds: a toolkit for building and optimizing compilers, standardizing source code to machine code.
- llvm
- compiler
- programming language
- toolkit
- intermediate representation
- ir
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
llvm a toolkit used to build and
- 0:03
optimize compilers building a
- 0:04
programming language from scratch is
- 0:06
hard you have humans who want to write
- 0:08
code in a nice simple syntax than
- 0:09
machines that need to run it on all
- 0:11
sorts of architectures llvm standardizes
- 0:14
the extremely complex process of turning
- 0:16
source code into machine code it was
- 0:18
created in 2003 by grad student chris
- 0:21
lattner at the university of illinois
- 0:23
and today it's the magic behind clang
- 0:24
for c and c plus as well as languages
- 0:27
like rust swift julia and many others
- 0:30
most importantly it represents high
- 0:31
level source code in a language agnostic
- 0:34
code called intermediate representation
- 0:36
or ir this means vastly different
- 0:38
languages like cuda and ruby produce the
- 0:40
same ir allowing them to share tools for
- 0:42
analysis and optimization before they're
- 0:45
converted to machine code for a specific
- 0:46
chip architecture a compiler can be
- 0:48
broken down into three parts the front
- 0:50
end parses the source code text and
- 0:52
converts it into ir the middle end
- 0:54
analyzes and optimizes this generated
- 0:57
code and finally the backend converts
- 0:59
the ir into native machine code to build
- 1:01
your own programming language from
- 1:02
scratch right now install llvm then
- 1:05
create a c
- 1:06
file now envision the programming
- 1:07
language syntax of your dreams to make
- 1:09
that high level code work you'll first
- 1:11
need to write a lexer to scan the raw
- 1:13
source code and break it into a
- 1:14
collection of tokens like literals
- 1:16
identifiers keywords operators and so on
- 1:19
next we'll need to define an abstract
- 1:20
syntax tree to represent the actual
- 1:22
structure of the code and how different
- 1:24
tokens relate to each other which is
- 1:25
accomplished by giving each node its own
- 1:27
class third we need a parser to loop
- 1:30
over each token and build out the
- 1:31
abstract syntax tree if you made it this
- 1:33
far congratulations because the hard
- 1:35
part is over now we can import a bunch
- 1:37
of llvm primitives to generate the
- 1:39
intermediate representation each type in
- 1:41
the abstract syntax tree is given a
- 1:43
method called cogen which always returns
- 1:45
an llbm value object used to represent a
- 1:48
single assignment register which is a
- 1:50
variable for the compiler that can only
- 1:52
be assigned once what's interesting
- 1:53
about these ir primitives is that unlike
- 1:55
assembly they're independent of any
- 1:57
particular machine architecture and that
- 1:59
dramatically simplifies things for


