Skip to content

Commit ce28d34

Browse files
committed
Clean up a bunch of tier two stuff
1 parent bb5c687 commit ce28d34

File tree

12 files changed

+93
-354
lines changed

12 files changed

+93
-354
lines changed

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,7 @@ dummy_func(
35133513
}
35143514

35153515
op(_MAYBE_EXPAND_METHOD, (callable[1], self_or_null[1], args[oparg] -- func[1], maybe_self[1], args[oparg])) {
3516+
(void)args;
35163517
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
35173518
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
35183519
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
@@ -3879,6 +3880,7 @@ dummy_func(
38793880
_CHECK_PERIODIC;
38803881

38813882
op(_CHECK_AND_ALLOCATE_OBJECT, (type_version/2, callable[1], null[1], args[oparg] -- init[1], self[1], args[oparg])) {
3883+
(void)args;
38823884
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
38833885
DEOPT_IF(!PyStackRef_IsNull(null[0]));
38843886
DEOPT_IF(!PyType_Check(callable_o));
@@ -4106,7 +4108,7 @@ dummy_func(
41064108
PyObject *res_o = PyLong_FromSsize_t(len_i);
41074109
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
41084110
if (res_o == NULL) {
4109-
GOTO_ERROR(error);
4111+
ERROR_NO_POP();
41104112
}
41114113
PyStackRef_CLOSE(arg_stackref);
41124114
DEAD(args);
@@ -4351,6 +4353,7 @@ dummy_func(
43514353
}
43524354

43534355
op(_MAYBE_EXPAND_METHOD_KW, (callable[1], self_or_null[1], args[oparg], kwnames_in -- func[1], maybe_self[1], args[oparg], kwnames_out)) {
4356+
(void)args;
43544357
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
43554358
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
43564359
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
@@ -5009,7 +5012,7 @@ dummy_func(
50095012
if (frame->lltrace >= 2) {
50105013
printf("SIDE EXIT: [UOp ");
50115014
_PyUOpPrint(&next_uop[-1]);
5012-
printf(", exit %u, temp %d, target %d -> %s]\n",
5015+
printf(", exit %lu, temp %d, target %d -> %s]\n",
50135016
exit - current_executor->exits, exit->temperature.value_and_backoff,
50145017
(int)(target - _PyFrame_GetBytecode(frame)),
50155018
_PyOpcode_OpName[target->op.code]);
@@ -5019,11 +5022,11 @@ dummy_func(
50195022
exit->temperature = initial_temperature_backoff_counter();
50205023
Py_CLEAR(exit->executor);
50215024
}
5025+
tstate->previous_executor = (PyObject *)current_executor;
50225026
if (exit->executor == NULL) {
50235027
_Py_BackoffCounter temperature = exit->temperature;
50245028
if (!backoff_counter_triggers(temperature)) {
50255029
exit->temperature = advance_backoff_counter(temperature);
5026-
tstate->previous_executor = (PyObject *)current_executor;
50275030
GOTO_TIER_ONE(target);
50285031
}
50295032
_PyExecutorObject *executor;
@@ -5036,20 +5039,13 @@ dummy_func(
50365039
int optimized = _PyOptimizer_Optimize(frame, target, stack_pointer, &executor, chain_depth);
50375040
if (optimized <= 0) {
50385041
exit->temperature = restart_backoff_counter(temperature);
5039-
if (optimized < 0) {
5040-
GOTO_UNWIND();
5041-
}
5042-
tstate->previous_executor = (PyObject *)current_executor;
5043-
GOTO_TIER_ONE(target);
5044-
}
5045-
else {
5046-
exit->temperature = initial_temperature_backoff_counter();
5042+
GOTO_TIER_ONE(optimized < 0 ? NULL : target);
50475043
}
5044+
exit->temperature = initial_temperature_backoff_counter();
50485045
}
50495046
exit->executor = executor;
50505047
}
50515048
Py_INCREF(exit->executor);
5052-
tstate->previous_executor = (PyObject *)current_executor;
50535049
GOTO_TIER_TWO(exit->executor);
50545050
}
50555051

@@ -5117,7 +5113,7 @@ dummy_func(
51175113
if (frame->lltrace >= 2) {
51185114
printf("DYNAMIC EXIT: [UOp ");
51195115
_PyUOpPrint(&next_uop[-1]);
5120-
printf(", exit %u, temp %d, target %d -> %s]\n",
5116+
printf(", exit %lu, temp %d, target %d -> %s]\n",
51215117
exit - current_executor->exits, exit->temperature.value_and_backoff,
51225118
(int)(target - _PyFrame_GetBytecode(frame)),
51235119
_PyOpcode_OpName[target->op.code]);
@@ -5137,21 +5133,15 @@ dummy_func(
51375133
int optimized = _PyOptimizer_Optimize(frame, target, stack_pointer, &executor, 0);
51385134
if (optimized <= 0) {
51395135
exit->temperature = restart_backoff_counter(exit->temperature);
5140-
if (optimized < 0) {
5141-
GOTO_UNWIND();
5142-
}
5143-
GOTO_TIER_ONE(target);
5144-
}
5145-
else {
5146-
exit->temperature = initial_temperature_backoff_counter();
5136+
GOTO_TIER_ONE(optimized < 0 ? NULL : target);
51475137
}
5138+
exit->temperature = initial_temperature_backoff_counter();
51485139
}
51495140
GOTO_TIER_TWO(executor);
51505141
}
51515142

51525143
tier2 op(_START_EXECUTOR, (executor/4 --)) {
5153-
Py_DECREF(tstate->previous_executor);
5154-
tstate->previous_executor = NULL;
5144+
Py_CLEAR(tstate->previous_executor);
51555145
#ifndef _Py_JIT
51565146
current_executor = (_PyExecutorObject*)executor;
51575147
#endif
@@ -5177,14 +5167,16 @@ dummy_func(
51775167
}
51785168

51795169
tier2 op(_DEOPT, (--)) {
5180-
EXIT_TO_TIER1();
5170+
tstate->previous_executor = (PyObject *)current_executor;
5171+
GOTO_TIER_ONE(_PyFrame_GetBytecode(frame) + CURRENT_TARGET());
51815172
}
51825173

51835174
tier2 op(_ERROR_POP_N, (target/2 --)) {
5175+
tstate->previous_executor = (PyObject *)current_executor;
51845176
assert(oparg == 0);
51855177
frame->instr_ptr = _PyFrame_GetBytecode(frame) + target;
51865178
SYNC_SP();
5187-
GOTO_UNWIND();
5179+
GOTO_TIER_ONE(NULL);
51885180
}
51895181

51905182
/* Progress is guaranteed if we DEOPT on the eval breaker, because

Python/ceval.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
870870
#undef LOAD_IP
871871
#define LOAD_IP(UNUSED) (void)0
872872

873-
#undef GOTO_ERROR
874-
#define GOTO_ERROR(LABEL) goto LABEL ## _tier_two
875-
876873
#ifdef Py_STATS
877874
// Disable these macros that apply to Tier 1 stats when we are in Tier 2
878875
#undef STAT_INC
@@ -948,46 +945,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
948945
_PyOpcode_OpName[frame->instr_ptr->op.code]);
949946
}
950947
#endif
951-
assert (next_uop[-1].format == UOP_FORMAT_JUMP);
948+
assert(next_uop[-1].format == UOP_FORMAT_JUMP);
952949
uint16_t target = uop_get_error_target(&next_uop[-1]);
953950
next_uop = current_executor->trace + target;
954951
goto tier2_dispatch;
955952

956-
error_tier_two:
957-
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
958-
assert(next_uop[-1].format == UOP_FORMAT_TARGET);
959-
frame->return_offset = 0; // Don't leave this random
960-
Py_DECREF(current_executor);
961-
tstate->previous_executor = NULL;
962-
next_instr = frame->instr_ptr;
963-
goto error;
964-
965953
jump_to_jump_target:
966954
assert(next_uop[-1].format == UOP_FORMAT_JUMP);
967955
target = uop_get_jump_target(&next_uop[-1]);
968956
next_uop = current_executor->trace + target;
969957
goto tier2_dispatch;
970958

971-
exit_to_tier1_dynamic:
972-
next_instr = frame->instr_ptr;
973-
goto goto_to_tier1;
974-
exit_to_tier1:
975-
assert(next_uop[-1].format == UOP_FORMAT_TARGET);
976-
next_instr = next_uop[-1].target + _PyFrame_GetBytecode(frame);
977-
goto_to_tier1:
978-
#ifdef Py_DEBUG
979-
if (frame->lltrace >= 2) {
980-
printf("DEOPT: [UOp ");
981-
_PyUOpPrint(&next_uop[-1]);
982-
printf(" -> %s]\n",
983-
_PyOpcode_OpName[next_instr->op.code]);
984-
}
985-
#endif
986-
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
987-
Py_DECREF(current_executor);
988-
tstate->previous_executor = NULL;
989-
DISPATCH();
990-
991959
#endif // _Py_JIT
992960

993961
#endif // _Py_TIER2

Python/ceval_macros.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ do { \
132132
goto start_frame; \
133133
} while (0)
134134

135-
// Use this instead of 'goto error' so Tier 2 can go to a different label
136-
#define GOTO_ERROR(LABEL) goto LABEL
137-
138135
/* Tuple access macros */
139136

140137
#ifndef Py_DEBUG
@@ -375,17 +372,19 @@ _PyFrame_SetStackPointer(frame, stack_pointer)
375372
#define GOTO_TIER_TWO(EXECUTOR) \
376373
do { \
377374
OPT_STAT_INC(traces_executed); \
378-
jit_func jitted = (EXECUTOR)->jit_code; \
375+
_PyExecutorObject *_executor = (EXECUTOR); \
376+
jit_func jitted = _executor->jit_code; \
377+
/* Keep the shim frame alive via the executor: */ \
378+
Py_INCREF(_executor); \
379379
next_instr = jitted(frame, stack_pointer, tstate); \
380-
Py_DECREF(tstate->previous_executor); \
381-
tstate->previous_executor = NULL; \
380+
Py_DECREF(_executor); \
381+
Py_CLEAR(tstate->previous_executor); \
382382
frame = tstate->current_frame; \
383+
stack_pointer = _PyFrame_GetStackPointer(frame); \
383384
if (next_instr == NULL) { \
384385
next_instr = frame->instr_ptr; \
385-
stack_pointer = _PyFrame_GetStackPointer(frame); \
386386
goto error; \
387387
} \
388-
stack_pointer = _PyFrame_GetStackPointer(frame); \
389388
DISPATCH(); \
390389
} while (0)
391390
#else
@@ -398,24 +397,25 @@ do { \
398397
} while (0)
399398
#endif
400399

401-
#define GOTO_TIER_ONE(TARGET) \
402-
do { \
403-
Py_DECREF(tstate->previous_executor); \
404-
tstate->previous_executor = NULL; \
405-
next_instr = target; \
406-
DISPATCH(); \
400+
#define GOTO_TIER_ONE(TARGET) \
401+
do { \
402+
next_instr = (TARGET); \
403+
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist); \
404+
Py_CLEAR(tstate->previous_executor); \
405+
if (next_instr == NULL) { \
406+
next_instr = frame->instr_ptr; \
407+
goto error; \
408+
} \
409+
DISPATCH(); \
407410
} while (0)
408411

409-
#define CURRENT_OPARG() (next_uop[-1].oparg)
410-
412+
#define CURRENT_OPARG() (next_uop[-1].oparg)
411413
#define CURRENT_OPERAND0() (next_uop[-1].operand0)
412414
#define CURRENT_OPERAND1() (next_uop[-1].operand1)
415+
#define CURRENT_TARGET() (next_uop[-1].target)
413416

414417
#define JUMP_TO_JUMP_TARGET() goto jump_to_jump_target
415418
#define JUMP_TO_ERROR() goto jump_to_error_target
416-
#define GOTO_UNWIND() goto error_tier_two
417-
#define EXIT_TO_TIER1() goto exit_to_tier1
418-
#define EXIT_TO_TIER1_DYNAMIC() goto exit_to_tier1_dynamic;
419419

420420
/* Stackref macros */
421421

0 commit comments

Comments
 (0)