2021-07-02 23:37:40 +01:00
|
|
|
#+TITLE: How to build a compiler with LLVM and MLIR
|
|
|
|
#+SEQ_TODO: TODO(t/!) NEXT(n/!) BLOCKED(b@/!) | DONE(d%) CANCELLED(c@/!) FAILED(f@/!)
|
|
|
|
#+TAGS: READER(r) MISC(m)
|
|
|
|
#+STARTUP: logdrawer logdone logreschedule indent content align constSI entitiespretty
|
|
|
|
|
2021-07-03 16:52:57 +01:00
|
|
|
* DONE Episode 1 - Introduction
|
2021-07-02 23:37:40 +01:00
|
|
|
** What is it all about?
|
|
|
|
- Create a programming lang
|
|
|
|
- Guide for contributors
|
|
|
|
- A LLVM/MLIR guide
|
|
|
|
** The Plan
|
|
|
|
- Git branches
|
|
|
|
- No live coding
|
|
|
|
- Feel free to contribute
|
|
|
|
** Serene and a bit of history
|
|
|
|
- Other Implementations
|
|
|
|
- Requirements
|
|
|
|
- C++ 14
|
|
|
|
- CMake
|
|
|
|
- Repository: https://devheroes.codes/Serene
|
|
|
|
- Website: lxsameer.com
|
|
|
|
Email: lxsameer@gnu.org
|
2021-07-10 17:52:53 +01:00
|
|
|
* DONE Episode 2 - Basic Setup
|
|
|
|
CLOSED: [2021-07-10 Sat 09:04]
|
2021-07-03 16:52:57 +01:00
|
|
|
** Installing Requirements
|
|
|
|
*** LLVM and Clang
|
|
|
|
- mlir-tblgen
|
|
|
|
*** ccache (optional)
|
|
|
|
** Building Serene and the =builder=
|
|
|
|
- git hooks
|
|
|
|
** Source tree structure
|
|
|
|
** =dev.org= resources and TODOs
|
2021-07-10 17:52:53 +01:00
|
|
|
* Episode 3 - Overview
|
|
|
|
** Generic Compiler
|
|
|
|
- [[https://www.cs.princeton.edu/~appel/modern/ml/whichver.html][Modern Compiler Implementation in ML: Basic Techniques]]
|
|
|
|
- [[https://suif.stanford.edu/dragonbook/][Compilers: Principles, Techniques, and Tools (The Dragon Book)]]
|
|
|
|
*** Common Steps
|
2021-07-10 18:43:33 +01:00
|
|
|
- Frontend
|
|
|
|
- Lexical analyzer (Lexer)
|
|
|
|
- Syntax analyzer (Parser)
|
|
|
|
- Semantic analyzer
|
2021-07-12 23:39:29 +01:00
|
|
|
- Middleend
|
2021-07-10 18:43:33 +01:00
|
|
|
- Intermediate code generation
|
|
|
|
- Code optimizer
|
|
|
|
- Backend
|
|
|
|
- Target code generation
|
2021-07-10 17:52:53 +01:00
|
|
|
** LLVM
|
|
|
|
[[llvm.org]]
|
|
|
|
*** Watch [[https://www.youtube.com/watch?v=J5xExRGaIIY][Introdution to LLVM]]
|
|
|
|
*** Quick overview
|
|
|
|
Deducted from https://www.aosabook.org/en/llvm.html
|
|
|
|
[[./imgs/llvm_dia.svg]]
|
2021-07-10 18:43:33 +01:00
|
|
|
- It's a set of libraries to create a compiler.
|
|
|
|
- Well engineered.
|
|
|
|
- we can focus only on the fronted of the compiler and what is
|
|
|
|
actually important to us and leave the tricky stuff to LLVM.
|
|
|
|
- LLVM IR enables us to use multiple languages together.
|
|
|
|
- It supports many targets.
|
|
|
|
- We can benefit from already made IR level optimizers.
|
|
|
|
- ....
|
|
|
|
|
2021-07-10 17:52:53 +01:00
|
|
|
** MLIR
|
|
|
|
[[mlir.llvm.org]]
|
|
|
|
[[./imgs/mlir_dia.svg]]
|
2021-07-10 18:43:33 +01:00
|
|
|
|
|
|
|
- With MLIR dialects provide higher level semantics than LLVM IR.
|
|
|
|
- It's easier to reason about higher level IR that is modeled after
|
|
|
|
the AST rather than a low level IR.
|
|
|
|
- We can use the pass infrastructure to efficiently process and transform the IR.
|
|
|
|
- With many ready to use dialects we can really focus on our language and us the other
|
|
|
|
dialect when ever necessary.
|
|
|
|
- ...
|
2021-07-10 17:52:53 +01:00
|
|
|
** Serene
|
|
|
|
*** A Compiler frontend
|
2021-07-10 18:43:33 +01:00
|
|
|
*** Flow
|
2021-07-10 17:52:53 +01:00
|
|
|
- =serenec= in parses the command lines args
|
|
|
|
- =reader= reads the input file and generates an =AST=
|
2021-07-10 18:43:33 +01:00
|
|
|
- =semantic analyzer= walks the =AST= and generates a new =AST= and rewrites
|
2021-07-10 17:52:53 +01:00
|
|
|
the necessary nodes.
|
|
|
|
- =slir= generator generates =slir= dialect code from =AST=.
|
|
|
|
- We lower =slir= to other dialects of the *MLIR* which we call the result =mlir=.
|
|
|
|
- Then, We lower everything to the =LLVMIR dialect= and call it =lir= (lowered IR).
|
|
|
|
- Finally we fully lower =lir= to =LLVM IR= and pass it to the object generator
|
|
|
|
to generate object files.
|
|
|
|
- Call the default =c compiler= to link the object files and generate the machine code.
|
2021-07-10 18:43:33 +01:00
|
|
|
* Episode 4 - The reader
|
|
|
|
- We have a hand written parser/lexer since lisp already has a structure.
|
|
|
|
- O(n)
|