Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Pythonのcopy-and-patch JITの実装を読む
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Arata
October 03, 2025
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pythonのcopy-and-patch JITの実装を読む
Arata
October 03, 2025
More Decks by Arata
See All by Arata
Black Hat USA 2026の面白かった発表を紹介
arata_nvm
1
22
Operating Systems: Three Easy Pieces, Chapter 49 / Sun's Network File System (NFS)
arata_nvm
0
23
デコンパイラ研究調査
arata_nvm
1
43
コンパイラ基盤を支える言語たち
arata_nvm
1
120
LLVMのDSL”TableGen”向け言語サーバーの開発
arata_nvm
0
46
5分でわかるPage-Fault Weird Machine
arata_nvm
2
450
eBPFを用いたAndroid向けデバッガ「eDBG」のx86_64 Linuxへの移植
arata_nvm
0
29
eBPFを使った動的解析手法
arata_nvm
1
840
カーネルハック実験の振り返り
arata_nvm
1
78
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
A Soul's Torment
seathinner
8
3.6k
The Cult of Friendly URLs
andyhume
79
7k
The Language of Interfaces
destraynor
162
27k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.1k
Music & Morning Musume
bryan
48
7.4k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
480
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.9k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
310
Transcript
copy-and-patch JITを書く Pythonのcopy-and-patch JITの 実装を読む 2025/10/03 コンパイラのコンパの部分 #01 @Arata
2025/10/02 コンパイラのコンパの部分 #01 @Arata 背景: Python 3.13での試験的なJITの導入 2
2025/10/02 コンパイラのコンパの部分 #01 @Arata Python 3.11: PEP659 - Specializing Adaptive
Interpreter • 汎用的な命令から特定の型に特化した命令へ実行時に書き換える ◦ e.g. x + 1: BINARY_OP(ADD) -> BINARY_OP_ADD_INT • データの型、よく実行される経路などのプロファイルも取る Python 3.12: DSLからのインタプリタ生成 • バイトコードのセマンティクスがより明確に Python 3.13ではこれらのプロファイラやDSLがJIT実装の基礎となった 背景: Faster CPythonの道のり 3
2025/10/02 コンパイラのコンパの部分 #01 @Arata copy-and-patchと呼ばれる仕組みを用いてJITを実装している Pythonビルド時: インタプリタのコードから各命令のステンシルを作成 1. インタプリタのコードから各命令の実行処理を抽出 2.
各命令の実行処理をそれぞれコンパイル 3. コンパイル結果からステンシル作成 Pythonコード実行時: ステンシルを使ってJITコンパイル 1. 各命令に対応するステンシルをコピー 2. ステンシル中の一部にパッチを当てる PythonのJIT実装 4
2025/10/02 コンパイラのコンパの部分 #01 @Arata Python/bytecodes.c (DSL)からPython/executor_cases.c.h (C)を生成 巨大なswitch-caseになる Pythonビルド時: インタープリタのコード生成
5 Python/bytecodes.c Python/executor_cases.c.h
2025/10/02 コンパイラのコンパの部分 #01 @Arata Python/executor_cases.c.hから正規表現で各命令の実装コードを抽出 Pythonビルド時: インタプリタからコード抽出 6 Tools/jit/_targets.py Python/execuor_cases.c.h
抽出対象
2025/10/02 コンパイラのコンパの部分 #01 @Arata Pythonビルド時: ステンシル作成 7 抽出した各命令の実装コードをテンプレートに挿入・コンパイル 実行時に決まる値はテンプレートで外部シンボルとして定義 コード挿入場所→
Tools/jit/template.c
2025/10/02 コンパイラのコンパの部分 #01 @Arata Pythonビルド時: ステンシルの最適化 8 ステンシルをアセンブリレベルで最適化 • 条件分岐命令でホットパスをフォールスルー側に配置
• 冗長な条件分岐命令の削除 Tools/jit/_optimizers.py
2025/10/02 コンパイラのコンパの部分 #01 @Arata Pythonビルド時: ステンシルのホール作成 9 実行時に決まる値=外部シンボルの参照には再配置エントリがあるはず それらの参照位置と参照シンボルを記録 Tools/jit/_stencils.py
2025/10/02 コンパイラのコンパの部分 #01 @Arata Pythonビルド時: ヘッダーファイルの作成 10 ここまででステンシルの情報が集まったので、JITコンパイルに使う 関数たちを生成
2025/10/02 コンパイラのコンパの部分 #01 @Arata Pythonビルド時: ヘッダーファイルの作成 11 ここまででステンシルの情報が集まったので、JITコンパイルに使う 関数たちを生成 ここではUNARY_NOTの例を紹介
ステンシルのアセンブリ UNARY_NOTの例 ステンシルのバイト列 code + data ステンシルのコピー&パッチ False,Trueへの参照を解決
2025/10/02 コンパイラのコンパの部分 #01 @Arata 結局何ができたのか? • 「あるバイトコード命令を実行する機械語」を生成する関数が全ての 命令について手に入った • これを命令ごとに呼び出せばバイトコードを機械語に変換できる
→ JITコンパイル達成 copy-and-patchの名前の由来は? • JITコンパイル時にステンシルをコピー&パッチするため • パッチは実行時に決まるオペランド、外部シンボルの参照等で必要 まとめ 12
2025/10/02 コンパイラのコンパの部分 #01 @Arata 余談: PyTorchのJITコンパイラ(TorchDynamo)も面白い 概要 • PEP 523で評価器を差し替えて、実行されたテンソル演算をトレース
• トレースから計算グラフを構築してコンパイル • 特殊化のため、コンパイルされたコードから参照する変数の値が変化 していないことを実行時に検証する仕組みがある 終わりに 13
2025/10/02 コンパイラのコンパの部分 #01 @Arata • https://peps.python.org/pep-0659/ • https://peps.python.org/pep-0744/ • https://github.com/python/cpython/tree/main/Tools/jit
• https://www.youtube.com/watch?v=HxSHIpEQRjs • https://docs.pytorch.org/docs/stable/torch.compiler_dynamo_ov erview.html 参考文献 14