Skip to content

Commit

Permalink
Consolidate _PyCode_{Quicken,DisableSpecialization} into _PyCode_Init…
Browse files Browse the repository at this point in the history
…Counters
  • Loading branch information
mpage committed Oct 17, 2024
1 parent 4580e3c commit b992f44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
16 changes: 6 additions & 10 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
return 0;
}

extern void _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size);
extern void
_PyCode_InitCounters(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable);

#ifdef Py_GIL_DISABLED
extern void _PyCode_DisableSpecialization(_Py_CODEUNIT *instructions, Py_ssize_t size);
static _PyCodeArray * _PyCodeArray_New(Py_ssize_t size);
#endif

Expand Down Expand Up @@ -536,14 +536,10 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
}
co->_co_firsttraceable = entry_point;
#ifdef Py_GIL_DISABLED
if (interp->config.tlbc_enabled) {
_PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co));
}
else {
_PyCode_DisableSpecialization(_PyCode_CODE(co), Py_SIZE(co));
}
_PyCode_InitCounters(_PyCode_CODE(co), Py_SIZE(co),
interp->config.tlbc_enabled);
#else
_PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co));
_PyCode_InitCounters(_PyCode_CODE(co), Py_SIZE(co), 1);
#endif
notify_code_watchers(PY_CODE_EVENT_CREATE, co);
return 0;
Expand Down Expand Up @@ -2754,7 +2750,7 @@ copy_code(_Py_CODEUNIT *dst, PyCodeObject *co)
for (int i = 0; i < code_len; i += _PyInstruction_GetLength(co, i)) {
dst[i] = _Py_GetBaseCodeUnit(co, i);
}
_PyCode_Quicken(dst, code_len);
_PyCode_InitCounters(dst, code_len, 1);
}

static Py_ssize_t
Expand Down
36 changes: 13 additions & 23 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,20 @@ do { \
# define SPECIALIZATION_FAIL(opcode, kind) ((void)0)
#endif

// Initialize warmup counters and insert superinstructions. This cannot fail.
// Initialize warmup counters. This cannot fail.
void
_PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size)
_PyCode_InitCounters(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable)
{
#if ENABLE_SPECIALIZATION_FT
_Py_BackoffCounter jump_counter, adaptive_counter;
if (enable) {
jump_counter = initial_jump_backoff_counter();
adaptive_counter = adaptive_counter_warmup();
}
else {
jump_counter = initial_unreachable_backoff_counter();
adaptive_counter = initial_unreachable_backoff_counter();
}
int opcode = 0;
/* The last code unit cannot have a cache, so we don't need to check it */
for (Py_ssize_t i = 0; i < size-1; i++) {
Expand All @@ -469,7 +478,7 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size)
// The initial value depends on the opcode
switch (opcode) {
case JUMP_BACKWARD:
instructions[i + 1].counter = initial_jump_backoff_counter();
instructions[i + 1].counter = jump_counter;
break;
case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
Expand All @@ -478,7 +487,7 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size)
instructions[i + 1].cache = 0x5555; // Alternating 0, 1 bits
break;
default:
instructions[i + 1].counter = adaptive_counter_warmup();
instructions[i + 1].counter = adaptive_counter;
break;
}
i += caches;
Expand All @@ -487,25 +496,6 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size)
#endif /* ENABLE_SPECIALIZATION_FT */
}

#ifdef Py_GIL_DISABLED

void
_PyCode_DisableSpecialization(_Py_CODEUNIT *instructions, Py_ssize_t size)
{
/* The last code unit cannot have a cache, so we don't need to check it */
for (Py_ssize_t i = 0; i < size - 1; i++) {
int opcode = instructions[i].op.code;
int caches = _PyOpcode_Caches[opcode];
if (caches) {
instructions[i + 1].counter =
initial_unreachable_backoff_counter();
i += caches;
}
}
}

#endif

#define SIMPLE_FUNCTION 0

/* Common */
Expand Down

0 comments on commit b992f44

Please sign in to comment.