Skip to content

gh-141504: Factor out tracing and optimization heuristics into a single object - #143381

Merged
corona10 merged 15 commits into
python:mainfrom
corona10:gh-141504
Jan 3, 2026
Merged

corona10 merged 15 commits into
python:mainfrom
corona10:gh-141504

Conversation

@corona10

@corona10 corona10 commented Jan 3, 2026 •

Copy link
Copy Markdown
Member
@corona10 corona10 changed the title gh-141504: Factor out tracing and optimization heuristics into a single object Jan 3, 2026
@corona10
corona10 marked this pull request as draft January 3, 2026 07:23
Comment thread Python/pystate.c Outdated
_tstate->jit_metrics.side_exit_initial_value = SIDE_EXIT_INITIAL_VALUE;
_tstate->jit_metrics.side_exit_initial_backoff = SIDE_EXIT_INITIAL_BACKOFF;

char *env = Py_GETENV("PYTHON_JIT_JUMP_BACKWARD_INITIAL_VALUE");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We may need to extract utility function to handle those things.

@corona10 corona10 changed the title [WIP] gh-141504: Factor out tracing and optimization heuristics into a single object Jan 3, 2026
@corona10
corona10 marked this pull request as ready for review January 3, 2026 07:59
Comment thread Include/internal/pycore_tstate.h Outdated
Comment on lines +56 to +62
typedef struct _PyJitMetrics {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitMetrics;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you rename this to _PyJitPolicy pleases?

Also please then wrap this into an overall struct called _PyPolicies. The reason is that we might want to add more structs later, like for the interpreter.

So it should be

Suggested change
typedef struct _PyJitMetrics {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitMetrics;
typedef struct _PyJitPolicy {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitPolicy;
typedef struct _PyPolicy {
_PyJitPolicy jit;
} _PyPolicy;
Comment thread Python/specialize.c Outdated
PyThreadState *tstate = _PyThreadState_GET();
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
jump_counter = initial_jump_backoff_counter(
tstate_impl->policy.jit.jump_backward_initial_value,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@Fidget-Spinner
The crash is directly related to this line.
But ENABLE_SPECIALIZATION_FT does not mean TIER2 is enabled.

Do we need to move jump_backward_initial_value and jump_backward_initial_backoff to policy.interpreter?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

JUMP_BACKWARD_INITIAL_VALUE is only used by the JIT. On normal build/FT build, the CACHE of JUMP_BACKWARD is unused. So we can just keep it for the JIT.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm I realised the CI is now failing because FT doesn't build with JIT.

In that case, yes please move this to an interpreter policy.

Comment thread Include/internal/pycore_tstate.h Outdated
#endif
#if _Py_TIER2
_PyJitTracerState jit_tracer_state;
_PyPolicy policy;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs to be moved out of the ifdef. CI is failing.

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some comments to cleanup the code. After this I will approve. Thanks!

Comment thread Include/internal/pycore_backoff.h Outdated
#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
Copy Markdown
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.

Comment thread Include/internal/pycore_backoff.h Outdated

static inline _Py_BackoffCounter
initial_temperature_backoff_counter(void)
initial_temperature_backoff_counter(uint16_t initial_value, uint16_t initial_backoff)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here.

Comment thread Python/pystate.c Outdated
}

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

Copy link
Copy Markdown
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

Comment thread Python/pystate.c Outdated
_tstate->asyncio_running_task = NULL;

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

Copy link
Copy Markdown
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

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great. Thanks!

@corona10
corona10 enabled auto-merge (squash) January 3, 2026 14:56
@corona10
corona10 disabled auto-merge January 3, 2026 14:58
@corona10
corona10 enabled auto-merge (squash) January 3, 2026 14:58
@corona10
corona10 merged commit 12283f6 into python:main Jan 3, 2026
@Fidget-Spinner

Copy link
Copy Markdown
Member

Note: the names are a little lengthy. But considering this is an internal CPython only env var used for testing, we can determine proper names later when/if we decide to expose them.

@markshannon

markshannon commented Jan 5, 2026 •

Copy link
Copy Markdown
Member

I have some questions.

Why set thresholds per thread, rather than per interpreter. Why would we want per-thread values?
Why set and store the threshold and backoff separately rather than as a single backoff counter?

I've added a new comment on the issue, that will hopefully clarify what I was after.

thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants