Coverage Report

Created: 2026-05-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/optimizer.c
Line
Count
Source
1
#include "Python.h"
2
3
#ifdef _Py_TIER2
4
5
#include "opcode.h"
6
#include "pycore_interp.h"
7
#include "pycore_backoff.h"
8
#include "pycore_bitutils.h"        // _Py_popcount32()
9
#include "pycore_ceval.h"       // _Py_set_eval_breaker_bit
10
#include "pycore_code.h"            // _Py_GetBaseCodeUnit
11
#include "pycore_interpframe.h"
12
#include "pycore_object.h"          // _PyObject_GC_UNTRACK()
13
#include "pycore_opcode_metadata.h" // _PyOpcode_OpName[]
14
#include "pycore_opcode_utils.h"  // MAX_REAL_OPCODE
15
#include "pycore_optimizer.h"     // _Py_uop_analyze_and_optimize()
16
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
17
#include "pycore_tuple.h"         // _PyTuple_FromArraySteal
18
#include "pycore_unicodeobject.h" // _PyUnicode_FromASCII
19
#include "pycore_uop_ids.h"
20
#include "pycore_jit.h"
21
#include <stdbool.h>
22
#include <stdint.h>
23
#include <stddef.h>
24
25
#define NEED_OPCODE_METADATA
26
#include "pycore_uop_metadata.h" // Uop tables
27
#undef NEED_OPCODE_METADATA
28
29
#define MAX_EXECUTORS_SIZE 256
30
31
// Trace too short, no progress:
32
// _START_EXECUTOR
33
// _MAKE_WARM
34
// _CHECK_VALIDITY
35
// _SET_IP
36
// is 4-5 instructions.
37
#define CODE_SIZE_NO_PROGRESS 5
38
// We start with _START_EXECUTOR, _MAKE_WARM
39
#define CODE_SIZE_EMPTY 2
40
41
#define _PyExecutorObject_CAST(op)  ((_PyExecutorObject *)(op))
42
43
#ifndef Py_GIL_DISABLED
44
static bool
45
has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
46
{
47
    if (code == (PyCodeObject *)&_Py_InitCleanup) {
48
        return false;
49
    }
50
    if (instr->op.code == ENTER_EXECUTOR) {
51
        return true;
52
    }
53
    if (code->co_executors == NULL) {
54
        return true;
55
    }
56
    return code->co_executors->size < MAX_EXECUTORS_SIZE;
57
}
58
59
static int32_t
60
get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
61
{
62
    if (instr->op.code == ENTER_EXECUTOR) {
63
        return instr->op.arg;
64
    }
65
    _PyExecutorArray *old = code->co_executors;
66
    int size = 0;
67
    int capacity = 0;
68
    if (old != NULL) {
69
        size = old->size;
70
        capacity = old->capacity;
71
        assert(size < MAX_EXECUTORS_SIZE);
72
    }
73
    assert(size <= capacity);
74
    if (size == capacity) {
75
        /* Array is full. Grow array */
76
        int new_capacity = capacity ? capacity * 2 : 4;
77
        _PyExecutorArray *new = PyMem_Realloc(
78
            old,
79
            offsetof(_PyExecutorArray, executors) +
80
            new_capacity * sizeof(_PyExecutorObject *));
81
        if (new == NULL) {
82
            return -1;
83
        }
84
        new->capacity = new_capacity;
85
        new->size = size;
86
        code->co_executors = new;
87
    }
88
    assert(size < code->co_executors->capacity);
89
    return size;
90
}
91
92
static void
93
insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorObject *executor)
94
{
95
    Py_INCREF(executor);
96
    if (instr->op.code == ENTER_EXECUTOR) {
97
        assert(index == instr->op.arg);
98
        _Py_ExecutorDetach(code->co_executors->executors[index]);
99
    }
100
    else {
101
        assert(code->co_executors->size == index);
102
        assert(code->co_executors->capacity > index);
103
        code->co_executors->size++;
104
    }
105
    executor->vm_data.opcode = instr->op.code;
106
    executor->vm_data.oparg = instr->op.arg;
107
    executor->vm_data.code = code;
108
    executor->vm_data.index = (int)(instr - _PyCode_CODE(code));
109
    code->co_executors->executors[index] = executor;
110
    assert(index < MAX_EXECUTORS_SIZE);
111
    instr->op.code = ENTER_EXECUTOR;
112
    instr->op.arg = index;
113
}
114
#endif // Py_GIL_DISABLED
115
116
static _PyExecutorObject *
117
make_executor_from_uops(_PyThreadStateImpl *tstate, _PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies);
118
119
static int
120
uop_optimize(_PyInterpreterFrame *frame, PyThreadState *tstate,
121
             _PyExecutorObject **exec_ptr,
122
             bool progress_needed);
123
124
/* Returns 1 if optimized, 0 if not optimized, and -1 for an error.
125
 * If optimized, *executor_ptr contains a new reference to the executor
126
 */
127
// gh-137573: inlining this function causes stack overflows
128
Py_NO_INLINE int
129
_PyOptimizer_Optimize(
130
    _PyInterpreterFrame *frame, PyThreadState *tstate)
