Skip to content

Commit

Permalink
Support EXTENDED_ARG; improve offsets in debug output
Browse files Browse the repository at this point in the history
Offsets in debug output identifying the start of the trace
are now measured in bytes (and the text shows it).

Note that there is a pre-existing bug where
```
./python -Xuops -m test test_builtin
```
fails. Will debug that later.
  • Loading branch information
gvanrossum committed Jul 5, 2023
1 parent 00525f0 commit d7df54b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2728,11 +2728,11 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
#endif

DPRINTF(3,
"Entering _PyUopExecute for %s (%s:%d) at offset %ld\n",
"Entering _PyUopExecute for %s (%s:%d) at byte offset %ld\n",
PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_qualname),
PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_filename),
_PyFrame_GetCode(frame)->co_firstlineno,
(long)(frame->prev_instr + 1 -
2 * (long)(frame->prev_instr + 1 -
(_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive));

PyThreadState *tstate = _PyThreadState_GET();
Expand Down
34 changes: 25 additions & 9 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ _PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNI
}
insert_executor(code, src, index, executor);
assert(frame->prev_instr == src);
frame->prev_instr = dest - 1;
return executor->execute(executor, frame, stack_pointer);
jump_to_destination:
frame->prev_instr = dest - 1;
Expand All @@ -201,7 +202,7 @@ PyUnstable_GetExecutor(PyCodeObject *code, int offset)
}
i += _PyInstruction_GetLength(code, i);
}
PyErr_SetString(PyExc_ValueError, "no executor at given offset");
PyErr_SetString(PyExc_ValueError, "no executor at given byte offset");
return NULL;
}

Expand Down Expand Up @@ -399,22 +400,28 @@ translate_bytecode_to_trace(
trace_length++;

DPRINTF(4,
"Optimizing %s (%s:%d) at offset %ld\n",
"Optimizing %s (%s:%d) at byte offset %ld\n",
PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno,
(long)(instr - (_Py_CODEUNIT *)code->co_code_adaptive));
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive));

for (;;) {
ADD_TO_TRACE(SAVE_IP, (int)(instr - (_Py_CODEUNIT *)code->co_code_adaptive));
int opcode = instr->op.code;
uint64_t operand = instr->op.arg;
// TODO: EXTENDED_ARG handling
int extras = 0;
while (opcode == EXTENDED_ARG) {
instr++;
extras += 1;
opcode = instr->op.code;
operand = (operand << 8) | instr->op.arg;
}
if (opcode == ENTER_EXECUTOR) {
_PyExecutorObject *executor = (_PyExecutorObject *)code->co_executors->executors[operand&255];
opcode = executor->vm_data.opcode;
DPRINTF(2, " * ENTER_EXECUTOR -> %s\n", _PyOpcode_OpName[opcode]);
operand = executor->vm_data.oparg; // TODO: EXTENDED_ARG handling
operand = (operand & 0xffffff00) | executor->vm_data.oparg;
}
switch (opcode) {
case LOAD_FAST_LOAD_FAST:
Expand Down Expand Up @@ -474,6 +481,15 @@ translate_bytecode_to_trace(
int offset = expansion->uops[i].offset;
switch (expansion->uops[i].size) {
case 0:
if (extras && OPCODE_HAS_JUMP(opcode)) {
if (opcode == JUMP_BACKWARD_NO_INTERRUPT) {
operand -= extras;
}
else {
assert(opcode != JUMP_BACKWARD);
operand += extras;
}
}
break;
case 1:
operand = read_u16(&instr[offset].cache);
Expand Down Expand Up @@ -512,21 +528,21 @@ translate_bytecode_to_trace(
if (trace_length > 3) {
ADD_TO_TRACE(EXIT_TRACE, 0);
DPRINTF(1,
"Created a trace for %s (%s:%d) at offset %ld -- length %d\n",
"Created a trace for %s (%s:%d) at byte offset %ld -- length %d\n",
PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno,
(long)(instr - (_Py_CODEUNIT *)code->co_code_adaptive),
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive),
trace_length);
return trace_length;
}
else {
DPRINTF(4,
"No trace for %s (%s:%d) at offset %ld\n",
"No trace for %s (%s:%d) at byte offset %ld\n",
PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno,
(long)(instr - (_Py_CODEUNIT *)code->co_code_adaptive));
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive));
}
return 0;

Expand Down

0 comments on commit d7df54b

Please sign in to comment.