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 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 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
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
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
109 changes: 109 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
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.
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ dummy_func(
DEOPT_IF(!res);
}

pure op(_BINARY_OP_EXTEND, (descr/4, left, right -- res)) {
pure op(_BINARY_OP_EXTEND, (descr/4, left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(INLINE_CACHE_ENTRIES_BINARY_OP == 5);
Expand Down
8 changes: 8 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "pycore_function.h"
#include "pycore_uop_ids.h"
#include "pycore_range.h"
#include "pycore_unicodeobject.h"
#include "pycore_ceval.h"

#include <stdarg.h>
#include <stdbool.h>
Expand Down Expand Up @@ -317,7 +319,10 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
#define sym_is_not_null _Py_uop_sym_is_not_null
#define sym_is_const _Py_uop_sym_is_const
#define sym_is_safe_const _Py_uop_sym_is_safe_const
#define sym_get_const _Py_uop_sym_get_const
#define sym_new_const_steal _Py_uop_sym_new_const_steal
#define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref
#define sym_new_unknown _Py_uop_sym_new_unknown
#define sym_new_not_null _Py_uop_sym_new_not_null
#define sym_new_type _Py_uop_sym_new_type
Expand All @@ -343,6 +348,8 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_is_immortal _Py_uop_sym_is_immortal
#define sym_new_truthiness _Py_uop_sym_new_truthiness

#define JUMP_TO_LABEL(label) goto label;

static int
optimize_to_bool(
_PyUOpInstruction *this_instr,
Expand Down Expand Up @@ -529,6 +536,7 @@ optimize_uops(
}
return trace_len;

pop_2_error:
error:
DPRINTF(3, "\n");
DPRINTF(1, "Encountered error in abstract interpreter\n");
Expand Down
127 changes: 15 additions & 112 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,136 +222,38 @@ dummy_func(void) {
}

op(_BINARY_OP_ADD_INT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = _PyLong_Add((PyLongObject *)sym_get_const(ctx, left),
(PyLongObject *)sym_get_const(ctx, right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_type(ctx, &PyLong_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyLong_Type);
}

op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)sym_get_const(ctx, left),
(PyLongObject *)sym_get_const(ctx, right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_type(ctx, &PyLong_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyLong_Type);
}

op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)sym_get_const(ctx, left),
(PyLongObject *)sym_get_const(ctx, right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_type(ctx, &PyLong_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyLong_Type);
}

op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) +
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
res = sym_new_type(ctx, &PyFloat_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyFloat_Type);
}

op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) -
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
res = sym_new_type(ctx, &PyFloat_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyFloat_Type);
}

op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) *
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
}
else {
res = sym_new_type(ctx, &PyFloat_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyFloat_Type);
}

op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
assert(PyUnicode_CheckExact(sym_get_const(ctx, left)));
assert(PyUnicode_CheckExact(sym_get_const(ctx, right)));
PyObject *temp = PyUnicode_Concat(sym_get_const(ctx, left), sym_get_const(ctx, right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Py_DECREF(temp);
}
else {
res = sym_new_type(ctx, &PyUnicode_Type);
}
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
res = sym_new_type(ctx, &PyUnicode_Type);
}

op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right -- )) {
Expand Down Expand Up @@ -463,6 +365,7 @@ dummy_func(void) {
}

op(_UNARY_NOT, (value -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(value);
sym_set_type(value, &PyBool_Type);
res = sym_new_truthiness(ctx, value, false);
}
Expand Down
Loading
Loading