131
{
132
    _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
133
    PyInterpreterState *interp = _PyInterpreterState_GET();
134
    if (!interp->jit) {
135
        // gh-140936: It is possible that interp->jit will become false during
136
        // interpreter finalization. However, the specialized JUMP_BACKWARD_JIT
137
        // instruction may still be present. In this case, we should
138
        // return immediately without optimization.
139
        return 0;
140
    }
141
    _PyExecutorObject *prev_executor = _tstate->jit_tracer_state->initial_state.executor;
142
    if (prev_executor != NULL && !prev_executor->vm_data.valid) {
143
        // gh-143604: If we are a side exit executor and the original executor is no
144
        // longer valid, don't compile to prevent a reference leak.
145
        return 0;
146
    }
147
    assert(!interp->compiling);
148
    assert(_tstate->jit_tracer_state->initial_state.stack_depth >= 0);
149
#ifndef Py_GIL_DISABLED
150
    assert(_tstate->jit_tracer_state->initial_state.func != NULL);
151
    interp->compiling = true;
152
    // The first executor in a chain and the MAX_CHAIN_DEPTH'th executor *must*
153
    // make progress in order to avoid infinite loops or excessively-long
154
    // side-exit chains. We can only insert the executor into the bytecode if
155
    // this is true, since a deopt won't infinitely re-enter the executor:
156
    int chain_depth = _tstate->jit_tracer_state->initial_state.chain_depth;
157
    chain_depth %= MAX_CHAIN_DEPTH;
158
    bool progress_needed = chain_depth == 0;
159
    PyCodeObject *code = (PyCodeObject *)_tstate->jit_tracer_state->initial_state.code;
160
    _Py_CODEUNIT *start = _tstate->jit_tracer_state->initial_state.start_instr;
161
    if (progress_needed && !has_space_for_executor(code, start)) {
162
        interp->compiling = false;
163
        return 0;
164
    }
165
    _PyExecutorObject *executor;
166
    int err = uop_optimize(frame, tstate, &executor, progress_needed);
167
    if (err <= 0) {
168
        interp->compiling = false;
169
        return err;
170
    }
171
    assert(executor != NULL);
172
    if (progress_needed) {
173
        int index = get_index_for_executor(code, start);
174
        if (index < 0) {
175
            /* Out of memory. Don't raise and assume that the
176
             * error will show up elsewhere.
177
             *
178
             * If an optimizer has already produced an executor,
179
             * it might get confused by the executor disappearing,
180
             * but there is not much we can do about that here. */
181
            Py_DECREF(executor);
182
            interp->compiling = false;
183
            return 0;
184
        }
185
        insert_executor(code, start, index, executor);
186
    }
187
    executor->vm_data.chain_depth = chain_depth;
188
    assert(executor->vm_data.valid);
189
    _PyExitData *exit = _tstate->jit_tracer_state->initial_state.exit;
190
    if (exit != NULL && !progress_needed) {
191
        exit->executor = executor;
192
    }
193
    else {
194
        // An executor inserted into the code object now has a strong reference
195
        // to it from the code object. Thus, we don't need this reference anymore.
196
        Py_DECREF(executor);
197
    }
198
    interp->compiling = false;
199
    return 1;
200
#else
201
    return 0;
202
#endif
203
}
204
205
static _PyExecutorObject *
206
get_executor_lock_held(PyCodeObject *code, int offset)
207
{
208
    int code_len = (int)Py_SIZE(code);
209
    for (int i = 0 ; i < code_len;) {
210
        if (_PyCode_CODE(code)[i].op.code == ENTER_EXECUTOR && i*2 == offset) {
211
            int oparg = _PyCode_CODE(code)[i].op.arg;
212
            _PyExecutorObject *res = code->co_executors->executors[oparg];
213
            Py_INCREF(res);
214
            return res;
215
        }
216
        i += _PyInstruction_GetLength(code, i);
217
    }
218
    PyErr_SetString(PyExc_ValueError, "no executor at given byte offset");
219
    return NULL;
220
}
221
222
_PyExecutorObject *
223
_Py_GetExecutor(PyCodeObject *code, int offset)
224
{
225
    _PyExecutorObject *executor;
226
    Py_BEGIN_CRITICAL_SECTION(code);
227
    executor = get_executor_lock_held(code, offset);
228
    Py_END_CRITICAL_SECTION();
229
    return executor;
230
}
231
232
static PyObject *
233
is_valid(PyObject *self, PyObject *Py_UNUSED(ignored))
234
{
235
    return PyBool_FromLong(((_PyExecutorObject *)self)->vm_data.valid);
236
}
237
238
static PyObject *
239
get_opcode(PyObject *self, PyObject *Py_UNUSED(ignored))
240
{
241
    return PyLong_FromUnsignedLong(((_PyExecutorObject *)self)->vm_data.opcode);
242
}
243
244
static PyObject *
245
get_oparg(PyObject *self, PyObject *Py_UNUSED(ignored))
246
{
247
    return PyLong_FromUnsignedLong(((_PyExecutorObject *)self)->vm_data.oparg);
248
}
249
250
///////////////////// Experimental UOp Optimizer /////////////////////
251
252
static int executor_clear(PyObject *executor);
253
254
void
255
_PyExecutor_Free(_PyExecutorObject *self)
256
{
257
#ifdef _Py_JIT
258
    _PyJIT_Free(self);
259
#endif
260
    PyObject_GC_Del(self);
261
}
262
263
static void executor_invalidate(PyObject *op);
264
265
static void
266
executor_clear_exits(_PyExecutorObject *executor)
267
{
268
    _PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
269
    _PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor();
270
    for (uint32_t i = 0; i < executor->exit_count; i++) {
271
        _PyExitData *exit = &executor->exits[i];
272
        exit->temperature = initial_unreachable_backoff_counter();
273
        _PyExecutorObject *old = executor->exits[i].executor;
274
        exit->executor = exit->is_dynamic ? cold_dynamic : cold;
275
        Py_DECREF(old);
276
    }
277
}
278
279
280
void
281
_Py_ClearExecutorDeletionList(PyInterpreterState *interp)
282
{
283
    if (interp->executor_deletion_list_head == NULL) {
284
        return;
285
    }
286
    _PyRuntimeState *runtime = &_PyRuntime;
287
    HEAD_LOCK(runtime);
288
    PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
289
    while (ts) {
290
        _PyExecutorObject *current = (_PyExecutorObject *)ts->current_executor;
291
        Py_XINCREF(current);
292
        ts = ts->next;
293
    }
294
    HEAD_UNLOCK(runtime);
295
    _PyExecutorObject *keep_list = NULL;
296
    do {
297
        _PyExecutorObject *exec = interp->executor_deletion_list_head;
298
        interp->executor_deletion_list_head = exec->vm_data.links.next;
299
        if (Py_REFCNT(exec) == 0) {
300
            _PyExecutor_Free(exec);
301
        } else {
302
            exec->vm_data.links.next = keep_list;
303
            keep_list = exec;
304
        }
305
    } while (interp->executor_deletion_list_head != NULL);
306
    interp->executor_deletion_list_head = keep_list;
307
    HEAD_LOCK(runtime);
308
    ts = PyInterpreterState_ThreadHead(interp);
309
    while (ts) {
310
        _PyExecutorObject *current = (_PyExecutorObject *)ts->current_executor;
311
        if (current != NULL) {
312
            Py_DECREF((PyObject *)current);
313
        }
314
        ts = ts->next;
315
    }
316
    HEAD_UNLOCK(runtime);
317
}
318
319
static void
320
add_to_pending_deletion_list(_PyExecutorObject *self)
321
{
322
    if (self->vm_data.pending_deletion) {
323
        return;
324
    }
325
    self->vm_data.pending_deletion = 1;
326
    PyInterpreterState *interp = PyInterpreterState_Get();
327
    self->vm_data.links.previous = NULL;
328
    self->vm_data.links.next = interp->executor_deletion_list_head;
329
    interp->executor_deletion_list_head = self;
330
}
331
332
static void
333
uop_dealloc(PyObject *op) {
334
    _PyExecutorObject *self = _PyExecutorObject_CAST(op);
335
    executor_invalidate(op);
336
    assert(self->vm_data.code == NULL);
337
    add_to_pending_deletion_list(self);
338
}
339
340
const char *
341
_PyUOpName(int index)
342
{
343
    if (index < 0 || index > MAX_UOP_REGS_ID) {
344
        return NULL;
345
    }
346
    return _PyOpcode_uop_name[index];
347
}
348
349
#ifdef Py_DEBUG
350
void
351
_PyUOpPrint(const _PyUOpInstruction *uop)
352
{
353
    const char *name = _PyUOpName(uop->opcode);
354
    if (name == NULL) {
355
        printf("<uop %d>", uop->opcode);
356
    }
357
    else {
358
        printf("%s", name);
359
    }
360
    switch(uop->format) {
361
        case UOP_FORMAT_TARGET:
362
            printf(" (%d, target=%d, operand0=%#" PRIx64 ", operand1=%#" PRIx64,
363
                uop->oparg,
364
                uop->target,
365
                (uint64_t)uop->operand0,
366
                (uint64_t)uop->operand1);
367
            break;
368
        case UOP_FORMAT_JUMP:
369
            printf(" (%d, jump_target=%d, operand0=%#" PRIx64 ", operand1=%#" PRIx64,
370
                uop->oparg,
371
                uop->jump_target,
372
                (uint64_t)uop->operand0,
373
                (uint64_t)uop->operand1);
374
            break;
375
        default:
376
            printf(" (%d, Unknown format)", uop->oparg);
377
    }
378
    if (_PyUop_Flags[_PyUop_Uncached[uop->opcode]] & HAS_ERROR_FLAG) {
379
        printf(", error_target=%d", uop->error_target);
380
    }
381
382
    printf(")");
383
}
384
#endif
385
386
static Py_ssize_t
387
uop_len(PyObject *op)
388
{
389
    _PyExecutorObject *self = _PyExecutorObject_CAST(op);
390
    return self->code_size;
391
}
392
393
static PyObject *
394
uop_item(PyObject *op, Py_ssize_t index)
395
{
396
    _PyExecutorObject *self = _PyExecutorObject_CAST(op);
397
    Py_ssize_t len = uop_len(op);
398
    if (index < 0 || index >= len) {
399
        PyErr_SetNone(PyExc_IndexError);
400
        return NULL;
401
    }
402
    int opcode = self->trace[index].opcode;
403
    int base_opcode = _PyUop_Uncached[opcode];
404
    const char *name = _PyUOpName(base_opcode);
405
    if (name == NULL) {
406
        name = "<nil>";
407
    }
408
    PyObject *oname = _PyUnicode_FromASCII(name, strlen(name));
409
    if (oname == NULL) {
410
        return NULL;
411
    }
412
    PyObject *oparg = PyLong_FromUnsignedLong(self->trace[index].oparg);
413
    if (oparg == NULL) {
414
        Py_DECREF(oname);
415
        return NULL;
416
    }
417
    PyObject *target = PyLong_FromUnsignedLong(self->trace[index].target);
418
    if (target == NULL) {
419
        Py_DECREF(oparg);
420
        Py_DECREF(oname);
421
        return NULL;
422
    }
423
    PyObject *operand = PyLong_FromUnsignedLongLong(self->trace[index].operand0);
424
    if (operand == NULL) {
425
        Py_DECREF(target);
426
        Py_DECREF(oparg);
427
        Py_DECREF(oname);
428
        return NULL;
429
    }
430
    PyObject *args[4] = { oname, oparg, target, operand };
431
    return _PyTuple_FromArraySteal(args, 4);
432
}
433
434
PySequenceMethods uop_as_sequence = {
435
    .sq_length = uop_len,
436
    .sq_item = uop_item,
437
};
438
439
static int
440
executor_traverse(PyObject *o, visitproc visit, void *arg)
441
{
442
    _PyExecutorObject *executor = _PyExecutorObject_CAST(o);
443
    for (uint32_t i = 0; i < executor->exit_count; i++) {
444
        Py_VISIT(executor->exits[i].executor);
445
    }
446
    return 0;
447
}
448
449
static PyObject *
450
get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored))
451
{
452
#ifndef _Py_JIT
453
    PyErr_SetString(PyExc_RuntimeError, "JIT support not enabled.");
454
    return NULL;
455
#else
456
    _PyExecutorObject *executor = _PyExecutorObject_CAST(self);
457
    if (executor->jit_code == NULL || executor->jit_size == 0) {
458
        Py_RETURN_NONE;
459
    }
460
    return PyBytes_FromStringAndSize(executor->jit_code, executor->jit_size);
461
#endif
462
}
463
464
static PyMethodDef uop_executor_methods[] = {
465
    { "is_valid", is_valid, METH_NOARGS, NULL },
466
    { "get_jit_code", get_jit_code, METH_NOARGS, NULL},
467
    { "get_opcode", get_opcode, METH_NOARGS, NULL },
468
    { "get_oparg", get_oparg, METH_NOARGS, NULL },
469
    { NULL, NULL },
470
};
471
472
static int
473
executor_is_gc(PyObject *o)
474
{
475
    return !_Py_IsImmortal(o);
476
}
477
478
PyTypeObject _PyUOpExecutor_Type = {
479
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
480
    .tp_name = "uop_executor",
481
    .tp_basicsize = offsetof(_PyExecutorObject, exits),
482
    .tp_itemsize = 1,
483
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC,
484
    .tp_dealloc = uop_dealloc,
485
    .tp_as_sequence = &uop_as_sequence,
486
    .tp_methods = uop_executor_methods,
487
    .tp_traverse = executor_traverse,
488
    .tp_clear = executor_clear,
489
    .tp_is_gc = executor_is_gc,
490
};
491
492
/* TO DO -- Generate these tables */
493
static const uint16_t
494
_PyUOp_Replacements[MAX_UOP_ID + 1] = {
495
    [_ITER_JUMP_RANGE] = _GUARD_NOT_EXHAUSTED_RANGE,
496
    [_ITER_JUMP_LIST] = _GUARD_NOT_EXHAUSTED_LIST,
497
    [_ITER_JUMP_TUPLE] = _GUARD_NOT_EXHAUSTED_TUPLE,
498
    [_FOR_ITER] = _FOR_ITER_TIER_TWO,
499
    [_FOR_ITER_VIRTUAL] = _FOR_ITER_VIRTUAL_TIER_TWO,
500
    [_ITER_NEXT_LIST] = _ITER_NEXT_LIST_TIER_TWO,
501
    [_CHECK_PERIODIC_AT_END] = _TIER2_RESUME_CHECK,
502
    [_LOAD_BYTECODE] = _NOP,
503
    [_SEND_VIRTUAL] = _SEND_VIRTUAL_TIER_TWO,
504
    [_SEND_ASYNC_GEN] = _SEND_ASYNC_GEN_TIER_TWO,
505
};
506
507
static const uint8_t
508
is_for_iter_test[MAX_UOP_ID + 1] = {
509
    [_GUARD_NOT_EXHAUSTED_RANGE] = 1,
510
    [_GUARD_NOT_EXHAUSTED_LIST] = 1,
511
    [_GUARD_NOT_EXHAUSTED_TUPLE] = 1,
512
    [_FOR_ITER_TIER_TWO] = 1,
513
    [_ITER_NEXT_INLINE] = 1,
514
};
515
516
static const uint16_t
517
BRANCH_TO_GUARD[4][2] = {
518
    [POP_JUMP_IF_FALSE - POP_JUMP_IF_FALSE][0] = _GUARD_IS_TRUE_POP,
519
    [POP_JUMP_IF_FALSE - POP_JUMP_IF_FALSE][1] = _GUARD_IS_FALSE_POP,
520
    [POP_JUMP_IF_TRUE - POP_JUMP_IF_FALSE][0] = _GUARD_IS_FALSE_POP,
521
    [POP_JUMP_IF_TRUE - POP_JUMP_IF_FALSE][1] = _GUARD_IS_TRUE_POP,
522
    [POP_JUMP_IF_NONE - POP_JUMP_IF_FALSE][0] = _GUARD_IS_NOT_NONE_POP,
523
    [POP_JUMP_IF_NONE - POP_JUMP_IF_FALSE][1] = _GUARD_IS_NONE_POP,
524
    [POP_JUMP_IF_NOT_NONE - POP_JUMP_IF_FALSE][0] = _GUARD_IS_NONE_POP,
525
    [POP_JUMP_IF_NOT_NONE - POP_JUMP_IF_FALSE][1] = _GUARD_IS_NOT_NONE_POP,
526
};
527
528
static const uint16_t
529
guard_ip_uop[MAX_UOP_ID + 1] = {
530
    [_PUSH_FRAME] = _GUARD_IP__PUSH_FRAME,
531
    [_RETURN_GENERATOR] = _GUARD_IP_RETURN_GENERATOR,
532
    [_RETURN_VALUE] = _GUARD_IP_RETURN_VALUE,
533
    [_YIELD_VALUE] = _GUARD_IP_YIELD_VALUE,
534
};
535
536
static const uint16_t
537
guard_code_version_uop[MAX_UOP_ID + 1] = {
538
    [_PUSH_FRAME] = _GUARD_CODE_VERSION__PUSH_FRAME,
539
    [_RETURN_GENERATOR] = _GUARD_CODE_VERSION_RETURN_GENERATOR,
540
    [_RETURN_VALUE] = _GUARD_CODE_VERSION_RETURN_VALUE,
541
    [_YIELD_VALUE] = _GUARD_CODE_VERSION_YIELD_VALUE,
542
};
543
544
static const uint16_t
545
dynamic_exit_uop[MAX_UOP_ID + 1] = {
546
    [_GUARD_IP__PUSH_FRAME] = 1,
547
    [_GUARD_IP_RETURN_GENERATOR] = 1,
548
    [_GUARD_IP_RETURN_VALUE] = 1,
549
    [_GUARD_IP_YIELD_VALUE] = 1,
550
    [_GUARD_CODE_VERSION__PUSH_FRAME] = 1,
551
    [_GUARD_CODE_VERSION_RETURN_GENERATOR] = 1,
552
    [_GUARD_CODE_VERSION_RETURN_VALUE] = 1,
553
    [_GUARD_CODE_VERSION_YIELD_VALUE] = 1,
554
};
555
556
557
558
#ifdef Py_DEBUG
559
#define DPRINTF(level, ...) \
560
    if (lltrace >= (level)) { printf(__VA_ARGS__); }
561
#else
562
#define DPRINTF(level, ...)
563
#endif
564
565
566
static inline void
567
add_to_trace(
568
    _PyJitTracerState *tracer,
569
    uint16_t opcode,
570
    uint16_t oparg,
571
    uint64_t operand,
572
    uint32_t target)
573
{
574
    _PyJitUopBuffer *trace = &tracer->code_buffer;
575
    _PyUOpInstruction *inst = trace->next;
576
    inst->opcode = opcode;
577
    inst->format = UOP_FORMAT_TARGET;
578
    inst->target = target;
579
    inst->oparg = oparg;
580
    inst->operand0 = operand;
581
#ifdef Py_STATS
582
    inst->execution_count = 0;
583
    inst->fitness = tracer->translator_state.fitness;
584
#endif
585
    trace->next++;
586
}
587
588
589
#ifdef Py_DEBUG
590
#define ADD_TO_TRACE(OPCODE, OPARG, OPERAND, TARGET) \
591
    add_to_trace(tracer, (OPCODE), (OPARG), (OPERAND), (TARGET)); \
592
    if (lltrace >= 2) { \
593
        printf("%4d ADD_TO_TRACE: ", uop_buffer_length(trace)); \
594
        _PyUOpPrint(uop_buffer_last(trace)); \
595
        printf("\n"); \
596
    }
597
#else
598
#define ADD_TO_TRACE(OPCODE, OPARG, OPERAND, TARGET) \
599
    add_to_trace(tracer, (OPCODE), (OPARG), (OPERAND), (TARGET))
600
#endif
601
602
#define INSTR_IP(INSTR, CODE) \
603
    ((uint32_t)((INSTR) - ((_Py_CODEUNIT *)(CODE)->co_code_adaptive)))
