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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.

3 changes: 3 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 @@ -256,6 +257,8 @@ 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 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
72 changes: 36 additions & 36 deletions Include/internal/pycore_uop_metadata.h

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

12 changes: 6 additions & 6 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1972,12 +1972,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 @@ -1991,7 +1991,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 @@ -2024,16 +2024,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
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