Skip to content

Commit 608772f

Browse files
refactor a little
1 parent e3f18e6 commit 608772f

File tree

8 files changed

+86
-84
lines changed

8 files changed

+86
-84
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,21 @@ struct _Py_unique_id_pool {
762762

763763
typedef _Py_CODEUNIT *(*_PyJitEntryFuncPtr)(struct _PyExecutorObject *exec, _PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate);
764764

765+
typedef struct _PyJitState {
766+
int jit_tracer_code_max_size;
767+
int jit_tracer_code_curr_size;
768+
_PyBloomFilter jit_tracer_dependencies;
769+
bool jit_tracer_dependencies_still_valid;
770+
_PyUOpInstruction *jit_tracer_code_buffer;
771+
_Py_CODEUNIT *jit_tracer_initial_instr;
772+
int jit_tracer_initial_stack_depth;
773+
int jit_tracer_initial_chain_depth;
774+
PyCodeObject *jit_tracer_initial_code; // Strong
775+
PyFunctionObject *jit_tracer_initial_func; // Strong
776+
struct _PyExitData *jit_tracer_previous_exit;
777+
_PyInterpreterFrame *jit_tracer_current_frame;
778+
} _PyJitState;
779+
765780
/* PyInterpreterState holds the global state for one of the runtime's
766781
interpreters. Typically the initial (main) interpreter is the only one.
767782
@@ -943,20 +958,7 @@ struct _is {
943958
struct types_state types;
944959
struct callable_cache callable_cache;
945960
PyObject *common_consts[NUM_COMMON_CONSTANTS];
946-
// JIT tracing state
947-
int jit_tracer_code_max_size;
948-
int jit_tracer_code_curr_size;
949-
_PyBloomFilter jit_tracer_dependencies;
950-
bool jit_tracer_dependencies_still_valid;
951-
_PyUOpInstruction *jit_tracer_code_buffer;
952-
_Py_CODEUNIT *jit_tracer_initial_instr;
953-
int jit_tracer_initial_stack_depth;
954-
int jit_tracer_initial_chain_depth;
955-
PyCodeObject *jit_tracer_initial_code; // Strong
956-
PyFunctionObject *jit_tracer_initial_func; // Strong
957-
struct _PyExitData *jit_tracer_previous_exit;
958-
_PyInterpreterFrame *jit_tracer_current_frame;
959-
// End Jit tracing state
961+
_PyJitState jit_state;
960962
bool jit;
961963
bool compiling;
962964
struct _PyExecutorObject *executor_list_head;

Python/bytecodes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,9 +2964,9 @@ dummy_func(
29642964
if (!IS_JIT_TRACING() && backoff_counter_triggers(counter) &&
29652965
this_instr->op.code == JUMP_BACKWARD_JIT &&
29662966
next_instr->op.code != ENTER_EXECUTOR) {
2967-
if (tstate->interp->jit_tracer_code_buffer == NULL) {
2968-
tstate->interp->jit_tracer_code_buffer = (_PyUOpInstruction *)_PyObject_VirtualAlloc(UOP_BUFFER_SIZE);
2969-
if (tstate->interp->jit_tracer_code_buffer == NULL) {
2967+
if (tstate->interp->jit_state.jit_tracer_code_buffer == NULL) {
2968+
tstate->interp->jit_state.jit_tracer_code_buffer = (_PyUOpInstruction *)_PyObject_VirtualAlloc(UOP_BUFFER_SIZE);
2969+
if (tstate->interp->jit_state.jit_tracer_code_buffer == NULL) {
29702970
// Don't error, just go to next instruction.
29712971
DISPATCH();
29722972
}

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ static int
978978
add_to_code_trace(PyThreadState *tstate, _PyInterpreterFrame *frame, PyCodeObject *old_code, PyFunctionObject *old_func, _Py_CODEUNIT *this_instr, _Py_CODEUNIT *next_instr, int opcode, int oparg, int jump_taken)
979979
{
980980
assert(frame != NULL);
981-
assert(tstate->interp->jit_tracer_code_curr_size < UOP_MAX_TRACE_LENGTH);
981+
assert(tstate->interp->jit_state.jit_tracer_code_curr_size < UOP_MAX_TRACE_LENGTH);
982982
return !_PyJIT_translate_single_bytecode_to_trace(tstate, frame, this_instr, next_instr, old_code, old_func, opcode, oparg, jump_taken);
983983
}
984984

Python/ceval_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ do { \
434434
} \
435435
if (keep_tracing_bit) { \
436436
assert(next_instr->op.code != ENTER_EXECUTOR); \
437-
assert(tstate->interp->jit_tracer_code_curr_size == 2); \
437+
assert(tstate->interp->jit_state.jit_tracer_code_curr_size == 2); \
438438
ENTER_TRACING(); \
439439
} \
440440
DISPATCH(); \

Python/generated_cases.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_tracer_cases.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@ _PyOptimizer_Optimize(
118118
_PyInterpreterFrame *frame, PyThreadState *tstate)
119119
{
120120
PyInterpreterState *interp = _PyInterpreterState_GET();
121-
int chain_depth = tstate->interp->jit_tracer_initial_chain_depth;
121+
int chain_depth = tstate->interp->jit_state.jit_tracer_initial_chain_depth;
122122
assert(interp->jit);
123123
assert(!interp->compiling);
124-
assert(tstate->interp->jit_tracer_initial_stack_depth >= 0);
124+
assert(tstate->interp->jit_state.jit_tracer_initial_stack_depth >= 0);
125125
interp->compiling = true;
126126
// The first executor in a chain and the MAX_CHAIN_DEPTH'th executor *must*
127127
// make progress in order to avoid infinite loops or excessively-long
128128
// side-exit chains. We can only insert the executor into the bytecode if
129129
// this is true, since a deopt won't infinitely re-enter the executor:
130130
chain_depth %= MAX_CHAIN_DEPTH;
131131
bool progress_needed = chain_depth == 0;
132-
PyCodeObject *code = (PyCodeObject *)tstate->interp->jit_tracer_initial_code;
133-
_Py_CODEUNIT *start = tstate->interp->jit_tracer_initial_instr;
132+
PyCodeObject *code = (PyCodeObject *)tstate->interp->jit_state.jit_tracer_initial_code;
133+
_Py_CODEUNIT *start = tstate->interp->jit_state.jit_tracer_initial_instr;
134134
// A recursive trace might've cleared the values. In that case, bail.
135135
if (code == NULL) {
136136
interp->compiling = false;
@@ -142,12 +142,12 @@ _PyOptimizer_Optimize(
142142
}
143143
// We are the only one still holding a reference to this code object that
144144
// is practically dead.
145-
if (_PyObject_IsUniquelyReferenced((PyObject *)code) || _PyObject_IsUniquelyReferenced((PyObject *)tstate->interp->jit_tracer_initial_func)) {
145+
if (_PyObject_IsUniquelyReferenced((PyObject *)code) || _PyObject_IsUniquelyReferenced((PyObject *)tstate->interp->jit_state.jit_tracer_initial_func)) {
146146
interp->compiling = false;
147147
return 0;
148148
}
149149
// One of our depencies while tracing was invalidated. Not worth compiling.
150-
if (!tstate->interp->jit_tracer_dependencies_still_valid) {
150+
if (!tstate->interp->jit_state.jit_tracer_dependencies_still_valid) {
151151
interp->compiling = false;
152152
return 0;
153153
}
@@ -177,7 +177,7 @@ _PyOptimizer_Optimize(
177177
executor->vm_data.code = NULL;
178178
}
179179
if (chain_depth > 0) {
180-
_PyExitData *prev_exit = tstate->interp->jit_tracer_previous_exit;
180+
_PyExitData *prev_exit = tstate->interp->jit_state.jit_tracer_previous_exit;
181181
assert(prev_exit != NULL);
182182
prev_exit->executor = executor;;
183183
}
@@ -565,14 +565,14 @@ _PyJIT_translate_single_bytecode_to_trace(
565565
int jump_taken)
566566
{
567567

568-
int is_first_instr = tstate->interp->jit_tracer_initial_instr == this_instr;
569-
bool progress_needed = (tstate->interp->jit_tracer_initial_chain_depth % MAX_CHAIN_DEPTH) == 0 && is_first_instr;;
570-
_PyBloomFilter *dependencies = &tstate->interp->jit_tracer_dependencies;
568+
int is_first_instr = tstate->interp->jit_state.jit_tracer_initial_instr == this_instr;
569+
bool progress_needed = (tstate->interp->jit_state.jit_tracer_initial_chain_depth % MAX_CHAIN_DEPTH) == 0 && is_first_instr;;
570+
_PyBloomFilter *dependencies = &tstate->interp->jit_state.jit_tracer_dependencies;
571571
_Py_BloomFilter_Add(dependencies, old_code);
572572
_Py_CODEUNIT *target_instr = this_instr;
573-
int trace_length = tstate->interp->jit_tracer_code_curr_size;
574-
_PyUOpInstruction *trace = tstate->interp->jit_tracer_code_buffer;
575-
int max_length = tstate->interp->jit_tracer_code_max_size;
573+
int trace_length = tstate->interp->jit_state.jit_tracer_code_curr_size;
574+
_PyUOpInstruction *trace = tstate->interp->jit_state.jit_tracer_code_buffer;
575+
int max_length = tstate->interp->jit_state.jit_tracer_code_max_size;
576576

577577
#ifdef Py_DEBUG
578578
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
@@ -595,7 +595,7 @@ _PyJIT_translate_single_bytecode_to_trace(
595595
if (jump_taken ||
596596
// This happens when a recursive call happens that we can't trace. Such as Python -> C -> Python calls
597597
// If we haven't guarded the IP, then it's untraceable.
598-
(frame != tstate->interp->jit_tracer_current_frame && !needs_guard_ip) ||
598+
(frame != tstate->interp->jit_state.jit_tracer_current_frame && !needs_guard_ip) ||
599599
// TODO handle extended args.
600600
oparg > 255 || opcode == EXTENDED_ARG ||
601601
// TODO handle BINARY_OP_INPLACE_ADD_UNICODE
@@ -627,7 +627,7 @@ _PyJIT_translate_single_bytecode_to_trace(
627627
}
628628
}
629629

630-
tstate->interp->jit_tracer_current_frame = frame;
630+
tstate->interp->jit_state.jit_tracer_current_frame = frame;
631631

632632
DPRINTF(2, "%p %d: %s(%d)\n", old_code, target, _PyOpcode_OpName[opcode], oparg);
633633

@@ -668,7 +668,7 @@ _PyJIT_translate_single_bytecode_to_trace(
668668
}
669669

670670
// Loop back to the start
671-
if (is_first_instr && tstate->interp->jit_tracer_code_curr_size > 2) {
671+
if (is_first_instr && tstate->interp->jit_state.jit_tracer_code_curr_size > 2) {
672672
ADD_TO_TRACE(_CHECK_PERIODIC, 0, 0, target);
673673
ADD_TO_TRACE(_JUMP_TO_TOP, 0, 0, 0);
674674
goto done;
@@ -822,24 +822,24 @@ _PyJIT_translate_single_bytecode_to_trace(
822822
if (needs_guard_ip) {
823823
ADD_TO_TRACE(_GUARD_IP, 0, (uintptr_t)next_instr, 0);
824824
}
825-
tstate->interp->jit_tracer_code_curr_size = trace_length;
826-
tstate->interp->jit_tracer_code_max_size = max_length;
825+
tstate->interp->jit_state.jit_tracer_code_curr_size = trace_length;
826+
tstate->interp->jit_state.jit_tracer_code_max_size = max_length;
827827
return 1;
828828
done:
829-
tstate->interp->jit_tracer_code_curr_size = trace_length;
830-
tstate->interp->jit_tracer_code_max_size = max_length;
829+
tstate->interp->jit_state.jit_tracer_code_curr_size = trace_length;
830+
tstate->interp->jit_state.jit_tracer_code_max_size = max_length;
831831
return 0;
832832
full:
833-
if (!is_terminator(&tstate->interp->jit_tracer_code_buffer[trace_length-1])) {
833+
if (!is_terminator(&tstate->interp->jit_state.jit_tracer_code_buffer[trace_length-1])) {
834834
// Undo the last few instructions.
835-
trace_length = tstate->interp->jit_tracer_code_curr_size;
836-
max_length = tstate->interp->jit_tracer_code_max_size;
835+
trace_length = tstate->interp->jit_state.jit_tracer_code_curr_size;
836+
max_length = tstate->interp->jit_state.jit_tracer_code_max_size;
837837
// We previously reversed one.
838838
max_length += 1;
839839
ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);
840840
}
841-
tstate->interp->jit_tracer_code_curr_size = trace_length;
842-
tstate->interp->jit_tracer_code_max_size = max_length;
841+
tstate->interp->jit_state.jit_tracer_code_curr_size = trace_length;
842+
tstate->interp->jit_state.jit_tracer_code_max_size = max_length;
843843
return 0;
844844
}
845845

@@ -860,28 +860,28 @@ _PyJIT_InitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_
860860
code->co_firstlineno,
861861
2 * INSTR_IP(next_instr, code));
862862
#endif
863-
add_to_trace(tstate->interp->jit_tracer_code_buffer, 0, _START_EXECUTOR, 0, (uintptr_t)next_instr, INSTR_IP(next_instr, code));
864-
add_to_trace(tstate->interp->jit_tracer_code_buffer, 1, _MAKE_WARM, 0, 0, 0);
865-
tstate->interp->jit_tracer_code_curr_size = 2;
866-
tstate->interp->jit_tracer_code_max_size = UOP_MAX_TRACE_LENGTH;
867-
tstate->interp->jit_tracer_initial_instr = next_instr;
868-
tstate->interp->jit_tracer_initial_code = (PyCodeObject *)Py_NewRef(code);
869-
tstate->interp->jit_tracer_initial_func = (PyFunctionObject *)Py_NewRef(_PyFrame_GetFunction(frame));
870-
tstate->interp->jit_tracer_previous_exit = exit;
871-
_Py_BloomFilter_Init(&tstate->interp->jit_tracer_dependencies);
872-
tstate->interp->jit_tracer_initial_stack_depth = curr_stackdepth;
873-
tstate->interp->jit_tracer_initial_chain_depth = chain_depth;
874-
tstate->interp->jit_tracer_current_frame = frame;
875-
tstate->interp->jit_tracer_dependencies_still_valid = true;
863+
add_to_trace(tstate->interp->jit_state.jit_tracer_code_buffer, 0, _START_EXECUTOR, 0, (uintptr_t)next_instr, INSTR_IP(next_instr, code));
864+
add_to_trace(tstate->interp->jit_state.jit_tracer_code_buffer, 1, _MAKE_WARM, 0, 0, 0);
865+
tstate->interp->jit_state.jit_tracer_code_curr_size = 2;
866+
tstate->interp->jit_state.jit_tracer_code_max_size = UOP_MAX_TRACE_LENGTH;
867+
tstate->interp->jit_state.jit_tracer_initial_instr = next_instr;
868+
tstate->interp->jit_state.jit_tracer_initial_code = (PyCodeObject *)Py_NewRef(code);
869+
tstate->interp->jit_state.jit_tracer_initial_func = (PyFunctionObject *)Py_NewRef(_PyFrame_GetFunction(frame));
870+
tstate->interp->jit_state.jit_tracer_previous_exit = exit;
871+
_Py_BloomFilter_Init(&tstate->interp->jit_state.jit_tracer_dependencies);
872+
tstate->interp->jit_state.jit_tracer_initial_stack_depth = curr_stackdepth;
873+
tstate->interp->jit_state.jit_tracer_initial_chain_depth = chain_depth;
874+
tstate->interp->jit_state.jit_tracer_current_frame = frame;
875+
tstate->interp->jit_state.jit_tracer_dependencies_still_valid = true;
876876
}
877877

878878
void
879879
_PyJIT_FinalizeTracing(PyThreadState *tstate)
880880
{
881-
Py_CLEAR(tstate->interp->jit_tracer_initial_code);
882-
Py_CLEAR(tstate->interp->jit_tracer_initial_func);
883-
tstate->interp->jit_tracer_code_curr_size = 2;
884-
tstate->interp->jit_tracer_code_max_size = UOP_MAX_TRACE_LENGTH - 1;
881+
Py_CLEAR(tstate->interp->jit_state.jit_tracer_initial_code);
882+
Py_CLEAR(tstate->interp->jit_state.jit_tracer_initial_func);
883+
tstate->interp->jit_state.jit_tracer_code_curr_size = 2;
884+
tstate->interp->jit_state.jit_tracer_code_max_size = UOP_MAX_TRACE_LENGTH - 1;
885885
}
886886

887887

@@ -1192,17 +1192,17 @@ uop_optimize(
11921192
_PyExecutorObject **exec_ptr,
11931193
bool progress_needed)
11941194
{
1195-
_PyBloomFilter *dependencies = &tstate->interp->jit_tracer_dependencies;
1195+
_PyBloomFilter *dependencies = &tstate->interp->jit_state.jit_tracer_dependencies;
11961196
PyInterpreterState *interp = _PyInterpreterState_GET();
1197-
_PyUOpInstruction *buffer = interp->jit_tracer_code_buffer;
1197+
_PyUOpInstruction *buffer = interp->jit_state.jit_tracer_code_buffer;
11981198
OPT_STAT_INC(attempts);
11991199
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
12001200
bool is_noopt = true;
12011201
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
12021202
is_noopt = false;
12031203
}
1204-
int curr_stackentries = tstate->interp->jit_tracer_initial_stack_depth;
1205-
int length = interp->jit_tracer_code_curr_size;
1204+
int curr_stackentries = tstate->interp->jit_state.jit_tracer_initial_stack_depth;
1205+
int length = interp->jit_state.jit_tracer_code_curr_size;
12061206
// Trace too short, don't bother.
12071207
if (length <= 5) {
12081208
return 0;
@@ -1211,7 +1211,7 @@ uop_optimize(
12111211
assert(length < UOP_MAX_TRACE_LENGTH);
12121212
OPT_STAT_INC(traces_created);
12131213
if (!is_noopt) {
1214-
length = _Py_uop_analyze_and_optimize(tstate->interp->jit_tracer_initial_func, buffer,
1214+
length = _Py_uop_analyze_and_optimize(tstate->interp->jit_state.jit_tracer_initial_func, buffer,
12151215
length,
12161216
curr_stackentries, dependencies);
12171217
if (length <= 0) {
@@ -1236,7 +1236,7 @@ uop_optimize(
12361236
OPT_HIST(effective_trace_length(buffer, length), optimized_trace_length_hist);
12371237
length = prepare_for_execution(buffer, length);
12381238
assert(length <= UOP_MAX_TRACE_LENGTH);
1239-
_PyExecutorObject *executor = make_executor_from_uops(buffer, length, dependencies, tstate->interp->jit_tracer_initial_chain_depth);
1239+
_PyExecutorObject *executor = make_executor_from_uops(buffer, length, dependencies, tstate->interp->jit_state.jit_tracer_initial_chain_depth);
12401240
if (executor == NULL) {
12411241
return -1;
12421242
}
@@ -1541,9 +1541,9 @@ _Py_JITTracer_InvalidateDependency(PyThreadState *old_tstate, void *obj)
15411541
PyInterpreterState *interp = old_tstate->interp;
15421542

15431543
_Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
1544-
if (bloom_filter_may_contain(&tstate->interp->jit_tracer_dependencies, &obj_filter))
1544+
if (bloom_filter_may_contain(&tstate->interp->jit_state.jit_tracer_dependencies, &obj_filter))
15451545
{
1546-
tstate->interp->jit_tracer_dependencies_still_valid = false;
1546+
tstate->interp->jit_state.jit_tracer_dependencies_still_valid = false;
15471547
}
15481548

15491549
}

Python/pystate.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ init_interpreter(PyInterpreterState *interp,
553553
#endif
554554

555555
#ifdef _Py_TIER2
556-
interp->jit_tracer_code_buffer = NULL;
557-
interp->jit_tracer_initial_instr = NULL;
558-
interp->jit_tracer_initial_stack_depth = -1;
559-
interp->jit_tracer_initial_chain_depth = -1;
560-
interp->jit_tracer_initial_code = NULL;
561-
interp->jit_tracer_initial_func = NULL;
556+
interp->jit_state.jit_tracer_code_buffer = NULL;
557+
interp->jit_state.jit_tracer_initial_instr = NULL;
558+
interp->jit_state.jit_tracer_initial_stack_depth = -1;
559+
interp->jit_state.jit_tracer_initial_chain_depth = -1;
560+
interp->jit_state.jit_tracer_initial_code = NULL;
561+
interp->jit_state.jit_tracer_initial_func = NULL;
562562
#endif
563563
llist_init(&interp->mem_free_queue.head);
564564
llist_init(&interp->asyncio_tasks_head);
@@ -808,9 +808,9 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
808808

809809
#ifdef _Py_TIER2
810810
_Py_ClearExecutorDeletionList(interp);
811-
if (interp->jit_tracer_code_buffer != NULL) {
812-
_PyObject_VirtualFree(interp->jit_tracer_code_buffer, UOP_BUFFER_SIZE);
813-
interp->jit_tracer_code_buffer = NULL;
811+
if (interp->jit_state.jit_tracer_code_buffer != NULL) {
812+
_PyObject_VirtualFree(interp->jit_state.jit_tracer_code_buffer, UOP_BUFFER_SIZE);
813+
interp->jit_state.jit_tracer_code_buffer = NULL;
814814
}
815815
#endif
816816
_PyAST_Fini(interp);

0 commit comments

Comments
 (0)