Skip to content

Commit 1173143

Browse files
authored
gh-104584: Emit macro expansions to opcode_metadata.h (#106163)
This produces longer traces (superblocks?). Also improved debug output (uop names are now printed instead of numeric opcodes). This would be simpler if the numeric opcode values were generated by generate_cases.py, but that's another project. Refactored some code in generate_cases.py so the essential algorithm for cache effects is only run once. (Deciding which effects are used and what the total cache size is, regardless of what's used.)
1 parent c283a0c commit 1173143

File tree

4 files changed

+153
-78
lines changed

4 files changed

+153
-78
lines changed

Python/ceval.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2817,10 +2817,10 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
28172817
oparg = (int)operand;
28182818
#ifdef LLTRACE
28192819
if (lltrace >= 3) {
2820-
const char *opname = opcode < 256 ? _PyOpcode_OpName[opcode] : "";
2820+
const char *opname = opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode];
28212821
int stack_level = (int)(stack_pointer - _PyFrame_Stackbase(frame));
2822-
fprintf(stderr, " uop %s %d, operand %" PRIu64 ", stack_level %d\n",
2823-
opname, opcode, operand, stack_level);
2822+
fprintf(stderr, " uop %s, operand %" PRIu64 ", stack_level %d\n",
2823+
opname, operand, stack_level);
28242824
}
28252825
#endif
28262826
pc++;

Python/opcode_metadata.h

