Skip to content

Commit 7e2bc1d

Browse files
Some fixups
1 parent f547880 commit 7e2bc1d

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
8888
// This value is arbitrary and was not optimized.
8989
#define JIT_CLEANUP_THRESHOLD 1000
9090

91-
#define TRACE_STACK_SIZE 5
92-
9391
int _Py_uop_analyze_and_optimize(
9492
PyFunctionObject *func,
9593
_PyUOpInstruction *trace, int trace_len, int curr_stackentries,
@@ -125,7 +123,7 @@ static inline uint16_t uop_get_error_target(const _PyUOpInstruction *inst)
125123
#define TY_ARENA_SIZE (UOP_MAX_TRACE_LENGTH * 5)
126124

127125
// Need extras for root frame and for overflow frame (see TRACE_STACK_PUSH())
128-
#define MAX_ABSTRACT_FRAME_DEPTH (TRACE_STACK_SIZE + 2)
126+
#define MAX_ABSTRACT_FRAME_DEPTH (16)
129127

130128
// The maximum number of side exits that we can take before requiring forward
131129
// progress (and inserting a new ENTER_EXECUTOR instruction). In practice, this

Python/optimizer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ _PyJit_translate_single_bytecode_to_trace(
616616

617617
bool needs_guard_ip = OPCODE_HAS_NEEDS_GUARD_IP(opcode);
618618
if (has_dynamic_jump_taken && !needs_guard_ip) {
619-
DPRINTF(2, "Unsupported: dynamic jump taken\n");
619+
DPRINTF(2, "Unsupported: dynamic jump taken %s\n", _PyOpcode_OpName[opcode]);
620620
goto unsupported;
621621
}
622622
DPRINTF(2, "%p %d: %s(%d) %d %d\n", old_code, target, _PyOpcode_OpName[opcode], oparg, needs_guard_ip, old_stack_level);
@@ -749,6 +749,8 @@ _PyJit_translate_single_bytecode_to_trace(
749749
if ((next_instr != _tstate->jit_state.initial_state.close_loop_instr) &&
750750
(next_instr != _tstate->jit_state.initial_state.start_instr) &&
751751
_tstate->jit_state.prev_state.code_curr_size > 5 &&
752+
// For side exits, we don't want to terminate them early.
753+
_tstate->jit_state.initial_state.exit == NULL &&
752754
// These are coroutines, and we want to unroll those usually.
753755
opcode != JUMP_BACKWARD_NO_INTERRUPT) {
754756
// We encountered a JUMP_BACKWARD but not to the top of our own loop.
@@ -867,7 +869,7 @@ _PyJit_translate_single_bytecode_to_trace(
867869
if (frame->owner < FRAME_OWNED_BY_INTERPRETER) {
868870
// Don't add nested code objects to the dependency.
869871
// It causes endless re-traces.
870-
if (new_func != NULL && !(new_code->co_flags & CO_NESTED)) {
872+
if (new_func != NULL && !Py_IsNone((PyObject*)new_func) && !(new_code->co_flags & CO_NESTED)) {
871873
operand = (uintptr_t)new_func;
872874
DPRINTF(2, "Adding %p func to op\n", (void *)operand);
873875
_Py_BloomFilter_Add(dependencies, new_func);

Python/optimizer_bytecodes.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ dummy_func(void) {
770770
}
771771

772772
op(_CREATE_INIT_FRAME, (init, self, args[oparg] -- init_frame)) {
773-
_Py_UOpsAbstractFrame *old_frame = ctx->frame;
773+
ctx->frame->stack_pointer = stack_pointer - oparg - 2;
774774
_Py_UOpsAbstractFrame *shim = frame_new(ctx, (PyCodeObject *)&_Py_InitCleanup, 0, NULL, 0);
775775
if (shim == NULL) {
776776
break;
@@ -799,6 +799,13 @@ dummy_func(void) {
799799
}
800800
_Py_BloomFilter_Add(dependencies, returning_code);
801801
int returning_stacklevel = this_instr->operand1;
802+
if (ctx->curr_frame_depth >= 2) {
803+
PyCodeObject *expected_code = ctx->frames[ctx->curr_frame_depth - 2].code;
804+
if (expected_code == returning_code) {
805+
assert((this_instr + 1)->opcode == _GUARD_IP_RETURN_VALUE);
806+
REPLACE_OP((this_instr + 1), _NOP, 0, 0);
807+
}
808+
}
802809
if (frame_pop(ctx, returning_code, returning_stacklevel)) {
803810
break;
804811
}
@@ -898,6 +905,12 @@ dummy_func(void) {
898905
_Py_BloomFilter_Add(dependencies, co);
899906
ctx->frame->func = func;
900907
}
908+
// Fixed calls don't need IP guards.
909+
if ((this_instr-1)->opcode == _SAVE_RETURN_OFFSET ||
910+
(this_instr-1)->opcode == _CREATE_INIT_FRAME) {
911+
assert((this_instr+1)->opcode == _GUARD_IP__PUSH_FRAME);
912+
REPLACE_OP(this_instr+1, _NOP, 0, 0);
913+
}
901914
}
902915

903916
op(_UNPACK_SEQUENCE, (seq -- values[oparg], top[0])) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)