8000 gh-132732: Automatically constant evaluate pure operations by Fidget-Spinner · Pull Request #132733 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132732: Automatically constant evaluate pure operations #132733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 58 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
1ffbb6b
Automatically constant evaluate pure operations
Fidget-Spinner Apr 19, 2025
691084d
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 19, 2025
b89e4dc
Fix tests
Fidget-Spinner Apr 19, 2025
0959918
Merge branch 'pure' of github.com:Fidget-Spinner/cpython into pure
Fidget-Spinner Apr 19, 2025
2541683
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Apr 24, 2025
d5b2208
Apply review suggestions
Fidget-Spinner Apr 25, 2025
71ced86
reduce diff
Fidget-Spinner Apr 25, 2025
a10d5a1
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 6, 2025
d22f165
Update pycore_opcode_metadata.h
Fidget-Spinner May 6, 2025
8ae38c7
Apply changes from code review
Fidget-Spinner May 10, 2025
712a810
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 10, 2025
dc2d922
Address review, add test
Fidget-Spinner May 10, 2025
f3f2a69
Add more tests
Fidget-Spinner May 12, 2025
53ce10f
Fix tests
Fidget-Spinner May 12, 2025
17634a8
Push fix noticed by Mark and Brandt
Fidget-Spinner May 19, 2025
4937c2f
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 19, 2025
ae08b79
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 21, 2025
c0c6600
remove pure from _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
Fidget-Spinner May 21, 2025
6bdd3f9
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 21, 2025
de8e170
use upstream changes for stackref
Fidget-Spinner May 21, 2025
c2f8e22
remove unused comment
Fidget-Spinner May 21, 2025
ac7e343
Address review
Fidget-Spinner May 22, 2025
05b822f
fix test
Fidget-Spinner May 22, 2025
b4c2e93
fix negative refcount
Fidget-Spinner May 23, 2025
d229f57
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 23, 2025
c4aae6c
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner May 27, 2025
8552182
Use `REPLACE_OPCODE_IF_EVALUTES_PURE`
Fidget-Spinner May 28, 2025
703dfc9
Fix test, move is_abstract to subclass attribute
Fidget-Spinner May 28, 2025
b278734
fix linter/mypy
Fidget-Spinner May 28, 2025
73a8b00
remove whitespace
Fidget-Spinner May 28, 2025
4116a31
Remove PyDict_Type
Fidget-Spinner May 28, 2025
548b67c
add bool type
Fidget-Spinner May 28, 2025
74a0208
reduce diff
Fidget-Spinner Jun 6, 2025
6a5dc12
Grab identifiers from REPLACE_OPCODE_IF_EVALUATES_PURE
Fidget-Spinner Jun 9, 2025
3896775
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 9, 2025
507c80a
Use replacer
Fidget-Spinner Jun 13, 2025
dc68b45
reduce diff
Fidget-Spinner Jun 13, 2025
41a271c
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 13, 2025
e88b71a
Update optimizer_generator.py
Fidget-Spinner Jun 13, 2025
866510f
Address review (add long functions to allowlist)
Fidget-Spinner Jun 13, 2025
01be0c6
fix mypy
Fidget-Spinner Jun 13, 2025
fce79a6
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 13, 2025
9bef4a4
revert changes for add,multiply,sub int
Fidget-Spinner Jun 13, 2025
1f76e2c
Fix tests
Fidget-Spinner Jun 13, 2025
efb9888
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 16, 2025
029c92b
Add deopt and error_if, and tests
Fidget-Spinner Jun 16, 2025
738d20e
remove is_abstract
Fidget-Spinner Jun 16, 2025
202af76
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 18, 2025
4c0c52c
fix tests
Fidget-Spinner Jun 18, 2025
e24e9a3
Fix test_opt
Fidget-Spinner Jun 18, 2025
f362866
Merge remote-tracking branch 'upstream/main' into pure
Fidget-Spinner Jun 20, 2025
d54a6f2
Update optimizer_bytecodes.c
Fidget-Spinner Jun 20, 2025
2644cb9
Address review
Fidget-Spinner Jun 20, 2025
634ed26
Add for _BINARY_OP
Fidget-Spinner Jun 20, 2025
0e9ce84
Address review
Fidget-Spinner Jun 27, 2025
f2fc3fc
reduce diff
Fidget-Spinner Jun 27, 2025
2408fb0
fix mypy
Fidget-Spinner Jun 27, 2025
e40cb06
fix wrong error message
Fidget-Spinner Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review (add long functions to allowlist)
  • Loading branch information
Fidget-Spinner committed Jun 13, 2025
commit 866510fe3d48442d5b467265cf1ec87013afc64c
6 changes: 3 additions & 3 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2225,9 +2225,11 @@ def test_validate_uop_unused_size_mismatch(self):
self.run_cases_test(input, input2, output)