604
605
606
/* Branch penalty: 0 for a fully biased branch and FITNESS_BRANCH_BALANCED for
607
 * a balanced or fully off-trace branch. This keeps any single branch from
608
 * consuming more than one balanced-branch cost.
609
 */
610
static inline int
611
compute_branch_penalty(uint16_t history)
612
{
613
    bool branch_taken = history & 1;
614
    int taken_count = _Py_popcount32((uint32_t)history);
615
    int on_trace_count = branch_taken ? taken_count : 16 - taken_count;
616
    int off_trace = 16 - on_trace_count;
617
    int penalty = off_trace * FITNESS_BRANCH_BALANCED / 8;
618
    if (penalty > FITNESS_BRANCH_BALANCED) {
619
        penalty = FITNESS_BRANCH_BALANCED;
620
    }
621
    return penalty;
622
}
623
624
/* Compute exit quality for the current trace position.
625
 * Higher values mean better places to stop the trace. */
626
static inline int32_t
627
compute_exit_quality(_Py_CODEUNIT *target_instr, int opcode,
628
                     const _PyJitTracerState *tracer)
629
{
630
    if (target_instr == tracer->initial_state.close_loop_instr) {
631
        return EXIT_QUALITY_CLOSE_LOOP;
632
    }
633
    else if (target_instr->op.code == ENTER_EXECUTOR) {
634
        return EXIT_QUALITY_ENTER_EXECUTOR;
635
    }
636
    else if (opcode == JUMP_BACKWARD_JIT ||
637
        opcode == JUMP_BACKWARD ||
638
        opcode == JUMP_BACKWARD_NO_INTERRUPT) {
639
        return EXIT_QUALITY_BACKWARD_EDGE;
640
    }
641
    else if (_PyOpcode_Caches[_PyOpcode_Deopt[opcode]] > 0) {
642
        return EXIT_QUALITY_SPECIALIZABLE;
643
    }
644
    return EXIT_QUALITY_DEFAULT;
645
}
646
647
/* Frame penalty: (MAX_ABSTRACT_FRAME_DEPTH-1) pushes exhaust fitness. */
648
static inline int32_t
649
compute_frame_penalty(uint16_t fitness_initial)
650
{
651
    return (int32_t)fitness_initial / (MAX_ABSTRACT_FRAME_DEPTH - 1) + 1;
652
}
653
654
static int
655
is_terminator(const _PyUOpInstruction *uop)
656
{
657
    int opcode = _PyUop_Uncached[uop->opcode];
658
    return (
659
        opcode == _EXIT_TRACE ||
660
        opcode == _DEOPT ||
661
        opcode == _JUMP_TO_TOP ||
662
        opcode == _DYNAMIC_EXIT
663
    );
664
}
665
666
/* Returns 1 on success (added to trace), 0 on trace end.
667
 */
668
// gh-142543: inlining this function causes stack overflows
669
Py_NO_INLINE int
670
_PyJit_translate_single_bytecode_to_trace(
671
    PyThreadState *tstate,
672
    _PyInterpreterFrame *frame,
673
    _Py_CODEUNIT *next_instr,
674
    int stop_tracing_opcode)
675
{
676
677
#ifdef Py_DEBUG
678
    char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
679
    int lltrace = 0;
680
    if (python_lltrace != NULL && *python_lltrace >= '0') {
681
        lltrace = *python_lltrace - '0';  // TODO: Parse an int and all that
682
    }
683
#endif
684
    _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
685
    _PyJitTracerState *tracer = _tstate->jit_tracer_state;
686
    PyCodeObject *old_code = tracer->prev_state.instr_code;
687
    bool progress_needed = (tracer->initial_state.chain_depth % MAX_CHAIN_DEPTH) == 0;
688
    _PyJitUopBuffer *trace = &tracer->code_buffer;
689
690
    _Py_CODEUNIT *this_instr =  tracer->prev_state.instr;
691
    _Py_CODEUNIT *target_instr = this_instr;
692
    uint32_t target = 0;
693
694
    target = Py_IsNone((PyObject *)old_code)
695
        ? (uint32_t)(target_instr - _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR)
696
        : INSTR_IP(target_instr, old_code);
697
698
    // Rewind EXTENDED_ARG so that we see the whole thing.
699
    // We must point to the first EXTENDED_ARG when deopting.
700
    int oparg = tracer->prev_state.instr_oparg;
701
    int opcode = this_instr->op.code;
702
    int rewind_oparg = oparg;
703
    while (rewind_oparg > 255) {
704
        rewind_oparg >>= 8;
705
        target--;
706
    }
707
708
    if (opcode == ENTER_EXECUTOR) {
709
        _PyExecutorObject *executor = old_code->co_executors->executors[oparg & 255];
710
        opcode = executor->vm_data.opcode;
711
        oparg = (oparg & ~255) | executor->vm_data.oparg;
712
    }
713
714
    if (_PyOpcode_Caches[_PyOpcode_Deopt[opcode]] > 0) {
715
        uint16_t backoff = (this_instr + 1)->counter.value_and_backoff;
716
        // adaptive_counter_cooldown is a fresh specialization.
717
        // trigger_backoff_counter is what we set during tracing.
718
        // All tracing backoffs should be freshly specialized or untouched.
719
        // If not, that indicates a deopt during tracing, and
720
        // thus the "actual" instruction executed is not the one that is
721
        // in the instruction stream, but rather the deopt.
722
        // It's important we check for this, as some specializations might make
723
        // no progress (they can immediately deopt after specializing).
724
        // We do this to improve performance, as otherwise a compiled trace
725
        // will just deopt immediately.
726
        if (backoff != adaptive_counter_cooldown().value_and_backoff &&
727
            backoff != trigger_backoff_counter().value_and_backoff) {
728
            OPT_STAT_INC(trace_immediately_deopts);
729
            opcode = _PyOpcode_Deopt[opcode];
730
        }
731
    }
732
733
    // Strange control-flow
734
    bool has_dynamic_jump_taken = OPCODE_HAS_UNPREDICTABLE_JUMP(opcode) &&
735
        (next_instr != this_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]);
736
737
    /* Special case the first instruction,
738
    * so that we can guarantee forward progress */
739
    if (progress_needed && uop_buffer_length(&tracer->code_buffer) < CODE_SIZE_NO_PROGRESS) {
740
        if (OPCODE_HAS_EXIT(opcode) || OPCODE_HAS_DEOPT(opcode)) {
741
            opcode = _PyOpcode_Deopt[opcode];
742
        }
743
        assert(!OPCODE_HAS_EXIT(opcode));
744
        assert(!OPCODE_HAS_DEOPT(opcode));
745
    }
746
747
    bool needs_guard_ip = OPCODE_HAS_NEEDS_GUARD_IP(opcode);
748
    if (has_dynamic_jump_taken && !needs_guard_ip) {
749
        DPRINTF(2, "Unsupported: dynamic jump taken %s\n", _PyOpcode_OpName[opcode]);
750
        goto unsupported;
751
    }
752
753
    int is_sys_tracing = (tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL);
754
    if (is_sys_tracing) {
755
        goto done;
756
    }
757
758
    if (stop_tracing_opcode == _DEOPT) {
759
        // gh-143183: It's important we rewind to the last known proper target.
760
        // The current target might be garbage as stop tracing usually indicates
761
        // we are in something that we can't trace.
762
        DPRINTF(2, "Told to stop tracing\n");
763
        goto unsupported;
764
    }
765
    else if (stop_tracing_opcode != 0) {
766
        assert(stop_tracing_opcode == _EXIT_TRACE);
767
        ADD_TO_TRACE(stop_tracing_opcode, 0, 0, target);
768
        goto done;
769
    }
770
771
    DPRINTF(2, "%p %d: %s(%d) %d\n", old_code, target, _PyOpcode_OpName[opcode], oparg, needs_guard_ip);
772
773
#ifdef Py_DEBUG
774
    if (oparg > 255) {
775
        assert(_Py_GetBaseCodeUnit(old_code, target).op.code == EXTENDED_ARG);
776
    }
777
#endif
778
779
    // This happens when a recursive call happens that we can't trace. Such as Python -> C -> Python calls
780
    // If we haven't guarded the IP, then it's untraceable.
781
    if (frame != tracer->prev_state.instr_frame && !needs_guard_ip) {
782
        DPRINTF(2, "Unsupported: unguardable jump taken\n");
783
        goto unsupported;
784
    }
785
786
    if (oparg > 0xFFFF) {
787
        DPRINTF(2, "Unsupported: oparg too large\n");
788
        unsupported:
789
        {
790
            _PyUOpInstruction *curr = uop_buffer_last(trace);
791
            while (curr->opcode != _SET_IP && uop_buffer_length(trace) > 2) {
792
                trace->next--;
793
                curr = uop_buffer_last(trace);
794
            }
795
            if (curr->opcode == _SET_IP) {
796
                int32_t old_target = (int32_t)uop_get_target(curr);
797
                curr->opcode = _DEOPT;
798
                curr->format = UOP_FORMAT_TARGET;
799
                curr->target = old_target;
800
            }
801
            goto done;
802
        }
803
    }
804
805
    if (opcode == NOP) {
806
        return 1;
807
    }
808
809
    if (opcode == JUMP_FORWARD) {
810
        return 1;
811
    }
812
813
    if (opcode == EXTENDED_ARG) {
814
        return 1;
815
    }
816
817
    // Stop the trace if fitness has dropped below the exit quality threshold.
818
    _PyJitTracerTranslatorState *ts = &tracer->translator_state;
819
    int32_t eq = compute_exit_quality(target_instr, opcode, tracer);
820
    DPRINTF(3, "Fitness check: %s(%d) fitness=%d, exit_quality=%d, depth=%d\n",
821
            _PyOpcode_OpName[opcode], oparg, ts->fitness, eq, ts->frame_depth);
822
823
    if (ts->fitness < eq) {
824
        // Heuristic exit: leave operand1=0 so the side exit increments chain_depth.
825
        ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);
826
        OPT_STAT_INC(fitness_terminated_traces);
827
        DPRINTF(2, "Fitness terminated: %s(%d) fitness=%d < exit_quality=%d\n",
828
                _PyOpcode_OpName[opcode], oparg, ts->fitness, eq);
829
        goto done;
830
    }
831
832
    // Snapshot remaining space so the later fitness charge reflects all buffer
833
    // space this bytecode consumed, including reserved tail slots.
834
    int32_t remaining_before = uop_buffer_remaining_space(trace);
835
836
    // One for possible _DEOPT, one because _CHECK_VALIDITY itself might _DEOPT
837
    trace->end -= 2;
838
839
    const _PyOpcodeRecordSlotMap *record_slot_map = &_PyOpcode_RecordSlotMaps[opcode];
840
841
    assert(opcode != ENTER_EXECUTOR && opcode != EXTENDED_ARG);
842
    assert(!_PyErr_Occurred(tstate));
843
844
845
    if (OPCODE_HAS_EXIT(opcode)) {
846
        // Make space for side exit
847
        trace->end--;
848
    }
849
    if (OPCODE_HAS_ERROR(opcode)) {
850
        // Make space for error stub
851
        trace->end--;
852
    }
853
    if (OPCODE_HAS_DEOPT(opcode)) {
854
        // Make space for side exit
855
        trace->end--;
856
    }
857
858
    // _GUARD_IP leads to an exit.
859
    trace->end -= needs_guard_ip;
860
861
#if Py_DEBUG
862
    const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode];
863
    int space_needed = expansion->nuops + needs_guard_ip + 2 + (!OPCODE_HAS_NO_SAVE_IP(opcode));
864
    assert(uop_buffer_remaining_space(trace) > space_needed);
865
#endif
866
867
    ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0, target);
868
869
    if (!OPCODE_HAS_NO_SAVE_IP(opcode)) {
870
        ADD_TO_TRACE(_SET_IP, 0, (uintptr_t)target_instr, target);
871
    }
872
873
    switch (opcode) {
874
        case POP_JUMP_IF_NONE:
875
        case POP_JUMP_IF_NOT_NONE:
876
        case POP_JUMP_IF_FALSE:
877
        case POP_JUMP_IF_TRUE:
878
        {
879
            _Py_CODEUNIT *computed_next_instr_without_modifiers = target_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
880
            _Py_CODEUNIT *computed_next_instr = computed_next_instr_without_modifiers + (computed_next_instr_without_modifiers->op.code == NOT_TAKEN);
881
            _Py_CODEUNIT *computed_jump_instr = computed_next_instr_without_modifiers + oparg;
882
            assert(next_instr == computed_next_instr || next_instr == computed_jump_instr);
883
            int jump_happened = target_instr[1].cache & 1;
884
            assert(jump_happened ? (next_instr == computed_jump_instr) : (next_instr == computed_next_instr));
885
            uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_happened];
886
            ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(jump_happened ? computed_next_instr : computed_jump_instr, old_code));
