From 70e18a51e383a246ef029301f25170183d56ff1f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 30 Apr 2024 07:40:51 -0700 Subject: [PATCH] Rip out _Py_TIER2, replace with _Py_JIT The value is a bitflag: - 0: off - 1: JIT, on by default - 3: JIT, off by default (not yet implemented, requires fiddling pystate.c) - 4: tier 2 interpreter, on by default --- Include/Python.h | 5 ----- Modules/_opcode.c | 2 +- Modules/_testinternalcapi.c | 8 ++++---- Objects/codeobject.c | 4 ++-- Objects/object.c | 4 ++-- Python/bytecodes.c | 8 ++++---- Python/ceval.c | 12 +++--------- Python/generated_cases.c.h | 8 ++++---- Python/instrumentation.c | 6 +++--- Python/optimizer.c | 6 +++--- Python/optimizer_analysis.c | 4 ++-- Python/optimizer_symbols.c | 5 ++--- Python/pylifecycle.c | 6 +++--- Python/pystate.c | 6 +++--- Python/sysmodule.c | 2 +- 15 files changed, 37 insertions(+), 49 deletions(-) diff --git a/Include/Python.h b/Include/Python.h index 9ece592db92197..bb771fb3aec980 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -51,11 +51,6 @@ # error "The limited API is not currently supported in the free-threaded build" #endif -// The JIT depends on TIER2 -#ifdef _Py_JIT -#define _Py_TIER2 _Py_JIT -#endif - // Include Python header files #include "pyport.h" #include "pymacro.h" diff --git a/Modules/_opcode.c b/Modules/_opcode.c index 0f28f006d917f7..02ad2590c8710b 100644 --- a/Modules/_opcode.c +++ b/Modules/_opcode.c @@ -367,7 +367,7 @@ _opcode_get_executor_impl(PyObject *module, PyObject *code, int offset) Py_TYPE(code)->tp_name); return NULL; } -#ifdef _Py_TIER2 +#ifdef _Py_JIT return (PyObject *)PyUnstable_GetExecutor((PyCodeObject *)code, offset); #else PyErr_Format(PyExc_RuntimeError, diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index f7952a119f0bd9..9c85a4fdd0ed69 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -985,7 +985,7 @@ get_co_framesize(PyObject *self, PyObject *arg) return PyLong_FromLong(code->co_framesize); } -#ifdef _Py_TIER2 +#ifdef _Py_JIT static PyObject * new_counter_optimizer(PyObject *self, PyObject *arg) @@ -1015,7 +1015,7 @@ static PyObject * get_optimizer(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *opt = NULL; -#ifdef _Py_TIER2 +#ifdef _Py_JIT opt = (PyObject *)PyUnstable_GetOptimizer(); #endif if (opt == NULL) { @@ -2027,7 +2027,7 @@ static PyMethodDef module_functions[] = { {"iframe_getline", iframe_getline, METH_O, NULL}, {"iframe_getlasti", iframe_getlasti, METH_O, NULL}, {"get_co_framesize", get_co_framesize, METH_O, NULL}, -#ifdef _Py_TIER2 +#ifdef _Py_JIT {"get_optimizer", get_optimizer, METH_NOARGS, NULL}, {"set_optimizer", set_optimizer, METH_O, NULL}, {"new_counter_optimizer", new_counter_optimizer, METH_NOARGS, NULL}, @@ -2081,7 +2081,7 @@ static PyMethodDef module_functions[] = { {"py_thread_id", get_py_thread_id, METH_NOARGS}, #endif {"set_immortalize_deferred", set_immortalize_deferred, METH_VARARGS}, -#ifdef _Py_TIER2 +#ifdef _Py_JIT {"uop_symbols_test", _Py_uop_symbols_test, METH_NOARGS}, #endif {NULL, NULL} /* sentinel */ diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 605167c5c0bd3a..76eb1a428fea56 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1496,7 +1496,7 @@ PyCode_GetFreevars(PyCodeObject *code) return _PyCode_GetFreevars(code); } -#ifdef _Py_TIER2 +#ifdef _Py_JIT static void clear_executors(PyCodeObject *co) @@ -1743,7 +1743,7 @@ code_dealloc(PyCodeObject *co) PyMem_Free(co_extra); } -#ifdef _Py_TIER2 +#ifdef _Py_JIT if (co->co_executors != NULL) { clear_executors(co); } diff --git a/Objects/object.c b/Objects/object.c index 45310a6c22d677..d0a8382d003aa5 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2281,7 +2281,7 @@ static PyTypeObject* static_types[] = { &_PyBufferWrapper_Type, &_PyContextTokenMissing_Type, &_PyCoroWrapper_Type, -#ifdef _Py_TIER2 +#ifdef _Py_JIT &_PyCounterExecutor_Type, &_PyCounterOptimizer_Type, &_PyDefaultOptimizer_Type, @@ -2306,7 +2306,7 @@ static PyTypeObject* static_types[] = { &_PyPositionsIterator, &_PyUnicodeASCIIIter_Type, &_PyUnion_Type, -#ifdef _Py_TIER2 +#ifdef _Py_JIT &_PyUOpExecutor_Type, &_PyUOpOptimizer_Type, #endif diff --git a/Python/bytecodes.c b/Python/bytecodes.c index da717ddbdaee18..8e2027efd11b21 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2355,7 +2355,7 @@ dummy_func( CHECK_EVAL_BREAKER(); assert(oparg <= INSTR_OFFSET()); JUMPBY(-oparg); - #ifdef _Py_TIER2 + #ifdef _Py_JIT #if ENABLE_SPECIALIZATION _Py_BackoffCounter counter = this_instr[1].counter; if (backoff_counter_triggers(counter) && this_instr->op.code == JUMP_BACKWARD) { @@ -2381,7 +2381,7 @@ dummy_func( ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter); } #endif /* ENABLE_SPECIALIZATION */ - #endif /* _Py_TIER2 */ + #endif /* _Py_JIT */ } pseudo(JUMP) = { @@ -2395,7 +2395,7 @@ dummy_func( }; tier1 inst(ENTER_EXECUTOR, (--)) { - #ifdef _Py_TIER2 + #ifdef _Py_JIT int prevoparg = oparg; CHECK_EVAL_BREAKER(); if (this_instr->op.code != ENTER_EXECUTOR || @@ -2415,7 +2415,7 @@ dummy_func( GOTO_TIER_TWO(executor); #else assert(0); - #endif /* _Py_TIER2 */ + #endif /* _Py_JIT */ } replaced op(_POP_JUMP_IF_FALSE, (cond -- )) { diff --git a/Python/ceval.c b/Python/ceval.c index 0e2e4ac7a0b64a..070ec88549a9a7 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -755,7 +755,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int _Py_CODEUNIT *next_instr; PyObject **stack_pointer; -#if defined(_Py_TIER2) && !defined(_Py_JIT) +#if _Py_JIT & 4 /* Tier 2 interpreter state */ _PyExecutorObject *current_executor = NULL; const _PyUOpInstruction *next_uop = NULL; @@ -959,15 +959,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int goto error; -#ifdef _Py_TIER2 +#if _Py_JIT & 4 /* Tier 2 interpreter */ // Tier 2 is also here! enter_tier_two: -#ifdef _Py_JIT - assert(0); -#else - #undef LOAD_IP #define LOAD_IP(UNUSED) (void)0 @@ -1112,9 +1108,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int tstate->previous_executor = (PyObject *)current_executor; GOTO_TIER_TWO(exit->executor); -#endif // _Py_JIT - -#endif // _Py_TIER2 +#endif /* _Py_JIT & 4 */ } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b6386f5189be3f..460a6dfeb99302 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2492,7 +2492,7 @@ (void)this_instr; next_instr += 1; INSTRUCTION_STATS(ENTER_EXECUTOR); - #ifdef _Py_TIER2 + #ifdef _Py_JIT int prevoparg = oparg; CHECK_EVAL_BREAKER(); if (this_instr->op.code != ENTER_EXECUTOR || @@ -2511,7 +2511,7 @@ GOTO_TIER_TWO(executor); #else assert(0); - #endif /* _Py_TIER2 */ + #endif /* _Py_JIT */ DISPATCH(); } @@ -3436,7 +3436,7 @@ CHECK_EVAL_BREAKER(); assert(oparg <= INSTR_OFFSET()); JUMPBY(-oparg); - #ifdef _Py_TIER2 + #ifdef _Py_JIT #if ENABLE_SPECIALIZATION _Py_BackoffCounter counter = this_instr[1].counter; if (backoff_counter_triggers(counter) && this_instr->op.code == JUMP_BACKWARD) { @@ -3462,7 +3462,7 @@ ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter); } #endif /* ENABLE_SPECIALIZATION */ - #endif /* _Py_TIER2 */ + #endif /* _Py_JIT */ DISPATCH(); } diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 7c66c6498c8eb2..c340718129942b 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -1702,7 +1702,7 @@ instrument_lock_held(PyCodeObject *code, PyInterpreterState *interp) ); return 0; } -#ifdef _Py_TIER2 +#ifdef _Py_JIT if (code->co_executors != NULL) { _PyCode_Clear_Executors(code); } @@ -1945,7 +1945,7 @@ _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events) goto done; } set_global_version(tstate, new_version); -#ifdef _Py_TIER2 +#ifdef _Py_JIT _Py_Executors_InvalidateAll(interp, 1); #endif res = instrument_all_executing_code_objects(interp); @@ -1987,7 +1987,7 @@ _PyMonitoring_SetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEvent code->_co_instrumentation_version -= MONITORING_VERSION_INCREMENT; } -#ifdef _Py_TIER2 +#ifdef _Py_JIT _Py_Executors_InvalidateDependency(interp, code, 1); #endif diff --git a/Python/optimizer.c b/Python/optimizer.c index 4ea3207e7eea47..bffc900e805a81 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1,3 +1,5 @@ +#ifdef _Py_JIT + #include "Python.h" #include "opcode.h" #include "pycore_interp.h" @@ -15,8 +17,6 @@ #include #include -#ifdef _Py_TIER2 - #define NEED_OPCODE_METADATA #include "pycore_uop_metadata.h" // Uop tables #undef NEED_OPCODE_METADATA @@ -1625,4 +1625,4 @@ _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation) } } -#endif /* _Py_TIER2 */ +#endif /* _Py_JIT */ diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index 326d6a5f630e45..6f0f39c51cd7ae 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -33,7 +33,7 @@ #include #include -#ifdef _Py_TIER2 +#ifdef _Py_JIT #ifdef Py_DEBUG extern const char *_PyUOpName(int index); @@ -606,4 +606,4 @@ _Py_uop_analyze_and_optimize( return length; } -#endif /* _Py_TIER2 */ +#endif /* _Py_JIT */ diff --git a/Python/optimizer_symbols.c b/Python/optimizer_symbols.c index 07d7598988aa5c..044622c2c10097 100644 --- a/Python/optimizer_symbols.c +++ b/Python/optimizer_symbols.c @@ -1,3 +1,4 @@ +#ifdef _Py_JIT #include "Python.h" @@ -11,8 +12,6 @@ #include #include -#ifdef _Py_TIER2 - /* Symbols ======= @@ -509,4 +508,4 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored)) return NULL; } -#endif /* _Py_TIER2 */ +#endif /* _Py_JIT */ diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 7c7d1c244c8579..17708f2e46aaf2 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -621,7 +621,7 @@ static int builtins_dict_watcher(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value) { PyInterpreterState *interp = _PyInterpreterState_GET(); -#ifdef _Py_TIER2 +#ifdef _Py_JIT if (interp->rare_events.builtin_dict < _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) { _Py_Executors_InvalidateAll(interp, 1); } @@ -1265,7 +1265,7 @@ init_interp_main(PyThreadState *tstate) // Turn on experimental tier 2 (uops-based) optimizer // This is also needed when the JIT is enabled -#ifdef _Py_TIER2 +#ifdef _Py_JIT if (is_main_interp) { PyObject *opt = PyUnstable_Optimizer_NewUOpOptimizer(); if (opt == NULL) { @@ -1636,7 +1636,7 @@ finalize_modules(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; -#ifdef _Py_TIER2 +#ifdef _Py_JIT // Invalidate all executors and turn off tier 2 optimizer _Py_Executors_InvalidateAll(interp, 0); _PyOptimizerObject *old = _Py_SetOptimizer(interp, NULL); diff --git a/Python/pystate.c b/Python/pystate.c index 173759ac347152..5ecb80f37492cd 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -653,7 +653,7 @@ init_interpreter(PyInterpreterState *interp, } interp->sys_profile_initialized = false; interp->sys_trace_initialized = false; -#ifdef _Py_TIER2 +#ifdef _Py_JIT (void)_Py_SetOptimizer(interp, NULL); interp->executor_list_head = NULL; #endif @@ -808,7 +808,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) tstate->_status.cleared = 0; } -#ifdef _Py_TIER2 +#ifdef _Py_JIT _PyOptimizerObject *old = _Py_SetOptimizer(interp, NULL); assert(old != NULL); Py_DECREF(old); @@ -2824,7 +2824,7 @@ _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, if (eval_frame == interp->eval_frame) { return; } -#ifdef _Py_TIER2 +#ifdef _Py_JIT if (eval_frame != NULL) { _Py_Executors_InvalidateAll(interp, 1); } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 726051521cf574..226e310545065c 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2165,7 +2165,7 @@ static PyObject * sys__clear_internal_caches_impl(PyObject *module) /*[clinic end generated code: output=0ee128670a4966d6 input=253e741ca744f6e8]*/ { -#ifdef _Py_TIER2 +#ifdef _Py_JIT PyInterpreterState *interp = _PyInterpreterState_GET(); _Py_Executors_InvalidateAll(interp, 0); #endif