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:
Mark Shannon 2024-01-24 12:08:31 +00:00 committed by GitHub
parent 1e4f00ebd8
commit 384429d1c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 66 additions and 6 deletions

View file

@ -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;
}