Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3194,6 +3194,48 @@ class B: ...
for i in range(TIER2_THRESHOLD * 10):
f1()

def test_143183(self):
# https://github.com/python/cpython/issues/143183

result = script_helper.run_python_until_end('-c', textwrap.dedent(f"""
def f1():
class AsyncIter:
def __init__(self):
self.limit = 0
self.count = 0
def __aiter__(self):
return self
async def __anext__(self):
if self.count >= self.limit:
...
self.count += 1j
class AsyncCtx:
async def async_for_driver():
try:
for _ in range({TIER2_THRESHOLD}):
try:
async for _ in AsyncIter():
...
except TypeError:
...
except Exception:
...
c = async_for_driver()
while True:
try:
c.send(None)
except StopIteration:
break
for _ in range({TIER2_THRESHOLD // 40}):
f1()
"""), PYTHON_JIT="1")
self.assertEqual(result[0].rc, 0, result)

def global_identity(x):
return x

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in the JIT when dealing with unsupported control-flow or operations.
20 changes: 8 additions & 12 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ _PyJit_translate_single_bytecode_to_trace(
int trace_length = _tstate->jit_tracer_state.prev_state.code_curr_size;
_PyUOpInstruction *trace = _tstate->jit_tracer_state.code_buffer;
int max_length = _tstate->jit_tracer_state.prev_state.code_max_size;
int exit_op = stop_tracing_opcode == 0 ? _EXIT_TRACE : stop_tracing_opcode;

_Py_CODEUNIT *this_instr = _tstate->jit_tracer_state.prev_state.instr;
_Py_CODEUNIT *target_instr = this_instr;
Expand Down Expand Up @@ -691,8 +692,11 @@ _PyJit_translate_single_bytecode_to_trace(
}

if (stop_tracing_opcode != 0) {
ADD_TO_TRACE(stop_tracing_opcode, 0, 0, target);
goto done;
// gh-143183: It's important we rewind to the last known proper target.
// The current target might be garbage as stop tracing usually indicates
// we are in something that we can't trace.
DPRINTF(2, "Told to stop tracing\n");
goto unsupported;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth to add a DPRINTF() here? All the other goto unsupported have it

}

DPRINTF(2, "%p %d: %s(%d) %d %d\n", old_code, target, _PyOpcode_OpName[opcode], oparg, needs_guard_ip, old_stack_level);
Expand All @@ -703,10 +707,6 @@ _PyJit_translate_single_bytecode_to_trace(
}
#endif

if (opcode == ENTER_EXECUTOR) {
goto full;
}

if (!_tstate->jit_tracer_state.prev_state.dependencies_still_valid) {
goto full;
}
Expand All @@ -720,11 +720,6 @@ _PyJit_translate_single_bytecode_to_trace(

if (oparg > 0xFFFF) {
DPRINTF(2, "Unsupported: oparg too large\n");
goto unsupported;
}

// TODO (gh-140277): The constituent use one extra stack slot. So we need to check for headroom.
if (opcode == BINARY_OP_SUBSCR_GETITEM && old_stack_level + 1 > old_code->co_stacksize) {
unsupported:
{
// Rewind to previous instruction and replace with _EXIT_TRACE.
Expand All @@ -738,14 +733,15 @@ _PyJit_translate_single_bytecode_to_trace(
int32_t old_target = (int32_t)uop_get_target(curr);
curr++;
trace_length++;
curr->opcode = _EXIT_TRACE;
curr->opcode = exit_op;
curr->format = UOP_FORMAT_TARGET;
curr->target = old_target;
}
goto done;
}
}


if (opcode == NOP) {
return 1;
}
Expand Down
Loading