LLVM框架、由淺⼊入淺
浪打、Hydai
Speakers
$ whoami
楊宗凡 — 浪打
‣ 成功⼤大學電機系四年級
‣ sonic.tw.tp (at) gmail.com
戴宏穎 — Hydai
‣ 清華⼤大學資⼯工碩⼀一年級
‣ z54981220 (at) gmail.com
Github Repos:
https://github.com/sonicyang/ws-frontend
https://github.com/sonicyang/llvm-z80
Code to Executable
$ gcc helloworld.c
#include <stdio.h>
int main(int argc, char* argv[]){
puts(“Hello World!”);
return 0;
}
In C
Hello World!
print “Hello World!”
In Python
Hello World!
魔法 我想學魔法!
The Magic
原始碼 組合語⾔言 機械碼
AssemblerCompiler
print “!@#$” mov d, msg$
mov c, 9
call 5
110100….
>./hw
機械碼
110100….
Hello World!
Compiler
原始碼
組合語⾔言
Lex Parse
Token
Stream
轉譯AST
bdos equ 0005H
start: mvi c,9
lxi d,msg$
call bdos
ret
msg$:db 'Hello, world!$'
end start
Intel 8080
Assembler
MSG: .ASCIIZ "Hello, world!"
LDX #0
LDA MSG,X
@LP: JSR $FFD2
INX
LDA MSG,X
BNE @LP
RTS
MOS 6502
8080
Assemble
6502
Assemble
110110.. 010111..
CP/M
Apple
Dos
Perl
Python
Java
C
Ruby
Javascript
他們都造⾃自⼰己的輪⼦子
對應不同的機器
IA-32 AMD64
IA-64 Arch32
AArch64
Sun Sparc
Compiler
原始碼
組合語⾔言
Lex Parse
Token
Stream
轉譯AST
Modern Compiler
$ export CFLAGS = “-O3”
Modern Wheel
原始碼
組合語⾔言
Lex
Yacc
AST
演算法
中介語⾔言
轉譯
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
Copy Propagation
Constant Propagation
Constant Folding
Dead Code Elimination(1)
Dead Code Elimination(2)
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
Python的故事
Python
組合語⾔言
Interpreter
C Code GCC
LLVM Framework
$ git clone http://llvm.org/git/llvm.git
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
IR
LLVM Framework -
Front End
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
IR
LLVM Framework -
Optimizer
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
IR
LLVM Framework -
Back End
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
模組化的開發
原始碼
Backend
組合語⾔言
OptimizerFrontend
LLVM IR
Front End
$ clang -S -emit-llvm main.c
以下使⽤用 Whitespace 語⾔言
Hello, world!
Hello, world(syntax hl)
Whitespace 簡介
• 只有三種 Tokens:
• Space 空⽩白(' ')
• Tabs 制表符(t)
• New lines 換⾏行(n)
• 其餘的字元全部都被當成註解
Front End to IR
程式碼 LLVM IR
Front End to IR
程式碼 LLVM IRParser LLVM API
• Module
• IRBuilder
• Function
• BasicBlock
• Instruction
LLVM IR
$ cat main.ll
LLVM IR
• RISC-style (Reduced Instruction Set Computing)
• ⼈人類可讀的
• 具備 SSA form (Static Single Assignment)
• 無限多的虛擬暫存器
• 任意位元⼤大⼩小
• 不改變⾏行為下 Transform IR ,來做最佳化。
C to IR
COSCUP 2016 - LLVM 由淺入淺
int	
  a	
  =	
  1;
int	
  c	
  =	
  a+b;
Optimizer
• LLVM 使⽤用 opt 做最佳化
• 每⼀一種類的分析跟轉換的 pass 都是 opt 的參數
• opt 會照順序幫你⼀一個⼀一個⾛走過這些 pass
• 每個 pass 會⾃自⼰己決定要不要做事
opt 剛才的 C Code
Backend
$ llc -march=z80 main.ll
Basic Ideas
LLVM IR 組合語⾔言
Basic Ideas
SelectionDAGLLVM IR 組合語⾔言
•Combining
•Legalizing
•Scheduling
•Register Allocation
What’s a DAG?
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
TableGen
組譯器
反組譯器
編譯器
除錯器
Target CPU
暫存器
指令格式
指令
代表A
代表B
代表C
代表D
TableGen
組譯器
反組譯器
編譯器
除錯器
Target CPU
Table
Gen
暫存器
指令格式
指令
Legalizing
LOAD LD
8bit
加法
除法
32bit
加法
⼀一堆
減法
Legal
Promote
Custom
Instruction Selecting
組合語⾔言
Legalized
DAG
Instruction
Selection
TableGe
n
Register Allocation
SSA
虛擬暫存器
CPU暫存器
分配
最佳化
Instruction Scheduling
原先指令順序 CPU
更適合的順續
順序
最佳化
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
Demo
A 40 years portal by LLVM, Linking 1976 and 2016
Demo
A 40 years portal by LLVM, Linking 1976 and 2016
WhiteSpace
LLVM IR 組合語⾔言
CP/M
LLVM
Zilog Z80
模組化的LLVM
原始碼
Backend
組合語⾔言
OptimizerFrontend
Reference
$ uname -a
1. Architecture for Next Generation GCC
ftp://gcc.gnu.org/pub/gcc/summit/2003/Architecture%20for%20a%20Next-Generation
%20GCC.pdf
2. Life of an Instruction in LLVM
http://eli.thegreenplace.net/2012/11/24/life-of-an-instruction-in-llvm
3. A deeper look into the LLVM code generator
http://eli.thegreenplace.net/2013/02/25/a-deeper-look-into-the-llvm-code-generator-
part-1
4. LLVM TableGen Documentation
http://llvm.org/docs/TableGen/index.html
5. LLVM TableGen Introduction
http://llvm.org/docs/TableGen/LangIntro.html
6. Independent Code Generator
http://llvm.org/docs/CodeGenerator.html
7. The Relationship between selectiondag and selectiondagisel
http://stackoverflow.com/questions/26814062/the-relationship-between-selectiondag-
and-selectiondagisel
8. How TableGen’s DAGISel Backend Works
https://github.com/draperlaboratory/fracture/wiki/How-TableGen's-DAGISel-Backend-
Works
9. ZASM - Z80 Assembler
http://k1.spdns.de/Develop/Projects/zasm/Documentation/
10. LLVM Z80 Backend
https://github.com/mostlyuseful/llvm-z80/network
11. Whitespace LLVM
https://github.com/Subv/Whitespace-LLVM
Q & A
$ man man
COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺

