Skip to content

Commit 278bbe6

Browse files
Support __init__ in the optimizer
1 parent a55d766 commit 278bbe6

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

Python/optimizer_analysis.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ incorrect_keys(PyObject *obj, uint32_t version)
142142
#define STACK_LEVEL() ((int)(stack_pointer - ctx->frame->stack))
143143
#define STACK_SIZE() ((int)(ctx->frame->stack_len))
144144

145+
#define CURRENT_FRAME_IS_INIT_SHIM() (ctx->frame->code == ((PyCodeObject *)&_Py_InitCleanup))
146+
145147
#define WITHIN_STACK_BOUNDS() \
146-
(STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE())
148+
(CURRENT_FRAME_IS_INIT_SHIM() || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
147149

148150

149151
#define GETLOCAL(idx) ((ctx->frame->locals[idx]))
@@ -316,13 +318,18 @@ optimize_uops(
316318
ctx->frame = frame;
317319

318320
_PyUOpInstruction *this_instr = NULL;
321+
JitOptRef *stack_pointer = ctx->frame->stack_pointer;
322+
319323
for (int i = 0; !ctx->done; i++) {
320324
assert(i < trace_len);
321325
this_instr = &trace[i];
322326

323327
int oparg = this_instr->oparg;
324328
opcode = this_instr->opcode;
325-
JitOptRef *stack_pointer = ctx->frame->stack_pointer;
329+
330+
if (!CURRENT_FRAME_IS_INIT_SHIM()) {
331+
stack_pointer = ctx->frame->stack_pointer;
332+
}
326333

327334
#ifdef Py_DEBUG
328335
if (get_lltrace() >= 3) {
@@ -341,9 +348,11 @@ optimize_uops(
341348
Py_UNREACHABLE();
342349
}
343350
assert(ctx->frame != NULL);
344-
DPRINTF(3, " stack_level %d\n", STACK_LEVEL());
345-
ctx->frame->stack_pointer = stack_pointer;
346-
assert(STACK_LEVEL() >= 0);
351+
if (!CURRENT_FRAME_IS_INIT_SHIM()) {
352+
DPRINTF(3, " stack_level %d\n", STACK_LEVEL());
353+
ctx->frame->stack_pointer = stack_pointer;
354+
assert(STACK_LEVEL() >= 0);
355+
}
347356
}
348357
if (ctx->out_of_space) {
349358
DPRINTF(3, "\n");

Python/optimizer_bytecodes.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,20 @@ dummy_func(void) {
764764
}
765765

766766
op(_CREATE_INIT_FRAME, (init, self, args[oparg] -- init_frame)) {
767-
init_frame = PyJitRef_NULL;
768-
ctx->done = true;
767+
_Py_UOpsAbstractFrame *old_frame = ctx->frame;
768+
_Py_UOpsAbstractFrame *shim = frame_new(ctx, (PyCodeObject *)&_Py_InitCleanup, 0, NULL, 0);
769+
if (shim == NULL) {
770+
break;
771+
}
772+
/* Push self onto stack of shim */
773+
shim->stack[0] = self;
774+
shim->stack_pointer++;
775+
assert((int)(shim->stack_pointer - shim->stack) == 1);
776+
ctx->frame = shim;
777+
ctx->curr_frame_depth++;
778+
assert((this_instr + 1)->opcode == _PUSH_FRAME);
779+
PyCodeObject *co = get_code_with_logging((this_instr + 1));
780+
init_frame = PyJitRef_Wrap((JitOptSymbol *)frame_new(ctx, co, 0, args-1, oparg+1));
769781
}
770782

771783
op(_RETURN_VALUE, (retval -- res)) {
@@ -863,7 +875,9 @@ dummy_func(void) {
863875

864876
op(_PUSH_FRAME, (new_frame -- )) {
865877
SYNC_SP();
866-
ctx->frame->stack_pointer = stack_pointer;
878+
if (!CURRENT_FRAME_IS_INIT_SHIM()) {
879+
ctx->frame->stack_pointer = stack_pointer;
880+
}
867881
ctx->frame = (_Py_UOpsAbstractFrame *)PyJitRef_Unwrap(new_frame);
868882
ctx->curr_frame_depth++;
869883
stack_pointer = ctx->frame->stack_pointer;

Python/optimizer_cases.c.h

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

Python/optimizer_symbols.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ _Py_uop_frame_new(
823823
return NULL;
824824
}
825825
_Py_UOpsAbstractFrame *frame = &ctx->frames[ctx->curr_frame_depth];
826-
827826
frame->code = co;
828827
frame->stack_len = co->co_stacksize;
829828
frame->locals_len = co->co_nlocalsplus;

0 commit comments

Comments
 (0)