Skip to content

Commit c0c14b4

Browse files
Restore the optimizer fully
1 parent 5102ab6 commit c0c14b4

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

Python/ceval_macros.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,23 @@
143143
# define LEAVE_TRACING() \
144144
DISPATCH_TABLE_VAR = DISPATCH_TABLE;
145145
# define BAIL_TRACING_NO_DISPATCH() \
146-
LEAVE_TRACING(); \
147-
_PyFrame_SetStackPointer(frame, stack_pointer); \
148-
int _err = _PyOptimizer_Optimize(frame, tstate); \
149-
_PyJIT_FinalizeTracing(tstate); \
150-
stack_pointer = _PyFrame_GetStackPointer(frame); \
151-
if (_err < 0) { \
152-
JUMP_TO_LABEL(error); \
153-
}
146+
do { \
147+
LEAVE_TRACING(); \
148+
if (!_PyErr_Occurred(tstate)) { \
149+
_PyFrame_SetStackPointer(frame, stack_pointer); \
150+
int _err = _PyOptimizer_Optimize(frame, tstate); \
151+
_PyJIT_FinalizeTracing(tstate); \
152+
stack_pointer = _PyFrame_GetStackPointer(frame); \
153+
if (_err < 0) { \
154+
JUMP_TO_LABEL(error); \
155+
} \
156+
} \
157+
else { \
158+
_PyFrame_SetStackPointer(frame, stack_pointer); \
159+
_PyJIT_FinalizeTracing(tstate); \
160+
stack_pointer = _PyFrame_GetStackPointer(frame); \
161+
} \
162+
} while (0);
154163
# define RECORD_TRACE_NO_DISPATCH() do { \
155164
if (IS_JIT_TRACING() && add_to_code_trace(tstate, frame, old_code, old_func, this_instr, next_instr, opcode, oparg, _jump_taken)) { \
156165
BAIL_TRACING_NO_DISPATCH(); \

Python/optimizer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,9 @@ _PyJIT_translate_single_bytecode_to_trace(
794794
}
795795
if (uop == _PUSH_FRAME || uop == _RETURN_VALUE || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE) {
796796
PyCodeObject *new_code = (PyCodeObject *)PyStackRef_AsPyObjectBorrow(frame->f_executable);
797-
if (func != NULL) {
798-
operand = (uintptr_t)func;
797+
PyFunctionObject *new_func = (PyCodeObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
798+
if (new_func != NULL) {
799+
operand = (uintptr_t)new_func;
799800
}
800801
else if (new_code != NULL) {
801802
operand = (uintptr_t)new_code | 1;

Python/optimizer_analysis.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,13 @@ _Py_uop_analyze_and_optimize(
528528
{
529529
OPT_STAT_INC(optimizer_attempts);
530530

531-
// int err = optimize_uops(
532-
// initial_func, buffer,
533-
// length, curr_stacklen, dependencies);
534-
//
535-
// if (err == 0) {
536-
// return err;
537-
// }
531+
int err = optimize_uops(
532+
initial_func, buffer,
533+
length, curr_stacklen, dependencies);
534+
535+
if (err == 0) {
536+
return err;
537+
}
538538

539539
assert(length > 0);
540540

Python/optimizer_bytecodes.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -794,25 +794,21 @@ dummy_func(void) {
794794

795795
op(_RETURN_GENERATOR, ( -- res)) {
796796
SYNC_SP();
797-
PyCodeObject *co = get_current_code_object(ctx);
798-
ctx->frame->stack_pointer = stack_pointer;
799-
if (frame_pop(ctx)) {
800-
break;
801-
}
802797
stack_pointer = ctx->frame->stack_pointer;
803798
res = sym_new_unknown(ctx);
804-
805-
/* Stack space handling */
806-
assert(corresponding_check_stack == NULL);
807-
assert(co != NULL);
808-
int framesize = co->co_framesize;
809-
assert(framesize > 0);
810-
assert(framesize <= curr_space);
811-
curr_space -= framesize;
799+
ctx->done = true;
800+
ctx->out_of_space = true;
812801
}
813802

814803
op(_YIELD_VALUE, (unused -- value)) {
804+
// TODO (gh-139109): handle this properly in a future optimization.
805+
// A possibility to handle underflows is to just restore the current frame information
806+
// from whatever is stored in the trace we record at that point of time.
807+
// E.g. we record at this YIELD_VALUE, func_obj=x , stack_level=4
808+
// We can restore it to there.
815809
value = sym_new_unknown(ctx);
810+
ctx->done = true;
811+
ctx->out_of_space = true;
816812
}
817813

818814
op(_GET_ITER, (iterable -- iter, index_or_null)) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)