887
            int bp = compute_branch_penalty(target_instr[1].cache);
888
            tracer->translator_state.fitness -= bp;
889
            DPRINTF(3, "  branch penalty: -%d (history=0x%04x, taken=%d) -> fitness=%d\n",
890
                    bp, target_instr[1].cache, jump_happened,
891
                    tracer->translator_state.fitness);
892
893
            break;
894
        }
895
        case JUMP_BACKWARD_JIT:
896
            // This is possible as the JIT might have re-activated after it was disabled
897
        case JUMP_BACKWARD_NO_JIT:
898
        case JUMP_BACKWARD:
899
            ADD_TO_TRACE(_CHECK_PERIODIC, 0, 0, target);
900
            break;
901
        case JUMP_BACKWARD_NO_INTERRUPT:
902
            break;
903
904
        case RESUME:
905
        case RESUME_CHECK:
906
        case RESUME_CHECK_JIT:
907
            /* Use a special tier 2 version of RESUME_CHECK to allow traces to
908
             *  start with RESUME_CHECK */
909
            ADD_TO_TRACE(_TIER2_RESUME_CHECK, 0, 0, target);
910
            break;
911
        default:
912
        {
913
            const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode];
914
            // Reserve space for nuops (+ _SET_IP + _EXIT_TRACE)
915
            int nuops = expansion->nuops;
916
            if (nuops == 0) {
917
                DPRINTF(2, "Unsupported opcode %s\n", _PyOpcode_OpName[opcode]);
918
                goto unsupported;
919
            }
920
            assert(nuops > 0);
921
            uint32_t orig_oparg = oparg;  // For OPARG_TOP/BOTTOM
922
            uint32_t orig_target = target;
923
            int record_idx = 0;
924
            for (int i = 0; i < nuops; i++) {
925
                oparg = orig_oparg;
926
                target = orig_target;
927
                uint32_t uop = expansion->uops[i].uop;
928
                uint64_t operand = 0;
929
                // Add one to account for the actual opcode/oparg pair:
930
                int offset = expansion->uops[i].offset + 1;
931
                switch (expansion->uops[i].size) {
932
                    case OPARG_SIMPLE:
933
                        assert(opcode != _JUMP_BACKWARD_NO_INTERRUPT && opcode != JUMP_BACKWARD);
934
                        break;
935
                    case OPARG_CACHE_1:
936
                        operand = read_u16(&this_instr[offset].cache);
937
                        break;
938
                    case OPARG_CACHE_2:
939
                        operand = read_u32(&this_instr[offset].cache);
940
                        break;
941
                    case OPARG_CACHE_4:
942
                        operand = read_u64(&this_instr[offset].cache);
943
                        break;
944
                    case OPARG_TOP:  // First half of super-instr
945
                        assert(orig_oparg <= 255);
946
                        oparg = orig_oparg >> 4;
947
                        break;
948
                    case OPARG_BOTTOM:  // Second half of super-instr
949
                        assert(orig_oparg <= 255);
950
                        oparg = orig_oparg & 0xF;
951
                        break;
952
                    case OPARG_SAVE_RETURN_OFFSET:  // op=_SAVE_RETURN_OFFSET; oparg=return_offset
953
                        oparg = offset;
954
                        assert(uop == _SAVE_RETURN_OFFSET);
955
                        break;
956
                    case OPARG_REPLACED:
957
                        uop = _PyUOp_Replacements[uop];
958
                        assert(uop != 0);
959
960
                        uint32_t next_inst = target + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
961
                        if (uop == _TIER2_RESUME_CHECK) {
962
                            target = next_inst;
963
                        }
964
                        else {
965
                            int extended_arg = orig_oparg > 255;
966
                            uint32_t jump_target = next_inst + orig_oparg + extended_arg;
967
                            /* Jump must be to an "END" either END_FOR or END_SEND */
968
                            assert((
969
                                    _Py_GetBaseCodeUnit(old_code, jump_target).op.code == END_FOR &&
970
                                    _Py_GetBaseCodeUnit(old_code, jump_target+1).op.code == POP_ITER
971
                                )
972
                                ||
973
                                _Py_GetBaseCodeUnit(old_code, jump_target).op.code == END_SEND
974
                            );
975
                            if (is_for_iter_test[uop]) {
976
                                target = jump_target + 1;
977
                            }
978
                        }
979
                        break;
980
                    case OPERAND1_1:
981
                        assert(uop_buffer_last(trace)->opcode == uop);
982
                        operand = read_u16(&this_instr[offset].cache);
983
                        uop_buffer_last(trace)->operand1 = operand;
984
                        continue;
985
                    case OPERAND1_2:
986
                        assert(uop_buffer_last(trace)->opcode == uop);
987
                        operand = read_u32(&this_instr[offset].cache);
988
                        uop_buffer_last(trace)->operand1 = operand;
989
                        continue;
990
                    case OPERAND1_4:
991
                        assert(uop_buffer_last(trace)->opcode == uop);
992
                        operand = read_u64(&this_instr[offset].cache);
993
                        uop_buffer_last(trace)->operand1 = operand;
994
                        continue;
995
                    default:
996
                        fprintf(stderr,
997
                                "opcode=%d, oparg=%d; nuops=%d, i=%d; size=%d, offset=%d\n",
998
                                opcode, oparg, nuops, i,
999
                                expansion->uops[i].size,
1000
                                expansion->uops[i].offset);
1001
                        Py_FatalError("garbled expansion");
1002
                }
1003
                if (uop == _BINARY_OP_INPLACE_ADD_UNICODE) {
1004
                    assert(i + 1 == nuops);
1005
                    _Py_CODEUNIT *next = target_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
1006
                    assert(next->op.code == STORE_FAST);
1007
                    operand = next->op.arg;
1008
                }
1009
                else if (uop == _PUSH_FRAME) {
1010
                    _PyJitTracerTranslatorState *ts_depth = &tracer->translator_state;
1011
                    ts_depth->frame_depth++;
1012
                    assert(ts_depth->frame_depth < MAX_ABSTRACT_FRAME_DEPTH);
1013
                    int32_t frame_penalty = compute_frame_penalty(tstate->interp->opt_config.fitness_initial);
1014
                    ts_depth->fitness -= frame_penalty;
1015
                    DPRINTF(3, "  _PUSH_FRAME: depth=%d, penalty=-%d -> fitness=%d\n",
1016
                            ts_depth->frame_depth, frame_penalty,
1017
                            ts_depth->fitness);
1018
                }
1019
                else if (uop == _RETURN_VALUE || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE) {
1020
                    _PyJitTracerTranslatorState *ts_depth = &tracer->translator_state;
1021
                    int32_t frame_penalty = compute_frame_penalty(tstate->interp->opt_config.fitness_initial);
1022
                    if (ts_depth->frame_depth <= 0) {
1023
                        // Returning past the traced root is normal for guarded
1024
                        // caller continuation. Charge a small penalty so these
1025
                        // paths still terminate.
1026
                        int32_t underflow_penalty = frame_penalty / 4;
1027
                        ts_depth->fitness -= underflow_penalty;
1028
                        DPRINTF(3, "  %s: underflow penalty=-%d -> fitness=%d\n",
1029
                                _PyOpcode_uop_name[uop], underflow_penalty,
1030
                                ts_depth->fitness);
1031
                    }
1032
                    else {
1033
                        // Symmetric with push: net-zero frame impact.
1034
                        ts_depth->fitness += frame_penalty;
1035
                        ts_depth->frame_depth--;
1036
                        DPRINTF(3, "  %s: return reward=+%d, depth=%d -> fitness=%d\n",
1037
                                _PyOpcode_uop_name[uop], frame_penalty,
1038
                                ts_depth->frame_depth,
1039
                                ts_depth->fitness);
1040
                    }
1041
                }
1042
                else if (_PyUop_Flags[uop] & HAS_RECORDS_VALUE_FLAG) {
1043
                    assert(record_idx < record_slot_map->count);
1044
                    uint8_t record_slot = record_slot_map->slots[record_idx];
1045
                    assert(record_slot < tracer->prev_state.recorded_count);
1046
                    PyObject *recorded_value = tracer->prev_state.recorded_values[record_slot];
1047
                    tracer->prev_state.recorded_values[record_slot] = NULL;
1048
                    if ((record_slot_map->transform_mask & (1u << record_idx)) &&
1049
                        recorded_value != NULL) {
1050
                        recorded_value = _PyOpcode_RecordTransformValue(uop, recorded_value);
1051
                    }
1052
                    record_idx++;
1053
                    operand = (uintptr_t)recorded_value;
1054
                }
1055
                // All other instructions
1056
                ADD_TO_TRACE(uop, oparg, operand, target);
1057
            }
1058
            break;
1059
        }  // End default
1060
1061
    }  // End switch (opcode)
1062
1063
    if (needs_guard_ip) {
1064
        int last_opcode = uop_buffer_last(trace)->opcode;
1065
        uint16_t guard_ip = guard_ip_uop[last_opcode];
1066
        if (guard_ip == 0) {
1067
            DPRINTF(1, "Unknown uop needing guard ip %s\n", _PyOpcode_uop_name[last_opcode]);
1068
            Py_UNREACHABLE();
1069
        }
1070
        PyObject *code = PyStackRef_AsPyObjectBorrow(frame->f_executable);
1071
        Py_INCREF(code);
1072
        ADD_TO_TRACE(_RECORD_CODE, 0, (uintptr_t)code, 0);
1073
        ADD_TO_TRACE(guard_ip, 0, (uintptr_t)next_instr, 0);
1074
        if (PyCode_Check(code)) {
1075
            /* Record stack depth, in operand1 */
1076
            int stack_depth = (int)(frame->stackpointer - _PyFrame_Stackbase(frame));
1077
            uop_buffer_last(trace)->operand1 = stack_depth;
1078
            ADD_TO_TRACE(guard_code_version_uop[last_opcode], 0, ((PyCodeObject *)code)->co_version, 0);
1079
        }
1080
    }
1081
    // Loop back to the start
1082
    int is_first_instr = tracer->initial_state.close_loop_instr == next_instr ||
1083
        tracer->initial_state.start_instr == next_instr;
1084
    if (is_first_instr && uop_buffer_length(trace) > CODE_SIZE_NO_PROGRESS) {
1085
        if (needs_guard_ip) {
1086
            ADD_TO_TRACE(_SET_IP, 0, (uintptr_t)next_instr, 0);
1087
        }
1088
        ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0, 0);
1089
        goto done;
1090
    }
1091
    // Charge fitness by trace-buffer capacity consumed for this bytecode,
1092
    // including both emitted uops and tail reservations.
1093
    {
1094
        int32_t slots_used = remaining_before - uop_buffer_remaining_space(trace);
1095
        tracer->translator_state.fitness -= slots_used;
1096
        DPRINTF(3, "  per-insn cost: -%d -> fitness=%d\n", slots_used,
1097
                tracer->translator_state.fitness);
1098
    }
1099
    DPRINTF(2, "Trace continuing (fitness=%d)\n", tracer->translator_state.fitness);
1100
    return 1;
1101
done:
1102
    DPRINTF(2, "Trace done\n");
1103
    if (!is_terminator(uop_buffer_last(trace))) {
1104
        ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);
1105
    }
1106
    return 0;
1107
}
1108
1109
// Returns 0 for do not enter tracing, 1 on enter tracing.
1110
// gh-142543: inlining this function causes stack overflows
1111
Py_NO_INLINE int
1112
_PyJit_TryInitializeTracing(
1113
    PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *curr_instr,
1114
    _Py_CODEUNIT *start_instr, _Py_CODEUNIT *close_loop_instr, _PyStackRef *stack_pointer, int chain_depth,
1115
    _PyExitData *exit, int oparg, _PyExecutorObject *current_executor)