More Related Content

PDF
PDF
Design and Implementation of GCC Register Allocation
PDF
2ヶ月前にgitを始めた私からこれから始める皆さんへ
PDF
Linux binary Exploitation - Basic knowledge
PDF
JVM JIT-compiler overview @ JavaOne Moscow 2013
PPTX
PDF
Fpgaでの非同期信号の扱い方とvivadoによるサポート(公開用)
PPTX
Understand more about C
Design and Implementation of GCC Register Allocation
2ヶ月前にgitを始めた私からこれから始める皆さんへ
Linux binary Exploitation - Basic knowledge
JVM JIT-compiler overview @ JavaOne Moscow 2013
Fpgaでの非同期信号の扱い方とvivadoによるサポート(公開用)
Understand more about C

What's hot (20)

PPT
PDF
磯野ー!関数型言語やろうぜー!
PDF
How A Compiler Works: GNU Toolchain
PDF
Go言語によるwebアプリの作り方
PDF
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
PDF
PHP-FPM の子プロセス制御方法と設定をおさらいしよう
PDF
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
PDF
from Source to Binary: How GNU Toolchain Works
PDF
Signal Handling in Linux
PDF
Marp入門
PDF
Using eBPF to Measure the k8s Cluster Health
PDF
The Usage and Patterns of MagicOnion
PPTX
C#や.NET Frameworkがやっていること
PDF
チームメイトのためにdocstringを書こう! pyconjp2019
PPTX
Distributed Compiler Icecc
PPTX
Chapter 1: Introduction to Unix / Linux Kernel
PDF
Formation autour de git et git lab
PPTX
eBPF Basics
PDF
ネットワーク ゲームにおけるTCPとUDPの使い分け
PDF
What Can Compilers Do for Us?
磯野ー!関数型言語やろうぜー!
How A Compiler Works: GNU Toolchain
Go言語によるwebアプリの作り方
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
PHP-FPM の子プロセス制御方法と設定をおさらいしよう
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
from Source to Binary: How GNU Toolchain Works
Signal Handling in Linux
Marp入門
Using eBPF to Measure the k8s Cluster Health
The Usage and Patterns of MagicOnion
C#や.NET Frameworkがやっていること
チームメイトのためにdocstringを書こう! pyconjp2019
Distributed Compiler Icecc
Chapter 1: Introduction to Unix / Linux Kernel
Formation autour de git et git lab
eBPF Basics
ネットワーク ゲームにおけるTCPとUDPの使い分け
What Can Compilers Do for Us?
Ad

Similar to COSCUP 2016 - LLVM 由淺入淺 (20)

PDF
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
PDF
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
PPTX
Hcsm lect-20120913
PDF
模块一-Go语言特性.pdf
PPTX
網路技術心得分享
PPTX
为啥别读HotSpot VM的源码(2012-03-03)
PDF
⼤語⾔模型 LLM 應⽤開發入⾨
PDF
Binary exploitation - AIS3
PPTX
Simple tech-talk
PDF
Tcfsh bootcamp day2
PDF
Python 于 webgame 的应用
PPTX
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
PDF
Rootkit 101
PPTX
Baidu LSP and DISQL for Log Analysis
PPTX
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
PDF
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
PDF
Build desktop app_by_xulrunner
PPTX
Shell,信号量以及java进程的退出
PDF
20240921 - HITCON 社群活動 - 《HITCON CTF 甘苦談》
PDF
Android C Library: Bionic 成長計畫
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
Hcsm lect-20120913
模块一-Go语言特性.pdf
網路技術心得分享
为啥别读HotSpot VM的源码(2012-03-03)
⼤語⾔模型 LLM 應⽤開發入⾨
Binary exploitation - AIS3
Simple tech-talk
Tcfsh bootcamp day2
Python 于 webgame 的应用
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Rootkit 101
Baidu LSP and DISQL for Log Analysis
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
Build desktop app_by_xulrunner
Shell,信号量以及java进程的退出
20240921 - HITCON 社群活動 - 《HITCON CTF 甘苦談》
Android C Library: Bionic 成長計畫
Ad

More from 宗凡 楊 (7)

PDF
uRock @ Jserv Course Final
PDF
uRock @ SITCON 2015
PDF
C4Labs PCB Miller Project
PDF
Lambda's CNC @FabLab 10/27/14
PDF
Cnc fablab
PDF
Cnc fablab
PDF
Lambda's CNC @FabLab 10/27/14
uRock @ Jserv Course Final
uRock @ SITCON 2015
C4Labs PCB Miller Project
Lambda's CNC @FabLab 10/27/14
Cnc fablab
Cnc fablab
Lambda's CNC @FabLab 10/27/14

COSCUP 2016 - LLVM 由淺入淺