coscup 2016 - llvm 由淺入淺

62
LLVM框架、由淺淺 浪打、Hydai

Upload: -

Post on 13-Jan-2017

1.497 views

Category:

Technology


0 download

TRANSCRIPT

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

AssemblerMSG: .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

PerlPython

JavaC

Ruby

Javascript

他們都造⾃自⼰己的輪⼦子 對應不同的機器

IA-32 AMD64

IA-64 Arch32

AArch64Sun 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

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

TableGen

llvm::SelectionDAGISel

LLVM IR 組合語⾔言

Original DAG

Combined DAG

Legalized DAG

Target Legalizing

DAG Combiner

Instruction Selection

TableGen

TableGen

組譯器

反組譯器

編譯器

除錯器

Target CPU

暫存器

指令格式

指令

代表A

代表B

代表C

代表D

TableGen

組譯器

反組譯器

編譯器

除錯器

Target CPU

TableGen

暫存器

指令格式

指令

Legalizing

LOAD LD

8bit 加法

除法

32bit 加法

⼀一堆 減法

Legal

Promote

Custom

Instruction Selecting

組合語⾔言

Legalized DAG

Instruction Selection

TableGen

Register AllocationSSA

虛擬暫存器 CPU暫存器分配 最佳化

Instruction Scheduling

原先指令順序 CPU 更適合的順續

順序 最佳化

llvm::SelectionDAGISel

LLVM IR 組合語⾔言

Original DAG

Combined DAG

Legalized DAG

Target Legalizing

DAG Combiner

Instruction Selection

TableGen

DemoA 40 years portal by LLVM, Linking 1976 and 2016

DemoA 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