gh-104584: readability improvements in optimizer.c (#106641)

This commit is contained in:
Irit Katriel 2023-07-11 23:25:41 +03:00 committed by GitHub
parent f520804b03
commit 3590c45a3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@
#include "pycore_interp.h" #include "pycore_interp.h"
#include "pycore_opcode.h" #include "pycore_opcode.h"
#include "opcode_metadata.h" #include "opcode_metadata.h"
#include "pycore_opcode_utils.h"
#include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_uops.h" #include "pycore_uops.h"
#include "cpython/optimizer.h" #include "cpython/optimizer.h"
@ -10,6 +11,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#define MAX_EXECUTORS_SIZE 256
static bool static bool
has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr) has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
{ {
@ -19,7 +22,7 @@ has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
if (code->co_executors == NULL) { if (code->co_executors == NULL) {
return true; return true;
} }
return code->co_executors->size < 256; return code->co_executors->size < MAX_EXECUTORS_SIZE;
} }
static int32_t static int32_t
@ -34,7 +37,7 @@ get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
if (old != NULL) { if (old != NULL) {
size = old->size; size = old->size;
capacity = old->capacity; capacity = old->capacity;
assert(size < 256); assert(size < MAX_EXECUTORS_SIZE);
} }
assert(size <= capacity); assert(size <= capacity);
if (size == capacity) { if (size == capacity) {
@ -74,7 +77,7 @@ insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorO
executor->vm_data.opcode = instr->op.code; executor->vm_data.opcode = instr->op.code;
executor->vm_data.oparg = instr->op.arg; executor->vm_data.oparg = instr->op.arg;
code->co_executors->executors[index] = executor; code->co_executors->executors[index] = executor;
assert(index < 256); assert(index < MAX_EXECUTORS_SIZE);
instr->op.code = ENTER_EXECUTOR; instr->op.code = ENTER_EXECUTOR;
instr->op.arg = index; instr->op.arg = index;
code->co_executors->size++; code->co_executors->size++;
@ -307,7 +310,7 @@ uop_dealloc(_PyUOpExecutorObject *self) {
static const char * static const char *
uop_name(int index) { uop_name(int index) {
if (index < 256) { if (index <= MAX_REAL_OPCODE) {
return _PyOpcode_OpName[index]; return _PyOpcode_OpName[index];
} }
return _PyOpcode_uop_name[index]; return _PyOpcode_uop_name[index];
@ -391,17 +394,20 @@ translate_bytecode_to_trace(
#define ADD_TO_TRACE(OPCODE, OPERAND) \ #define ADD_TO_TRACE(OPCODE, OPERAND) \
DPRINTF(2, \ DPRINTF(2, \
" ADD_TO_TRACE(%s, %" PRIu64 ")\n", \ " ADD_TO_TRACE(%s, %" PRIu64 ")\n", \
(OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)], \ uop_name(OPCODE), \
(uint64_t)(OPERAND)); \ (uint64_t)(OPERAND)); \
assert(trace_length < max_length); \ assert(trace_length < max_length); \
trace[trace_length].opcode = (OPCODE); \ trace[trace_length].opcode = (OPCODE); \
trace[trace_length].operand = (OPERAND); \ trace[trace_length].operand = (OPERAND); \
trace_length++; trace_length++;
#define INSTR_IP(INSTR, CODE) \
((long)((INSTR) - ((_Py_CODEUNIT *)(CODE)->co_code_adaptive)))
#define ADD_TO_STUB(INDEX, OPCODE, OPERAND) \ #define ADD_TO_STUB(INDEX, OPCODE, OPERAND) \
DPRINTF(2, " ADD_TO_STUB(%d, %s, %" PRIu64 ")\n", \ DPRINTF(2, " ADD_TO_STUB(%d, %s, %" PRIu64 ")\n", \
(INDEX), \ (INDEX), \
(OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)], \ uop_name(OPCODE), \
(uint64_t)(OPERAND)); \ (uint64_t)(OPERAND)); \
trace[(INDEX)].opcode = (OPCODE); \ trace[(INDEX)].opcode = (OPCODE); \
trace[(INDEX)].operand = (OPERAND); trace[(INDEX)].operand = (OPERAND);
@ -411,10 +417,10 @@ translate_bytecode_to_trace(
PyUnicode_AsUTF8(code->co_qualname), PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename), PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno, code->co_firstlineno,
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive)); 2 * INSTR_IP(initial_instr, code));
for (;;) { for (;;) {
ADD_TO_TRACE(SAVE_IP, instr - (_Py_CODEUNIT *)code->co_code_adaptive); ADD_TO_TRACE(SAVE_IP, INSTR_IP(instr, code));
int opcode = instr->op.code; int opcode = instr->op.code;
int oparg = instr->op.arg; int oparg = instr->op.arg;
int extras = 0; int extras = 0;
@ -448,8 +454,7 @@ translate_bytecode_to_trace(
int uopcode = opcode == POP_JUMP_IF_TRUE ? int uopcode = opcode == POP_JUMP_IF_TRUE ?
_POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE; _POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE;
ADD_TO_TRACE(uopcode, max_length); ADD_TO_TRACE(uopcode, max_length);
ADD_TO_STUB(max_length, SAVE_IP, ADD_TO_STUB(max_length, SAVE_IP, INSTR_IP(target_instr, code));
target_instr - (_Py_CODEUNIT *)code->co_code_adaptive);
ADD_TO_STUB(max_length + 1, EXIT_TRACE, 0); ADD_TO_STUB(max_length + 1, EXIT_TRACE, 0);
break; break;
} }
@ -474,9 +479,7 @@ translate_bytecode_to_trace(
// Reserve space for nuops (+ SAVE_IP + EXIT_TRACE) // Reserve space for nuops (+ SAVE_IP + EXIT_TRACE)
int nuops = expansion->nuops; int nuops = expansion->nuops;
if (trace_length + nuops + 2 > max_length) { if (trace_length + nuops + 2 > max_length) {
DPRINTF(1, DPRINTF(1, "Ran out of space for %s\n", uop_name(opcode));
"Ran out of space for %s\n",
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode]);
goto done; goto done;
} }
for (int i = 0; i < nuops; i++) { for (int i = 0; i < nuops; i++) {
@ -522,9 +525,7 @@ translate_bytecode_to_trace(
} }
break; break;
} }
DPRINTF(2, DPRINTF(2, "Unsupported opcode %s\n", uop_name(opcode));
"Unsupported opcode %s\n",
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode]);
goto done; // Break out of loop goto done; // Break out of loop
} }
} }
@ -542,7 +543,7 @@ done:
PyUnicode_AsUTF8(code->co_qualname), PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename), PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno, code->co_firstlineno,
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive), 2 * INSTR_IP(initial_instr, code),
trace_length); trace_length);
if (max_length < buffer_size && trace_length < max_length) { if (max_length < buffer_size && trace_length < max_length) {
// Move the stubs back to be immediately after the main trace // Move the stubs back to be immediately after the main trace
@ -576,7 +577,7 @@ done:
PyUnicode_AsUTF8(code->co_qualname), PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename), PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno, code->co_firstlineno,
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive)); 2 * INSTR_IP(initial_instr, code));
} }
return 0; return 0;