1116
{
1117
    _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1118
    if (_tstate->jit_tracer_state == NULL) {
1119
        _tstate->jit_tracer_state = (_PyJitTracerState *)_PyObject_VirtualAlloc(sizeof(_PyJitTracerState));
1120
        if (_tstate->jit_tracer_state == NULL) {
1121
            // Don't error, just go to next instruction.
1122
            return 0;
1123
        }
1124
        _tstate->jit_tracer_state->is_tracing = false;
1125
    }
1126
    _PyJitTracerState *tracer = _tstate->jit_tracer_state;
1127
    // A recursive trace.
1128
    if (tracer->is_tracing) {
1129
        return 0;
1130
    }
1131
    if (oparg > 0xFFFF) {
1132
        return 0;
1133
    }
1134
    PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
1135
    if (func == NULL || !PyFunction_Check(func)) {
1136
        return 0;
1137
    }
1138
    PyCodeObject *code = _PyFrame_GetCode(frame);
1139
#ifdef Py_DEBUG
1140
    char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
1141
    int lltrace = 0;
1142
    if (python_lltrace != NULL && *python_lltrace >= '0') {
1143
        lltrace = *python_lltrace - '0';  // TODO: Parse an int and all that
1144
    }
1145
    DPRINTF(2,
1146
        "Tracing %s (%s:%d) at byte offset %d at chain depth %d\n",
1147
        PyUnicode_AsUTF8(code->co_qualname),
1148
        PyUnicode_AsUTF8(code->co_filename),
1149
        code->co_firstlineno,
1150
        2 * INSTR_IP(close_loop_instr, code),
1151
        chain_depth);
1152
#endif
1153
    /* Set up tracing buffer*/
1154
    _PyJitUopBuffer *trace = &tracer->code_buffer;
1155
    uop_buffer_init(trace, &tracer->uop_array[0], UOP_MAX_TRACE_LENGTH);
1156
    _PyJitTracerTranslatorState *ts = &tracer->translator_state;
1157
    ts->fitness = tstate->interp->opt_config.fitness_initial;
1158
    ts->frame_depth = 0;
1159
    ADD_TO_TRACE(_START_EXECUTOR, 0, (uintptr_t)start_instr, INSTR_IP(start_instr, code));
1160
    ADD_TO_TRACE(_MAKE_WARM, 0, 0, 0);
1161
1162
    tracer->initial_state.start_instr = start_instr;
1163
    tracer->initial_state.close_loop_instr = close_loop_instr;
1164
    tracer->initial_state.code = (PyCodeObject *)Py_NewRef(code);
1165
    tracer->initial_state.func = (PyFunctionObject *)Py_NewRef(func);
1166
    tracer->initial_state.executor = (_PyExecutorObject *)Py_XNewRef(current_executor);
1167
    tracer->initial_state.exit = exit;
1168
    tracer->initial_state.stack_depth = (int)(stack_pointer - _PyFrame_Stackbase(frame));
1169
    tracer->initial_state.chain_depth = chain_depth;
1170
    tracer->prev_state.instr_code = (PyCodeObject *)Py_NewRef(_PyFrame_GetCode(frame));
1171
    tracer->prev_state.instr = curr_instr;
1172
    tracer->prev_state.instr_frame = frame;
1173
    tracer->prev_state.instr_oparg = oparg;
1174
    tracer->prev_state.instr_stacklevel = tracer->initial_state.stack_depth;
1175
    tracer->prev_state.recorded_count = 0;
1176
    for (int i = 0; i < MAX_RECORDED_VALUES; i++) {
1177
        tracer->prev_state.recorded_values[i] = NULL;
1178
    }
1179
    const _PyOpcodeRecordEntry *record_entry = &_PyOpcode_RecordEntries[curr_instr->op.code];
1180
    for (int i = 0; i < record_entry->count; i++) {
1181
        _Py_RecordFuncPtr record_func = _PyOpcode_RecordFunctions[record_entry->indices[i]];
1182
        record_func(frame, stack_pointer, oparg, &tracer->prev_state.recorded_values[i]);
1183
    }
1184
    tracer->prev_state.recorded_count = record_entry->count;
1185
    assert(curr_instr->op.code == JUMP_BACKWARD_JIT || curr_instr->op.code == RESUME_CHECK_JIT || (exit != NULL));
1186
    tracer->initial_state.jump_backward_instr = curr_instr;
1187
1188
    DPRINTF(3, "Fitness init: chain_depth=%d, fitness=%d\n",
1189
            chain_depth, ts->fitness);
1190
1191
    tracer->is_tracing = true;
1192
    return 1;
1193
}
1194
1195
Py_NO_INLINE void
1196
_PyJit_FinalizeTracing(PyThreadState *tstate, int err)
1197
{
1198
    _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1199
    _PyJitTracerState *tracer = _tstate->jit_tracer_state;
1200
    // Deal with backoffs
1201
    assert(tracer != NULL);
1202
    _PyExitData *exit = tracer->initial_state.exit;
1203
    if (exit == NULL) {
1204
        // We hold a strong reference to the code object, so the instruction won't be freed.
1205
        if (err <= 0) {
1206
            _Py_BackoffCounter counter = tracer->initial_state.jump_backward_instr[1].counter;
1207
            tracer->initial_state.jump_backward_instr[1].counter = restart_backoff_counter(counter);
1208
        }
1209
        else {
1210
            if (tracer->initial_state.jump_backward_instr[0].op.code == JUMP_BACKWARD_JIT) {
1211
                tracer->initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(&tstate->interp->opt_config);
1212
            }
1213
            else {
1214
                tracer->initial_state.jump_backward_instr[1].counter = initial_resume_backoff_counter(&tstate->interp->opt_config);
1215
            }
1216
        }
1217
    }
1218
    else if (tracer->initial_state.executor->vm_data.valid) {
1219
        // Likewise, we hold a strong reference to the executor containing this exit, so the exit is guaranteed
1220
        // to be valid to access.
1221
        if (err <= 0) {
1222
            exit->temperature = restart_backoff_counter(exit->temperature);
1223
        }
1224
        else {
1225
            exit->temperature = initial_temperature_backoff_counter(&tstate->interp->opt_config);
1226
        }
1227
    }
1228
    // Clear all recorded values
1229
    _PyJitUopBuffer *buffer = &tracer->code_buffer;
1230
    for (_PyUOpInstruction *inst = buffer->start; inst < buffer->next; inst++) {
1231
        if (_PyUop_Flags[inst->opcode] & HAS_RECORDS_VALUE_FLAG) {
1232
            Py_XDECREF((PyObject *)(uintptr_t)inst->operand0);
1233
        }
1234
    }
1235
    Py_CLEAR(tracer->initial_state.code);
1236
    Py_CLEAR(tracer->initial_state.func);
1237
    Py_CLEAR(tracer->initial_state.executor);
1238
    Py_CLEAR(tracer->prev_state.instr_code);
1239
    for (int i = 0; i < MAX_RECORDED_VALUES; i++) {
1240
        Py_CLEAR(tracer->prev_state.recorded_values[i]);
1241
    }
1242
    tracer->prev_state.recorded_count = 0;
1243
    uop_buffer_init(buffer, &tracer->uop_array[0], UOP_MAX_TRACE_LENGTH);
1244
    tracer->is_tracing = false;
1245
}
1246
1247
bool
1248
_PyJit_EnterExecutorShouldStopTracing(int og_opcode)
1249
{
1250
    // Continue tracing (skip over the executor). If it's a RESUME
1251
    // trace to form longer, more optimizeable traces.
1252
    // We want to trace over RESUME traces. Otherwise, functions with lots of RESUME
1253
    // end up with many fragmented traces which perform badly.
1254
    // See for example, the richards benchmark in pyperformance.
1255
    // For consideration: We may want to consider tracing over side traces
1256
    // inserted into bytecode as well in the future.
1257
    return og_opcode == RESUME_CHECK_JIT;
1258
}
1259
1260
void
1261
_PyJit_TracerFree(_PyThreadStateImpl *_tstate)
1262
{
1263
    if (_tstate->jit_tracer_state != NULL) {
1264
        _PyObject_VirtualFree(_tstate->jit_tracer_state, sizeof(_PyJitTracerState));
1265
        _tstate->jit_tracer_state = NULL;
1266
    }
1267
}
1268
1269
#undef RESERVE
1270
#undef INSTR_IP
1271
#undef ADD_TO_TRACE
1272
#undef DPRINTF
1273
1274
#define UNSET_BIT(array, bit) (array[(bit)>>5] &= ~(1<<((bit)&31)))
1275
#define SET_BIT(array, bit) (array[(bit)>>5] |= (1<<((bit)&31)))
1276
#define BIT_IS_SET(array, bit) (array[(bit)>>5] & (1<<((bit)&31)))
1277
1278
/* Count the number of unused uops and exits
1279
*/
1280
static int
1281
count_exits(_PyUOpInstruction *buffer, int length)
1282
{
1283
    int exit_count = 0;
1284
    for (int i = 0; i < length; i++) {
1285
        uint16_t base_opcode = _PyUop_Uncached[buffer[i].opcode];
1286
        if (base_opcode == _EXIT_TRACE || base_opcode == _DYNAMIC_EXIT) {
1287
            exit_count++;
1288
        }
1289
    }
1290
    return exit_count;
1291
}
1292
1293
/* The number of cached registers at any exit (`EXIT_IF` or `DEOPT_IF`)
1294
 * This is the number of cached at entries at start, unless the uop is
1295
 * marked as `exit_depth_is_output` in which case it is the number of
1296
 * cached entries at the end */
1297
static int
1298
get_cached_entries_for_side_exit(_PyUOpInstruction *inst)
1299
{
1300
    // Maybe add another generated table for this?
1301
    int base_opcode = _PyUop_Uncached[inst->opcode];
1302
    assert(base_opcode != 0);
1303
    for (int i = 0; i <= MAX_CACHED_REGISTER; i++) {
1304
        const _PyUopTOSentry *entry = &_PyUop_Caching[base_opcode].entries[i];
1305
        if (entry->opcode == inst->opcode) {
1306
            return entry->exit;
1307
        }
1308
    }
1309
    Py_UNREACHABLE();
1310
}
1311
1312
static void make_exit(_PyUOpInstruction *inst, int opcode, int target, bool is_control_flow)
1313
{
1314
    assert(opcode > MAX_UOP_ID && opcode <= MAX_UOP_REGS_ID);
1315
    inst->opcode = opcode;
1316
    inst->oparg = 0;
1317
    inst->operand0 = 0;
1318
    inst->format = UOP_FORMAT_TARGET;
1319
    inst->target = target;
1320
    inst->operand1 = is_control_flow;
1321
#ifdef Py_STATS
1322
    inst->fitness = 0;
1323
    inst->execution_count = 0;
1324
#endif
1325
}
1326
1327
/* Convert implicit exits, errors and deopts
1328
 * into explicit ones. */
