Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Include/internal/pycore_backoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ trigger_backoff_counter(void)
#define JUMP_BACKWARD_INITIAL_VALUE 4000
#define JUMP_BACKWARD_INITIAL_BACKOFF 6
static inline _Py_BackoffCounter
initial_jump_backoff_counter(void)
initial_jump_backoff_counter(uint16_t initial_value, uint16_t initial_backoff)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should take a single value _tstate/tstate and grab the jump backward values from it.

{
return make_backoff_counter(JUMP_BACKWARD_INITIAL_VALUE,
JUMP_BACKWARD_INITIAL_BACKOFF);
return make_backoff_counter(initial_value, initial_backoff);
}

/* Initial exit temperature.
Expand All @@ -141,10 +140,9 @@ initial_jump_backoff_counter(void)
#define SIDE_EXIT_INITIAL_BACKOFF 6

static inline _Py_BackoffCounter
initial_temperature_backoff_counter(void)
initial_temperature_backoff_counter(uint16_t initial_value, uint16_t initial_backoff)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

{
return make_backoff_counter(SIDE_EXIT_INITIAL_VALUE,
SIDE_EXIT_INITIAL_BACKOFF);
return make_backoff_counter(initial_value, initial_backoff);
}

/* Unreachable backoff counter. */
Expand Down
21 changes: 21 additions & 0 deletions Include/internal/pycore_tstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,27 @@ typedef struct _PyJitTracerState {
_PyJitTracerInitialState initial_state;
_PyJitTracerPreviousState prev_state;
} _PyJitTracerState;

typedef struct _PyJitPolicy {
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitPolicy;

#endif

typedef struct _PyInterpreterPolicy {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
} _PyInterpreterPolicy;


typedef struct _PyPolicy {

#ifdef _Py_TIER2
_PyJitPolicy jit;
#endif
_PyInterpreterPolicy interp;
} _PyPolicy;

// Every PyThreadState is actually allocated as a _PyThreadStateImpl. The
// PyThreadState fields are exposed as part of the C API, although most fields
Expand Down Expand Up @@ -132,6 +152,7 @@ typedef struct _PyThreadStateImpl {
#if _Py_TIER2
_PyJitTracerState jit_tracer_state;
#endif
_PyPolicy policy;
} _PyThreadStateImpl;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Factor out tracing and optimization heuristics into a single object.
Patch by Donghee Na.
8 changes: 6 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,9 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
_tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter = restart_backoff_counter(counter);
}
else {
_tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter();
_tstate->jit_tracer_state.initial_state.jump_backward_instr[1].counter = initial_jump_backoff_counter(
_tstate->policy.interp.jump_backward_initial_value,
_tstate->policy.interp.jump_backward_initial_backoff);
}
}
else {
Expand All @@ -1483,7 +1485,9 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
exit->temperature = restart_backoff_counter(exit->temperature);
}
else {
exit->temperature = initial_temperature_backoff_counter();
exit->temperature = initial_temperature_backoff_counter(
_tstate->policy.jit.side_exit_initial_value,
_tstate->policy.jit.side_exit_initial_backoff);
}
}
_PyJit_FinalizeTracing(tstate);
Expand Down
11 changes: 7 additions & 4 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorO
}

static _PyExecutorObject *
make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies, int chain_depth);
make_executor_from_uops(_PyThreadStateImpl *tstate, _PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies);

static int
uop_optimize(_PyInterpreterFrame *frame, PyThreadState *tstate,
Expand Down Expand Up @@ -1328,7 +1328,7 @@ sanity_check(_PyExecutorObject *executor)
* and not a NOP.
*/
static _PyExecutorObject *
make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies, int chain_depth)
make_executor_from_uops(_PyThreadStateImpl *tstate, _PyUOpInstruction *buffer, int length, const _PyBloomFilter *dependencies)
{
int exit_count = count_exits(buffer, length);
_PyExecutorObject *executor = allocate_executor(exit_count, length);
Expand All @@ -1337,12 +1337,15 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil
}

