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

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 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
Fidg 8000 et-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
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
30 changes: 15 additions & 15 deletions Include/internal/pycore_opcode_metadata.h

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

4 changes: 4 additions & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {

#include "pycore_typedefs.h" // _PyInterpreterFrame
#include "pycore_uop_ids.h"
#include "pycore_stackref.h" // _PyStackRef
#include <stdbool.h>


Expand Down Expand Up @@ -259,7 +260,10 @@ typedef struct _JitOptContext {
extern bool _Py_uop_sym_is_null(JitOptSymbol *sym);
extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym);
extern bool _Py_uop_sym_is_const(JitOptContext *ctx, JitOptSymbol *sym);
extern bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptSymbol *sym);
extern PyObject *_Py_uop_sym_get_const(JitOptContext *ctx, JitOptSymbol *sym);
extern JitOptSymbol *_Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject *const_val);
extern _PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptSymbol *sym);
extern JitOptSymbol *_Py_uop_sym_new_unknown(JitOptContext *ctx);
extern JitOptSymbol *_Py_uop_sym_new_not_null(JitOptContext *ctx);
extern JitOptSymbol *_Py_uop_sym_new_type(
Expand Down
68 changes: 34 additions & 34 deletions Include/internal/pycore_uop_metadata.h

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

121 changes: 115 additions & 6 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,12 +1969,12 @@ def run_cases_test(self, input: str, input2: str, expected: str):

def test_overridden_abstract(self):
input = """
pure op(OP, (--)) {
op(OP, (--)) {
SPAM();
}
"""
input2 = """
pure op(OP, (--)) {
op(OP, (--)) {
eggs();
}
"""
Expand All @@ -1988,7 +1988,7 @@ def test_overridden_abstract(self):

def test_overridden_abstract_args(self):
input = """
pure op(OP, (arg1 -- out)) {
op(OP, (arg1 -- out)) {
out = SPAM(arg1);
}
op(OP2, (arg1 -- out)) {
Expand Down Expand Up @@ -2021,16 +2021,16 @@ def test_overridden_abstract_args(self):

def test_no_overridden_case(self):
input = """
pure op(OP, (arg1 -- out)) {
op(OP, (arg1 -- out)) {
out = SPAM(arg1);
}

pure op(OP2, (arg1 -- out)) {
op(OP2, (arg1 -- out)) {
}

"""
input2 = """
pure op(OP2, (arg1 -- out)) {
op(OP2, (arg1 -- out)) {
out = NULL;
}
"""
Expand Down Expand Up @@ -2250,5 +2250,114 @@ def test_validate_uop_unused_size_mismatch(self):
"Inputs must have equal sizes"):
self.run_cases_test(input, input2, output)

def test_pure_uop_body_copied_in(self):
input = """
pure op(OP, (foo -- res)) {
res = body(foo);
}
"""
input2 = """
op(OP, (foo -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(foo);
res = sym_new_known(ctx, foo);
}
"""
output = """
case OP: {
JitOptSymbol *foo;
JitOptSymbol *res;
foo = stack_pointer[-1];
if (
sym_is_safe_const(ctx, foo)
) {
JitOptSymbol *foo_sym = foo;
_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);
/* End of uop copied from bytecodes for constant evaluation */
res = sym_new_const_steal(ctx, PyStackRef_AsPyObjectSteal(res_stackref));
stack_pointer[-1] = res;
}
else {
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
}
break;
}
"""
self.run_cases_test(input, input2, output)

def test_pure_uop_body_copied_in_complex(self):
input = """
pure op(OP, (foo -- res)) {
if (foo) {
res = body(foo);
}
else {
res = 1;
}
}
"""
input2 = """
op(OP, (foo -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(foo);
res = sym_new_known(ctx, foo);
}
"""
output = """
case OP: {
JitOptSymbol *foo;
JitOptSymbol *res;
foo = stack_pointer[-1];
if (
sym_is_safe_const(ctx, foo)
) {
JitOptSymbol *foo_sym = foo;
_PyStackRef foo = sym_get_const_as_stackref(ctx, foo_sym);
_PyStackRef res_stackref;
/* Start of uop copied from bytecodes for constant evaluation */
if (foo) {
res_stackref = body(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;
}
else {
res = sym_new_known(ctx, foo);
stack_pointer[-1] = res;
}
break;
}
"""
self.run_cases_test(input, input2, output)

def test_pure_uop_reject_array_effects(self):
input = """
pure op(OP, (foo[2] -- res)) {
if (foo) {
res = body(foo);
}
else {
res = 1;
}
}
"""
input2 = """
op(OP, (foo[2] -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(foo[2]);
res = sym_new_unknown(ctx);
}
"""
output = """
"""
with self.assertRaisesRegex(AssertionError,
"Unsafe to convert a symbol to an array-like StackRef."):
self.run_cases_test(input, input2, output)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Automatically constant evaluate bytecode operations marked as pure in the JIT optimizer.
Loading
Loading
0