1329
static int
1330
prepare_for_execution(_PyUOpInstruction *buffer, int length)
1331
{
1332
    int32_t current_jump = -1;
1333
    int32_t current_jump_target = -1;
1334
    int32_t current_error = -1;
1335
    int32_t current_error_target = -1;
1336
    int32_t current_popped = -1;
1337
    int32_t current_exit_op = -1;
1338
    /* Leaving in NOPs slows down the interpreter and messes up the stats */
1339
    _PyUOpInstruction *copy_to = &buffer[0];
1340
    for (int i = 0; i < length; i++) {
1341
        _PyUOpInstruction *inst = &buffer[i];
1342
        if (inst->opcode != _NOP) {
1343
            if (copy_to != inst) {
1344
                *copy_to = *inst;
1345
            }
1346
            copy_to++;
1347
        }
1348
    }
1349
    length = (int)(copy_to - buffer);
1350
    int next_spare = length;
1351
    for (int i = 0; i < length; i++) {
1352
        _PyUOpInstruction *inst = &buffer[i];
1353
        int base_opcode = _PyUop_Uncached[inst->opcode];
1354
        assert(inst->opcode != _NOP);
1355
        int32_t target = (int32_t)uop_get_target(inst);
1356
        uint16_t exit_flags = _PyUop_Flags[base_opcode] & (HAS_EXIT_FLAG | HAS_DEOPT_FLAG | HAS_PERIODIC_FLAG);
1357
        if (exit_flags) {
1358
            uint16_t base_exit_op = _EXIT_TRACE;
1359
            if (exit_flags & HAS_DEOPT_FLAG) {
1360
                base_exit_op = _DEOPT;
1361
            }
1362
            else if (exit_flags & HAS_PERIODIC_FLAG) {
1363
                base_exit_op = _HANDLE_PENDING_AND_DEOPT;
1364
            }
1365
            int32_t jump_target = target;
1366
            if (dynamic_exit_uop[base_opcode]) {
1367
                base_exit_op = _DYNAMIC_EXIT;
1368
            }
1369
            int exit_depth = get_cached_entries_for_side_exit(inst);
1370
            assert(_PyUop_Caching[base_exit_op].entries[exit_depth].opcode > 0);
1371
            int16_t exit_op = _PyUop_Caching[base_exit_op].entries[exit_depth].opcode;
1372
            bool is_control_flow = (base_opcode == _GUARD_IS_FALSE_POP || base_opcode == _GUARD_IS_TRUE_POP || is_for_iter_test[base_opcode]);
1373
            if (jump_target != current_jump_target || current_exit_op != exit_op) {
1374
                make_exit(&buffer[next_spare], exit_op, jump_target, is_control_flow);
1375
                current_exit_op = exit_op;
1376
                current_jump_target = jump_target;
1377
                current_jump = next_spare;
1378
                next_spare++;
1379
            }
1380
            buffer[i].jump_target = current_jump;
1381
            buffer[i].format = UOP_FORMAT_JUMP;
1382
        }
1383
        if (_PyUop_Flags[base_opcode] & HAS_ERROR_FLAG) {
1384
            int popped = (_PyUop_Flags[base_opcode] & HAS_ERROR_NO_POP_FLAG) ?
1385
                0 : _PyUop_num_popped(base_opcode, inst->oparg);
1386
            if (target != current_error_target || popped != current_popped) {
1387
                current_popped = popped;
1388
                current_error = next_spare;
1389
                current_error_target = target;
1390
                make_exit(&buffer[next_spare], _ERROR_POP_N_r00, 0, false);
1391
                buffer[next_spare].operand0 = target;
1392
                next_spare++;
1393
            }
1394
            buffer[i].error_target = current_error;
1395
            if (buffer[i].format == UOP_FORMAT_TARGET) {
1396
                buffer[i].format = UOP_FORMAT_JUMP;
1397
                buffer[i].jump_target = 0;
1398
            }
1399
        }
1400
        if (base_opcode == _JUMP_TO_TOP) {
1401
            assert(_PyUop_Uncached[buffer[0].opcode] == _START_EXECUTOR);
1402
            buffer[i].format = UOP_FORMAT_JUMP;
1403
            buffer[i].jump_target = 1;
1404
        }
1405
    }
1406
    return next_spare;
1407
}
1408
1409
/* Executor side exits */
1410
1411
static _PyExecutorObject *
1412
allocate_executor(int exit_count, int length)
1413
{
1414
    int size = exit_count*sizeof(_PyExitData) + length*sizeof(_PyUOpInstruction);
1415
    _PyExecutorObject *res = PyObject_GC_NewVar(_PyExecutorObject, &_PyUOpExecutor_Type, size);
1416
    if (res == NULL) {
1417
        return NULL;
1418
    }
1419
    res->trace = (_PyUOpInstruction *)(res->exits + exit_count);
1420
    res->code_size = length;
1421
    res->exit_count = exit_count;
1422
    res->jit_registration = NULL;
1423
    return res;
1424
}
1425
1426
#ifdef Py_DEBUG
1427
1428
#define CHECK(PRED) \
1429
if (!(PRED)) { \
1430
    printf(#PRED " at %d\n", i); \
1431
    assert(0); \
1432
}
1433
1434
static int
1435
target_unused(int opcode)
1436
{
1437
    return (_PyUop_Flags[opcode] & (HAS_ERROR_FLAG | HAS_EXIT_FLAG | HAS_DEOPT_FLAG)) == 0;
1438
}
1439
1440
static void
1441
sanity_check(_PyExecutorObject *executor)
1442
{
1443
    for (uint32_t i = 0; i < executor->exit_count; i++) {
1444
        _PyExitData *exit = &executor->exits[i];
1445
        CHECK(exit->target < (1 << 25));
1446
    }
1447
    bool ended = false;
1448
    uint32_t i = 0;
1449
    CHECK(_PyUop_Uncached[executor->trace[0].opcode] == _START_EXECUTOR ||
1450
        _PyUop_Uncached[executor->trace[0].opcode] == _COLD_EXIT ||
1451
        _PyUop_Uncached[executor->trace[0].opcode] == _COLD_DYNAMIC_EXIT);
1452
    for (; i < executor->code_size; i++) {
1453
        const _PyUOpInstruction *inst = &executor->trace[i];
1454
        uint16_t opcode = inst->opcode;
1455
        uint16_t base_opcode = _PyUop_Uncached[opcode];
1456
        CHECK(opcode > MAX_UOP_ID);
1457
        CHECK(opcode <= MAX_UOP_REGS_ID);
1458
        CHECK(base_opcode <= MAX_UOP_ID);
1459
        CHECK(base_opcode != 0);
1460
        switch(inst->format) {
1461
            case UOP_FORMAT_TARGET:
1462
                CHECK(target_unused(base_opcode));
1463
                break;
1464
            case UOP_FORMAT_JUMP:
1465
                CHECK(inst->jump_target < executor->code_size);
1466
                break;
1467
        }
1468
        if (_PyUop_Flags[base_opcode] & HAS_ERROR_FLAG) {
1469
            CHECK(inst->format == UOP_FORMAT_JUMP);
1470
            CHECK(inst->error_target < executor->code_size);
1471
        }
1472
        if (is_terminator(inst)) {
1473
            ended = true;
1474
            i++;
1475
            break;
1476
        }
1477
    }
1478
    CHECK(ended);
1479
    for (; i < executor->code_size; i++) {
1480
        const _PyUOpInstruction *inst = &executor->trace[i];
1481
        uint16_t base_opcode = _PyUop_Uncached[inst->opcode];
1482
        CHECK(
1483
            base_opcode == _DEOPT ||
1484
            base_opcode == _HANDLE_PENDING_AND_DEOPT ||
1485
            base_opcode == _EXIT_TRACE ||
1486
            base_opcode == _ERROR_POP_N ||
1487
            base_opcode == _DYNAMIC_EXIT);
1488
    }
1489
}
1490
1491
#undef CHECK
1492
#endif
1493
1494
/* Makes an executor from a buffer of uops.
1495
 * Account for the buffer having gaps and NOPs by computing a "used"
1496
 * bit vector and only copying the used uops. Here "used" means reachable
1497
 * and not a NOP.
1498
 */
1499
static _PyExecutorObject *
1500
make_executor_from_uops(_PyThreadStateImpl *tstate, _PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies)
1501
{
1502
    int exit_count = count_exits(buffer, length);
1503
    _PyExecutorObject *executor = allocate_executor(exit_count, length);
1504
    if (executor == NULL) {
1505
        return NULL;
1506
    }
1507
1508
    /* Initialize exits */
1509
    int chain_depth = tstate->jit_tracer_state->initial_state.chain_depth;
1510
    _PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
1511
    _PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor();
1512
    cold->vm_data.chain_depth = chain_depth;
1513
    PyInterpreterState *interp = tstate->base.interp;
1514
    for (int i = 0; i < exit_count; i++) {
1515
        executor->exits[i].index = i;
1516
        executor->exits[i].temperature = initial_temperature_backoff_counter(&interp->opt_config);
1517
    }
1518
    int next_exit = exit_count-1;
1519
    _PyUOpInstruction *dest = (_PyUOpInstruction *)&executor->trace[length];
1520
    assert(_PyUop_Uncached[buffer[0].opcode] == _START_EXECUTOR);
1521
    buffer[0].operand0 = (uint64_t)executor;
1522
    for (int i = length-1; i >= 0; i--) {
1523
        uint16_t base_opcode = _PyUop_Uncached[buffer[i].opcode];
1524
        dest--;
1525
        *dest = buffer[i];
1526
        if (base_opcode == _EXIT_TRACE || base_opcode == _DYNAMIC_EXIT) {
1527
            _PyExitData *exit = &executor->exits[next_exit];
1528
            exit->target = buffer[i].target;
1529
            dest->operand0 = (uint64_t)exit;
1530
            exit->executor = base_opcode == _EXIT_TRACE ? cold : cold_dynamic;
1531
            exit->is_dynamic = (char)(base_opcode == _DYNAMIC_EXIT);
1532
            exit->is_control_flow = (char)buffer[i].operand1;
1533
            next_exit--;
1534
        }
1535
    }
1536
    assert(next_exit == -1);
1537
    assert(dest == executor->trace);
1538
    assert(_PyUop_Uncached[dest->opcode] == _START_EXECUTOR);
1539
    // Note: we MUST track it here before any Py_DECREF(executor) or
1540
    // linking of executor. Otherwise, the GC tries to untrack a
1541
    // still untracked object during dealloc.
1542
    _PyObject_GC_TRACK(executor);
1543
    if (_Py_ExecutorInit(executor, dependencies) < 0) {
1544
        Py_DECREF(executor);
1545
        return NULL;
1546
    }
1547
#ifdef Py_DEBUG
1548
    char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
1549
    int lltrace = 0;
1550
    if (python_lltrace != NULL && *python_lltrace >= '0') {
1551
        lltrace = *python_lltrace - '0';  // TODO: Parse an int and all that
1552
    }
1553
    if (lltrace >= 2) {
1554
        printf("Optimized trace (length %d):\n", length);
1555
        for (int i = 0; i < length; i++) {
1556
            printf("%4d OPTIMIZED: ", i);
1557
            _PyUOpPrint(&executor->trace[i]);
1558
            printf("\n");
1559
        }
1560
    }
1561
    sanity_check(executor);
1562
#endif
1563
#ifdef _Py_JIT
1564
    executor->jit_code = NULL;
1565
    executor->jit_size = 0;
1566
    // This is initialized to false so we can prevent the executor
1567
    // from being immediately detected as cold and invalidated.
1568
    executor->vm_data.cold = false;
1569
    if (_PyJIT_Compile(executor, executor->trace, length)) {
1570
        Py_DECREF(executor);
1571
        return NULL;
1572
    }
1573
#endif
1574
    return executor;
1575
}
1576
1577
#ifdef Py_STATS
1578
/* Returns the effective trace length.
1579
 * Ignores NOPs and trailing exit and error handling.*/
1580
int effective_trace_length(_PyUOpInstruction *buffer, int length)
1581
{
1582
    int nop_count = 0;
1583
    for (int i = 0; i < length; i++) {
1584
        int opcode = buffer[i].opcode;
1585
        if (opcode == _NOP) {
1586
            nop_count++;
1587
        }
1588
        if (is_terminator(&buffer[i])) {
1589
            return i+1-nop_count;
1590
        }
1591
    }
1592
    Py_FatalError("No terminating instruction");
1593
    Py_UNREACHABLE();
1594
}
1595
#endif
1596
1597
1598
static int
1599
stack_allocate(_PyUOpInstruction *buffer, _PyUOpInstruction *output, int length)
1600
{
1601
    assert(buffer[0].opcode == _START_EXECUTOR);
1602
    /* The input buffer and output buffers will overlap.
1603
       Make sure that we can move instructions to the output
1604
       without overwriting the input. */
1605
    if (buffer == output) {
1606
        // This can only happen if optimizer has not been run
1607
        for (int i = 0; i < length; i++) {
1608
            buffer[i + UOP_MAX_TRACE_LENGTH] = buffer[i];
1609
        }
1610
        buffer += UOP_MAX_TRACE_LENGTH;
1611
    }
1612
    else {
1613
        assert(output + UOP_MAX_TRACE_LENGTH == buffer);
1614
    }
1615
    int depth = 0;
1616
    _PyUOpInstruction *write = output;
1617
    for (int i = 0; i < length; i++) {
1618
        int uop = buffer[i].opcode;
1619
        if (uop == _NOP) {
1620
            continue;
1621
        }
1622
        int new_depth = _PyUop_Caching[uop].best[depth];
1623
        if (new_depth != depth) {
1624
            write->opcode = _PyUop_SpillsAndReloads[depth][new_depth];
1625
            assert(write->opcode != 0);
1626
            write->format = UOP_FORMAT_TARGET;
1627
            write->oparg = 0;
1628
            write->target = 0;
1629
            write++;
1630
            depth = new_depth;
1631
        }
1632
        *write = buffer[i];
1633
        uint16_t new_opcode = _PyUop_Caching[uop].entries[depth].opcode;
1634
        assert(new_opcode != 0);
1635
        write->opcode = new_opcode;
1636
        write++;
1637
        depth = _PyUop_Caching[uop].entries[depth].output;
1638
    }
1639
    return (int)(write - output);
1640
}
1641
1642
static int
1643
uop_optimize(
1644
    _PyInterpreterFrame *frame,
1645
    PyThreadState *tstate,
1646
    _PyExecutorObject **exec_ptr,
1647
    bool progress_needed)
1648
{
1649
    _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1650
    assert(_tstate->jit_tracer_state != NULL);
1651
    _PyUOpInstruction *buffer = _tstate->jit_tracer_state->code_buffer.start;
1652
    OPT_STAT_INC(attempts);
1653
    bool is_noopt = !tstate->interp->opt_config.uops_optimize_enabled;
1654
    int curr_stackentries = _tstate->jit_tracer_state->initial_state.stack_depth;
1655
    int length = uop_buffer_length(&_tstate->jit_tracer_state->code_buffer);
1656
    if (length <= CODE_SIZE_NO_PROGRESS) {
1657
        return 0;
1658
    }
1659
    assert(length > 0);
1660
    assert(length < UOP_MAX_TRACE_LENGTH);
1661
    OPT_STAT_INC(traces_created);
1662
1663
    _PyBloomFilter dependencies;
1664
    _Py_BloomFilter_Init(&dependencies);
1665
    if (!is_noopt) {
1666
        _PyUOpInstruction *output = &_tstate->jit_tracer_state->uop_array[UOP_MAX_TRACE_LENGTH];
1667
        length = _Py_uop_analyze_and_optimize(
1668
            _tstate, buffer, length, curr_stackentries,
1669
            output, &dependencies);
1670
1671
        if (length <= 0) {
1672
            return length;
1673
        }
1674
        buffer = output;
1675
    }
1676
    assert(length < UOP_MAX_TRACE_LENGTH);
1677
    assert(length >= 1);
1678
    /* Fix up */
1679
    for (int pc = 0; pc < length; pc++) {
1680
        int opcode = buffer[pc].opcode;
1681
        int oparg = buffer[pc].oparg;
1682
        if (oparg < _PyUop_Replication[opcode].stop && oparg >= _PyUop_Replication[opcode].start) {
1683
            buffer[pc].opcode = opcode + oparg + 1 - _PyUop_Replication[opcode].start;
1684
            assert(strncmp(_PyOpcode_uop_name[buffer[pc].opcode], _PyOpcode_uop_name[opcode], strlen(_PyOpcode_uop_name[opcode])) == 0);
1685
        }
1686
        else if (_PyUop_Flags[opcode] & HAS_RECORDS_VALUE_FLAG) {
1687
            Py_XDECREF((PyObject *)(uintptr_t)buffer[pc].operand0);
1688
            buffer[pc].opcode = _NOP;
1689
        }
1690
        else if (is_terminator(&buffer[pc])) {
1691
            break;
1692
        }
1693
        assert(_PyOpcode_uop_name[buffer[pc].opcode]);
1694
    }
1695
    // We've cleaned up the references in the buffer, so discard the code buffer
1696
    // to avoid doing it again during tracer cleanup
1697
    _PyJitUopBuffer *code_buffer = &_tstate->jit_tracer_state->code_buffer;
1698
    code_buffer->next = code_buffer->start;
1699
1700
    OPT_HIST(effective_trace_length(buffer, length), optimized_trace_length_hist);
1701
    _PyUOpInstruction *output = &_tstate->jit_tracer_state->uop_array[0];
1702
    length = stack_allocate(buffer, output, length);
1703
    buffer = output;
1704
    length = prepare_for_execution(buffer, length);
1705
    assert(length <= UOP_MAX_TRACE_LENGTH);
1706
    _PyExecutorObject *executor = make_executor_from_uops(
1707
        _tstate, buffer, length, &dependencies);
1708
    if (executor == NULL) {
1709
        return -1;
1710
    }
1711
    assert(length <= UOP_MAX_TRACE_LENGTH);
1712
1713
    // Check executor coldness
1714
    // It's okay if this ends up going negative.
1715
    if (--tstate->interp->executor_creation_counter == 0) {
1716
        _Py_set_eval_breaker_bit(tstate, _PY_EVAL_JIT_INVALIDATE_COLD_BIT);
1717
    }
1718
1719
    *exec_ptr = executor;
1720
    return 1;
1721
}
1722
1723
1724
/*****************************************
1725
 *        Executor management
1726
 ****************************************/
1727
1728
static int
1729
link_executor(_PyExecutorObject *executor, const _PyBloomFilter *bloom)
1730
{
1731
    PyInterpreterState *interp = _PyInterpreterState_GET();
1732
    if (interp->executor_count == interp->executor_capacity) {
1733
        size_t new_cap = interp->executor_capacity ? interp->executor_capacity * 2 : 64;
1734
        _PyBloomFilter *new_blooms = PyMem_Realloc(
1735
            interp->executor_blooms, new_cap * sizeof(_PyBloomFilter));
1736
        if (new_blooms == NULL) {
1737
            return -1;
1738
        }
1739
        _PyExecutorObject **new_ptrs = PyMem_Realloc(
1740
            interp->executor_ptrs, new_cap * sizeof(_PyExecutorObject *));
1741
        if (new_ptrs == NULL) {
1742
            /* Revert blooms realloc — the old pointer may have been freed by
1743
             * a successful realloc, but new_blooms is the valid pointer. */
1744
            interp->executor_blooms = new_blooms;
1745
            return -1;
1746
        }
1747
        interp->executor_blooms = new_blooms;
1748
        interp->executor_ptrs = new_ptrs;
1749
        interp->executor_capacity = new_cap;
1750
    }
1751
    size_t idx = interp->executor_count++;
1752
    interp->executor_blooms[idx] = *bloom;
1753
    interp->executor_ptrs[idx] = executor;
1754
    executor->vm_data.bloom_array_idx = (int32_t)idx;
1755
    return 0;
1756
}
1757
1758
static void
1759
unlink_executor(_PyExecutorObject *executor)
1760
{
1761
    PyInterpreterState *interp = PyInterpreterState_Get();
1762
    int32_t idx = executor->vm_data.bloom_array_idx;
1763
    assert(idx >= 0 && (size_t)idx < interp->executor_count);
1764
    size_t last = --interp->executor_count;
1765
    if ((size_t)idx != last) {
1766
        /* Swap-remove: move the last element into the vacated slot */
1767
        interp->executor_blooms[idx] = interp->executor_blooms[last];
1768
        interp->executor_ptrs[idx] = interp->executor_ptrs[last];
1769
        interp->executor_ptrs[idx]->vm_data.bloom_array_idx = idx;
1770
    }
1771
    executor->vm_data.bloom_array_idx = -1;
1772
}
1773
1774
/* This must be called by optimizers before using the executor */
1775
int
1776
_Py_ExecutorInit(_PyExecutorObject *executor, const _PyBloomFilter *dependency_set)
1777
{
1778
    executor->vm_data.valid = true;
1779
    executor->vm_data.pending_deletion = 0;
1780
    executor->vm_data.code = NULL;
1781
    if (link_executor(executor, dependency_set) < 0) {
1782
        return -1;
1783
    }
1784
    return 0;
1785
}
1786
1787
static _PyExecutorObject *
1788
make_cold_executor(uint16_t opcode)
1789
{
1790
    _PyExecutorObject *cold = allocate_executor(0, 1);
1791
    if (cold == NULL) {
1792
        Py_FatalError("Cannot allocate core JIT code");
1793
    }
1794
    ((_PyUOpInstruction *)cold->trace)->opcode = opcode;
1795
    // This is initialized to false so we can prevent the executor
1796
    // from being immediately detected as cold and invalidated.
1797
    cold->vm_data.cold = false;
1798
#ifdef _Py_JIT
1799
    cold->jit_code = NULL;
1800
    cold->jit_size = 0;
1801
    if (_PyJIT_Compile(cold, cold->trace, 1)) {
1802
        Py_DECREF(cold);
1803
        Py_FatalError("Cannot allocate core JIT code");
1804
    }
1805
#endif
1806
    _Py_SetImmortal((PyObject *)cold);
1807
    return cold;
1808
}
1809
1810
_PyExecutorObject *
1811
_PyExecutor_GetColdExecutor(void)
1812
{
1813
    PyInterpreterState *interp = _PyInterpreterState_GET();
1814
    if (interp->cold_executor == NULL) {
1815
        return interp->cold_executor = make_cold_executor(_COLD_EXIT_r00);;
1816
    }
1817
    return interp->cold_executor;
1818
}
1819
1820
_PyExecutorObject *
1821
_PyExecutor_GetColdDynamicExecutor(void)
1822
{
1823
    PyInterpreterState *interp = _PyInterpreterState_GET();
1824
    if (interp->cold_dynamic_executor == NULL) {
1825
        interp->cold_dynamic_executor = make_cold_executor(_COLD_DYNAMIC_EXIT_r00);
1826
    }
1827
    return interp->cold_dynamic_executor;
1828
}
1829
1830
void
1831
_PyExecutor_ClearExit(_PyExitData *exit)
1832
{
1833
    if (exit == NULL) {
1834
        return;
1835
    }
1836
    _PyExecutorObject *old = exit->executor;
1837
    if (exit->is_dynamic) {
1838
        exit->executor = _PyExecutor_GetColdDynamicExecutor();
1839
    }
1840
    else {
1841
        exit->executor = _PyExecutor_GetColdExecutor();
1842
    }
1843
    Py_DECREF(old);
1844
}
1845
1846
/* Detaches the executor from the code object (if any) that
1847
 * holds a reference to it */
1848
void
1849
_Py_ExecutorDetach(_PyExecutorObject *executor)
1850
{
1851
    PyCodeObject *code = executor->vm_data.code;
1852
    if (code == NULL) {
1853
        return;
1854
    }
1855
    _Py_CODEUNIT *instruction = &_PyCode_CODE(code)[executor->vm_data.index];
1856
    assert(instruction->op.code == ENTER_EXECUTOR);
1857
    int index = instruction->op.arg;
1858
    assert(code->co_executors->executors[index] == executor);
1859
    instruction->op.code = _PyOpcode_Deopt[executor->vm_data.opcode];
1860
    instruction->op.arg = executor->vm_data.oparg;
1861
    executor->vm_data.code = NULL;
1862
    code->co_executors->executors[index] = NULL;
1863
    Py_DECREF(executor);
1864
}
1865
1866
/* Executors can be invalidated at any time,
1867
   even with a stop-the-world lock held.
1868
   Consequently it must not run arbitrary code,
1869
   including Py_DECREF with a non-executor. */
1870
static void
1871
executor_invalidate(PyObject *op)
1872
{
1873
    _PyExecutorObject *executor = _PyExecutorObject_CAST(op);
1874
    if (!executor->vm_data.valid) {
1875
        return;
1876
    }
1877
    executor->vm_data.valid = 0;
1878
    unlink_executor(executor);
1879
    executor_clear_exits(executor);
1880
    _Py_ExecutorDetach(executor);
1881
    _PyObject_GC_UNTRACK(op);
1882
}
1883
1884
static int
1885
executor_clear(PyObject *op)
1886
{
1887
    executor_invalidate(op);
1888
    return 0;
1889
}
1890
1891
void
1892
_Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj)
1893
{
1894
    assert(executor->vm_data.valid);
1895
    PyInterpreterState *interp = _PyInterpreterState_GET();
1896
    int32_t idx = executor->vm_data.bloom_array_idx;
1897
    assert(idx >= 0 && (size_t)idx < interp->executor_count);
1898
    _Py_BloomFilter_Add(&interp->executor_blooms[idx], obj);
1899
}
1900
1901
/* Invalidate all executors that depend on `obj`
1902
 * May cause other executors to be invalidated as well.
1903
 * Uses contiguous bloom filter array for cache-friendly scanning.
1904
 */
