8000 GH-112354: Initial implementation of warm up on exits and trace-stitching by markshannon · Pull Request #114142 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

GH-112354: Initial implementation of warm up on exits and trace-stitching #114142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
1975b4c
Cold exits: Work in progress.
markshannon Jan 10, 2024
9fb97f7
Merge branch 'main' into cold-exits
markshannon Jan 11, 2024
f9aa235
Optimize on side exits
markshannon Jan 11, 2024
1288258
Merge branch 'main' into cold-exits
markshannon Jan 11, 2024
7c6267a
Modify internal interfaces
markshannon Jan 11, 2024
55c48e8
Merge branch 'main' into cold-exits
markshannon Jan 11, 2024
8b3c2e0
Jump to next executor without updating current_executor.
markshannon Jan 11, 2024
92a3b61
Support cycle GC for executors.
markshannon Jan 12, 2024
e3def48
Give cold exits their own class, to fix GC handling of exits
markshannon Jan 16, 2024
87e544b
Generate table of cold exits
markshannon Jan 16, 2024
2172d68
Treat EXIT_TRACE as a side exit
markshannon Jan 16, 2024
4448793
Treat most common guard failures as side exits
markshannon Jan 16, 2024
c70f12f
Tweak generated tble to help C analyzer
markshannon Jan 16, 2024
d73fe0a
Add some documentation about the tier 2 engine
markshannon Jan 16, 2024
5c8f0bd
Fix constness and rename hotness
markshannon Jan 17, 2024
140486b
Add new static objects to ignored file.
markshannon Jan 17, 2024
3362c93
Address review comments
markshannon Jan 18, 2024
63fe653
Transfer executor on thread-state and othe minor changes to be more j…
markshannon Feb 8, 2024
b0991a7
Merge branch 'main' into cold-exits
markshannon Feb 8, 2024
625bce2
Get side exits to build with jit enabled.
markshannon Feb 9, 2024
e191fd7
Initialize cold exits dynamically on demand
markshannon Feb 9, 2024
941a14c
Tidy tier 2 code a bit
markshannon Feb 9, 2024
cfd3285
Add Brandt's fixes
markshannon Feb 9, 2024
1025495
Free the correct amount of memory
markshannon Feb 9, 2024
171dad7
Merge branch 'main' into cold-exits
markshannon Feb 9, 2024
e6ca3fe
Remove unreachable code
markshannon Feb 9, 2024
308b2a7
Merge branch 'main' into cold-exits
markshannon Feb 9, 2024
518143e
Clear executors attached to exits when clearing executors
markshannon Feb 9, 2024
9d8cab8
Merge branch 'main' into cold-exits
markshannon Feb 9, 2024
bf07dad
Merge branch 'main' into cold-exits
markshannon Feb 9, 2024
19b6b84
Keep c-analyzer happy
markshannon Feb 9, 2024
c959e8f
Merge branch 'main' into cold-exits
markshannon Feb 14, 2024
f393ba5
Use threshold for side exits
markshannon Feb 14, 2024
bd66b01
Statically allocate cold exits
markshannon Feb 14, 2024
fe75484
Handle errors in JIT compile
markshannon Feb 14, 2024
3d0110c
Merge branch 'main' into cold-exits
markshannon Feb 14, 2024
de93130
Fix possible leak
markshannon Feb 14, 2024
77a6740
Fix refleak transfering from JIT to tier 1
markshannon Feb 14, 2024
0a61d29
Check that only one of EXIT_IF and DEOPT_IF is present
markshannon Feb 14, 2024
b3e306d
Address review comments
markshannon Feb 14, 2024
8f3aa33
Make exit_index 32 bits to avoid endianness issues in JIT
markshannon Feb 14, 2024
7c84967
Run black
markshannon Feb 15, 2024
8ee6710
Address code review
markshannon Feb 15, 2024
f37d7fc
Update comment
markshannon Feb 15, 2024
1f8967d
Address review comments
markshannon Feb 15, 2024
8e4c601
Fix compiler warning
markshannon Feb 15, 2024
4eb2cfc
Address review comments
markshannon Feb 15, 2024
ebe804f
Add missing brace
markshannon Feb 15, 2024
c38d4e8
Address review comments
markshannon Feb 15, 2024
830eb4e
Keep c-analyzer quiet
markshannon Feb 15, 2024
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
Prev Previous commit
Next Next commit
Support cycle GC for executors.
  • Loading branch information
markshannon committed Jan 16, 2024
commit 92a3b614b0c57694c2c1fde2037537b8e8cca18f
1 change: 0 additions & 1 deletion Include/internal/pycore_uop_ids.h

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

