Slide 1

Slide 1 text

Douglas Chen 不深不淺,帶你認識 LLVM 1

Slide 2

Slide 2 text

Douglas Chen Found LLVM in our Life Did you find it ! 2

Slide 3

Slide 3 text

3 Life is short and we can’t change it. But we can make it interesting. 陳鍵源 [Douglas Chen]

Slide 4

Slide 4 text

Why I am HERE ! ? Because I believe the best way to learn something is sharing. Not trying to teach you programming still, or show you how to use LLVM libraries. I just wanna introduce something new related to LLVM to you.

Slide 5

Slide 5 text

Agenda 5 1. Begin with a story ○ Before the story ○ The story 1. Free the Free ○ Self hosting? ○ What’s diff btn LLVM&GCC? 1. Compiler ○ Understand the Magic ○ Optimation ○ LLVM 1. Go, let’s find it (Products) ○ Apple’s Projects ○ Google’s Projects ○ Other Projects 4. Go, let’s find it (JIT) ○ What is JIT? ○ JVM/GraalVM ○ Virtual Machine/QEMU 4. Go, let’s find it (Web) ○ What is WebAssembly? ○ Project with WebAssembly 4. Q&A

Slide 6

Slide 6 text

Agenda 6 1.Begin with a story

Slide 7

Slide 7 text

Before the story 1. Begin with a story 7

Slide 8

Slide 8 text

What is compiler ? Compiler is a magic making source code to application.

Slide 9

Slide 9 text

What is compiler ? Compiler Source Code Application

Slide 10

Slide 10 text

The story 1. Begin with a story 10

Slide 11

Slide 11 text

?

Slide 12

Slide 12 text

Apple Computer & NeXT NeXTSTEP 1976 1985 1985 1988 Operating System 1989 Apple 1 Apple 2 Macintosh Apple 3 Lisa

Slide 13

Slide 13 text

NeXT’s NeXTSTEP OS

Slide 14

Slide 14 text

Return to Glory NeXTSTEP 1997 1997 2001 1998 Power Macintosh G3 1999 Power Macintosh G4 2000 PowerBook 2001 iPod 2002 iPod2 2003 iPod3 2004 iPod4 & Mini & Photo 2005 iPod5 iPod Shuffle iPod Nano Power Macintosh G5 (Intel) 2006 MacBook Pro 2007 Apple TV iPhone 2008 MacBook Air iPod Touch iPhone 3G

Slide 15

Slide 15 text

Complicated Ecosystem CPU OS Language ARMv6 macOS C ARMv7 iOS C++ ARMv8 watchOS Objective-C Intel x86 tvOS Swift PowerPC

Slide 16

Slide 16 text

Complicated Ecosystem Objective-C Swift C C++ ARMv6 ARMv7 ARMv8 Intel x86 PowerPC Xcode SDK Application Driver OS

Slide 17

Slide 17 text

Apple needs find a way out GCC is developed for solving real problems, it has no time to make a good everything perfect. FSF GCC master Apple’s branch all the mess ↓...↑... ... ...

Slide 18

Slide 18 text

Apple met LLVM LLVM Chris Lattner Twitter : https://twitter.com/clattner_llvm Website : http://nondot.org/sabre/

Slide 19

Slide 19 text

Apple met LLVM NeXTSTEP 1997 1997 2001 1998 Power Macintosh G3 1999 Power Macintosh G4 2000 PowerBook 2001 iPod 2002 iPod2 2003 iPod3 2004 iPod4 & Mini & Photo 2005 iPod5 iPod Shuffle iPod Nano Power Macintosh G5 (Intel) 2006 MacBook Pro 2007 Apple TV iPhone 2008 MacBook Air iPod Touch iPhone 3G 2000 2005 2007 Xcode 3.x 2011 Xcode 4.x 2013 Xcode 5.x 2011 gcc > llvm 10% 2013 gcc ≈ llvm (run-time performance)

Slide 20

Slide 20 text

