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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Akira Moroo
March 19, 2017
Programming
0
520
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
99
Exploring x86 MSR Space
retrage
0
1.4k
LLMでバイナリ解析支援
retrage
0
210
GitHub ActionsでDevSecOpsごっこ
retrage
0
81
Practical Rust (Hypervisor) Firmware
retrage
3
1.8k
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
630
Other Decks in Programming
See All in Programming
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
Oxlintはいいぞ
yug1224
5
1.3k
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
430
Fragmented Architectures
denyspoltorak
0
150
ぼくの開発環境2026
yuzneri
0
200
Implementation Patterns
denyspoltorak
0
280
高速開発のためのコード整理術
sutetotanuki
1
400
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
AgentCoreとHuman in the Loop
har1101
5
230
Architectural Extensions
denyspoltorak
0
280
Featured
See All Featured
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
54
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
130
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3k
The Spectacular Lies of Maps
axbom
PRO
1
520
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.3k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
240
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を使ったいいデバッグ⽅法を知りたい