11 changes: 6 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,7 @@ dummy_func(
}

inst(JUMP_BACKWARD, (unused/1 --)) {
TIER_ONE_ONLY
CHECK_EVAL_BREAKER();
assert(oparg <= INSTR_OFFSET());
JUMPBY(-oparg);
Expand Down Expand Up @@ -4087,25 +4088,26 @@ dummy_func(
assert(current_executor->trace[0].opcode != _COLD_EXIT);
_PyExitData *exit = &current_executor->exits[oparg];
exit->hotness++;
assert(exit->executor->trace[0].opcode == _COLD_EXIT);
Copy link
Member

Choose a reason for hiding this comment

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

Could you assert here that ...->trace is equal to next_uop (or maybe it's next_uop - 1)?

Copy link
Member Author

Choose a reason for hiding this comment

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

In the future, trace will only be valid in the T2 interpreter, not the JIT, so these asserts don't actually have much value.

PyCodeObject *code = _PyFrame_GetCode(frame);
_Py_CODEUNIT *target = _PyCode_CODE(code) + exit->target;
if (exit->hotness < 0) {
next_instr = exit->target + _PyCode_CODE(_PyFrame_GetCode(frame));
next_instr = target;
Py_DECREF(current_executor);
current_executor = NULL;
DISPATCH();
}
PyCodeObject *code = _PyFrame_GetCode(frame);
_Py_CODEUNIT *target = _PyCode_CODE(code) + exit->target;
_PyExecutorObject *executor;
if (target->op.code == ENTER_EXECUTOR) {
executor = code->co_executors->executors[target->op.arg];
Py_INCREF(executor);
} else {
int optimized = _PyOptimizer_Optimize(frame, target, stack_pointer, &executor);
if (optimized <= 0) {
exit->hotness = -10000; /* Choose a better number */
Py_DECREF(current_executor);
current_executor = NULL;
next_instr = target;
exit->hotness = -10000; /* Choose a better number */
ERROR_IF(optimized < 0, error);
DISPATCH();
}
Expand All @@ -4119,7 +4121,6 @@ dummy_func(
op(_START_EXECUTOR, (executor/4 --)) {
TIER_TWO_ONLY
Py_DECREF(current_executor);
Py_INCREF(executor);
current_executor = (_PyExecutorObject*)executor;
}

Expand Down
12 changes: 5 additions & 7 deletions Python/executor_cases.c.h

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

1 change: 1 addition & 0 deletions Python/generated_cases.c.h

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

108 changes: 68 additions & 40 deletions Python/optimizer.c
CD1E
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "opcode.h"
#include "pycore_interp.h"
#include "pycore_bitutils.h" // _Py_popcount32()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_OpName[]
#include "pycore_opcode_utils.h" // MAX_REAL_OPCODE
#include "pycore_optimizer.h" // _Py_uop_analyze_and_optimize()
Expand Down Expand Up @@ -230,8 +231,9 @@ static PyMethodDef executor_methods[] = {

static void
uop_dealloc(_PyExecutorObject *self) {
Copy link
Member

Choose a reason for hiding this comment

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

(Nit/off-topic: I just realized that these functions would now be better off being named exe_blah instead of uop_blah.)

Copy link
Member

Choose a reason for hiding this comment

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

Or better, executor_blah, matching e.g. executor_clear.

_PyObject_GC_UNTRACK(self);
_Py_ExecutorClear(self);
PyObject_Free(self);
PyObject_GC_Del(self);
}

const char *
Expand Down Expand Up @@ -282,15 +284,69 @@ PySequenceMethods uop_as_sequence = {
.sq_item = (ssizeargfunc)uop_item,
};

static void
unlink_executor(_PyExecutorObject *executor)
{
if (!executor->vm_data.linked) {
return;
}
_PyExecutorLinkListNode *links = &executor->vm_data.links;
_PyExecutorObject *next = links->next;
_PyExecutorObject *prev = links->previous;
if (next != NULL) {
next->vm_data.links.previous = prev;
}
if (prev != NULL) {
prev->vm_data.links.next = next;
}
else {
// prev == NULL implies that executor is the list head
PyInterpreterState *interp = PyInterpreterState_Get();
assert(interp->executor_list_head == executor);
interp->executor_list_head = next;
}
executor->vm_data.linked = false;
}

/* This must be called by executors during dealloc */
void
_Py_ExecutorClear(_PyExecutorObject *executor)
Copy link
Member

Choose a reason for hiding this comment

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

Do you expect to be calling this from outside this file? There currently don't seem to be callers in other files. (Maybe there would be once we merge the JIT code?)

{
executor->vm_data.valid = 0;
unlink_executor(executor);
for (uint32_t i = 0; i < executor->exit_count; i++) {
Py_CLEAR(executor->exits[i].executor);
}
}

static int
executor_clear(PyObject *o)
{
_Py_ExecutorClear((_PyExecutorObject *)o);
return 0;
}

static int
executor_traverse(PyObject *o, visitproc visit, void *arg)
{
_PyExecutorObject *executor = (_PyExecutorObject *)o;
for (uint32_t i = 0; i < executor->exit_count; i++) {
Py_VISIT(executor->exits[i].executor);
}
return 0;
}

PyTypeObject _PyUOpExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "uop_executor",
.tp_basicsize = offsetof(_PyExecutorObject, exits),
.tp_itemsize = 1,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC,
.tp_dealloc = (destructor)uop_dealloc,
.tp_as_sequence = &uop_as_sequence,
.tp_methods = executor_methods,
.tp_traverse = executor_traverse,
.tp_clear = executor_clear,
};

/* TO DO -- Generate these tables */
Expand All @@ -316,6 +372,7 @@ BRANCH_TO_GUARD[4][2] = {

#define TRACE_STACK_SIZE 5


#define CONFIDENCE_RANGE 1000
#define CONFIDENCE_CUTOFF 333

Expand Down Expand Up @@ -751,7 +808,7 @@ static _PyExecutorObject *
allocate_executor(int exit_count, int length)
{
int size = exit_count*sizeof(_PyExitData) + length*sizeof(_PyUOpInstruction);
_PyExecutorObject *res = PyObject_NewVar(_PyExecutorObject, &_PyUOpExecutor_Type, size);
_PyExecutorObject *res = PyObject_GC_NewVar(_PyExecutorObject, &_PyUOpExecutor_Type, size);
if (res == NULL) {
return NULL;
}
Expand Down Expand Up @@ -852,6 +909,7 @@ make_executor_from_uops(_PyUOpInstruction *buffer, _PyBloomFilter *dependencies)
}
}
#endif
_PyObject_GC_TRACK(executor);
return executor;
}

Expand Down Expand Up @@ -919,19 +977,22 @@ PyUnstable_Optimizer_NewUOpOptimizer(void)

static void
counter_dealloc(_PyExecutorObject *self) {
PyObject *opt = (PyObject *)self->trace[0].operand;
/* The optimizer is the operand of the first uop. */
PyObject *opt = (PyObject *)self->trace[1].operand;
Py_DECREF(opt);
uop_dealloc(self);
}

PyTypeObject _PyCounterExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "counting_executor",
.tp_basicsize = offsetof(_PyExecutorObject, trace),
.tp_itemsize = sizeof(_PyUOpInstruction),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_basicsize = offsetof(_PyExecutorObject, exits),
.tp_itemsize = 1,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC,
.tp_dealloc = (destructor)counter_dealloc,
.tp_methods = executor_methods,
.tp_traverse = executor_traverse,
.tp_clear = executor_clear,
};

static int
Expand Down Expand Up @@ -1120,29 +1181,6 @@ link_executor(_PyExecutorObject *executor)
assert(interp->executor_list_head->vm_data.links.previous == NULL);
}

static void
unlink_executor(_PyExecutorObject *executor)
{
if (!executor->vm_data.linked) {
return;
}
_PyExecutorLinkListNode *links = &executor->vm_data.links;
_PyExecutorObject *next = links->next;
_PyExecutorObject *prev = links->previous;
if (next != NULL) {
next->vm_data.links.previous = prev;
}
if (prev != NULL) {
prev->vm_data.links.next = next;
}
else {
// prev == NULL implies that executor is the list head
PyInterpreterState *interp = PyInterpreterState_Get();
assert(interp->executor_list_head == executor);
interp->executor_list_head = next;
}
executor->vm_data.linked = false;
}

/* This must be called by optimizers before using the executor */
void
Expand All @@ -1155,16 +1193,6 @@ _Py_ExecutorInit(_PyExecutorObject *executor, _PyBloomFilter *dependency_set)
link_executor(executor);
}

/* This must be called by executors during dealloc */
void
_Py_ExecutorClear(_PyExecutorObject *executor)
{
unlink_executor(executor);
for (uint32_t i = 0; i < executor->exit_count; i++) {
Py_DECREF(executor->exits[i].executor);
}
}

void
_Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj)
{
Expand Down
0