Fireship · @fireship

LLVM in 100 Seconds

Uploaded May 23, 2022 · Length 2:36

LLVM in 100 Seconds

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.

  1. 0:00

    llvm a toolkit used to build and

  2. 0:03

    optimize compilers building a

  3. 0:04

    programming language from scratch is

  4. 0:06

    hard you have humans who want to write

  5. 0:08

    code in a nice simple syntax than

  6. 0:09

    machines that need to run it on all

  7. 0:11

    sorts of architectures llvm standardizes

  8. 0:14

    the extremely complex process of turning

  9. 0:16

    source code into machine code it was

  10. 0:18

    created in 2003 by grad student chris

  11. 0:21

    lattner at the university of illinois

  12. 0:23

    and today it's the magic behind clang

  13. 0:24

    for c and c plus as well as languages

  14. 0:27

    like rust swift julia and many others

  15. 0:30

    most importantly it represents high

  16. 0:31

    level source code in a language agnostic

  17. 0:34

    code called intermediate representation

  18. 0:36

    or ir this means vastly different

  19. 0:38

    languages like cuda and ruby produce the

  20. 0:40

    same ir allowing them to share tools for

  21. 0:42

    analysis and optimization before they're

  22. 0:45

    converted to machine code for a specific

  23. 0:46

    chip architecture a compiler can be

  24. 0:48

    broken down into three parts the front

  25. 0:50

    end parses the source code text and

  26. 0:52

    converts it into ir the middle end

  27. 0:54

    analyzes and optimizes this generated

  28. 0:57

    code and finally the backend converts

  29. 0:59

    the ir into native machine code to build

  30. 1:01

    your own programming language from

  31. 1:02

    scratch right now install llvm then

  32. 1:05

    create a c

  33. 1:06

    file now envision the programming

  34. 1:07

    language syntax of your dreams to make

  35. 1:09

    that high level code work you'll first

  36. 1:11

    need to write a lexer to scan the raw

  37. 1:13

    source code and break it into a

  38. 1:14

    collection of tokens like literals

  39. 1:16

    identifiers keywords operators and so on

  40. 1:19

    next we'll need to define an abstract

  41. 1:20

    syntax tree to represent the actual

  42. 1:22

    structure of the code and how different

  43. 1:24

    tokens relate to each other which is

  44. 1:25

    accomplished by giving each node its own

  45. 1:27

    class third we need a parser to loop

  46. 1:30

    over each token and build out the

  47. 1:31

    abstract syntax tree if you made it this

  48. 1:33

    far congratulations because the hard

  49. 1:35

    part is over now we can import a bunch

  50. 1:37

    of llvm primitives to generate the

  51. 1:39

    intermediate representation each type in

  52. 1:41

    the abstract syntax tree is given a

  53. 1:43

    method called cogen which always returns

  54. 1:45

    an llbm value object used to represent a

  55. 1:48

    single assignment register which is a

  56. 1:50

    variable for the compiler that can only

  57. 1:52

    be assigned once what's interesting

  58. 1:53

    about these ir primitives is that unlike

  59. 1:55

    assembly they're independent of any

  60. 1:57

    particular machine architecture and that

  61. 1:59

    dramatically simplifies things for