Skip to content

Commit 7830f69

Browse files
committed
Post merge fixups
1 parent e350669 commit 7830f69

File tree

11 files changed

+89
-104
lines changed

11 files changed

+89
-104
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ typedef struct {
3535
uint8_t linked:1;
3636
uint8_t chain_depth:6; // Must be big enough for MAX_CHAIN_DEPTH - 1.
3737
bool warm;
38-
int index; // Index of ENTER_EXECUTOR (if code isn't NULL, below).
38+
uint8_t tos_cache;
39+
int16_t index; // Index of ENTER_EXECUTOR (if code isn't NULL, below).
3940
_PyBloomFilter bloom;
4041
_PyExecutorLinkListNode links;
4142
PyCodeObject *code; // Weak (NULL if no corresponding ENTER_EXECUTOR).
@@ -94,7 +95,7 @@ typedef struct _PyExecutorObject {
9495
// Export for '_opcode' shared extension (JIT compiler).
9596
PyAPI_FUNC(_PyExecutorObject*) _Py_GetExecutor(PyCodeObject *code, int offset);
9697

97-
void _Py_ExecutorInit(_PyExecutorObject *, const _PyBloomFilter *);
98+
void _Py_ExecutorInit(_PyExecutorObject *, const _PyBloomFilter *, int tos_cache);
9899
void _Py_ExecutorDetach(_PyExecutorObject *);
99100
void _Py_BloomFilter_Init(_PyBloomFilter *);
100101
void _Py_BloomFilter_Add(_PyBloomFilter *bloom, void *obj);

Python/bytecodes.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,10 +2972,7 @@ dummy_func(
29722972
else {
29732973
this_instr[1].counter = initial_jump_backoff_counter();
29742974
assert(tstate->current_executor == NULL);
2975-
tstate->jit_exit = NULL;
2976-
#if defined(_Py_TIER2) & defined(Py_DEBUG)
2977-
current_cached_values = 0;
2978-
#endif
2975+
assert(executor->vm_data.tos_cache == 0);
29792976
TIER1_TO_TIER2(executor);
29802977
}
29812978
}
@@ -3040,10 +3037,6 @@ dummy_func(
30403037
}
30413038
DISPATCH_GOTO();
30423039
}
3043-
#if defined(_Py_TIER2) & defined(Py_DEBUG)
3044-
current_cached_values = 0;
3045-
#endif
3046-
tstate->jit_exit = NULL;
30473040
TIER1_TO_TIER2(executor);
30483041
#else
30493042
Py_FatalError("ENTER_EXECUTOR is not supported in this build");
@@ -5432,13 +5425,19 @@ dummy_func(
54325425
GOTO_TIER_ONE(target);
54335426
Py_UNREACHABLE();
54345427
}
5435-
_PyExecutorObject *executor;
5428+
_PyExecutorObject *executor = NULL;
54365429
if (target->op.code == ENTER_EXECUTOR) {
54375430
PyCodeObject *code = _PyFrame_GetCode(frame);
54385431
executor = code->co_executors->executors[target->op.arg];
5439-
Py_INCREF(executor);
5432+
int tos_cache = uopcode - _COLD_EXIT_r00;
5433+
if (tos_cache == executor->vm_data.tos_cache) {
5434+
Py_INCREF(executor);
5435+
}
5436+
else {
5437+
executor = NULL;
5438+
}
54405439
}
5441-
else {
5440+
if (executor == NULL) {
54425441
_PyExecutorObject *previous_executor = _PyExecutor_FromExit(exit);
54435442
assert(tstate->current_executor == (PyObject *)previous_executor);
54445443
int chain_depth = previous_executor->vm_data.chain_depth + 1;

Python/ceval.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11321132
#endif
11331133
}
11341134

1135-
#if defined(_Py_TIER2) & defined(Py_DEBUG)
1136-
int current_cached_values = -1;
1137-
#endif
1138-
11391135
#if Py_TAIL_CALL_INTERP
11401136
# if Py_STATS
11411137
return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, 0, lastopcode);
@@ -1175,15 +1171,24 @@ _PyJitEntryFuncPtr _Py_jit_entry = _PyTier2Interpreter;
11751171
_Py_CODEUNIT *
11761172
_PyTier2Interpreter(
11771173
_PyExecutorObject *current_executor, _PyInterpreterFrame *frame,
1178-
_PyStackRef *stack_pointer, PyThreadState *tstate,
1179-
_PyStackRef _tos_cache0, _PyStackRef _tos_cache1, _PyStackRef _tos_cache2
1174+
_PyStackRef *stack_pointer, PyThreadState *tstate
11801175
) {
11811176
const _PyUOpInstruction *next_uop;
11821177
int oparg;
1183-
tier2_start:
11841178

1179+
/* Set up "jit" state after entry from tier 1.
1180+
* This mimics what the jit trampoline function does. */
1181+
int current_cached_values = current_executor->vm_data.tos_cache;
1182+
_PyStackRef _tos_cache0 = current_cached_values ? stack_pointer[-current_cached_values] : PyStackRef_ZERO_BITS;
1183+
_PyStackRef _tos_cache1 = stack_pointer[-1-(current_cached_values&1)]; /* Correct value for 2 or 3, harmless junk for 0 or 1 */
1184+
_PyStackRef _tos_cache2 = stack_pointer[-1]; /* Correct value for 3, harmless junk otherwise */
1185+
stack_pointer -= current_cached_values;
1186+
tstate->jit_exit = NULL;
1187+
1188+
tier2_start:
11851189
next_uop = current_executor->trace;
1186-
assert(next_uop->opcode == _START_EXECUTOR || next_uop->opcode == _COLD_EXIT);
1190+
assert(next_uop->opcode == _START_EXECUTOR_r00 + current_cached_values ||
1191+
next_uop->opcode == _COLD_EXIT_r00 + current_cached_values);
11871192

11881193
#undef LOAD_IP
11891194
#define LOAD_IP(UNUSED) (void)0

Python/ceval_macros.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ _PyFrame_SetStackPointer(frame, stack_pointer)
358358

359359
/* Tier-switching macros. */
360360

361+
/* It is always safe to read stack_pointer[-1] and stack_pointer[-2]
362+
* due to stack layout, even if the value is meaningless */
361363
#define TIER1_TO_TIER2(EXECUTOR) \
362364
do { \
363365
OPT_STAT_INC(traces_executed); \
@@ -375,9 +377,6 @@ do { \
375377
do { \
376378
OPT_STAT_INC(traces_executed); \
377379
current_executor = (EXECUTOR); \
378-
goto tier2_start; \
379-
assert(next_uop->opcode == _START_EXECUTOR_r00 + current_cached_values || \
380-
next_uop->opcode == _COLD_EXIT_r00 + current_cached_values); \
381380
goto tier2_start; \
382381
} while (0)
383382

Python/executor_cases.c.h

Lines changed: 39 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

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

Python/jit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz
535535
unsigned char *code = memory;
536536
state.trampolines.mem = memory + code_size;
537537
unsigned char *data = memory + code_size + state.trampolines.size + code_padding;
538-
assert(trace[0].opcode == _START_EXECUTOR_r00 || trace[0].opcode == _COLD_EXIT_r00);
538+
assert(trace[0].opcode == _START_EXECUTOR_r00 + executor->vm_data.tos_cache ||
539+
trace[0].opcode == _COLD_EXIT_r00 + executor->vm_data.tos_cache);
539540
for (size_t i = 0; i < length; i++) {
540541
const _PyUOpInstruction *instruction = &trace[i];
541542
group = &stencil_groups[instruction->opcode];

0 commit comments

Comments
 (0)