`LOAD_FAST` is not always converted to `LOAD_FAST_BORROW` in a `basicblock`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
Originally addressed in Issue #144388, the bytecode compiler does not always convert a LOAD_FAST instruction into a LOAD_FAST_BORROW. For example, the following python code, which gets compiled into only one basicblock, has a LOAD_FAST when it could have a LOAD_FAST_BORROW:
from dis import dis
def f(a,b):
return a if a < b else b
dis(f)
Byte code
3 RESUME 0
4 LOAD_FAST_BORROW_LOAD_FAST_BORROW 1 (a, b)
COMPARE_OP 18 (bool(<))
POP_JUMP_IF_FALSE 3 (to L1)
NOT_TAKEN
LOAD_FAST_BORROW 0 (a)
RETURN_VALUE
L1: LOAD_FAST 1 (b)
RETURN_VALUE
In contrast, the following expanded version of f does convert all LOAD_FAST instructions into LOAD_FAST_BORROW and has three different basic blocks:
from dis import dis
def g(a,b):
if a < b:
return a
else:
return b
dis(g)
Byte code
3 RESUME 0
4 LOAD_FAST_BORROW_LOAD_FAST_BORROW 1 (a, b)
COMPARE_OP 18 (bool(<))
POP_JUMP_IF_FALSE 3 (to L1)
NOT_TAKEN
5 LOAD_FAST_BORROW 0 (a)
RETURN_VALUE
7 L1: LOAD_FAST_BORROW 1 (b)
RETURN_VALUE
The function of interest is optimize_load_fast in Python/flowgraph.c. I have tried to fix this bug but have not been able to yet. All I have found so far is that the LOAD_FAST instruction is misclassified as REF_UNCONSUMED here for the case of function f:
https://github.com/python/cpython/blob/149c4657507d17f78dd0938419a5a24ed71dc07e/Python/flowgraph.c#L3006-L3011
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-146503
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Python/flowgraph.c 中的 optimize_load_fast 开始,尤其关注第 3006–3011 行附近的分类逻辑。比较为单个 basicblock 的函数 f 和展开后的函数 g 生成的字节码,然后验证受影响的 LOAD_FAST 指令是否在不改变生成的控制流的情况下转换为 LOAD_FAST_BORROW。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100