Skip to content

Commit 3590c45

Browse files
authored
gh-104584: readability improvements in optimizer.c (#106641)
1 parent f520804 commit 3590c45

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

Python/optimizer.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#include "pycore_interp.h"
44
#include "pycore_opcode.h"
55
#include "opcode_metadata.h"
6+
#include "pycore_opcode_utils.h"
67
#include "pycore_pystate.h" // _PyInterpreterState_GET()
78
#include "pycore_uops.h"
89
#include "cpython/optimizer.h"
910
#include <stdbool.h>
1011
#include <stdint.h>
1112
#include <stddef.h>
1213

14+
#define MAX_EXECUTORS_SIZE 256
15+
1316
static bool
1417
has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
1518
{
@@ -19,7 +22,7 @@ has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
1922
if (code->co_executors == NULL) {
2023
return true;
2124
}
22-
return code->co_executors->size < 256;
25+
return code->co_executors->size < MAX_EXECUTORS_SIZE;
2326
}
2427

2528
static int32_t
@@ -34,7 +37,7 @@ get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
3437
if (old != NULL) {
3538
size = old->size;
3639
capacity = old->capacity;
37-
assert(size < 256);
40+
assert(size < MAX_EXECUTORS_SIZE);
3841
}
3942
assert(size <= capacity);
4043
if (size == capacity) {
@@ -74,7 +77,7 @@ insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorO
7477
executor->vm_data.opcode = instr->op.code;
7578
executor->vm_data.oparg = instr->op.arg;
7679
code->co_executors->executors[index] = executor;
77-
assert(index < 256);
80+
assert(index < MAX_EXECUTORS_SIZE);
7881
instr->op.code = ENTER_EXECUTOR;
7982
instr->op.arg = index;
8083
code->co_executors->size++;
@@ -307,7 +310,7 @@ uop_dealloc(_PyUOpExecutorObject *self) {
307310

308311
static const char *
309312
uop_name(int index) {
310-
if (index < 256) {
313+
if (index <= MAX_REAL_OPCODE) {
311314
return _PyOpcode_OpName[index];
312315
}
313316
return _PyOpcode_uop_name[index];
@@ -391,17 +394,20 @@ translate_bytecode_to_trace(
391394
#define ADD_TO_TRACE(OPCODE, OPERAND) \
392395
DPRINTF(2, \
393396
" ADD_TO_TRACE(%s, %" PRIu64 ")\n", \
394-
(OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)], \
397+
uop_name(OPCODE), \
395398
(uint64_t)(OPERAND)); \
396399
assert(trace_length < max_length); \
397400
trace[trace_length].opcode = (OPCODE); \
398401
trace[trace_length].operand = (OPERAND); \
399402
trace_length++;
400403

404+
#define INSTR_IP(INSTR, CODE) \
405+
((long)((INSTR) - ((_Py_CODEUNIT *)(CODE)->co_code_adaptive)))
406+
401407
#define ADD_TO_STUB(INDEX, OPCODE, OPERAND) \
402408
DPRINTF(2, " ADD_TO_STUB(%d, %s, %" PRIu64 ")\n", \
403409
(INDEX), \
404-
(OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)], \
410+
uop_name(OPCODE), \
405411
(uint64_t)(OPERAND)); \
406412
trace[(INDEX)].opcode = (OPCODE); \
407413
trace[(INDEX)].operand = (OPERAND);
@@ -411,10 +417,10 @@ translate_bytecode_to_trace(
411417
PyUnicode_AsUTF8(code->co_qualname),
412418
PyUnicode_AsUTF8(code->co_filename),
413419
code->co_firstlineno,
414-
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive));
420+
2 * INSTR_IP(initial_instr, code));
415421

416422
for (;;) {
417-
ADD_TO_TRACE(SAVE_IP, instr - (_Py_CODEUNIT *)code->co_code_adaptive);
423+
ADD_TO_TRACE(SAVE_IP, INSTR_IP(instr, code));
418424
int opcode = instr->op.code;
419425
int oparg = instr->op.arg;
420426
int extras = 0;
@@ -448,8 +454,7 @@ translate_bytecode_to_trace(
448454
int uopcode = opcode == POP_JUMP_IF_TRUE ?
449455
_POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE;
450456
ADD_TO_TRACE(uopcode, max_length);
451-
ADD_TO_STUB(max_length, SAVE_IP,
452-
target_instr - (_Py_CODEUNIT *)code->co_code_adaptive);
457+
ADD_TO_STUB(max_length, SAVE_IP, INSTR_IP(target_instr, code));
453458
ADD_TO_STUB(max_length + 1, EXIT_TRACE, 0);
454459
break;
455460
}
@@ -474,9 +479,7 @@ translate_bytecode_to_trace(
474479
// Reserve space for nuops (+ SAVE_IP + EXIT_TRACE)
475480
int nuops = expansion->nuops;
476481
if (trace_length + nuops + 2 > max_length) {
477-
DPRINTF(1,
478-
"Ran out of space for %s\n",
479-
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode]);
482+
DPRINTF(1, "Ran out of space for %s\n", uop_name(opcode));
480483
goto done;
481484
}
482485
for (int i = 0; i < nuops; i++) {
@@ -522,9 +525,7 @@ translate_bytecode_to_trace(
522525
}
523526
break;
524527
}
525-
DPRINTF(2,
526-
"Unsupported opcode %s\n",
527-
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode]);
528+
DPRINTF(2, "Unsupported opcode %s\n", uop_name(opcode));
528529
goto done; // Break out of loop
529530
}
530531
}
@@ -542,7 +543,7 @@ translate_bytecode_to_trace(
542543
PyUnicode_AsUTF8(code->co_qualname),
543544
PyUnicode_AsUTF8(code->co_filename),
544545
code->co_firstlineno,
545-
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive),
546+
2 * INSTR_IP(initial_instr, code),
546547
trace_length);
547548
if (max_length < buffer_size && trace_length < max_length) {
548549
// Move the stubs back to be immediately after the main trace
@@ -576,7 +577,7 @@ translate_bytecode_to_trace(
576577
PyUnicode_AsUTF8(code->co_qualname),
577578
PyUnicode_AsUTF8(code->co_filename),
578579
code->co_firstlineno,
579-
2 * (long)(initial_instr - (_Py_CODEUNIT *)code->co_code_adaptive));
580+
2 * INSTR_IP(initial_instr, code));
580581
}
581582
return 0;
582583

0 commit comments

Comments
 (0)