+32
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ struct opcode_macro_expansion {
913913
#ifndef NEED_OPCODE_METADATA
914914
extern const struct opcode_metadata _PyOpcode_opcode_metadata[512];
915915
extern const struct opcode_macro_expansion _PyOpcode_macro_expansion[256];
916+
#ifdef Py_DEBUG
917+
extern const char * const _PyOpcode_uop_name[512];
918+
#endif
916919
#else
917920
const struct opcode_metadata _PyOpcode_opcode_metadata[512] = {
918921
[NOP] = { true, INSTR_FMT_IX, 0 },
@@ -1131,10 +1134,18 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[256] = {
11311134
[STORE_FAST] = { .nuops = 1, .uops = { { STORE_FAST, 0, 0 } } },
11321135
[POP_TOP] = { .nuops = 1, .uops = { { POP_TOP, 0, 0 } } },
11331136
[PUSH_NULL] = { .nuops = 1, .uops = { { PUSH_NULL, 0, 0 } } },
1137+
[END_FOR] = { .nuops = 2, .uops = { { POP_TOP, 0, 0 }, { POP_TOP, 0, 0 } } },
11341138
[END_SEND] = { .nuops = 1, .uops = { { END_SEND, 0, 0 } } },
11351139
[UNARY_NEGATIVE] = { .nuops = 1, .uops = { { UNARY_NEGATIVE, 0, 0 } } },
11361140
[UNARY_NOT] = { .nuops = 1, .uops = { { UNARY_NOT, 0, 0 } } },
11371141
[UNARY_INVERT] = { .nuops = 1, .uops = { { UNARY_INVERT, 0, 0 } } },
1142+
[BINARY_OP_MULTIPLY_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_MULTIPLY_INT, 0, 0 } } },
1143+
[BINARY_OP_ADD_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_ADD_INT, 0, 0 } } },
1144+
[BINARY_OP_SUBTRACT_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_SUBTRACT_INT, 0, 0 } } },
1145+
[BINARY_OP_MULTIPLY_FLOAT] = { .nuops = 2, .uops = { { _GUARD_BOTH_FLOAT, 0, 0 }, { _BINARY_OP_MULTIPLY_FLOAT, 0, 0 } } },
1146+
[BINARY_OP_ADD_FLOAT] = { .nuops = 2, .uops = { { _GUARD_BOTH_FLOAT, 0, 0 }, { _BINARY_OP_ADD_FLOAT, 0, 0 } } },
1147+
[BINARY_OP_SUBTRACT_FLOAT] = { .nuops = 2, .uops = { { _GUARD_BOTH_FLOAT, 0, 0 }, { _BINARY_OP_SUBTRACT_FLOAT, 0, 0 } } },
1148+
[BINARY_OP_ADD_UNICODE] = { .nuops = 2, .uops = { { _GUARD_BOTH_UNICODE, 0, 0 }, { _BINARY_OP_ADD_UNICODE, 0, 0 } } },
11381149
[BINARY_SLICE] = { .nuops = 1, .uops = { { BINARY_SLICE, 0, 0 } } },
11391150
[STORE_SLICE] = { .nuops = 1, .uops = { { STORE_SLICE, 0, 0 } } },
11401151
[BINARY_SUBSCR_LIST_INT] = { .nuops = 1, .uops = { { BINARY_SUBSCR_LIST_INT, 0, 0 } } },
@@ -1162,6 +1173,9 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[256] = {
11621173
[DELETE_ATTR] = { .nuops = 1, .uops = { { DELETE_ATTR, 0, 0 } } },
11631174
[STORE_GLOBAL] = { .nuops = 1, .uops = { { STORE_GLOBAL, 0, 0 } } },
11641175
[DELETE_GLOBAL] = { .nuops = 1, .uops = { { DELETE_GLOBAL, 0, 0 } } },
1176+
[LOAD_LOCALS] = { .nuops = 1, .uops = { { _LOAD_LOCALS, 0, 0 } } },
1177+
[LOAD_NAME] = { .nuops = 2, .uops = { { _LOAD_LOCALS, 0, 0 }, { _LOAD_FROM_DICT_OR_GLOBALS, 0, 0 } } },
1178+
[LOAD_FROM_DICT_OR_GLOBALS] = { .nuops = 1, .uops = { { _LOAD_FROM_DICT_OR_GLOBALS, 0, 0 } } },
11651179
[DELETE_DEREF] = { .nuops = 1, .uops = { { DELETE_DEREF, 0, 0 } } },
11661180
[LOAD_FROM_DICT_OR_DEREF] = { .nuops = 1, .uops = { { LOAD_FROM_DICT_OR_DEREF, 0, 0 } } },
11671181
[LOAD_DEREF] = { .nuops = 1, .uops = { { LOAD_DEREF, 0, 0 } } },
@@ -1207,4 +1221,22 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[256] = {
12071221
[COPY] = { .nuops = 1, .uops = { { COPY, 0, 0 } } },
12081222
[SWAP] = { .nuops = 1, .uops = { { SWAP, 0, 0 } } },
12091223
};
1224+
#ifdef Py_DEBUG
1225+
const char * const _PyOpcode_uop_name[512] = {
1226+
[300] = "EXIT_TRACE",
1227+
[301] = "SET_IP",
1228+
[302] = "_GUARD_BOTH_INT",
1229+
[303] = "_BINARY_OP_MULTIPLY_INT",
1230+
[304] = "_BINARY_OP_ADD_INT",
1231+
[305] = "_BINARY_OP_SUBTRACT_INT",
1232+
[306] = "_GUARD_BOTH_FLOAT",
1233+
[307] = "_BINARY_OP_MULTIPLY_FLOAT",
1234+
[308] = "_BINARY_OP_ADD_FLOAT",
1235+
[309] = "_BINARY_OP_SUBTRACT_FLOAT",
1236+
[310] = "_GUARD_BOTH_UNICODE",
1237+
[311] = "_BINARY_OP_ADD_UNICODE",
1238+
[312] = "_LOAD_LOCALS",
1239+
[313] = "_LOAD_FROM_DICT_OR_GLOBALS",
1240+
};
1241+
#endif
12101242
#endif

Python/optimizer.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ translate_bytecode_to_trace(
325325
}
326326
#define ADD_TO_TRACE(OPCODE, OPERAND) \
327327
if (lltrace >= 2) { \
328-
const char *opname = (OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : ""; \
329-
fprintf(stderr, " ADD_TO_TRACE(%s %d, %" PRIu64 ")\n", opname, (OPCODE), (uint64_t)(OPERAND)); \
328+
const char *opname = (OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)]; \
329+
fprintf(stderr, " ADD_TO_TRACE(%s, %" PRIu64 ")\n", opname, (uint64_t)(OPERAND)); \
330330
} \
331331
trace[trace_length].opcode = (OPCODE); \
332332
trace[trace_length].operand = (OPERAND); \
@@ -474,6 +474,8 @@ PyUnstable_Optimizer_NewUOpOptimizer(void)
474474
}
475475
opt->optimize = uop_optimize;
476476
opt->resume_threshold = UINT16_MAX;
477-
opt->backedge_threshold = 0;
477+
// Need at least 3 iterations to settle specializations.
478+
// A few lower bits of the counter are reserved for other flags.
479+
opt->backedge_threshold = 3 << OPTIMIZER_BITS_IN_COUNTER;
478480
return (PyObject *)opt;
479481
}

0 commit comments

Comments
 (0)