Skip to content

Commit

Permalink
pythonGH-111339: Fix initialization and finalization of static optimi…
Browse files Browse the repository at this point in the history
…zer types (pythonGH-111430)
  • Loading branch information
savannahostrowski authored and aisk committed Feb 11, 2024
1 parent 24e5782 commit 151fb9d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
5 changes: 5 additions & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ extern "C" {
int _Py_uop_analyze_and_optimize(PyCodeObject *code,
_PyUOpInstruction *trace, int trace_len, int curr_stackentries);

extern PyTypeObject _PyCounterExecutor_Type;
extern PyTypeObject _PyCounterOptimizer_Type;
extern PyTypeObject _PyDefaultOptimizer_Type;
extern PyTypeObject _PyUOpExecutor_Type;
extern PyTypeObject _PyUOpOptimizer_Type;

#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pycore_memoryobject.h" // _PyManagedBuffer_Type
#include "pycore_namespace.h" // _PyNamespace_Type
#include "pycore_object.h" // PyAPI_DATA() _Py_SwappedOp definition
#include "pycore_optimizer.h" // _PyUOpExecutor_Type, _PyUOpOptimizer_Type, ...
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down Expand Up @@ -2157,6 +2158,9 @@ static PyTypeObject* static_types[] = {
&_PyBufferWrapper_Type,
&_PyContextTokenMissing_Type,
&_PyCoroWrapper_Type,
&_PyCounterExecutor_Type,
&_PyCounterOptimizer_Type,
&_PyDefaultOptimizer_Type,
&_Py_GenericAliasIterType,
&_PyHamtItems_Type,
&_PyHamtKeys_Type,
Expand All @@ -2176,6 +2180,8 @@ static PyTypeObject* static_types[] = {
&_PyPositionsIterator,
&_PyUnicodeASCIIIter_Type,
&_PyUnion_Type,
&_PyUOpExecutor_Type,
&_PyUOpOptimizer_Type,
&_PyWeakref_CallableProxyType,
&_PyWeakref_ProxyType,
&_PyWeakref_RefType,
Expand Down
24 changes: 10 additions & 14 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ error_optimize(
return -1;
}

static PyTypeObject DefaultOptimizer_Type = {
PyTypeObject _PyDefaultOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "noop_optimizer",
.tp_basicsize = sizeof(_PyOptimizerObject),
Expand All @@ -120,7 +120,7 @@ static PyTypeObject DefaultOptimizer_Type = {
};

_PyOptimizerObject _PyOptimizer_Default = {
PyObject_HEAD_INIT(&DefaultOptimizer_Type)
PyObject_HEAD_INIT(&_PyDefaultOptimizer_Type)
.optimize = error_optimize,
.resume_threshold = UINT16_MAX,
.backedge_threshold = UINT16_MAX,
Expand Down Expand Up @@ -236,7 +236,7 @@ static PyMethodDef executor_methods[] = {
{ NULL, NULL },
};

static PyTypeObject CounterExecutor_Type = {
PyTypeObject _PyCounterExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "counting_executor",
.tp_basicsize = sizeof(_PyCounterExecutorObject),
Expand Down Expand Up @@ -265,7 +265,7 @@ counter_optimize(
int Py_UNUSED(curr_stackentries)
)
{
_PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&CounterExecutor_Type);
_PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&_PyCounterExecutor_Type);
if (executor == NULL) {
return -1;
}
Expand All @@ -291,7 +291,7 @@ static PyMethodDef counter_optimizer_methods[] = {
{ NULL, NULL },
};

static PyTypeObject CounterOptimizer_Type = {
PyTypeObject _PyCounterOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "Counter optimizer",
.tp_basicsize = sizeof(_PyCounterOptimizerObject),
Expand All @@ -304,9 +304,7 @@ static PyTypeObject CounterOptimizer_Type = {
PyObject *
PyUnstable_Optimizer_NewCounter(void)
{
PyType_Ready(&CounterExecutor_Type);
PyType_Ready(&CounterOptimizer_Type);
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&CounterOptimizer_Type);
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&_PyCounterOptimizer_Type);
if (opt == NULL) {
return NULL;
}
Expand Down Expand Up @@ -375,7 +373,7 @@ PySequenceMethods uop_as_sequence = {
.sq_item = (ssizeargfunc)uop_item,
};

static PyTypeObject UOpExecutor_Type = {
PyTypeObject _PyUOpExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "uop_executor",
.tp_basicsize = sizeof(_PyUOpExecutorObject) - sizeof(_PyUOpInstruction),
Expand Down Expand Up @@ -929,7 +927,7 @@ uop_optimize(
trace_length = _Py_uop_analyze_and_optimize(code, trace, trace_length, curr_stackentries);
}
trace_length = remove_unneeded_uops(trace, trace_length);
_PyUOpExecutorObject *executor = PyObject_NewVar(_PyUOpExecutorObject, &UOpExecutor_Type, trace_length);
_PyUOpExecutorObject *executor = PyObject_NewVar(_PyUOpExecutorObject, &_PyUOpExecutor_Type, trace_length);
if (executor == NULL) {
return -1;
}
Expand All @@ -946,7 +944,7 @@ uop_opt_dealloc(PyObject *self) {
PyObject_Free(self);
}

static PyTypeObject UOpOptimizer_Type = {
PyTypeObject _PyUOpOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "uop_optimizer",
.tp_basicsize = sizeof(_PyOptimizerObject),
Expand All @@ -958,9 +956,7 @@ static PyTypeObject UOpOptimizer_Type = {
PyObject *
PyUnstable_Optimizer_NewUOpOptimizer(void)
{
PyType_Ready(&UOpExecutor_Type);
PyType_Ready(&UOpOptimizer_Type);
_PyOptimizerObject *opt = PyObject_New(_PyOptimizerObject, &UOpOptimizer_Type);
_PyOptimizerObject *opt = PyObject_New(_PyOptimizerObject, &_PyUOpOptimizer_Type);
if (opt == NULL) {
return NULL;
}
Expand Down
10 changes: 5 additions & 5 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ Python/sysmodule.c - perf_map_state -
Python/sysmodule.c - _PySys_ImplCacheTag -
Python/sysmodule.c - _PySys_ImplName -
Python/sysmodule.c - whatstrings -
Python/optimizer.c - DefaultOptimizer_Type -
Python/optimizer.c - CounterExecutor_Type -
Python/optimizer.c - CounterOptimizer_Type -
Python/optimizer.c - UOpExecutor_Type -
Python/optimizer.c - UOpOptimizer_Type -
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

0 comments on commit 151fb9d

Please sign in to comment.