Skip to content

Commit 59d41a9

Browse files
committed
Trace into function calls
- This uses the function-by-version cache I just added - There's not yet a way to trace back via `RETURN_VALUE`
1 parent f62b675 commit 59d41a9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Python/optimizer.c

+29
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ translate_bytecode_to_trace(
444444
code->co_firstlineno,
445445
2 * INSTR_IP(initial_instr, code));
446446

447+
top: // Jump here after _PUSH_FRAME
447448
for (;;) {
448449
RESERVE_RAW(2, "epilogue"); // Always need space for SAVE_IP and EXIT_TRACE
449450
ADD_TO_TRACE(SAVE_IP, INSTR_IP(instr, code), 0);
@@ -617,6 +618,34 @@ translate_bytecode_to_trace(
617618
ADD_TO_TRACE(expansion->uops[i].uop, oparg, operand);
618619
if (expansion->uops[i].uop == _PUSH_FRAME) {
619620
assert(i + 1 == nuops);
621+
int func_version_offset =
622+
offsetof(_PyCallCache, func_version)/sizeof(_Py_CODEUNIT)
623+
// Add one to account for the actual opcode/oparg pair:
624+
+ 1;
625+
uint32_t func_version = read_u32(&instr[func_version_offset].cache);
626+
PyFunctionObject *func = _PyFunction_LookupByVersion(func_version);
627+
DPRINTF(3, "Function object: %p\n", func);
628+
if (func != NULL) {
629+
PyCodeObject *new_code = (PyCodeObject *)PyFunction_GET_CODE(func);
630+
if (new_code == code) {
631+
// Recursive call, bail (we could be here forever).
632+
DPRINTF(2, "Bailing on recursive call to %s (%s:%d)\n",
633+
PyUnicode_AsUTF8(new_code->co_qualname),
634+
PyUnicode_AsUTF8(new_code->co_filename),
635+
new_code->co_firstlineno);
636+
ADD_TO_TRACE(SAVE_IP, 0, 0);
637+
goto done;
638+
}
639+
code = new_code;
640+
initial_instr = instr = _PyCode_CODE(code);
641+
DPRINTF(2,
642+
"Continuing in %s (%s:%d) at byte offset %d\n",
643+
PyUnicode_AsUTF8(code->co_qualname),
644+
PyUnicode_AsUTF8(code->co_filename),
645+
code->co_firstlineno,
646+
2 * INSTR_IP(initial_instr, code));
647+
goto top;
648+
}
620649
ADD_TO_TRACE(SAVE_IP, 0, 0);
621650
goto done;
622651
}

0 commit comments

Comments
 (0)