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 1 commit
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
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
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
Apply review suggestions
  • Loading branch information
Fidget-Spinner committed Apr 25, 2025
commit d5b2208a531424b22d53b79230eebe77159df506
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

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

2 changes: 2 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 @@ -257,6 +258,7 @@ extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym);
extern bool _Py_uop_sym_is_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
26 changes: 26 additions & 0 deletions Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ _PyStackRef_FromPyObjectImmortal(PyObject *obj, const char *filename, int linenu
}
#define PyStackRef_FromPyObjectImmortal(obj) _PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj), __FILE__, __LINE__)

static inline _PyStackRef
_PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj, const char *filename, int linenumber)
{
return _Py_stackref_create(obj, filename, linenumber);
}
#define PyStackRef_FromPyObjectImmortalUnchecked(obj) _PyStackRef_FromPyObjectImmortalUnchecked(_PyObject_CAST(obj), __FILE__, __LINE__)



static inline void
_PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber)
{
Expand Down Expand Up @@ -324,6 +333,17 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
}
#define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))

static inline _PyStackRef
PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj)
{
// Make sure we don't take an already tagged value.
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
assert(obj != NULL);
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_DEFERRED };
}
#define PyStackRef_FromPyObjectImmortalUnchecked(obj) PyStackRef_FromPyObjectImmortalUnchecked(_PyObject_CAST(obj))


#define PyStackRef_CLOSE(REF) \
do { \
_PyStackRef _close_tmp = (REF); \
Expand Down Expand Up @@ -535,6 +555,12 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_REFCNT};
}

static inline _PyStackRef
PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj)
{
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_REFCNT};
}

/* WARNING: This macro evaluates its argument more than once */
#ifdef _WIN32
#define PyStackRef_DUP(REF) \
Expand Down
10 changes: 5 additions & 5 8000 deletions Include/internal/pycore_uop_metadata.h

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

10 changes: 5 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ dummy_func(
PyStackRef_CLOSE(receiver);
}

pure inst(UNARY_NEGATIVE, (value -- res)) {
inst(UNARY_NEGATIVE, (value -- res)) {
PyObject *res_o = PyNumber_Negative(PyStackRef_AsPyObjectBorrow(value));
PyStackRef_CLOSE(value);
ERROR_IF(res_o == NULL, error);
Expand Down Expand Up @@ -573,7 +573,7 @@ dummy_func(
_GUARD_TYPE_VERSION +
_REPLACE_WITH_TRUE;

pure inst(UNARY_INVERT, (value -- res)) {
inst(UNARY_INVERT, (value -- res)) {
PyObject *res_o = PyNumber_Invert(PyStackRef_AsPyObjectBorrow(value));
PyStackRef_CLOSE(value);
ERROR_IF(res_o == NULL, error);
Expand Down Expand Up @@ -669,7 +669,7 @@ dummy_func(
EXIT_IF(!PyFloat_CheckExact(value_o));
}

op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
pure op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
Expand All @@ -684,7 +684,7 @@ dummy_func(
ERROR_IF(PyStackRef_IsNull(res), error);
}

op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
pure op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
Expand All @@ -699,7 +699,7 @@ dummy_func(
ERROR_IF(PyStackRef_IsNull(res), error);
}

op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
pure op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
Expand Down
2 changes: 2 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#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 @@ -323,6 +324,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_is_const _Py_uop_sym_is_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 Down
66 changes: 15 additions & 51 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,63 +234,27 @@ dummy_func(void) {
}

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);
}
// We need to tell the cases generator that it's being used by the constant generator.
// We should fix this in the cases generator.
(void)(left);
(void)(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);
}
// We need to tell the cases generator that it's being used by the constant generator.
// We should fix this in the cases generator.
(void)(left);
(void)(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);
}
// We need to tell the cases generator that it's being used by the constant generator.
// We should fix this in the cases generator.
(void)(left);
(void)(right);
res = sym_new_type(ctx, &PyFloat_Type);
}

op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {
Expand Down
Loading
Loading
0