Agenda 20 2.Free the Free

Slide 21

Slide 21 text

Why self hosting is important ! 2. Free the Free 21

Slide 22

Slide 22 text

Why self-hosting is important ! https://bbs.saraba1st.com/2b/thread-1375402-113-1.html

Slide 23

Slide 23 text

Why self hosting is important ! Open Source Project *.h *.cpp License

Slide 24

Slide 24 text

Why self hosting is important ! Open Source Project *.h *.cpp Free Free

Slide 25

Slide 25 text

Why self hosting is important ! GNU's Not Unix! RMS GNU Compiler Collection (GNU C Compiler) Richard M. Stallman

Slide 26

Slide 26 text

Why self hosting is important ! Free the Free Open Source Project *.h *.cpp

Slide 27

Slide 27 text

What’s the different between LLVM & GCC ? 2. Free the Free 27

Slide 28

Slide 28 text

Which compilers do your regularly use? c++ c https://www.jetbrains.com/lp/devecosystem-2019/cpp/

Slide 29

Slide 29 text

What is LLVM 1. LLVM is a Compiler 2. LLVM is a Compiler Infrastructure 3. LLVM is a series of Compiler Tools 4. LLVM is a Compiler Toolchain 5. LLVM is an open source C++ implementation

Slide 30

Slide 30 text

Pro's of GCC vs Clang: ● GCC supports languages that Clang does not aim to, such as Java, Ada, FORTRAN, Go, etc. ● GCC supports more targets than LLVM. ● GCC supports many language extensions. https://clang.llvm.org/comparison.html

Slide 31

Slide 31 text

Pro's of Clang vs GCC: ● The Clang ASTs and design are intended to be easily understandable by anyone. ● Clang is designed as an API from its inception, allowing it to be reused by source analysis tools, refactoring, IDEs (etc) as well as for code generation. GCC is built as a monolithic static compiler. ● Various GCC design decisions make it very difficult to reuse , ... . Clang has none of these problems. https://clang.llvm.org/comparison.html

Slide 32

Slide 32 text

Pro's of Clang vs GCC: ● Clang can serialize its AST out to disk and read it back into another program, which is useful for whole program analysis. GCC does not have this. ● Clang is much faster and uses far less memory than GCC. ● Clang has been designed from the start to provide extremely clear and concise diagnostics (error and warning messages). ● GCC is licensed under the GPL license. Clang uses a BSD license. https://clang.llvm.org/comparison.html

Slide 33

Slide 33 text

What I see the different like this ... GCC LLVM Clay LEGO https://seriousplaypro.com/wp-content/uploads/2017/06/LEGO-Idea-House-26.jpg

Slide 34

Slide 34 text

Agenda 34 3.Compiler

Slide 35

Slide 35 text

Understand the magic 3. Compiler 35

Slide 36

Slide 36 text

Computer Language stacks CPU Human Language Assembly Language Machine Code C / C++ VB / Swift / ObjectiveC Java / C# / VB / Python / JavaScript / Ruby / VB / Perl / Shell Low level languages Middle level languages High level languages ASIC Engineers ASIC / FPGA System C Verilog / VHDL Hardware Description languages Firmware Engineers Mobile App Engineers Web Tech Engineers Software Engineers Compiler Engineers ⭐

Slide 37

Slide 37 text

Compiler Compiler Source Code Exe Binary Front-End Optimizer Back-End Source Code Machine Code

Slide 38

Slide 38 text

Frontend 3. Compiler 38

Slide 39

Slide 39 text

What is compiler ? Compiler is a magic (making ...). 1 (token) 2 (token) 3 (token) 4 (token) 5 ... (tokens) Compiler is a magic making ... (S) (V) (C) (C) Lexical Analyzer Syntax Analyzer Semantic Analyzer AST (Abstract Syntax Tree) Source Code

Slide 40

Slide 40 text