/* Initialize exits */
int chain_depth = tstate->jit_tracer_state.initial_state.chain_depth;
_PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
_PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor();
cold->vm_data.chain_depth = chain_depth;
for (int i = 0; i < exit_count; i++) {
executor->exits[i].index = i;
executor->exits[i].temperature = initial_temperature_backoff_counter();
executor->exits[i].temperature = initial_temperature_backoff_counter(
tstate->policy.jit.side_exit_initial_value,
tstate->policy.jit.side_exit_initial_backoff);
}
int next_exit = exit_count-1;
_PyUOpInstruction *dest = (_PyUOpInstruction *)&executor->trace[length];
Expand Down Expand Up @@ -1510,7 +1513,7 @@ uop_optimize(
length = prepare_for_execution(buffer, length);
assert(length <= UOP_MAX_TRACE_LENGTH);
_PyExecutorObject *executor = make_executor_from_uops(
buffer, length, dependencies, _tstate->jit_tracer_state.initial_state.chain_depth);
_tstate, buffer, length, dependencies);
if (executor == NULL) {
return -1;
}
Expand Down
30 changes: 29 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_audit.h" // _Py_AuditHookEntry
#include "pycore_backoff.h" // JUMP_BACKWARD_INITIAL_VALUE, SIDE_EXIT_INITIAL_VALUE
#include "pycore_ceval.h" // _PyEval_AcquireLock()
#include "pycore_codecs.h" // _PyCodec_Fini()
#include "pycore_critical_section.h" // _PyCriticalSection_Resume()
Expand Down Expand Up @@ -1438,6 +1439,20 @@ decref_threadstate(_PyThreadStateImpl *tstate)
}
}

static inline void
init_jit_metric(uint16_t *target, const char *env_name, uint16_t default_value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to init_policy

long min_value, long max_value)
{
*target = default_value;
char *env = Py_GETENV(env_name);
if (env && *env != '\0') {
long value = atol(env);
if (value >= min_value && value <= max_value) {
*target = (uint16_t)value;
}
}
}

/* Get the thread state to a minimal consistent state.
Further init happens in pylifecycle.c before it can be used.
All fields not initialized here are expected to be zeroed out,
Expand Down Expand Up @@ -1523,8 +1538,21 @@ init_threadstate(_PyThreadStateImpl *_tstate,

_tstate->asyncio_running_loop = NULL;
_tstate->asyncio_running_task = NULL;

// Initialize interpreter policy from environment variables
init_jit_metric(&_tstate->policy.interp.jump_backward_initial_value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename here as well

"PYTHON_JIT_JUMP_BACKWARD_INITIAL_VALUE",
JUMP_BACKWARD_INITIAL_VALUE, 1, MAX_VALUE);
init_jit_metric(&_tstate->policy.interp.jump_backward_initial_backoff,
"PYTHON_JIT_JUMP_BACKWARD_INITIAL_BACKOFF",
JUMP_BACKWARD_INITIAL_BACKOFF, 0, MAX_BACKOFF);
#ifdef _Py_TIER2
// Initialize JIT policy from environment variables
init_jit_metric(&_tstate->policy.jit.side_exit_initial_value,
"PYTHON_JIT_SIDE_EXIT_INITIAL_VALUE",
SIDE_EXIT_INITIAL_VALUE, 1, MAX_VALUE);
init_jit_metric(&_tstate->policy.jit.side_exit_initial_backoff,
"PYTHON_JIT_SIDE_EXIT_INITIAL_BACKOFF",
SIDE_EXIT_INITIAL_BACKOFF, 0, MAX_BACKOFF);
_tstate->jit_tracer_state.code_buffer = NULL;
#endif
tstate->delete_later = NULL;
Expand Down
6 changes: 5 additions & 1 deletion Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters
#if ENABLE_SPECIALIZATION_FT
_Py_BackoffCounter jump_counter, adaptive_counter;
if (enable_counters) {
jump_counter = initial_jump_backoff_counter();
PyThreadState *tstate = _PyThreadState_GET();
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
jump_counter = initial_jump_backoff_counter(
tstate_impl->policy.interp.jump_backward_initial_value,
tstate_impl->policy.interp.jump_backward_initial_backoff);
adaptive_counter = adaptive_counter_warmup();
}
else {
Expand Down
Loading