pypy-style constant promotion to pool
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Proposal:
We should have a JIT constant pool and a JIT directive in the optimizer_bytecodes.c to promote certain values to constants. This would unlock significantly more future optimizations.
Steps needed:
- Add a constant pool to the executor object, so we can keep promoted constant alive. We already need this to unlock more constant propagation opportunities. The pool would be a
PyListObjectliving in the vmdata field here https://github.com/python/cpython/blob/main/Include/internal/pycore_optimizer.h#L43 . - Introduce a new optimizer macro:
PROMOTE_TO_CONST_POOLand associated functionpromote_to_const_pool(ctx, obj)inoptimizer_symbols.c. This macro will store any value it receives as a constant in the constant pool. This allows us to promote contants we see to the constant pool. This takes inspiration from RPython'shint(obj, promote=True)
What optimizations does this unlock?
- More constant propagation: we can now constant propagate not just immortal stuff but non-immortal things as well.
- Refcount removal. Anything in the the pool can use the cheaper
_LOAD_CONST_INLINE_BORROW. Which will automatically benefit this work https://github.com/python/cpython/issues/134584 and vice versa. Ie, we automatically make it cheaper to load and store these values! - On GIL/FT, we can promote method objects to the pool as well, which will allow us to perform more optimizations on them, such as LOAD_ATTR_METHOD folding.
We should also perhaps explore promoting globals to pool constants. Though that's a little risky, since we don't want to hold large objects around longer than needed.
The first PR should implement the first two steps and optimization 1. Optimization 2 should be a separate PR.
Linked PRs
- gh-140968
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從 Include/internal/pycore_optimizer.h 和 executor 的 vmdata 欄位開始,接著檢查 optimizer_bytecodes.c 和 optimizer_symbols.c。第一個 PR 在 executor 具有 PyListObject 常數池、PROMOTE_TO_CONST_POOL 和 promote_to_const_pool 存在,且非 immortal 值獲得常數傳播時完成;移除 refcount 明確是獨立的工作。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c
- 領域
- compilers
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100