mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
GH-113710: Add a tier 2 peephole optimization pass. (GH-114487)
* Convert _LOAD_CONST to inline versions * Remove PEP 523 checks
This commit is contained in:
parent
1e4f00ebd8
commit
384429d1c0
7 changed files with 66 additions and 6 deletions
|
@ -12,6 +12,39 @@
|
|||
#include <stddef.h>
|
||||
#include "pycore_optimizer.h"
|
||||
|
||||
static void
|
||||
peephole_opt(PyCodeObject *co, _PyUOpInstruction *buffer, int buffer_size)
|
||||
{
|
||||
for (int pc = 0; pc < buffer_size; pc++) {
|
||||
int opcode = buffer[pc].opcode;
|
||||
switch(opcode) {
|
||||
case _LOAD_CONST: {
|
||||
assert(co != NULL);
|
||||
PyObject *val = PyTuple_GET_ITEM(co->co_consts, buffer[pc].oparg);
|
||||
buffer[pc].opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
|
||||
buffer[pc].operand = (uintptr_t)val;
|
||||
break;
|
||||
}
|
||||
case _CHECK_PEP_523:
|
||||
{
|
||||
/* Setting the eval frame function invalidates
|
||||
* all executors, so no need to check dynamically */
|
||||
if (_PyInterpreterState_GET()->eval_frame == NULL) {
|
||||
buffer[pc].opcode = _NOP;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case _PUSH_FRAME:
|
||||
case _POP_FRAME:
|
||||
co = (PyCodeObject *)buffer[pc].operand;
|
||||
break;
|
||||
case _JUMP_TO_TOP:
|
||||
case _EXIT_TRACE:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
|
||||
{
|
||||
|
@ -59,6 +92,7 @@ _Py_uop_analyze_and_optimize(
|
|||
int curr_stacklen
|
||||
)
|
||||
{
|
||||
peephole_opt(co, buffer, buffer_size);
|
||||
remove_unneeded_uops(buffer, buffer_size);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue