gh-130704: Strength reduce LOAD_FAST{_LOAD_FAST} (#130708)

Optimize `LOAD_FAST` opcodes into faster versions that load borrowed references onto the operand stack when we can prove that the lifetime of the local outlives the lifetime of the temporary that is loaded onto the stack.
This commit is contained in:
mpage
2025-04-01 10:18:42 -07:00
committed by GitHub
parent e9556e1004
commit 053c285f6b
35 changed files with 1282 additions and 345 deletions

View File

@@ -580,6 +580,7 @@ NON_ESCAPING_FUNCTIONS = (
"PyStackRef_AsPyObjectNew",
"PyStackRef_FromPyObjectNewMortal",
"PyStackRef_AsPyObjectSteal",
"PyStackRef_Borrow",
"PyStackRef_CLEAR",
"PyStackRef_CLOSE_SPECIALIZED",
"PyStackRef_DUP",
@@ -595,6 +596,7 @@ NON_ESCAPING_FUNCTIONS = (
"PyStackRef_IsTrue",
"PyStackRef_IsFalse",
"PyStackRef_IsNull",
"PyStackRef_MakeHeapSafe",
"PyStackRef_None",
"PyStackRef_TYPE",
"PyStackRef_True",

View File

@@ -227,9 +227,10 @@ def generate_expansion_table(analysis: Analysis, out: CWriter) -> None:
expansions: list[tuple[str, str, int]] = [] # [(name, size, offset), ...]
if inst.is_super():
pieces = inst.name.split("_")
assert len(pieces) == 4, f"{inst.name} doesn't look like a super-instr"
name1 = "_".join(pieces[:2])
name2 = "_".join(pieces[2:])
assert len(pieces) % 2 == 0, f"{inst.name} doesn't look like a super-instr"
parts_per_piece = int(len(pieces) / 2)
name1 = "_".join(pieces[:parts_per_piece])
name2 = "_".join(pieces[parts_per_piece:])
assert name1 in analysis.instructions, f"{name1} doesn't match any instr"
assert name2 in analysis.instructions, f"{name2} doesn't match any instr"
instr1 = analysis.instructions[name1]