def test_pure_uop_body_copied_in(self):
# Note: any non-escaping call works.
# In this case, we use _PyLong_Add.
input = """
pure op(OP, (foo -- res)) {
res = body(foo);
res = _PyLong_Add(foo);
}
"""
input2 = """
Expand All @@ -2248,15 +2250,14 @@ def test_pure_uop_body_copied_in(self):
_PyStackRef foo = sym_get_const_as_stackref(ctx, foo_sym);
_PyStackRef res_stackref;
/* Start of uop copied from bytecodes for constant evaluation */
res_stackref = body(foo);
res_stackref = _PyLong_Add(foo);
/* End of uop copied from bytecodes for constant evaluation */
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
stack_pointer[-1] = res;
break;
}
else {
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
}
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
break;
}
"""
Expand All @@ -2266,7 +2267,7 @@ def test_pure_uop_body_copied_in_complex(self):
input = """
pure op(OP, (foo -- res)) {
if (foo) {
res = body(foo);
res = _PyLong_Add(foo);
}
else {
res = 1;
Expand All @@ -2292,19 +2293,18 @@ def test_pure_uop_body_copied_in_complex(self):
_PyStackRef res_stackref;
/* Start of uop copied from bytecodes for constant evaluation */
if (foo) {
res_stackref = body(foo);
res_stackref = _PyLong_Add(foo);
}
else {
res_stackref = 1;
}
/* End of uop copied from bytecodes for constant evaluation */
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
stack_pointer[-1] = res;
break;
}
else {
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
}
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
break;
}
"""
Expand Down
6 changes: 0 additions & 6 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
"_PyLong_IsNegative",
"_PyLong_IsNonNegativeCompact",
"_PyLong_IsZero",
"_PyLong_Add",
"_PyLong_Multiply",
"_PyLong_Subtract",
"_PyManagedDictPointer_IsValues",
"_PyObject_GC_IS_SHARED",
"_PyObject_GC_IS_TRACKED",
Expand Down
5 changes: 2 additions & 3 deletions Tools/cases_generator/generators_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def __init__(self, out: CWriter, labels: dict[str, Label]):
}
self.out = out
self.labels = labels
self.is_abstract = False

def emit_to_with_replacement(
self,
Expand Down Expand Up @@ -512,7 +511,7 @@ def emit_SimpleStmt(
reachable = True
tkn = stmt.contents[-1]
try:
if stmt in uop.properties.escaping_calls and not self.is_abstract:
if stmt in uop.properties.escaping_calls:
escape = uop.properties.escaping_calls[stmt]
if escape.kills is not None:
self.stackref_kill(escape.kills, storage, True)
Expand Down Expand Up @@ -549,7 +548,7 @@ def emit_SimpleStmt(
self.out.emit(tkn)
else:
self.out.emit(tkn)
if stmt in uop.properties.escaping_calls and not self.is_abstract:
if stmt in uop.properties.escaping_calls:
self.emit_reload(storage)
return reachable, None, storage
except StackError as ex:
Expand Down
15 changes: 11 additions & 4 deletions Tools/cases_generator/optimizer_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class OptimizerEmitter(Emitter):
def __init__(self, out: CWriter, labels: dict[str, Label], original_uop: Uop, stack: Stack):
super().__init__(out, labels)
self._replacers["REPLACE_OPCODE_IF_EVALUATES_PURE"] = self.replace_opcode_if_evaluates_pure
self.is_abstract = True
self.original_uop = original_uop
self.stack = stack

Expand All @@ -169,6 +168,12 @@ def replace_opcode_if_evaluates_pure(
inst: Instruction | None,
) -> bool:
skip_to(tkn_iter, "SEMI")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're just going to throw away the arguments, why have them?
Either check the names match or, better still, don't require the inputs at all.


if self.original_uop.properties.escapes:
raise analysis_error(
f"REPLACE_OPCODE_IF_EVALUATES_PURE cannot be used with an escaping uop {self.original_uop.properties.escaping_calls}",
self.original_uop.body.open
)
emitter = OptimizerConstantEmitter(self.out, {}, self.original_uop, copy.deepcopy(self.stack))
emitter.emit("if (\n")
input_identifiers = replace_opcode_if_evaluates_pure_identifiers(uop)
Expand Down Expand Up @@ -256,11 +261,12 @@ def write_uop(
override: Uop | None,
uop: Uop,
out: CWriter,
stack: Stack,
debug: bool,
skip_inputs: bool,
) -> None:
locals: dict[str, Local] = {}
prototype = override if override else uop
stack = Stack()
try:
out.start_line()
if override:
Expand All @@ -285,8 +291,8 @@ def write_uop(
# No reference management of inputs needed.
for var in storage.inputs: # type: ignore[possibly-undefined]
var.in_local = False
_, storage = emitter.emit_tokens(override, storage, None, False)
out.start_line()
_, storage = emitter.emit_tokens(override, storage, inst=None, emit_braces=False)
storage.flush(out)
out.start_line()
else:
Expand Down Expand Up @@ -335,7 +341,8 @@ def generate_abstract_interpreter(
declare_variables(override, out, skip_inputs=False)
else:
declare_variables(uop, out, skip_inputs=True)
write_uop(override, uop, out, debug)
stack = Stack()
write_uop(override, uop, out, stack, debug, skip_inputs=(override is None))
out.start_line()
out.emit("break;\n")
out.emit("}")
Expand Down
41AD
Loading
0