From 151fb9d0f213ad07833803d28174d23e0401b0f0 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Sun, 29 Oct 2023 13:53:25 -0700 Subject: [PATCH] GH-111339: Fix initialization and finalization of static optimizer types (GH-111430) --- Include/internal/pycore_optimizer.h | 5 +++++ Objects/object.c | 6 ++++++ Python/optimizer.c | 24 ++++++++++-------------- Tools/c-analyzer/cpython/ignored.tsv | 10 +++++----- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Include/internal/pycore_optimizer.h b/Include/internal/pycore_optimizer.h index f9f16c48cbc21c2..b052460b44b7913 100644 --- a/Include/internal/pycore_optimizer.h +++ b/Include/internal/pycore_optimizer.h @@ -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 } diff --git a/Objects/object.c b/Objects/object.c index 3ed272afdced7c6..791db755663e68b 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -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() @@ -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, @@ -2176,6 +2180,8 @@ static PyTypeObject* static_types[] = { &_PyPositionsIterator, &_PyUnicodeASCIIIter_Type, &_PyUnion_Type, + &_PyUOpExecutor_Type, + &_PyUOpOptimizer_Type, &_PyWeakref_CallableProxyType, &_PyWeakref_ProxyType, &_PyWeakref_RefType, diff --git a/Python/optimizer.c b/Python/optimizer.c index 6402287a412a35f..c239b03053a896f 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -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), @@ -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, @@ -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), @@ -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; } @@ -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), @@ -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; } @@ -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), @@ -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; } @@ -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), @@ -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; } diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index d436886fe73d992..eea633a15056dde 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -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 - ##-----------------------