Skip to content

Commit

Permalink
Rip out _Py_TIER2, replace with _Py_JIT
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gvanrossum committed Apr 30, 2024
1 parent f228a06 commit 70e18a5
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 49 deletions.
5 changes: 0 additions & 5 deletions Include/Python.h
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Modules/_opcode.c
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions Modules/_testinternalcapi.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions Objects/codeobject.c
Expand Up @@ -1496,7 +1496,7 @@ PyCode_GetFreevars(PyCodeObject *code)
return _PyCode_GetFreevars(code);
}

#ifdef _Py_TIER2
#ifdef _Py_JIT

static void
clear_executors(PyCodeObject *co)
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/object.c
Expand Up @@ -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,
Expand All @@ -2306,7 +2306,7 @@ static PyTypeObject* static_types[] = {
&_PyPositionsIterator,
&_PyUnicodeASCIIIter_Type,
&_PyUnion_Type,
#ifdef _Py_TIER2
#ifdef _Py_JIT
&_PyUOpExecutor_Type,
&_PyUOpOptimizer_Type,
#endif
Expand Down
8 changes: 4 additions & 4 deletions Python/bytecodes.c
Expand Up @@ -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) {
Expand All @@ -2381,7 +2381,7 @@ dummy_func(
ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter);
}
#endif /* ENABLE_SPECIALIZATION */
#endif /* _Py_TIER2 */
#endif /* _Py_JIT */
}

pseudo(JUMP) = {
Expand All @@ -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 ||
Expand All @@ -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 -- )) {
Expand Down
12 changes: 3 additions & 9 deletions Python/ceval.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 */

}

Expand Down
8 changes: 4 additions & 4 deletions Python/generated_cases.c.h

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

6 changes: 3 additions & 3 deletions Python/instrumentation.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions Python/optimizer.c
@@ -1,3 +1,5 @@
#ifdef _Py_JIT

#include "Python.h"
#include "opcode.h"
#include "pycore_interp.h"
Expand All @@ -15,8 +17,6 @@
#include <stdint.h>
#include <stddef.h>

#ifdef _Py_TIER2

#define NEED_OPCODE_METADATA
#include "pycore_uop_metadata.h" // Uop tables
#undef NEED_OPCODE_METADATA
Expand Down Expand Up @@ -1625,4 +1625,4 @@ _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation)
}
}

#endif /* _Py_TIER2 */
#endif /* _Py_JIT */
4 changes: 2 additions & 2 deletions Python/optimizer_analysis.c
Expand Up @@ -33,7 +33,7 @@
#include <stdint.h>
#include <stddef.h>

#ifdef _Py_TIER2
#ifdef _Py_JIT

#ifdef Py_DEBUG
extern const char *_PyUOpName(int index);
Expand Down Expand Up @@ -606,4 +606,4 @@ _Py_uop_analyze_and_optimize(
return length;
}

#endif /* _Py_TIER2 */
#endif /* _Py_JIT */
5 changes: 2 additions & 3 deletions Python/optimizer_symbols.c
@@ -1,3 +1,4 @@
#ifdef _Py_JIT

#include "Python.h"

Expand All @@ -11,8 +12,6 @@
#include <stdint.h>
#include <stddef.h>

#ifdef _Py_TIER2

/* Symbols
=======
Expand Down Expand Up @@ -509,4 +508,4 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
return NULL;
}

#endif /* _Py_TIER2 */
#endif /* _Py_JIT */
6 changes: 3 additions & 3 deletions Python/pylifecycle.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Python/pystate.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Python/sysmodule.c
Expand Up @@ -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
Expand Down

0 comments on commit 70e18a5

Please sign in to comment.