Tokenization // min.c int min(int a, int b) { if (a < b) return a; return b; } int 'int' [StartOfLine] identifier 'min' [LeadingSpace] l_paren '(' int 'int' identifier 'a' [LeadingSpace] comma ',' int 'int' [LeadingSpace] identifier 'b' [LeadingSpace] r_paren ')' l_brace '{' [LeadingSpace] if 'if' [StartOfLine] [LeadingSpace] l_paren '(' [LeadingSpace] identifier 'a' less '<' [LeadingSpace] identifier 'b' [LeadingSpace] r_paren ')' return 'return' [StartOfLine] [LeadingSpace] $ clang -cc1 -dump-tokens min.c

Slide 41

Slide 41 text

AST Dump TranslationUnitDecl 0x2c8ce56b660 <> `-FunctionDecl 0x2c8ce56be18 line:2:5 min 'int (int, int)' |-ParmVarDecl 0x2c8ce56bcc0 col:13 used a 'int' |-ParmVarDecl 0x2c8ce56bd38 col:20 used b 'int' `-CompoundStmt 0x2c8ce56c0a0 |-IfStmt 0x2c8ce56c018 | |-<<>> | |-BinaryOperator 0x2c8ce56bf98 'int' '<' | | |-ImplicitCastExpr 0x2c8ce56bf68 'int' | | | `-DeclRefExpr 0x2c8ce56bf18 'int' lvalue ParmVar 0x2c8ce56bcc0 'a' 'int' | | `-ImplicitCastExpr 0x2c8ce56bf80 'int' | | `-DeclRefExpr 0x2c8ce56bf40 'int' lvalue ParmVar 0x2c8ce56bd38 'b' 'int' | |-ReturnStmt 0x2c8ce56c000 | | `-ImplicitCastExpr 0x2c8ce56bfe8 'int' | | `-DeclRefExpr 0x2c8ce56bfc0 'int' lvalue ParmVar 0x2c8ce56bcc0 'a' 'int' | `-<<>> `-ReturnStmt 0x2c8ce56c088 `-ImplicitCastExpr 0x2c8ce56c070 'int' `-DeclRefExpr 0x2c8ce56c048 'int' lvalue ParmVar 0x2c8ce56bd38 'b' 'int' // min.c int min(int a, int b) { if (a < b) return a; return b; } $ clang -cc1 -ast-dump min.c

Slide 42

Slide 42 text

CppNameLint cppnamelint utility v0.2.5 --------------------------------------------------- File = Detection.cpp Config = cppnamelint.toml Checked = 191 [File:0 | Func: 44 | Param: 37 | Var:110] Error = 7 [File:0 | Func: 0 | Param: 7 | Var: 0] --------------------------------------------------- <93, 5 > Variable : wayToSort (auto) <93, 25 > Variable : strA (string) <93, 38 > Variable : strB (string) <168, 5 > Variable : wayToSort (auto) <168, 25 > Variable : strA (string) <168, 38 > Variable : strB (string) <239, 9 > Variable : nLowerPCount (size_t)

Slide 43

Slide 43 text

Optimization 3. Compiler 43

Slide 44

Slide 44 text

Compiler ➊--> compiler ➋--> assembly code(.s) ➌--> assembler ➍--> object file (.o) ➎--> linker ➏--> binary file (.exe/.elf/.a) Compiler Source Code Executable Binary .c .s .o .elf ➊ cl gcc clang ➌ ml as llvm-as ➎ link ld lld Optimize Here Optimize Here Optimize Here ➋ ➍ ➏

Slide 45

Slide 45 text

Optimization ● SSA (Static Single Assignment) ● Constant Propagation ● Dead Code Elimination ● Branch Free

Slide 46

Slide 46 text

Optimization SSA (Static Single Assignment)

Slide 47

Slide 47 text

Optimization::SSA SSA (Static Single Assignment)

Slide 48

Slide 48 text

SSA (Static Single Assignment)

Slide 49

Slide 49 text

SSA (Static Single Assignment) 1 2 3

Slide 50

Slide 50 text

Optimization Constant Propagation & Dead Code Elimination

Slide 51

Slide 51 text

Constant Propagation & Dead Code Elimination gcc -O0

Slide 52

Slide 52 text

Constant Propagation & Dead Code Elimination Constant Propagation Dead Code Elimination Optitmized GetValue() = GetValue4()

Slide 53

Slide 53 text

Optimization Branch Free

Slide 54

Slide 54 text

How to tell Branchs

Slide 55

Slide 55 text

Instruction pipeline https://techdecoded.intel.io/resources/understanding-the-instruction-pipeline/

Slide 56

Slide 56 text

Branch Free Student Age Now ‘A' → ‘a’ ... ‘Z' → ‘z’ ... ‘5’ --> ‘5’ ...

Slide 57

Slide 57 text

Branch Free (tolower1) ‘A' → ‘a’

Slide 58

Slide 58 text

Branch Free (tolower2) ‘A' → ‘a’

Slide 59

Slide 59 text

Branch Free (CLANG -O3)

Slide 60

Slide 60 text

Backend 3. Compiler 60

Slide 61

Slide 61 text

Backend synthesis Machine Independent Code Improvement Target Code Generation Modified Assembly or Object Code AST or IR

Slide 62

Slide 62 text

LLVM 3. Compiler 62

Slide 63

Slide 63 text

Optimization Traditional Compiler

Slide 64

Slide 64 text

Traditional Compiler Front-End Optimizer Back-End Source Code Machine Code

Slide 65

Slide 65 text

LLVM Front-End IR Optimizer IR Back-End Source Code Machine Code Portable IR Transformed IR

Slide 66

Slide 66 text

Traditional Compiler V.S. Modern Compiler C C++ Java PHP Go Rust x86 ARM MIPS RISC-V PowerPC SPARC C C++ Java PHP Go Rust x86 ARM MIPS RISC-V PowerPC SPARC IR

Slide 67

Slide 67 text

One Day I create a new language ... Day Dream++ ddcc

Slide 68

Slide 68 text

One Day I create a new CPU/GPU/FPGA/ASIC ... Day Dream CPU

Slide 69

Slide 69 text

One Day I make a new OPTIMIZATION ... Day Dream Optimization

Slide 70

Slide 70 text

Agenda 70 4.Go, let’s find it

Slide 71

Slide 71 text

Where to find it ? 71 a.Apple’s Projects

Slide 72

Slide 72 text

Xcode

Slide 73

Slide 73 text

Xcode Xcode Version Release Date Compilers Xcode 2.0 2005/04/29 GCC Xcode 3.x 2007/10/25 GCC & LLVM-GCC Xcode 4.x 2011/03/09 LLVM-GCC Xcode 5.x 2013/06/11 LLVM https://xcodereleases.com/

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Where to find it ? 75 Google’s Projects

Slide 76

Slide 76 text

Google’s Products MLIR

Slide 77

Slide 77 text

MLIR: accelerating AI with open-source infrastructure https://github.com/tensorflow/mlir

Slide 78

Slide 78 text

You might don’t know about MLIR, but you MUST know this ...

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Google’s Products V8

Slide 81

Slide 81 text

V8: Emscripten is switching to the LLVM WebAssembly backend https://twitter.com/v8js/status/1145704863377981445

Slide 82

Slide 82 text

You might don’t know about V8, but you MUST know this ...

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Google’s Products gollvm

Slide 85

Slide 85 text

gollvm: an LLVM-based Go compiler https://go.googlesource.com/gollvm/

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

Where to find it ? 87 Other Projects

Slide 88

Slide 88 text

Other Products Rust Lang

Slide 89

Slide 89 text

Rust Lang

Slide 90

Slide 90 text

You might don’t know about Rust, but you MUST know this ...

Slide 91

Slide 91 text

Firefox Quantum is super fast, while still conserving memory

Slide 92

Slide 92 text

Microsoft to explore using Rust

Slide 93

Slide 93 text

Microsoft to explore using Rust

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Agenda 95 5. Go, let’s find it ( JIT)

Slide 96

Slide 96 text

What is JIT? 4. Go, let’s find it (Just-In-Time) 96

Slide 97

Slide 97 text

What is JIT? Why we need JIT? Develop FAST & Run FAST

Slide 98

Slide 98 text

What is JIT? ⇅ ⇅ ⇅ ⇅ ⇅ ⇅ CPU JIT Programming Language Interpreter Library FASTER SLOWER

Slide 99

Slide 99 text

How JIT works? #include #include #include #include // prints out the error and returns NULL. void* alloc_executable_memory(size_t size) { void* ptr = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (ptr == (void*)-1) { perror("mmap"); return NULL; } return ptr; } https://eli.thegreenplace.net/2013/11/05/how-to-jit-an- introduction

Slide 100

Slide 100 text

How JIT works? void emit_code_into_memory(unsigned char* m) { unsigned char code[] = { 0x48, 0x89, 0xf8, // mov %rdi, %rax 0x48, 0x83, 0xc0, 0x04, // add $4, %rax 0xc3 // ret }; memcpy(m, code, sizeof(code)); } const size_t SIZE = 1024; typedef long (*JittedFunc)(long); void run_from_rwx() { void* m = alloc_executable_memory(SIZE); emit_code_into_memory(m); JittedFunc func = m; int result = func(2); printf("result = %d\n", result); } long add4(long num) { return num + 4; } https://eli.thegreenplace.net/2013/11/05/how-to-jit-an- introduction

Slide 101

Slide 101 text

Project with LLVM JVM

Slide 102

Slide 102 text

Java Virtual Machine https://javatutorial.net/wp-content/uploads/2017/10/jvm-architecture-768x793.png

Slide 103

Slide 103 text

Project with JIT QEMU / Virtual Box/ VMware

Slide 104

Slide 104 text

QEMU (Quick Emulator) Hardware Host OS QEMU App2 App1 AppN Guest OS Emulated Hardware unmodified OS

Slide 105

Slide 105 text

QEMU (Quick Emulator) Hardware Host OS QEMU App2 App1 AppN Guest OS Emulated Hardware QEMU (Dynamic Binary Translation) TCG (Tiny Code Generator) Guest Code Host Code gen_intermediate_code() tb_gen_code() TB Buffer (Translated Block) tb_find() tcg/arm tcg/i386 tcg/mips tcg/riscv tcg/sparc

Slide 106

Slide 106 text

Agenda 106 6. Go, let’s find it ( Web Technology)

Slide 107

Slide 107 text

What is WebAssembly 4. Go, let’s find it (WebAssembly) 107

Slide 108

Slide 108 text

Project with WebAssembly JSLinux

Slide 109

Slide 109 text

JSLinux https://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/win2k.cfg&mem=192&graphic=1&w=1024&h=768 Run Windows 2000 on Web Browser

Slide 110

Slide 110 text

JSLinux Hardware Host OS Chrome.exe Minesweeper AppN Windows 2000 ASM.js / WebAssembly Hardware Host OS QEMU App1 Guest OS Emulated Hardware AppN App2 QEMU Emulated Hardware

Slide 111

Slide 111 text

Project with WebAssembly vim.wasm

Slide 112

Slide 112 text

vim.wasm https://rhysd.github.io/vim.wasm/

Slide 113

Slide 113 text

Project with WebAssembly Google Earth

Slide 114

Slide 114 text

Google Earth https://earth.google.com/web/

Slide 115

Slide 115 text

Project with WebAssembly Others

Slide 116

Slide 116 text

Project with WebAssembly https://webassembly.eu/

Slide 117

Slide 117 text

Agenda 117 7. Q&A

Slide 118

Slide 118 text

END HackMD Note http://bit.ly/369THkW Twitter https://twitter.com/dougpuob