Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ELVMでLLVM IR
Search
Akira Moroo
March 19, 2017
Programming
0
510
ELVMでLLVM IR
ELVMのLLVM IRバックエンドを作って遊んだときのスライドです.
Akira Moroo
March 19, 2017
Tweet
Share
More Decks by Akira Moroo
See All by Akira Moroo
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
37
Exploring x86 MSR Space
retrage
0
1.4k
LLMでバイナリ解析支援
retrage
0
200
GitHub ActionsでDevSecOpsごっこ
retrage
0
68
Practical Rust (Hypervisor) Firmware
retrage
3
1.7k
Bypassing UEFI Secure Boot with Thin-Hypervisor
retrage
0
1.2k
Porting Linux to Nabla Containers
retrage
0
1.2k
Network Boot from Bell Labs
retrage
2
1.7k
Unikernelで始める自作OS/OS Development with Unikernel
retrage
1
610
Other Decks in Programming
See All in Programming
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
330
Developing static sites with Ruby
okuramasafumi
0
340
Patterns of Patterns
denyspoltorak
0
400
ThorVG Viewer In VS Code
nors
0
490
Pythonではじめるオープンデータ分析〜書籍の紹介と書籍で紹介しきれなかった事例の紹介〜
welliving
3
680
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
120
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
150
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
530
Cap'n Webについて
yusukebe
0
160
re:Invent 2025 のイケてるサービスを紹介する
maroon1st
0
160
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
440
Featured
See All Featured
It's Worth the Effort
3n
187
29k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
30 Presentation Tips
portentint
PRO
1
180
Discover your Explorer Soul
emna__ayadi
2
1k
Paper Plane (Part 1)
katiecoart
PRO
0
2.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Building Flexible Design Systems
yeseniaperezcruz
330
39k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
The Language of Interfaces
destraynor
162
26k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
Transcript
ELVMでLLVM IR 2017/03/19 @retrage
動機 • LLVMで遊んでみたい • フロントエンドを作ったりはしたくない • clang –emit-llvmで出⼒させたコードで遊ぶのも⾯⽩ くない •
LLVMを始めよう! 〜 LLVM IRの基礎はclangが 教えてくれた・Brainf**kコンパイラを作ってみ よう 〜 • http://itchyny.hatenablog.com/entry/2017/02/27/ 100000 • BrainF**kからLLVM IRを吐くようなものを作るとい う話
LLVM • コンパイラ基盤 • LLVM IRという中間表現を使う フロントエンド LLVM IR バックエンド
最適化 コード ターゲット clang opt llc • opt: LLVM IRに対し最適化 • lli:LLVM RIをVMで実⾏
ELVM • “LLVMのパロディ” フロントエンド ELVM IR バックエンド コード ターゲット 8cc
elc • ターゲット • BrainF**k, WhiteSpace, C++14(compile time)など • 環境依存でない中間表現ELVM IR
ELVMでLLVM IR • ELVMのLLVM IRバックエンドを作ろう • Q. 何かいいことor新しいことはある? • A.⼀切ない
• C ->ELVM IR -> C -> LLVM IRと同じ • LLVMの恩恵を感じてみたい • ELVMのバックエンドを作るのは⽐較的簡単
LLVM IRについて • 例: • unsigned int a = 5;
• a += 5; • %a = alloca i32 • store i32 5, i32* %a • %1 = load i32* %a • %2 = add i32 %1, 5 • store i32 %2, i32* @a
C->ELVM IR • ELVMでは8ccをフロントエンドにELVM IRを出⼒ $ cat hello.c #include <stdio.h>
int main() { const char* p = "Hello, world!\n"; for (; *p; p++) putchar(*p); return 0; } $ out/8cc -S -I. -Ilibc -Iout hello.c $ wc –l hello.s 12554 ../elvm/hello.s
ELVM IR -> LLVM IR $ ./elvm/out/elc –ll hello.c.eir >
hello.c.eir.ll $ wc –l hello.c.eir.ll 33882 hello.c.eir.ll $ clang –S –O0 –emit-llvm hello.c $wc –l hello.ll 45 hello.ll $ time lli hello.c.eir.ll Hello, world! real 0m0.339s user 0m0.300s sys 0m0.037s $ time lli hello.ll Hello, world! real 0m0.009s user 0m0.009s sys 0m0.000s
最適化させてみる $ wc –l hello.c.eir.ll 33882 hello.c.eir.ll $ time lli
hello.c.eir.ll Hello, world! real 0m0.339s user 0m0.300s sys 0m0.037s $ opt -O3 -S hello.c.eir.ll –o hello.c.eir.ll.opt 15865 hello.c.eir.ll.opt $ time lli hello.c.eir.ll.opt Hello, world! real 0m0.198s user 0m0.163s sys 0m0.032s
まとめ • LLVM IRのことが少しわかったような気がする • まだまだ使っていない仕様がたくさんある • ELVM IRへの変換によるオーバーヘッドはoptで ある程度削減できるっぽい
• LLVMのELVM IR向けバックエンドが実装されている ようなので,ELVM IR <-> LLVM IRできるかも • コンパイラでの最適化について良い本を知って いたら教えてください
参考⽂献 • http://shinh.skr.jp/slide/elvm/000.html • http://itchyny.hatenablog.com/entry/2017/02/27/ 100000 • http://llvm.org/docs/LangRef.html • http://llvm.org/docs/CommandGuide/opt.html
LLVM IRバックエンドつらい • 無名の識別⼦は%から始まる番号になっている • 少しでもずれたりすると怒られる • ELVM IRのsrcがREG/IMMで命令が結構変わる •
デバッグむずい • C->ELVM IRだとすぐにコード量が膨れあがる • lliを使ったいいデバッグ⽅法を知りたい