Skip to content

Commit

Permalink
pythongh-111848: Clean up RESERVE() macro (python#112274)
Browse files Browse the repository at this point in the history
Also avoid compiler warnings about unused 'reserved' variable.
  • Loading branch information
gvanrossum authored and Glyphack committed Jan 27, 2024
1 parent e235eb1 commit 2384007
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ translate_bytecode_to_trace(
_Py_CODEUNIT *initial_instr = instr;
int trace_length = 0;
int max_length = buffer_size;
int reserved = 0;
struct {
PyCodeObject *code;
_Py_CODEUNIT *instr;
Expand Down Expand Up @@ -456,8 +455,6 @@ translate_bytecode_to_trace(
(OPARG), \
(uint64_t)(OPERAND)); \
assert(trace_length < max_length); \
assert(reserved > 0); \
reserved--; \
trace[trace_length].opcode = (OPCODE); \
trace[trace_length].oparg = (OPARG); \
trace[trace_length].operand = (OPERAND); \
Expand All @@ -474,11 +471,10 @@ translate_bytecode_to_trace(
(opname), (n), max_length - trace_length); \
OPT_STAT_INC(trace_too_long); \
goto done; \
} \
reserved = (n); // Keep ADD_TO_TRACE honest
}

// Reserve space for main+stub uops, plus 3 for _SET_IP, _CHECK_VALIDITY and _EXIT_TRACE
#define RESERVE(main, stub) RESERVE_RAW((main) + (stub) + 3, _PyUopName(opcode))
// Reserve space for N uops, plus 3 for _SET_IP, _CHECK_VALIDITY and _EXIT_TRACE
#define RESERVE(needed) RESERVE_RAW((needed) + 3, _PyUopName(opcode))

// Trace stack operations (used by _PUSH_FRAME, _POP_FRAME)
#define TRACE_STACK_PUSH() \
Expand Down Expand Up @@ -543,7 +539,7 @@ translate_bytecode_to_trace(
case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
{
RESERVE(1, 0);
RESERVE(1);
int counter = instr[1].cache;
int bitcount = _Py_popcount32(counter);
int jump_likely = bitcount > 8;
Expand All @@ -566,7 +562,7 @@ translate_bytecode_to_trace(
case JUMP_BACKWARD:
{
if (instr + 2 - oparg == initial_instr && code == initial_code) {
RESERVE(1, 0);
RESERVE(1);
ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0, 0);
}
else {
Expand All @@ -578,7 +574,7 @@ translate_bytecode_to_trace(

case JUMP_FORWARD:
{
RESERVE(0, 0);
RESERVE(0);
// This will emit two _SET_IP instructions; leave it to the optimizer
instr += oparg;
break;
Expand All @@ -590,7 +586,7 @@ translate_bytecode_to_trace(
if (expansion->nuops > 0) {
// Reserve space for nuops (+ _SET_IP + _EXIT_TRACE)
int nuops = expansion->nuops;
RESERVE(nuops, 0);
RESERVE(nuops);
if (expansion->uops[nuops-1].uop == _POP_FRAME) {
// Check for trace stack underflow now:
// We can't bail e.g. in the middle of
Expand Down Expand Up @@ -737,13 +733,12 @@ translate_bytecode_to_trace(
if (trace_length > 4) {
ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);
DPRINTF(1,
"Created a trace for %s (%s:%d) at byte offset %d -- length %d+%d\n",
"Created a trace for %s (%s:%d) at byte offset %d -- length %d\n",
PyUnicode_AsUTF8(code->co_qualname),
PyUnicode_AsUTF8(code->co_filename),
code->co_firstlineno,
2 * INSTR_IP(initial_instr, code),
trace_length,
buffer_size - max_length);
trace_length);
OPT_HIST(trace_length + buffer_size - max_length, trace_length_hist);
return 1;
}
Expand Down

0 comments on commit 2384007

Please sign in to comment.