| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "opcode.h"
 | 
					
						
							|  |  |  | #include "pycore_interp.h"
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  | #include "pycore_bitutils.h"        // _Py_popcount32()
 | 
					
						
							| 
									
										
										
										
											2023-09-06 15:56:08 +02:00
										 |  |  | #include "pycore_opcode_metadata.h" // _PyOpcode_OpName()
 | 
					
						
							|  |  |  | #include "pycore_opcode_utils.h"  // MAX_REAL_OPCODE
 | 
					
						
							|  |  |  | #include "pycore_optimizer.h"     // _Py_uop_analyze_and_optimize()
 | 
					
						
							| 
									
										
										
										
											2023-07-02 18:37:37 +02:00
										 |  |  | #include "pycore_pystate.h"       // _PyInterpreterState_GET()
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | #include "pycore_uops.h"
 | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | #include "cpython/optimizer.h"
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  | #define MAX_EXECUTORS_SIZE 256
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  | static bool | 
					
						
							|  |  |  | has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (instr->op.code == ENTER_EXECUTOR) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (code->co_executors == NULL) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |     return code->co_executors->size < MAX_EXECUTORS_SIZE; | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | static int32_t | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  | get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr) | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     if (instr->op.code == ENTER_EXECUTOR) { | 
					
						
							|  |  |  |         return instr->op.arg; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     _PyExecutorArray *old = code->co_executors; | 
					
						
							|  |  |  |     int size = 0; | 
					
						
							|  |  |  |     int capacity = 0; | 
					
						
							|  |  |  |     if (old != NULL) { | 
					
						
							|  |  |  |         size = old->size; | 
					
						
							|  |  |  |         capacity = old->capacity; | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |         assert(size < MAX_EXECUTORS_SIZE); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     assert(size <= capacity); | 
					
						
							|  |  |  |     if (size == capacity) { | 
					
						
							|  |  |  |         /* Array is full. Grow array */ | 
					
						
							|  |  |  |         int new_capacity = capacity ? capacity * 2 : 4; | 
					
						
							|  |  |  |         _PyExecutorArray *new = PyMem_Realloc( | 
					
						
							|  |  |  |             old, | 
					
						
							|  |  |  |             offsetof(_PyExecutorArray, executors) + | 
					
						
							|  |  |  |             new_capacity * sizeof(_PyExecutorObject *)); | 
					
						
							|  |  |  |         if (new == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         new->capacity = new_capacity; | 
					
						
							|  |  |  |         new->size = size; | 
					
						
							|  |  |  |         code->co_executors = new; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(size < code->co_executors->capacity); | 
					
						
							|  |  |  |     return size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorObject *executor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     Py_INCREF(executor); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     if (instr->op.code == ENTER_EXECUTOR) { | 
					
						
							|  |  |  |         assert(index == instr->op.arg); | 
					
						
							|  |  |  |         _PyExecutorObject *old = code->co_executors->executors[index]; | 
					
						
							|  |  |  |         executor->vm_data.opcode = old->vm_data.opcode; | 
					
						
							|  |  |  |         executor->vm_data.oparg = old->vm_data.oparg; | 
					
						
							|  |  |  |         old->vm_data.opcode = 0; | 
					
						
							|  |  |  |         code->co_executors->executors[index] = executor; | 
					
						
							|  |  |  |         Py_DECREF(old); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |         assert(code->co_executors->size == index); | 
					
						
							|  |  |  |         assert(code->co_executors->capacity > index); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |         executor->vm_data.opcode = instr->op.code; | 
					
						
							|  |  |  |         executor->vm_data.oparg = instr->op.arg; | 
					
						
							|  |  |  |         code->co_executors->executors[index] = executor; | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |         assert(index < MAX_EXECUTORS_SIZE); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |         instr->op.code = ENTER_EXECUTOR; | 
					
						
							|  |  |  |         instr->op.arg = index; | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |         code->co_executors->size++; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyUnstable_Replace_Executor(PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutorObject *new) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (instr->op.code != ENTER_EXECUTOR) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_ValueError, "No executor to replace"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     int index = instr->op.arg; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     assert(index >= 0); | 
					
						
							|  |  |  |     insert_executor(code, instr, index, new); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | error_optimize( | 
					
						
							|  |  |  |     _PyOptimizerObject* self, | 
					
						
							|  |  |  |     PyCodeObject *code, | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |     _Py_CODEUNIT *instr, | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     _PyExecutorObject **exec, | 
					
						
							|  |  |  |     int Py_UNUSED(stack_entries)) | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     PyErr_Format(PyExc_SystemError, "Should never call error_optimize"); | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject DefaultOptimizer_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     .tp_name = "noop_optimizer", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(_PyOptimizerObject), | 
					
						
							|  |  |  |     .tp_itemsize = 0, | 
					
						
							|  |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _PyOptimizerObject _PyOptimizer_Default = { | 
					
						
							|  |  |  |     PyObject_HEAD_INIT(&DefaultOptimizer_Type) | 
					
						
							|  |  |  |     .optimize = error_optimize, | 
					
						
							|  |  |  |     .resume_threshold = UINT16_MAX, | 
					
						
							|  |  |  |     .backedge_threshold = UINT16_MAX, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _PyOptimizerObject * | 
					
						
							|  |  |  | PyUnstable_GetOptimizer(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-02 18:37:37 +02:00
										 |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     if (interp->optimizer == &_PyOptimizer_Default) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     assert(interp->optimizer_backedge_threshold == interp->optimizer->backedge_threshold); | 
					
						
							|  |  |  |     assert(interp->optimizer_resume_threshold == interp->optimizer->resume_threshold); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     Py_INCREF(interp->optimizer); | 
					
						
							|  |  |  |     return interp->optimizer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | PyUnstable_SetOptimizer(_PyOptimizerObject *optimizer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-02 18:37:37 +02:00
										 |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     if (optimizer == NULL) { | 
					
						
							|  |  |  |         optimizer = &_PyOptimizer_Default; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _PyOptimizerObject *old = interp->optimizer; | 
					
						
							|  |  |  |     Py_INCREF(optimizer); | 
					
						
							|  |  |  |     interp->optimizer = optimizer; | 
					
						
							|  |  |  |     interp->optimizer_backedge_threshold = optimizer->backedge_threshold; | 
					
						
							|  |  |  |     interp->optimizer_resume_threshold = optimizer->resume_threshold; | 
					
						
							|  |  |  |     Py_DECREF(old); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:26:50 -07:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | _PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest, PyObject **stack_pointer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-20 09:35:39 -07:00
										 |  |  |     assert(src->op.code == JUMP_BACKWARD); | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  |     PyCodeObject *code = (PyCodeObject *)frame->f_executable; | 
					
						
							|  |  |  |     assert(PyCode_Check(code)); | 
					
						
							| 
									
										
										
										
											2023-07-02 18:37:37 +02:00
										 |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     if (!has_space_for_executor(code, src)) { | 
					
						
							| 
									
										
										
										
											2023-09-13 10:26:50 -07:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     _PyOptimizerObject *opt = interp->optimizer; | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     _PyExecutorObject *executor = NULL; | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     int err = opt->optimize(opt, code, dest, &executor, (int)(stack_pointer - _PyFrame_Stackbase(frame))); | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |     if (err <= 0) { | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |         assert(executor == NULL); | 
					
						
							| 
									
										
										
										
											2023-09-13 10:26:50 -07:00
										 |  |  |         return err; | 
					
						
							| 
									
										
										
										
											2023-06-19 10:32:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     int index = get_index_for_executor(code, src); | 
					
						
							|  |  |  |     if (index < 0) { | 
					
						
							|  |  |  |         /* Out of memory. Don't raise and assume that the
 | 
					
						
							|  |  |  |          * error will show up elsewhere. | 
					
						
							|  |  |  |          * | 
					
						
							|  |  |  |          * If an optimizer has already produced an executor, | 
					
						
							|  |  |  |          * it might get confused by the executor disappearing, | 
					
						
							|  |  |  |          * but there is not much we can do about that here. */ | 
					
						
							|  |  |  |         Py_DECREF(executor); | 
					
						
							| 
									
										
										
										
											2023-09-13 10:26:50 -07:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  |     insert_executor(code, src, index, executor); | 
					
						
							| 
									
										
										
										
											2023-09-13 10:26:50 -07:00
										 |  |  |     Py_DECREF(executor); | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  | _PyExecutorObject * | 
					
						
							|  |  |  | PyUnstable_GetExecutor(PyCodeObject *code, int offset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int code_len = (int)Py_SIZE(code); | 
					
						
							|  |  |  |     for (int i = 0 ; i < code_len;) { | 
					
						
							|  |  |  |         if (_PyCode_CODE(code)[i].op.code == ENTER_EXECUTOR && i*2 == offset) { | 
					
						
							|  |  |  |             int oparg = _PyCode_CODE(code)[i].op.arg; | 
					
						
							|  |  |  |             _PyExecutorObject *res = code->co_executors->executors[oparg]; | 
					
						
							|  |  |  |             Py_INCREF(res); | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         i += _PyInstruction_GetLength(code, i); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-06 11:39:53 -07:00
										 |  |  |     PyErr_SetString(PyExc_ValueError, "no executor at given byte offset"); | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | /** Test support **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     _PyOptimizerObject base; | 
					
						
							|  |  |  |     int64_t count; | 
					
						
							|  |  |  | } _PyCounterOptimizerObject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     _PyExecutorObject executor; | 
					
						
							|  |  |  |     _PyCounterOptimizerObject *optimizer; | 
					
						
							|  |  |  |     _Py_CODEUNIT *next_instr; | 
					
						
							|  |  |  | } _PyCounterExecutorObject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | counter_dealloc(_PyCounterExecutorObject *self) { | 
					
						
							|  |  |  |     Py_DECREF(self->optimizer); | 
					
						
							|  |  |  |     PyObject_Free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject CounterExecutor_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     .tp_name = "counting_executor", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(_PyCounterExecutorObject), | 
					
						
							|  |  |  |     .tp_itemsize = 0, | 
					
						
							|  |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
					
						
							|  |  |  |     .tp_dealloc = (destructor)counter_dealloc, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static _PyInterpreterFrame * | 
					
						
							|  |  |  | counter_execute(_PyExecutorObject *self, _PyInterpreterFrame *frame, PyObject **stack_pointer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ((_PyCounterExecutorObject *)self)->optimizer->count++; | 
					
						
							|  |  |  |     _PyFrame_SetStackPointer(frame, stack_pointer); | 
					
						
							|  |  |  |     frame->prev_instr = ((_PyCounterExecutorObject *)self)->next_instr - 1; | 
					
						
							|  |  |  |     Py_DECREF(self); | 
					
						
							|  |  |  |     return frame; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | counter_optimize( | 
					
						
							|  |  |  |     _PyOptimizerObject* self, | 
					
						
							|  |  |  |     PyCodeObject *code, | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |     _Py_CODEUNIT *instr, | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     _PyExecutorObject **exec_ptr, | 
					
						
							|  |  |  |     int Py_UNUSED(curr_stackentries) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     _PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&CounterExecutor_Type); | 
					
						
							|  |  |  |     if (executor == NULL) { | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     executor->executor.execute = counter_execute; | 
					
						
							|  |  |  |     Py_INCREF(self); | 
					
						
							|  |  |  |     executor->optimizer = (_PyCounterOptimizerObject *)self; | 
					
						
							|  |  |  |     executor->next_instr = instr; | 
					
						
							| 
									
										
										
										
											2023-06-05 09:44:23 +01:00
										 |  |  |     *exec_ptr = (_PyExecutorObject *)executor; | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | counter_get_counter(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyLong_FromLongLong(((_PyCounterOptimizerObject *)self)->count); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef counter_methods[] = { | 
					
						
							|  |  |  |     { "get_count", counter_get_counter, METH_NOARGS, NULL }, | 
					
						
							|  |  |  |     { NULL, NULL }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject CounterOptimizer_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     .tp_name = "Counter optimizer", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(_PyCounterOptimizerObject), | 
					
						
							|  |  |  |     .tp_itemsize = 0, | 
					
						
							|  |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
					
						
							|  |  |  |     .tp_methods = counter_methods, | 
					
						
							| 
									
										
										
										
											2023-09-01 10:16:09 +01:00
										 |  |  |     .tp_dealloc = (destructor)PyObject_Del, | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyUnstable_Optimizer_NewCounter(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&CounterOptimizer_Type); | 
					
						
							|  |  |  |     if (opt == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     opt->base.optimize = counter_optimize; | 
					
						
							|  |  |  |     opt->base.resume_threshold = UINT16_MAX; | 
					
						
							|  |  |  |     opt->base.backedge_threshold = 0; | 
					
						
							|  |  |  |     opt->count = 0; | 
					
						
							|  |  |  |     return (PyObject *)opt; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ///////////////////// Experimental UOp Optimizer /////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | uop_dealloc(_PyUOpExecutorObject *self) { | 
					
						
							|  |  |  |     PyObject_Free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  | static const char * | 
					
						
							|  |  |  | uop_name(int index) { | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |     if (index <= MAX_REAL_OPCODE) { | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  |         return _PyOpcode_OpName[index]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return _PyOpcode_uop_name[index]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Py_ssize_t | 
					
						
							|  |  |  | uop_len(_PyUOpExecutorObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-05 12:10:46 +08:00
										 |  |  |     return Py_SIZE(self); | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | uop_item(_PyUOpExecutorObject *self, Py_ssize_t index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     Py_ssize_t len = uop_len(self); | 
					
						
							|  |  |  |     if (index < 0 || index >= len) { | 
					
						
							|  |  |  |         PyErr_SetNone(PyExc_IndexError); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     const char *name = uop_name(self->trace[index].opcode); | 
					
						
							|  |  |  |     if (name == NULL) { | 
					
						
							|  |  |  |         name = "<nil>"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *oname = _PyUnicode_FromASCII(name, strlen(name)); | 
					
						
							|  |  |  |     if (oname == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |     PyObject *oparg = PyLong_FromUnsignedLong(self->trace[index].oparg); | 
					
						
							|  |  |  |     if (oparg == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(oname); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     PyObject *operand = PyLong_FromUnsignedLongLong(self->trace[index].operand); | 
					
						
							|  |  |  |     if (operand == NULL) { | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |         Py_DECREF(oparg); | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |         Py_DECREF(oname); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |     PyObject *args[3] = { oname, oparg, operand }; | 
					
						
							|  |  |  |     return _PyTuple_FromArraySteal(args, 3); | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PySequenceMethods uop_as_sequence = { | 
					
						
							|  |  |  |     .sq_length = (lenfunc)uop_len, | 
					
						
							|  |  |  |     .sq_item = (ssizeargfunc)uop_item, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | static PyTypeObject UOpExecutor_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     .tp_name = "uop_executor", | 
					
						
							| 
									
										
										
										
											2023-08-05 12:10:46 +08:00
										 |  |  |     .tp_basicsize = sizeof(_PyUOpExecutorObject) - sizeof(_PyUOpInstruction), | 
					
						
							|  |  |  |     .tp_itemsize = sizeof(_PyUOpInstruction), | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
					
						
							|  |  |  |     .tp_dealloc = (destructor)uop_dealloc, | 
					
						
							| 
									
										
										
										
											2023-07-04 17:23:00 +01:00
										 |  |  |     .tp_as_sequence = &uop_as_sequence, | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | move_stubs( | 
					
						
							|  |  |  |     _PyUOpInstruction *trace, | 
					
						
							|  |  |  |     int trace_length, | 
					
						
							|  |  |  |     int stubs_start, | 
					
						
							|  |  |  |     int stubs_end | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     memmove(trace + trace_length, | 
					
						
							|  |  |  |             trace + stubs_start, | 
					
						
							|  |  |  |             (stubs_end - stubs_start) * sizeof(_PyUOpInstruction)); | 
					
						
							|  |  |  |     // Patch up the jump targets
 | 
					
						
							|  |  |  |     for (int i = 0; i < trace_length; i++) { | 
					
						
							|  |  |  |         if (trace[i].opcode == _POP_JUMP_IF_FALSE || | 
					
						
							|  |  |  |             trace[i].opcode == _POP_JUMP_IF_TRUE) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int target = trace[i].oparg; | 
					
						
							|  |  |  |             if (target >= stubs_start) { | 
					
						
							|  |  |  |                 target += trace_length - stubs_start; | 
					
						
							|  |  |  |                 trace[i].oparg = target; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return trace_length + stubs_end - stubs_start; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  | #define TRACE_STACK_SIZE 5
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | translate_bytecode_to_trace( | 
					
						
							|  |  |  |     PyCodeObject *code, | 
					
						
							|  |  |  |     _Py_CODEUNIT *instr, | 
					
						
							|  |  |  |     _PyUOpInstruction *trace, | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     int buffer_size) | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |     PyCodeObject *initial_code = code; | 
					
						
							| 
									
										
										
										
											2023-07-06 11:39:53 -07:00
										 |  |  |     _Py_CODEUNIT *initial_instr = instr; | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     int trace_length = 0; | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     int max_length = buffer_size; | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |     int reserved = 0; | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |     struct { | 
					
						
							|  |  |  |         PyCodeObject *code; | 
					
						
							|  |  |  |         _Py_CODEUNIT *instr; | 
					
						
							|  |  |  |     } trace_stack[TRACE_STACK_SIZE]; | 
					
						
							|  |  |  |     int trace_stack_depth = 0; | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG"); | 
					
						
							|  |  |  |     int lltrace = 0; | 
					
						
							|  |  |  |     if (uop_debug != NULL && *uop_debug >= '0') { | 
					
						
							|  |  |  |         lltrace = *uop_debug - '0';  // TODO: Parse an int and all that
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | #define DPRINTF(level, ...) \
 | 
					
						
							| 
									
										
										
										
											2023-08-07 21:36:25 -07:00
										 |  |  |     if (lltrace >= (level)) { printf(__VA_ARGS__); } | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | #define DPRINTF(level, ...)
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  | #define ADD_TO_TRACE(OPCODE, OPARG, OPERAND) \
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     DPRINTF(2, \ | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |             "  ADD_TO_TRACE(%s, %d, %" PRIu64 ")\n", \ | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |             uop_name(OPCODE), \ | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |             (OPARG), \ | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |             (uint64_t)(OPERAND)); \ | 
					
						
							|  |  |  |     assert(trace_length < max_length); \ | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |     assert(reserved > 0); \ | 
					
						
							|  |  |  |     reserved--; \ | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     trace[trace_length].opcode = (OPCODE); \ | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |     trace[trace_length].oparg = (OPARG); \ | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     trace[trace_length].operand = (OPERAND); \ | 
					
						
							|  |  |  |     trace_length++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  | #define INSTR_IP(INSTR, CODE) \
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |     ((uint32_t)((INSTR) - ((_Py_CODEUNIT *)(CODE)->co_code_adaptive))) | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  | #define ADD_TO_STUB(INDEX, OPCODE, OPARG, OPERAND) \
 | 
					
						
							|  |  |  |     DPRINTF(2, "    ADD_TO_STUB(%d, %s, %d, %" PRIu64 ")\n", \ | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |             (INDEX), \ | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |             uop_name(OPCODE), \ | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |             (OPARG), \ | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |             (uint64_t)(OPERAND)); \ | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |     assert(reserved > 0); \ | 
					
						
							|  |  |  |     reserved--; \ | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     trace[(INDEX)].opcode = (OPCODE); \ | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |     trace[(INDEX)].oparg = (OPARG); \ | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |     trace[(INDEX)].operand = (OPERAND); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | // Reserve space for n uops
 | 
					
						
							|  |  |  | #define RESERVE_RAW(n, opname) \
 | 
					
						
							|  |  |  |     if (trace_length + (n) > max_length) { \ | 
					
						
							|  |  |  |         DPRINTF(2, "No room for %s (need %d, got %d)\n", \ | 
					
						
							|  |  |  |                 (opname), (n), max_length - trace_length); \ | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |         OPT_STAT_INC(trace_too_long); \ | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |         goto done; \ | 
					
						
							|  |  |  |     } \ | 
					
						
							|  |  |  |     reserved = (n);  // Keep ADD_TO_TRACE / ADD_TO_STUB honest
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  | // Reserve space for main+stub uops, plus 2 for _SET_IP and _EXIT_TRACE
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | #define RESERVE(main, stub) RESERVE_RAW((main) + (stub) + 2, uop_name(opcode))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  | // Trace stack operations (used by _PUSH_FRAME, _POP_FRAME)
 | 
					
						
							|  |  |  | #define TRACE_STACK_PUSH() \
 | 
					
						
							|  |  |  |     if (trace_stack_depth >= TRACE_STACK_SIZE) { \ | 
					
						
							|  |  |  |         DPRINTF(2, "Trace stack overflow\n"); \ | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |         OPT_STAT_INC(trace_stack_overflow); \ | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         ADD_TO_TRACE(_SET_IP, 0, 0); \ | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |         goto done; \ | 
					
						
							|  |  |  |     } \ | 
					
						
							|  |  |  |     trace_stack[trace_stack_depth].code = code; \ | 
					
						
							|  |  |  |     trace_stack[trace_stack_depth].instr = instr; \ | 
					
						
							|  |  |  |     trace_stack_depth++; | 
					
						
							|  |  |  | #define TRACE_STACK_POP() \
 | 
					
						
							|  |  |  |     if (trace_stack_depth <= 0) { \ | 
					
						
							|  |  |  |         Py_FatalError("Trace stack underflow\n"); \ | 
					
						
							|  |  |  |     } \ | 
					
						
							|  |  |  |     trace_stack_depth--; \ | 
					
						
							|  |  |  |     code = trace_stack[trace_stack_depth].code; \ | 
					
						
							|  |  |  |     instr = trace_stack[trace_stack_depth].instr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     DPRINTF(4, | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |             "Optimizing %s (%s:%d) at byte offset %d\n", | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |             PyUnicode_AsUTF8(code->co_qualname), | 
					
						
							|  |  |  |             PyUnicode_AsUTF8(code->co_filename), | 
					
						
							|  |  |  |             code->co_firstlineno, | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |             2 * INSTR_IP(initial_instr, code)); | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  | top:  // Jump here after _PUSH_FRAME or likely branches
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         RESERVE_RAW(2, "epilogue");  // Always need space for _SET_IP and _EXIT_TRACE
 | 
					
						
							|  |  |  |         ADD_TO_TRACE(_SET_IP, INSTR_IP(instr, code), 0); | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |         uint32_t opcode = instr->op.code; | 
					
						
							|  |  |  |         uint32_t oparg = instr->op.arg; | 
					
						
							|  |  |  |         uint32_t extras = 0; | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 16:46:06 -07:00
										 |  |  |         while (opcode == EXTENDED_ARG) { | 
					
						
							|  |  |  |             instr++; | 
					
						
							|  |  |  |             extras += 1; | 
					
						
							|  |  |  |             opcode = instr->op.code; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |             oparg = (oparg << 8) | instr->op.arg; | 
					
						
							| 
									
										
										
										
											2023-07-06 16:46:06 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 11:41:42 -07:00
										 |  |  |         if (opcode == ENTER_EXECUTOR) { | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |             _PyExecutorObject *executor = | 
					
						
							|  |  |  |                 (_PyExecutorObject *)code->co_executors->executors[oparg&255]; | 
					
						
							| 
									
										
										
										
											2023-07-07 11:41:42 -07:00
										 |  |  |             opcode = executor->vm_data.opcode; | 
					
						
							|  |  |  |             DPRINTF(2, "  * ENTER_EXECUTOR -> %s\n",  _PyOpcode_OpName[opcode]); | 
					
						
							|  |  |  |             oparg = (oparg & 0xffffff00) | executor->vm_data.oparg; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |         switch (opcode) { | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 10:06:05 -07:00
										 |  |  |             case POP_JUMP_IF_NONE: | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 RESERVE(2, 2); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                 ADD_TO_TRACE(_IS_NONE, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-07-17 10:06:05 -07:00
										 |  |  |                 opcode = POP_JUMP_IF_TRUE; | 
					
						
							|  |  |  |                 goto pop_jump_if_bool; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case POP_JUMP_IF_NOT_NONE: | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 RESERVE(2, 2); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                 ADD_TO_TRACE(_IS_NONE, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-07-17 10:06:05 -07:00
										 |  |  |                 opcode = POP_JUMP_IF_FALSE; | 
					
						
							|  |  |  |                 goto pop_jump_if_bool; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |             case POP_JUMP_IF_FALSE: | 
					
						
							|  |  |  |             case POP_JUMP_IF_TRUE: | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-07-17 10:06:05 -07:00
										 |  |  | pop_jump_if_bool: | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                 RESERVE(1, 2); | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |                 max_length -= 2;  // Really the start of the stubs
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  |                 int counter = instr[1].cache; | 
					
						
							|  |  |  |                 int bitcount = _Py_popcount32(counter); | 
					
						
							|  |  |  |                 bool jump_likely = bitcount > 8; | 
					
						
							|  |  |  |                 bool jump_sense = opcode == POP_JUMP_IF_TRUE; | 
					
						
							|  |  |  |                 uint32_t uopcode = jump_sense ^ jump_likely ? | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |                     _POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE; | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  |                 _Py_CODEUNIT *next_instr = instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]; | 
					
						
							|  |  |  |                 _Py_CODEUNIT *target_instr = next_instr + oparg; | 
					
						
							|  |  |  |                 _Py_CODEUNIT *stub_target = jump_likely ? next_instr : target_instr; | 
					
						
							|  |  |  |                 DPRINTF(4, "%s(%d): counter=%x, bitcount=%d, likely=%d, sense=%d, uopcode=%s\n", | 
					
						
							|  |  |  |                         uop_name(opcode), oparg, | 
					
						
							|  |  |  |                         counter, bitcount, jump_likely, jump_sense, uop_name(uopcode)); | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                 ADD_TO_TRACE(uopcode, max_length, 0); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                 ADD_TO_STUB(max_length, _SET_IP, INSTR_IP(stub_target, code), 0); | 
					
						
							|  |  |  |                 ADD_TO_STUB(max_length + 1, _EXIT_TRACE, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  |                 if (jump_likely) { | 
					
						
							|  |  |  |                     DPRINTF(2, "Jump likely (%x = %d bits), continue at byte offset %d\n", | 
					
						
							|  |  |  |                             instr[1].cache, bitcount, 2 * INSTR_IP(target_instr, code)); | 
					
						
							|  |  |  |                     instr = target_instr; | 
					
						
							|  |  |  |                     goto top; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 11:08:10 -07:00
										 |  |  |             case JUMP_BACKWARD: | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                 if (instr + 2 - oparg == initial_instr && code == initial_code) { | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                     RESERVE(1, 0); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                     ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-07-11 11:08:10 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |                     OPT_STAT_INC(inner_loop); | 
					
						
							| 
									
										
										
										
											2023-07-11 11:08:10 -07:00
										 |  |  |                     DPRINTF(2, "JUMP_BACKWARD not to top ends trace\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 goto done; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:13:57 -07:00
										 |  |  |             case JUMP_FORWARD: | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                 RESERVE(0, 0); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                 // This will emit two _SET_IP instructions; leave it to the optimizer
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:13:57 -07:00
										 |  |  |                 instr += oparg; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |             case FOR_ITER_LIST: | 
					
						
							|  |  |  |             case FOR_ITER_TUPLE: | 
					
						
							| 
									
										
										
										
											2023-07-12 10:23:59 -07:00
										 |  |  |             case FOR_ITER_RANGE: | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                 RESERVE(4, 3); | 
					
						
							|  |  |  |                 int check_op, exhausted_op, next_op; | 
					
						
							|  |  |  |                 switch (opcode) { | 
					
						
							|  |  |  |                     case FOR_ITER_LIST: | 
					
						
							|  |  |  |                         check_op = _ITER_CHECK_LIST; | 
					
						
							|  |  |  |                         exhausted_op = _IS_ITER_EXHAUSTED_LIST; | 
					
						
							|  |  |  |                         next_op = _ITER_NEXT_LIST; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case FOR_ITER_TUPLE: | 
					
						
							|  |  |  |                         check_op = _ITER_CHECK_TUPLE; | 
					
						
							|  |  |  |                         exhausted_op = _IS_ITER_EXHAUSTED_TUPLE; | 
					
						
							|  |  |  |                         next_op = _ITER_NEXT_TUPLE; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case FOR_ITER_RANGE: | 
					
						
							|  |  |  |                         check_op = _ITER_CHECK_RANGE; | 
					
						
							|  |  |  |                         exhausted_op = _IS_ITER_EXHAUSTED_RANGE; | 
					
						
							|  |  |  |                         next_op = _ITER_NEXT_RANGE; | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     default: | 
					
						
							|  |  |  |                         Py_UNREACHABLE(); | 
					
						
							| 
									
										
										
										
											2023-07-12 10:23:59 -07:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                 // Assume jump unlikely (can a for-loop exit be likely?)
 | 
					
						
							| 
									
										
										
										
											2023-07-12 10:23:59 -07:00
										 |  |  |                 _Py_CODEUNIT *target_instr =  // +1 at the end skips over END_FOR
 | 
					
						
							|  |  |  |                     instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]] + oparg + 1; | 
					
						
							|  |  |  |                 max_length -= 3;  // Really the start of the stubs
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                 ADD_TO_TRACE(check_op, 0, 0); | 
					
						
							|  |  |  |                 ADD_TO_TRACE(exhausted_op, 0, 0); | 
					
						
							|  |  |  |                 ADD_TO_TRACE(_POP_JUMP_IF_TRUE, max_length, 0); | 
					
						
							|  |  |  |                 ADD_TO_TRACE(next_op, 0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 ADD_TO_STUB(max_length + 0, POP_TOP, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                 ADD_TO_STUB(max_length + 1, _SET_IP, INSTR_IP(target_instr, code), 0); | 
					
						
							|  |  |  |                 ADD_TO_STUB(max_length + 2, _EXIT_TRACE, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-07-12 10:23:59 -07:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |             default: | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode]; | 
					
						
							|  |  |  |                 if (expansion->nuops > 0) { | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                     // Reserve space for nuops (+ _SET_IP + _EXIT_TRACE)
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                     int nuops = expansion->nuops; | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |                     RESERVE(nuops, 0); | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                     if (expansion->uops[nuops-1].uop == _POP_FRAME) { | 
					
						
							|  |  |  |                         // Check for trace stack underflow now:
 | 
					
						
							|  |  |  |                         // We can't bail e.g. in the middle of
 | 
					
						
							|  |  |  |                         // LOAD_CONST + _POP_FRAME.
 | 
					
						
							|  |  |  |                         if (trace_stack_depth == 0) { | 
					
						
							|  |  |  |                             DPRINTF(2, "Trace stack underflow\n"); | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |                             OPT_STAT_INC(trace_stack_underflow); | 
					
						
							|  |  |  |                             goto done; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                     uint32_t orig_oparg = oparg;  // For OPARG_TOP/BOTTOM
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                     for (int i = 0; i < nuops; i++) { | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                         oparg = orig_oparg; | 
					
						
							| 
									
										
										
										
											2023-10-12 10:34:32 +01:00
										 |  |  |                         uint32_t uop = expansion->uops[i].uop; | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                         uint64_t operand = 0; | 
					
						
							| 
									
										
										
										
											2023-07-31 14:16:57 -07:00
										 |  |  |                         // Add one to account for the actual opcode/oparg pair:
 | 
					
						
							|  |  |  |                         int offset = expansion->uops[i].offset + 1; | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                         switch (expansion->uops[i].size) { | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                             case OPARG_FULL: | 
					
						
							| 
									
										
										
										
											2023-07-06 16:46:06 -07:00
										 |  |  |                                 if (extras && OPCODE_HAS_JUMP(opcode)) { | 
					
						
							|  |  |  |                                     if (opcode == JUMP_BACKWARD_NO_INTERRUPT) { | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                                         oparg -= extras; | 
					
						
							| 
									
										
										
										
											2023-07-06 16:46:06 -07:00
										 |  |  |                                     } | 
					
						
							|  |  |  |                                     else { | 
					
						
							|  |  |  |                                         assert(opcode != JUMP_BACKWARD); | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                                         oparg += extras; | 
					
						
							| 
									
										
										
										
											2023-07-06 16:46:06 -07:00
										 |  |  |                                     } | 
					
						
							|  |  |  |                                 } | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                                 break; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                             case OPARG_CACHE_1: | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                                 operand = read_u16(&instr[offset].cache); | 
					
						
							|  |  |  |                                 break; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                             case OPARG_CACHE_2: | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                                 operand = read_u32(&instr[offset].cache); | 
					
						
							|  |  |  |                                 break; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                             case OPARG_CACHE_4: | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                                 operand = read_u64(&instr[offset].cache); | 
					
						
							|  |  |  |                                 break; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                             case OPARG_TOP:  // First half of super-instr
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                                 oparg = orig_oparg >> 4; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                                 break; | 
					
						
							|  |  |  |                             case OPARG_BOTTOM:  // Second half of super-instr
 | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                                 oparg = orig_oparg & 0xF; | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                                 break; | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                             case OPARG_SET_IP:  // op==_SET_IP; oparg=next instr
 | 
					
						
							| 
									
										
										
										
											2023-08-16 16:26:43 -07:00
										 |  |  |                                 oparg = INSTR_IP(instr + offset, code); | 
					
						
							| 
									
										
										
										
											2023-10-12 10:34:32 +01:00
										 |  |  |                                 uop = _SET_IP; | 
					
						
							| 
									
										
										
										
											2023-08-16 16:26:43 -07:00
										 |  |  |                                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                             default: | 
					
						
							|  |  |  |                                 fprintf(stderr, | 
					
						
							| 
									
										
										
										
											2023-07-07 10:42:10 -07:00
										 |  |  |                                         "opcode=%d, oparg=%d; nuops=%d, i=%d; size=%d, offset=%d\n", | 
					
						
							|  |  |  |                                         opcode, oparg, nuops, i, | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                                         expansion->uops[i].size, | 
					
						
							|  |  |  |                                         expansion->uops[i].offset); | 
					
						
							|  |  |  |                                 Py_FatalError("garbled expansion"); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2023-10-12 10:34:32 +01:00
										 |  |  |                         ADD_TO_TRACE(uop, oparg, operand); | 
					
						
							|  |  |  |                         if (uop == _POP_FRAME) { | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                             TRACE_STACK_POP(); | 
					
						
							|  |  |  |                             DPRINTF(2, | 
					
						
							|  |  |  |                                 "Returning to %s (%s:%d) at byte offset %d\n", | 
					
						
							|  |  |  |                                 PyUnicode_AsUTF8(code->co_qualname), | 
					
						
							|  |  |  |                                 PyUnicode_AsUTF8(code->co_filename), | 
					
						
							|  |  |  |                                 code->co_firstlineno, | 
					
						
							|  |  |  |                                 2 * INSTR_IP(instr, code)); | 
					
						
							|  |  |  |                             goto top; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2023-10-12 10:34:32 +01:00
										 |  |  |                         if (uop == _PUSH_FRAME) { | 
					
						
							| 
									
										
										
										
											2023-08-16 16:26:43 -07:00
										 |  |  |                             assert(i + 1 == nuops); | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                             int func_version_offset = | 
					
						
							|  |  |  |                                 offsetof(_PyCallCache, func_version)/sizeof(_Py_CODEUNIT) | 
					
						
							|  |  |  |                                 // Add one to account for the actual opcode/oparg pair:
 | 
					
						
							|  |  |  |                                 + 1; | 
					
						
							|  |  |  |                             uint32_t func_version = read_u32(&instr[func_version_offset].cache); | 
					
						
							|  |  |  |                             PyFunctionObject *func = _PyFunction_LookupByVersion(func_version); | 
					
						
							|  |  |  |                             DPRINTF(3, "Function object: %p\n", func); | 
					
						
							|  |  |  |                             if (func != NULL) { | 
					
						
							|  |  |  |                                 PyCodeObject *new_code = (PyCodeObject *)PyFunction_GET_CODE(func); | 
					
						
							|  |  |  |                                 if (new_code == code) { | 
					
						
							|  |  |  |                                     // Recursive call, bail (we could be here forever).
 | 
					
						
							|  |  |  |                                     DPRINTF(2, "Bailing on recursive call to %s (%s:%d)\n", | 
					
						
							|  |  |  |                                             PyUnicode_AsUTF8(new_code->co_qualname), | 
					
						
							|  |  |  |                                             PyUnicode_AsUTF8(new_code->co_filename), | 
					
						
							|  |  |  |                                             new_code->co_firstlineno); | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |                                     OPT_STAT_INC(recursive_call); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                                     ADD_TO_TRACE(_SET_IP, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                                     goto done; | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                                 if (new_code->co_version != func_version) { | 
					
						
							|  |  |  |                                     // func.__code__ was updated.
 | 
					
						
							|  |  |  |                                     // Perhaps it may happen again, so don't bother tracing.
 | 
					
						
							|  |  |  |                                     // TODO: Reason about this -- is it better to bail or not?
 | 
					
						
							|  |  |  |                                     DPRINTF(2, "Bailing because co_version != func_version\n"); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                                     ADD_TO_TRACE(_SET_IP, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |                                     goto done; | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                                 // Increment IP to the return address
 | 
					
						
							|  |  |  |                                 instr += _PyOpcode_Caches[_PyOpcode_Deopt[opcode]] + 1; | 
					
						
							|  |  |  |                                 TRACE_STACK_PUSH(); | 
					
						
							|  |  |  |                                 code = new_code; | 
					
						
							|  |  |  |                                 instr = _PyCode_CODE(code); | 
					
						
							|  |  |  |                                 DPRINTF(2, | 
					
						
							|  |  |  |                                     "Continuing in %s (%s:%d) at byte offset %d\n", | 
					
						
							|  |  |  |                                     PyUnicode_AsUTF8(code->co_qualname), | 
					
						
							|  |  |  |                                     PyUnicode_AsUTF8(code->co_filename), | 
					
						
							|  |  |  |                                     code->co_firstlineno, | 
					
						
							|  |  |  |                                     2 * INSTR_IP(instr, code)); | 
					
						
							|  |  |  |                                 goto top; | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |                             ADD_TO_TRACE(_SET_IP, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-08-16 16:26:43 -07:00
										 |  |  |                             goto done; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |                 DPRINTF(2, "Unsupported opcode %s\n", uop_name(opcode)); | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |                 OPT_UNSUPPORTED_OPCODE(opcode); | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |                 goto done;  // Break out of loop
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |             }  // End default
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }  // End switch (opcode)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |         instr++; | 
					
						
							|  |  |  |         // Add cache size for opcode
 | 
					
						
							|  |  |  |         instr += _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]; | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  |     }  // End for (;;)
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | done: | 
					
						
							| 
									
										
										
										
											2023-08-17 11:29:58 -07:00
										 |  |  |     while (trace_stack_depth > 0) { | 
					
						
							|  |  |  |         TRACE_STACK_POP(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(code == initial_code); | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |     // Skip short traces like _SET_IP, LOAD_FAST, _SET_IP, _EXIT_TRACE
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     if (trace_length > 3) { | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         ADD_TO_TRACE(_EXIT_TRACE, 0, 0); | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |         DPRINTF(1, | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |                 "Created a trace for %s (%s:%d) at byte offset %d -- length %d+%d\n", | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |                 PyUnicode_AsUTF8(code->co_qualname), | 
					
						
							|  |  |  |                 PyUnicode_AsUTF8(code->co_filename), | 
					
						
							|  |  |  |                 code->co_firstlineno, | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |                 2 * INSTR_IP(initial_instr, code), | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |                 trace_length, | 
					
						
							|  |  |  |                 buffer_size - max_length); | 
					
						
							|  |  |  |         if (max_length < buffer_size) { | 
					
						
							|  |  |  |             // There are stubs
 | 
					
						
							|  |  |  |             if (trace_length < max_length) { | 
					
						
							|  |  |  |                 // There's a gap before the stubs
 | 
					
						
							|  |  |  |                 // Move the stubs back to be immediately after the main trace
 | 
					
						
							|  |  |  |                 // (which ends at trace_length)
 | 
					
						
							|  |  |  |                 DPRINTF(2, | 
					
						
							|  |  |  |                         "Moving %d stub uops back by %d\n", | 
					
						
							|  |  |  |                         buffer_size - max_length, | 
					
						
							|  |  |  |                         max_length - trace_length); | 
					
						
							|  |  |  |                 trace_length = move_stubs(trace, trace_length, max_length, buffer_size); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 assert(trace_length == max_length); | 
					
						
							|  |  |  |                 // There's no gap
 | 
					
						
							|  |  |  |                 trace_length = buffer_size; | 
					
						
							| 
									
										
										
										
											2023-07-10 16:04:26 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |         return trace_length; | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-10-05 11:12:06 -04:00
										 |  |  |         OPT_STAT_INC(trace_too_short); | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |         DPRINTF(4, | 
					
						
							| 
									
										
										
										
											2023-07-17 12:12:33 -07:00
										 |  |  |                 "No trace for %s (%s:%d) at byte offset %d\n", | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |                 PyUnicode_AsUTF8(code->co_qualname), | 
					
						
							|  |  |  |                 PyUnicode_AsUTF8(code->co_filename), | 
					
						
							|  |  |  |                 code->co_firstlineno, | 
					
						
							| 
									
										
										
										
											2023-07-11 23:25:41 +03:00
										 |  |  |                 2 * INSTR_IP(initial_instr, code)); | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:27:35 -07:00
										 |  |  | #undef RESERVE
 | 
					
						
							|  |  |  | #undef RESERVE_RAW
 | 
					
						
							|  |  |  | #undef INSTR_IP
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | #undef ADD_TO_TRACE
 | 
					
						
							| 
									
										
										
										
											2023-07-03 13:05:11 -07:00
										 |  |  | #undef DPRINTF
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | remove_unneeded_uops(_PyUOpInstruction *trace, int trace_length) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |     // Stage 1: Replace unneeded _SET_IP uops with NOP.
 | 
					
						
							|  |  |  |     // Note that we don't enter stubs, those SET_IPs are needed.
 | 
					
						
							|  |  |  |     int last_set_ip = -1; | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |     int last_instr = 0; | 
					
						
							|  |  |  |     bool need_ip = true; | 
					
						
							|  |  |  |     for (int pc = 0; pc < trace_length; pc++) { | 
					
						
							|  |  |  |         int opcode = trace[pc].opcode; | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         if (opcode == _SAVE_CURRENT_IP) { | 
					
						
							|  |  |  |             // Special case: never remove preceding _SET_IP
 | 
					
						
							|  |  |  |             last_set_ip = -1; | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         else if (opcode == _SET_IP) { | 
					
						
							|  |  |  |             if (!need_ip && last_set_ip >= 0) { | 
					
						
							|  |  |  |                 trace[last_set_ip].opcode = NOP; | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |             need_ip = false; | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |             last_set_ip = pc; | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-11 15:39:19 -07:00
										 |  |  |         else if (opcode == _JUMP_TO_TOP || opcode == _EXIT_TRACE) { | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |             last_instr = pc + 1; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // If opcode has ERROR or DEOPT, set need_up to true
 | 
					
						
							|  |  |  |             if (_PyOpcode_opcode_metadata[opcode].flags & (HAS_ERROR_FLAG | HAS_DEOPT_FLAG)) { | 
					
						
							|  |  |  |                 need_ip = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Stage 2: Squash NOP opcodes (pre-existing or set above).
 | 
					
						
							|  |  |  |     int dest = 0; | 
					
						
							|  |  |  |     for (int pc = 0; pc < last_instr; pc++) { | 
					
						
							|  |  |  |         int opcode = trace[pc].opcode; | 
					
						
							|  |  |  |         if (opcode != NOP) { | 
					
						
							|  |  |  |             if (pc != dest) { | 
					
						
							|  |  |  |                 trace[dest] = trace[pc]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             dest++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Stage 3: Move the stubs back.
 | 
					
						
							|  |  |  |     if (dest < last_instr) { | 
					
						
							|  |  |  |         int new_trace_length = move_stubs(trace, dest, last_instr, trace_length); | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  |         char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG"); | 
					
						
							|  |  |  |         int lltrace = 0; | 
					
						
							|  |  |  |         if (uop_debug != NULL && *uop_debug >= '0') { | 
					
						
							|  |  |  |             lltrace = *uop_debug - '0';  // TODO: Parse an int and all that
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (lltrace >= 2) { | 
					
						
							|  |  |  |             printf("Optimized trace (length %d+%d = %d, saved %d):\n", | 
					
						
							|  |  |  |                 dest, trace_length - last_instr, new_trace_length, | 
					
						
							|  |  |  |                 trace_length - new_trace_length); | 
					
						
							|  |  |  |             for (int pc = 0; pc < new_trace_length; pc++) { | 
					
						
							|  |  |  |                 printf("%4d: (%s, %d, %" PRIu64 ")\n", | 
					
						
							|  |  |  |                     pc, | 
					
						
							|  |  |  |                     uop_name(trace[pc].opcode), | 
					
						
							|  |  |  |                     (trace[pc].oparg), | 
					
						
							|  |  |  |                     (uint64_t)(trace[pc].operand)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         trace_length = new_trace_length; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return trace_length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | uop_optimize( | 
					
						
							|  |  |  |     _PyOptimizerObject *self, | 
					
						
							|  |  |  |     PyCodeObject *code, | 
					
						
							|  |  |  |     _Py_CODEUNIT *instr, | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     _PyExecutorObject **exec_ptr, | 
					
						
							|  |  |  |     int curr_stackentries) | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     _PyUOpInstruction trace[_Py_UOP_MAX_TRACE_LENGTH]; | 
					
						
							|  |  |  |     int trace_length = translate_bytecode_to_trace(code, instr, trace, _Py_UOP_MAX_TRACE_LENGTH); | 
					
						
							|  |  |  |     if (trace_length <= 0) { | 
					
						
							|  |  |  |         // Error or nothing translated
 | 
					
						
							|  |  |  |         return trace_length; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |     OPT_HIST(trace_length, trace_length_hist); | 
					
						
							|  |  |  |     OPT_STAT_INC(traces_created); | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     char *uop_optimize = Py_GETENV("PYTHONUOPSOPTIMIZE"); | 
					
						
							|  |  |  |     if (uop_optimize != NULL && *uop_optimize > '0') { | 
					
						
							|  |  |  |         trace_length = _Py_uop_analyze_and_optimize(code, trace, trace_length, curr_stackentries); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-29 09:51:51 -07:00
										 |  |  |     trace_length = remove_unneeded_uops(trace, trace_length); | 
					
						
							| 
									
										
										
										
											2023-08-05 12:10:46 +08:00
										 |  |  |     _PyUOpExecutorObject *executor = PyObject_NewVar(_PyUOpExecutorObject, &UOpExecutor_Type, trace_length); | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     if (executor == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-04 17:52:28 -04:00
										 |  |  |     OPT_HIST(trace_length, optimized_trace_length_hist); | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     executor->base.execute = _PyUopExecute; | 
					
						
							|  |  |  |     memcpy(executor->trace, trace, trace_length * sizeof(_PyUOpInstruction)); | 
					
						
							|  |  |  |     *exec_ptr = (_PyExecutorObject *)executor; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-06 15:45:56 -07:00
										 |  |  | static void | 
					
						
							|  |  |  | uop_opt_dealloc(PyObject *self) { | 
					
						
							|  |  |  |     PyObject_Free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | static PyTypeObject UOpOptimizer_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     .tp_name = "uop_optimizer", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(_PyOptimizerObject), | 
					
						
							|  |  |  |     .tp_itemsize = 0, | 
					
						
							|  |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | 
					
						
							| 
									
										
										
										
											2023-07-06 15:45:56 -07:00
										 |  |  |     .tp_dealloc = uop_opt_dealloc, | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyUnstable_Optimizer_NewUOpOptimizer(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-06 15:45:56 -07:00
										 |  |  |     _PyOptimizerObject *opt = PyObject_New(_PyOptimizerObject, &UOpOptimizer_Type); | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     if (opt == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     opt->optimize = uop_optimize; | 
					
						
							|  |  |  |     opt->resume_threshold = UINT16_MAX; | 
					
						
							| 
									
										
										
										
											2023-06-28 11:28:07 -07:00
										 |  |  |     // Need at least 3 iterations to settle specializations.
 | 
					
						
							|  |  |  |     // A few lower bits of the counter are reserved for other flags.
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  |     opt->backedge_threshold = 16 << OPTIMIZER_BITS_IN_COUNTER; | 
					
						
							| 
									
										
										
										
											2023-06-26 19:02:57 -07:00
										 |  |  |     return (PyObject *)opt; | 
					
						
							|  |  |  | } |