1905
void
1906
_Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is_invalidation)
1907
{
1908
    _PyBloomFilter obj_filter;
1909
    _Py_BloomFilter_Init(&obj_filter);
1910
    _Py_BloomFilter_Add(&obj_filter, obj);
1911
    /* Scan contiguous bloom filter array */
1912
    PyObject *invalidate = PyList_New(0);
1913
    if (invalidate == NULL) {
1914
        goto error;
1915
    }
1916
    /* Clearing an executor can clear others, so we need to make a list of
1917
     * executors to invalidate first */
1918
    for (size_t i = 0; i < interp->executor_count; i++) {
1919
        assert(interp->executor_ptrs[i]->vm_data.valid);
1920
        if (bloom_filter_may_contain(&interp->executor_blooms[i], &obj_filter) &&
1921
            PyList_Append(invalidate, (PyObject *)interp->executor_ptrs[i]))
1922
        {
1923
            goto error;
1924
        }
1925
    }
1926
    for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
1927
        PyObject *exec = PyList_GET_ITEM(invalidate, i);
1928
        executor_invalidate(exec);
1929
        if (is_invalidation) {
1930
            OPT_STAT_INC(executors_invalidated);
1931
        }
1932
    }
1933
    Py_DECREF(invalidate);
1934
    return;
1935
error:
1936
    PyErr_Clear();
1937
    Py_XDECREF(invalidate);
1938
    // If we're truly out of memory, wiping out everything is a fine fallback:
1939
    _Py_Executors_InvalidateAll(interp, is_invalidation);
