Skip to content
Closed
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
35 changes: 35 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6152,6 +6152,41 @@ def __exit__(self, e, v, t): ...
f1()
"""), PYTHON_JIT="1")

def test_dynamic_exit_boosts_resume(self):
# gh-149564: When a hot loop calls many distinct exec()-generated
# functions, _COLD_EXIT should boost the callee's RESUME counter
# so it gets traced sooner (propagate call-site hotness to callees).
script_helper.assert_python_ok("-s", "-c", textwrap.dedent(f"""
import _opcode
ns = {{}}
for i in range(20):
exec(f"def fn_{{i}}(x): return x + {{i}}", ns)
fns = [ns[f'fn_{{i}}'] for i in range(20)]
# Hot loop calling many exec'd functions triggers dynamic exits
for _ in range({TIER2_THRESHOLD + 100}):
for fn in fns:
fn(42)
# At least some callees should have gotten their own executors
# thanks to the counter boost on dynamic exit
count = 0
for fn in fns:
code = fn.__code__
co_code = code.co_code
for i in range(0, len(co_code), 2):
try:
_opcode.get_executor(code, i)
count += 1
break
except ValueError:
pass
assert count > 0, f"Expected at least one callee to get an executor, got {{count}}"
"""), PYTHON_JIT="1")

def global_identity(x):
return x

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The JIT now traces ``exec()``-generated functions called from hot loops by
propagating call-site hotness to callees via dynamic exits.
31 changes: 27 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6190,9 +6190,9 @@ dummy_func(
}

tier2 op(_DYNAMIC_EXIT, (exit_p/4 --)) {
_Py_CODEUNIT *target = frame->instr_ptr;
#if defined(Py_DEBUG) && !defined(_Py_JIT)
_PyExitData *exit = (_PyExitData *)exit_p;
_Py_CODEUNIT *target = frame->instr_ptr;
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
if (frame->lltrace >= 3) {
printf("DYNAMIC EXIT: [UOp ");
Expand All @@ -6203,9 +6203,13 @@ dummy_func(
_PyOpcode_OpName[target->op.code]);
}
#endif
// Disabled for now (gh-139109) as it slows down dynamic code tremendously.
// Compile and jump to the cold dynamic executors in the future.
GOTO_TIER_ONE(frame->instr_ptr);
// gh-149564: Propagate call-site hotness to callees.
// If we're landing on a callee's RESUME, boost its counter so it
// gets traced sooner (it was called from a hot trace).
if (target->op.code == RESUME_CHECK_JIT) {
target[1].counter = trigger_backoff_counter();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How can it "boost" its counter? This sets its counter to 0 here. So it resets it. Not boost it.

}
GOTO_TIER_ONE(target);
}

tier2 op(_CHECK_VALIDITY, (--)) {
Expand Down Expand Up @@ -6304,6 +6308,21 @@ dummy_func(
}
else {
SYNC_SP();
// gh-149564: Propagate call-site hotness to callees.
// If exiting to a CALL instruction, boost the callee's RESUME
// counter so it gets traced sooner (called from a hot trace).
if (_PyOpcode_Deopt[target->op.code] == CALL) {
int call_oparg = target->op.arg;
_PyStackRef callable_ref = stack_pointer[-(call_oparg + 2)];
PyObject *callable = PyStackRef_AsPyObjectBorrow(callable_ref);
if (PyFunction_Check(callable)) {
PyCodeObject *code = (PyCodeObject *)((PyFunctionObject *)callable)->func_code;
_Py_CODEUNIT *resume_instr = _PyCode_CODE(code);
if (resume_instr->op.code == RESUME_CHECK_JIT) {
resume_instr[1].counter = trigger_backoff_counter();
}
}
}
if (!backoff_counter_triggers(temperature)) {
exit->temperature = advance_backoff_counter(temperature);
GOTO_TIER_ONE(target);
Expand All @@ -6330,6 +6349,10 @@ dummy_func(
SYNC_SP();
// TODO (gh-139109): This should be similar to _COLD_EXIT in the future.
_Py_CODEUNIT *target = frame->instr_ptr;
// gh-149564: Propagate call-site hotness to callees.
if (target->op.code == RESUME_CHECK_JIT) {
target[1].counter = trigger_backoff_counter();
}
GOTO_TIER_ONE(target);
Py_UNREACHABLE();
}
Expand Down
50 changes: 42 additions & 8 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading