python / python/cpython

`LOAD_FAST` is not always converted to `LOAD_FAST_BORROW` in a `basicblock`

オープン
#145,629 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core performance type-feature
主要言語
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Python/flowgraph.c の optimize_load_fast から始め、特に 3006–3011 行付近の分類を確認します。1 つの basicblock で構成された関数 f と、展開された関数 g に対して生成されるバイトコードを比較し、その後、影響を受ける LOAD_FAST 命令が、生成される制御フローを変更せずに LOAD_FAST_BORROW に変換されることを検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。