Skip to content

Commit b84a763

Browse files
gh-120619: Optimize through _Py_FRAME_GENERAL (GH-124518)
* Optimize through _Py_FRAME_GENERAL * refactor
1 parent 8cc5aa4 commit b84a763

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

Python/optimizer_analysis.c

+24
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ get_code(_PyUOpInstruction *op)
385385
return co;
386386
}
387387

388+
static PyCodeObject *
389+
get_code_with_logging(_PyUOpInstruction *op)
390+
{
391+
PyCodeObject *co = NULL;
392+
uint64_t push_operand = op->operand;
393+
if (push_operand & 1) {
394+
co = (PyCodeObject *)(push_operand & ~1);
395+
DPRINTF(3, "code=%p ", co);
396+
assert(PyCode_Check(co));
397+
}
398+
else {
399+
PyFunctionObject *func = (PyFunctionObject *)push_operand;
400+
DPRINTF(3, "func=%p ", func);
401+
if (func == NULL) {
402+
DPRINTF(3, "\n");
403+
DPRINTF(1, "Missing function\n");
404+
return NULL;
405+
}
406+
co = (PyCodeObject *)func->func_code;
407+
DPRINTF(3, "code=%p ", co);
408+
}
409+
return co;
410+
}
411+
388412
/* 1 for success, 0 for not ready, cannot error at the moment. */
389413
static int
390414
optimize_uops(

Python/optimizer_bytecodes.c

+16-23
Original file line numberDiff line numberDiff line change
@@ -575,25 +575,13 @@ dummy_func(void) {
575575

576576
PyCodeObject *co = NULL;
577577
assert((this_instr + 2)->opcode == _PUSH_FRAME);
578-
uint64_t push_operand = (this_instr + 2)->operand;
579-
if (push_operand & 1) {
580-
co = (PyCodeObject *)(push_operand & ~1);
581-
DPRINTF(3, "code=%p ", co);
582-
assert(PyCode_Check(co));
583-
}
584-
else {
585-
PyFunctionObject *func = (PyFunctionObject *)push_operand;
586-
DPRINTF(3, "func=%p ", func);
587-
if (func == NULL) {
588-
DPRINTF(3, "\n");
589-
DPRINTF(1, "Missing function\n");
590-
ctx->done = true;
591-
break;
592-
}
593-
co = (PyCodeObject *)func->func_code;
594-
DPRINTF(3, "code=%p ", co);
578+
co = get_code_with_logging((this_instr + 2));
579+
if (co == NULL) {
580+
ctx->done = true;
581+
break;
595582
}
596583

584+
597585
assert(self_or_null != NULL);
598586
assert(args != NULL);
599587
if (sym_is_not_null(self_or_null)) {
@@ -619,12 +607,17 @@ dummy_func(void) {
619607
}
620608

621609
op(_PY_FRAME_GENERAL, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) {
622-
/* The _Py_UOpsAbstractFrame design assumes that we can copy arguments across directly */
623-
(void)callable;
624-
(void)self_or_null;
625-
(void)args;
626-
new_frame = NULL;
627-
ctx->done = true;
610+
(void)(self_or_null);
611+
(void)(callable);
612+
PyCodeObject *co = NULL;
613+
assert((this_instr + 2)->opcode == _PUSH_FRAME);
614+
co = get_code_with_logging((this_instr + 2));
615+
if (co == NULL) {
616+
ctx->done = true;
617+
break;
618+
}
619+
620+
new_frame = frame_new(ctx, co, 0, NULL, 0);
628621
}
629622

630623
op(_PY_FRAME_KW, (callable, self_or_null, args[oparg], kwnames -- new_frame: _Py_UOpsAbstractFrame *)) {

Python/optimizer_cases.c.h

+14-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)