Skip to content
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

gh-126599: Refactor the _PyOptimizer APIs #126853

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
5 changes: 3 additions & 2 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ void _Py_ExecutorInit(_PyExecutorObject *, const _PyBloomFilter *);
void _Py_ExecutorDetach(_PyExecutorObject *);
void _Py_BloomFilter_Init(_PyBloomFilter *);
void _Py_BloomFilter_Add(_PyBloomFilter *bloom, void *obj);

// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(void) _Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj);

// For testing
// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(_PyOptimizerObject *) _Py_GetOptimizer(void);
PyAPI_FUNC(int) _Py_SetTier2Optimizer(_PyOptimizerObject* optimizer);
PyAPI_FUNC(PyObject *) _PyOptimizer_NewCounter(void);
PyAPI_FUNC(PyObject *) _PyOptimizer_NewUOpOptimizer(void);

#define _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS 3
Expand Down Expand Up @@ -151,7 +152,6 @@ int _Py_uop_analyze_and_optimize(struct _PyInterpreterFrame *frame,
_PyBloomFilter *dependencies);

extern PyTypeObject _PyCounterExecutor_Type;
extern PyTypeObject _PyCounterOptimizer_Type;
extern PyTypeObject _PyDefaultOptimizer_Type;
extern PyTypeObject _PyUOpExecutor_Type;
extern PyTypeObject _PyUOpOptimizer_Type;
Expand Down Expand Up @@ -276,6 +276,7 @@ extern int _Py_uop_frame_pop(_Py_UOpsContext *ctx);

PyAPI_FUNC(PyObject *) _Py_uop_symbols_test(PyObject *self, PyObject *ignored);

// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(int) _PyOptimizer_Optimize(struct _PyInterpreterFrame *frame, _Py_CODEUNIT *start, _PyStackRef *stack_pointer, _PyExecutorObject **exec_ptr, int chain_depth);

static inline int is_terminator(const _PyUOpInstruction *uop)
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def clear_executors(func):

@requires_specialization
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer") and
hasattr(_testinternalcapi, "new_counter_optimizer"),
"Requires optimizer infrastructure")
class TestOptimizerAPI(unittest.TestCase):

Expand Down Expand Up @@ -140,7 +141,8 @@ def get_opnames(ex):

@requires_specialization
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
xuantengh marked this conversation as resolved.
Show resolved Hide resolved
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer") and
hasattr(_testinternalcapi, "new_counter_optimizer"),
"Requires optimizer infrastructure")
class TestExecutorInvalidation(unittest.TestCase):

Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from test.support.import_helper import import_module

_testcapi = test.support.import_helper.import_module("_testcapi")
_testinternalcapi = test.support.import_helper.import_module("_testinternalcapi")

PAIR = (0,1)

Expand Down Expand Up @@ -2003,10 +2004,11 @@ def callback(code, instruction_offset):
sys.monitoring.set_events(0, 0)


@unittest.skipUnless(hasattr(_testinternalcapi, "new_counter_optimizer"),
"Requires counter optimizer")
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
xuantengh marked this conversation as resolved.
Show resolved Hide resolved

def setUp(self):
_testinternalcapi = import_module("_testinternalcapi")
if hasattr(_testinternalcapi, "get_optimizer"):
self.old_opt = _testinternalcapi.get_optimizer()
opt = _testinternalcapi.new_counter_optimizer()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove the internal opmization API ``_PyOptimizer_NewCounter``.
7 changes: 0 additions & 7 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,12 +989,6 @@ get_co_framesize(PyObject *self, PyObject *arg)

#ifdef _Py_TIER2

static PyObject *
new_counter_optimizer(PyObject *self, PyObject *arg)
{
return _PyOptimizer_NewCounter();
}

static PyObject *
new_uop_optimizer(PyObject *self, PyObject *arg)
{
Expand Down Expand Up @@ -2101,7 +2095,6 @@ static PyMethodDef module_functions[] = {
#ifdef _Py_TIER2
{"get_optimizer", get_optimizer, METH_NOARGS, NULL},
{"set_optimizer", set_optimizer, METH_O, NULL},
{"new_counter_optimizer", new_counter_optimizer, METH_NOARGS, NULL},
{"new_uop_optimizer", new_uop_optimizer, METH_NOARGS, NULL},
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},
{"invalidate_executors", invalidate_executors, METH_O, NULL},
Expand Down
1 change: 0 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,6 @@ static PyTypeObject* static_types[] = {
&_PyCoroWrapper_Type,
#ifdef _Py_TIER2
&_PyCounterExecutor_Type,
&_PyCounterOptimizer_Type,
&_PyDefaultOptimizer_Type,
#endif
&_Py_GenericAliasIterType,
Expand Down
22 changes: 0 additions & 22 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1394,28 +1394,6 @@ static PyMethodDef counter_optimizer_methods[] = {
{ NULL, NULL },
};

PyTypeObject _PyCounterOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "Counter optimizer",
.tp_basicsize = sizeof(_PyCounterOptimizerObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_methods = counter_optimizer_methods,
.tp_dealloc = (destructor)PyObject_Free,
};

PyObject *
_PyOptimizer_NewCounter(void)
{
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&_PyCounterOptimizer_Type);
if (opt == NULL) {
return NULL;
}
opt->base.optimize = counter_optimize;
opt->count = 0;
return (PyObject *)opt;
}


/*****************************************
* Executor management
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ Python/sysmodule.c - _PySys_ImplName -
Python/sysmodule.c - whatstrings -
Python/optimizer.c - _PyDefaultOptimizer_Type -
Python/optimizer.c - _PyCounterExecutor_Type -
Python/optimizer.c - _PyCounterOptimizer_Type -
Python/optimizer.c - _PyUOpExecutor_Type -
Python/optimizer.c - _PyUOpOptimizer_Type -
Python/optimizer.c - _PyOptimizer_Default -
Expand Down
Loading