1940
}
1941
1942
/* Invalidate all executors */
1943
void
1944
_Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation)
1945
{
1946
    while (interp->executor_count > 0) {
1947
        /* Invalidate from the end to avoid repeated swap-remove shifts */
1948
        _PyExecutorObject *executor = interp->executor_ptrs[interp->executor_count - 1];
1949
        assert(executor->vm_data.valid);
1950
        if (executor->vm_data.code) {
1951
            // Clear the entire code object so its co_executors array be freed:
1952
            _PyCode_Clear_Executors(executor->vm_data.code);
1953
        }
1954
        else {
1955
            executor_invalidate((PyObject *)executor);
1956
        }
1957
        if (is_invalidation) {
1958
            OPT_STAT_INC(executors_invalidated);
1959
        }
1960
    }
1961
}
1962
1963
void
1964
_Py_Executors_InvalidateCold(PyInterpreterState *interp)
1965
{
1966
    /* Scan contiguous executor array */
1967
    PyObject *invalidate = PyList_New(0);
1968
    if (invalidate == NULL) {
1969
        goto error;
1970
    }
1971
1972
    /* Clearing an executor can deallocate others, so we need to make a list of
1973
     * executors to invalidate first */
1974
    for (size_t i = 0; i < interp->executor_count; i++) {
1975
        _PyExecutorObject *exec = interp->executor_ptrs[i];
1976
        assert(exec->vm_data.valid);
1977
1978
        if (exec->vm_data.cold && PyList_Append(invalidate, (PyObject *)exec) < 0) {
1979
            goto error;
1980
        }
1981
        else {
1982
            exec->vm_data.cold = true;
1983
        }
1984
    }
1985
    for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
1986
        PyObject *exec = PyList_GET_ITEM(invalidate, i);
1987
        executor_invalidate(exec);
1988
    }
1989
    Py_DECREF(invalidate);
1990
    return;
1991
error:
1992
    PyErr_Clear();
1993
    Py_XDECREF(invalidate);
1994
    // If we're truly out of memory, wiping out everything is a fine fallback
1995
    _Py_Executors_InvalidateAll(interp, 0);
1996
}
1997
1998
#include "record_functions.c.h"
1999
2000
static int
2001
escape_angles(const char *input, Py_ssize_t size, char *buffer) {
2002
    int written = 0;
2003
    for (Py_ssize_t i = 0; i < size; i++) {
2004
        char c = input[i];
2005
        if (c == '<' || c == '>') {
2006
            buffer[written++] = '&';
2007
            buffer[written++] = c == '>' ? 'g' : 'l';
2008
            buffer[written++] = 't';
2009
            buffer[written++] = ';';
2010
        }
2011
        else {
2012
            buffer[written++] = c;
2013
        }
2014
    }
2015
    return written;
2016
}
2017
2018
static void
2019
write_str(PyObject *str, FILE *out)
2020
{
2021
    // Encode the Unicode object to the specified encoding
2022
    PyObject *encoded_obj = PyUnicode_AsEncodedString(str, "utf8", "strict");
2023
    if (encoded_obj == NULL) {
2024
        PyErr_Clear();
2025
        return;
2026
    }
2027
    const char *encoded_str = PyBytes_AsString(encoded_obj);
2028
    Py_ssize_t encoded_size = PyBytes_Size(encoded_obj);
2029
    char buffer[120];
2030
    bool truncated = false;
2031
    if (encoded_size > 24) {
2032
        encoded_size = 24;
2033
        truncated = true;
2034
    }
2035
    int size = escape_angles(encoded_str, encoded_size, buffer);
2036
    fwrite(buffer, 1, size, out);
2037
    if (truncated) {
2038
        fwrite("...", 1, 3, out);
2039
    }
2040
    Py_DECREF(encoded_obj);
2041
}
2042
2043
static int
2044
find_line_number(PyCodeObject *code, _PyExecutorObject *executor)
2045
{
2046
    int code_len = (int)Py_SIZE(code);
2047
    for (int i = 0; i < code_len; i++) {
2048
        _Py_CODEUNIT *instr = &_PyCode_CODE(code)[i];
2049
        int opcode = instr->op.code;
2050
        if (opcode == ENTER_EXECUTOR) {
2051
            _PyExecutorObject *exec = code->co_executors->executors[instr->op.arg];
2052
            if (exec == executor) {
2053
                return PyCode_Addr2Line(code, i*2);
2054
            }
2055
        }
2056
        i += _PyOpcode_Caches[_Py_GetBaseCodeUnit(code, i).op.code];
2057
    }
2058
    return -1;
2059
}
2060
2061
#define RED "#ff0000"
2062
#define WHITE "#ffffff"
2063
#define BLUE "#0000ff"
2064
#define BLACK "#000000"
2065
#define LOOP "#00c000"
2066
2067
#ifdef Py_STATS
2068
2069
static const char *COLORS[10] = {
2070
    "9",
2071
    "8",
2072
    "7",
2073
    "6",
2074
    "5",
2075
    "4",
2076
    "3",
2077
    "2",
2078
    "1",
2079
    WHITE,
2080
};
2081
const char *
2082
get_background_color(_PyUOpInstruction const *inst, uint64_t max_hotness)
2083
{
2084
    uint64_t hotness = inst->execution_count;
2085
    int index = (hotness * 10)/max_hotness;
2086
    if (index > 9) {
2087
        index = 9;
2088
    }
2089
    if (index < 0) {
2090
        index = 0;
2091
    }
2092
    return COLORS[index];
2093
}
2094
2095
const char *
2096
get_foreground_color(_PyUOpInstruction const *inst, uint64_t max_hotness)
2097
{
2098
    if(_PyUop_Uncached[inst->opcode] == _DEOPT) {
2099
        return RED;
2100
    }
2101
    uint64_t hotness = inst->execution_count;
2102
    int index = (hotness * 10)/max_hotness;
2103
    if (index > 3) {
2104
        return BLACK;
2105
    }
2106
    return WHITE;
2107
}
2108
#endif
2109
2110
static void
2111
write_row_for_uop(_PyExecutorObject *executor, uint32_t i, FILE *out)
2112
{
2113
    /* Write row for uop.
2114
        * The `port` is a marker so that outgoing edges can
2115
        * be placed correctly. If a row is marked `port=17`,
2116
        * then the outgoing edge is `{EXEC_NAME}:17 -> {TARGET}`
2117
        * https://graphviz.readthedocs.io/en/stable/manual.html#node-ports-compass
2118
        */
2119
    _PyUOpInstruction const *inst = &executor->trace[i];
2120
    const char *opname = _PyOpcode_uop_name[inst->opcode];
2121
#ifdef Py_STATS
2122
    const char *bg_color = get_background_color(inst, executor->trace[0].execution_count);
2123
    const char *color = get_foreground_color(inst, executor->trace[0].execution_count);
2124
    fprintf(out, "        <tr><td port=\"i%d\" border=\"1\" color=\"%s\" bgcolor=\"%s\" ><font color=\"%s\"> %s [%d]&nbsp;--&nbsp; %" PRIu64 "</font></td></tr>\n",
2125
        i, color, bg_color, color, opname, inst->fitness, inst->execution_count);
2126
#else
2127
    const char *color = (_PyUop_Uncached[inst->opcode] == _DEOPT) ? RED : BLACK;
2128
    fprintf(out, "        <tr><td port=\"i%d\" border=\"1\" color=\"%s\" >%s op0=%" PRIu64 "</td></tr>\n", i, color, opname, inst->operand0);
2129
#endif
2130
}
2131
2132
static bool
2133
is_stop(_PyUOpInstruction const *inst)
2134
{
2135
    uint16_t base_opcode = _PyUop_Uncached[inst->opcode];
2136
    return (base_opcode == _EXIT_TRACE || base_opcode == _DEOPT || base_opcode == _JUMP_TO_TOP);
2137
}
2138
2139
2140
/* Writes the node and outgoing edges for a single tracelet in graphviz format.
2141
 * Each tracelet is presented as a table of the uops it contains.
2142
 * If Py_STATS is enabled, execution counts are included.
2143
 *
2144
 * https://graphviz.readthedocs.io/en/stable/manual.html
2145
 * https://graphviz.org/gallery/
2146
 */
2147
static void
2148
executor_to_gv(_PyExecutorObject *executor, FILE *out)
2149
{
2150
    PyCodeObject *code = executor->vm_data.code;
2151
    fprintf(out, "executor_%p [\n", executor);
2152
    fprintf(out, "    shape = none\n");
2153
2154
    /* Write the HTML table for the uops */
2155
    fprintf(out, "    label = <<table border=\"0\" cellspacing=\"0\">\n");
2156
    fprintf(out, "        <tr><td port=\"start\" border=\"1\" ><b>Executor</b></td></tr>\n");
2157
    if (code == NULL) {
2158
        fprintf(out, "        <tr><td border=\"1\" >No code object</td></tr>\n");
2159
    }
2160
    else {
2161
        fprintf(out, "        <tr><td  border=\"1\" >");
2162
        write_str(code->co_qualname, out);
2163
        int line = find_line_number(code, executor);
2164
        fprintf(out, ": %d</td></tr>\n", line);
2165
    }
2166
    for (uint32_t i = 0; i < executor->code_size; i++) {
2167
        write_row_for_uop(executor, i, out);
2168
        if (is_stop(&executor->trace[i])) {
2169
            break;
2170
        }
2171
    }
2172
    fprintf(out, "    </table>>\n");
2173
    fprintf(out, "]\n\n");
2174
2175
    /* Write all the outgoing edges */
2176
    _PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
2177
    _PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor();
2178
    for (uint32_t i = 0; i < executor->code_size; i++) {
2179
        _PyUOpInstruction const *inst = &executor->trace[i];
2180
        uint16_t base_opcode = _PyUop_Uncached[inst->opcode];
2181
        uint16_t flags = _PyUop_Flags[base_opcode];
2182
        _PyExitData *exit = NULL;
2183
        if (base_opcode == _JUMP_TO_TOP) {
2184
            fprintf(out, "executor_%p:i%d -> executor_%p:i%d [color = \"" LOOP "\"]\n", executor, i, executor, inst->jump_target);
2185
            break;
2186
        }
2187
        if (base_opcode == _EXIT_TRACE) {
2188
            exit = (_PyExitData *)inst->operand0;
2189
        }
2190
        else if (flags & HAS_EXIT_FLAG) {
2191
            assert(inst->format == UOP_FORMAT_JUMP);
2192
            _PyUOpInstruction const *exit_inst = &executor->trace[inst->jump_target];
2193
            uint16_t base_exit_opcode = _PyUop_Uncached[exit_inst->opcode];
2194
            (void)base_exit_opcode;
2195
            assert(base_exit_opcode == _EXIT_TRACE || base_exit_opcode == _DYNAMIC_EXIT);
2196
            exit = (_PyExitData *)exit_inst->operand0;
2197
        }
2198
        if (exit != NULL) {
2199
            if (exit->executor == cold || exit->executor == cold_dynamic) {
2200
#ifdef Py_STATS
2201
                /* Only mark as have cold exit if it has actually exited */
2202
                uint64_t diff = inst->execution_count - executor->trace[i+1].execution_count;
2203
                if (diff) {
2204
                    fprintf(out, "cold_%p%d [ label = \"%"  PRIu64  "\" shape = ellipse color=\"" BLUE "\" ]\n", executor, i, diff);
2205
                    fprintf(out, "executor_%p:i%d -> cold_%p%d\n", executor, i, executor, i);
2206
                }
2207
#endif
2208
            }
2209
            else {
2210
                fprintf(out, "executor_%p:i%d -> executor_%p:start\n", executor, i, exit->executor);
2211
            }
2212
        }
2213
        if (is_stop(inst)) {
2214
            break;
2215
        }
2216
    }
2217
}
2218
2219
/* Write the graph of all the live tracelets in graphviz format. */
2220
int
2221
_PyDumpExecutors(FILE *out)
2222
{
2223
    fprintf(out, "digraph ideal {\n\n");
2224
    fprintf(out, "    rankdir = \"LR\"\n\n");
2225
    fprintf(out, "    node [colorscheme=greys9]\n");
2226
    PyInterpreterState *interp = PyInterpreterState_Get();
2227
    for (size_t i = 0; i < interp->executor_count; i++) {
2228
        executor_to_gv(interp->executor_ptrs[i], out);
2229
    }
2230
    fprintf(out, "}\n\n");
2231
    return 0;
2232
}
2233
2234
#else
2235
2236
int
2237
_PyDumpExecutors(FILE *out)
2238
0
{
2239
0
    PyErr_SetString(PyExc_NotImplementedError, "No JIT available");
2240
0
    return -1;
2241
0
}
2242
2243
void
2244
_PyExecutor_Free(struct _PyExecutorObject *self)
2245
0
{
2246
    /* This should never be called */
2247
0
    Py_UNREACHABLE();
2248
0
}
2249
2250
#endif /* _Py_TIER2 */