Skip to content

Commit 7d17741

Browse files
First half of reviews
1 parent d18c1a1 commit 7d17741

File tree

13 files changed

+46
-65
lines changed

13 files changed

+46
-65
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ struct _Py_unique_id_pool {
757757

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

760-
typedef struct _PyJitState {
760+
typedef struct _PyJitTracerState {
761761
int jit_tracer_code_max_size;
762762
int jit_tracer_code_curr_size;
763763
_PyBloomFilter jit_tracer_dependencies;
@@ -772,7 +772,7 @@ typedef struct _PyJitState {
772772
PyFunctionObject *jit_tracer_initial_func; // Strong
773773
struct _PyExitData *jit_tracer_previous_exit;
774774
_PyInterpreterFrame *jit_tracer_current_frame;
775-
} _PyJitState;
775+
} _PyJitTracerState;
776776

777777
/* PyInterpreterState holds the global state for one of the runtime's
778778
interpreters. Typically the initial (main) interpreter is the only one.
@@ -949,7 +949,7 @@ struct _is {
949949
struct types_state types;
950950
struct callable_cache callable_cache;
951951
PyObject *common_consts[NUM_COMMON_CONSTANTS];
952-
_PyJitState jit_state;
952+
_PyJitTracerState jit_state;
953953
bool jit;
954954
bool compiling;
955955
struct _PyExecutorObject *executor_list_head;

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_optimizer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ typedef struct {
3737
typedef struct _PyExitData {
3838
uint32_t target;
3939
uint16_t index;
40-
char is_dynamic;
4140
_Py_BackoffCounter temperature;
4241
struct _PyExecutorObject *executor;
4342
} _PyExitData;

Python/bytecodes.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ dummy_func(
13781378
if (err == 0) {
13791379
assert(retval_o != NULL);
13801380
JUMPBY(oparg);
1381-
RECORD_JUMP_TAKEN();
1381+
RECORD_DYNAMIC_JUMP_TAKEN();
13821382
}
13831383
else {
13841384
PyStackRef_CLOSE(v);
@@ -3241,7 +3241,7 @@ dummy_func(
32413241
}
32423242
// Jump forward by oparg and skip the following END_FOR
32433243
JUMPBY(oparg + 1);
3244-
RECORD_JUMP_TAKEN();
3244+
RECORD_DYNAMIC_JUMP_TAKEN();
32453245
DISPATCH();
32463246
}
32473247
next = item;
@@ -3303,7 +3303,7 @@ dummy_func(
33033303
null_or_index = PyStackRef_TagInt(-1);
33043304
/* Jump forward oparg, then skip following END_FOR instruction */
33053305
JUMPBY(oparg + 1);
3306-
RECORD_JUMP_TAKEN();
3306+
RECORD_DYNAMIC_JUMP_TAKEN();
33073307
DISPATCH();
33083308
}
33093309
#endif
@@ -3381,7 +3381,7 @@ dummy_func(
33813381
null_or_index = PyStackRef_TagInt(-1);
33823382
/* Jump forward oparg, then skip following END_FOR instruction */
33833383
JUMPBY(oparg + 1);
3384-
RECORD_JUMP_TAKEN();
3384+
RECORD_DYNAMIC_JUMP_TAKEN();
33853385
DISPATCH();
33863386
}
33873387
}
@@ -3426,7 +3426,7 @@ dummy_func(
34263426
if (r->len <= 0) {
34273427
// Jump over END_FOR instruction.
34283428
JUMPBY(oparg + 1);
3429-
RECORD_JUMP_TAKEN();
3429+
RECORD_DYNAMIC_JUMP_TAKEN();
34303430
DISPATCH();
34313431
}
34323432
}
@@ -5011,7 +5011,7 @@ dummy_func(
50115011
LOAD_IP(frame->return_offset);
50125012
#endif
50135013
#if TIER_TWO
5014-
frame->instr_ptr += (frame->return_offset);
5014+
TIER2_STORE_IP(frame->return_offset);
50155015
#endif
50165016
RELOAD_STACK();
50175017
res = PyStackRef_FromPyObjectStealMortal((PyObject *)gen);
@@ -5278,7 +5278,6 @@ dummy_func(
52785278

52795279
tier2 op(_EXIT_TRACE, (exit_p/4 --)) {
52805280
_PyExitData *exit = (_PyExitData *)exit_p;
5281-
assert(!exit->is_dynamic);
52825281
#if defined(Py_DEBUG) && !defined(_Py_JIT)
52835282
_Py_CODEUNIT *target = _PyFrame_GetBytecode(frame) + exit->target;
52845283
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);

Python/ceval_macros.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
#endif
131131

132132
#define TRACING_JUMP_TO_LABEL(label) \
133-
RECORD_JUMP_TAKEN() \
133+
RECORD_DYNAMIC_JUMP_TAKEN() \
134134
RECORD_TRACE_NO_DISPATCH() \
135135
assert(!IS_JIT_TRACING()); \
136136
JUMP_TO_LABEL(label);
@@ -381,7 +381,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
381381
#define RECORD_BRANCH_TAKEN(bitset, flag)
382382
#endif
383383

384-
#define RECORD_JUMP_TAKEN() _jump_taken = 1;
384+
#define RECORD_DYNAMIC_JUMP_TAKEN() _jump_taken = 1;
385385

386386
#define UNBOUNDLOCAL_ERROR_MSG \
387387
"cannot access local variable '%s' where it is not associated with a value"

Python/executor_cases.c.h

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

Python/generated_cases.c.h

Lines changed: 1 addition & 1 deletion
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: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,7 @@ _PyOptimizer_Optimize(
141141
interp->compiling = false;
142142
return 0;
143143
}
144-
// We are the only one still holding a reference to this code object that
145-
// is practically dead.
146-
if (_PyObject_IsUniquelyReferenced((PyObject *)code) || _PyObject_IsUniquelyReferenced((PyObject *)tstate->interp->jit_state.jit_tracer_initial_func)) {
147-
interp->compiling = false;
148-
return 0;
149-
}
150-
// One of our depencies while tracing was invalidated. Not worth compiling.
144+
// One of our dependencies while tracing was invalidated. Not worth compiling.
151145
if (!tstate->interp->jit_state.jit_tracer_dependencies_still_valid) {
152146
interp->compiling = false;
153147
return 0;
@@ -603,20 +597,21 @@ _PyJIT_translate_single_bytecode_to_trace(
603597
}
604598
#endif
605599

600+
if (!tstate->interp->jit_state.jit_tracer_dependencies_still_valid) {
601+
goto done;
602+
}
603+
606604
DPRINTF(2, "%p %d: %s(%d) %d\n", old_code, target, _PyOpcode_OpName[opcode], oparg, progress_needed);
607605

608-
bool needs_guard_ip = _PyOpcode_NeedsGuardIp[opcode] &&
609-
!(opcode == FOR_ITER_RANGE || opcode == FOR_ITER_LIST || opcode == FOR_ITER_TUPLE) &&
610-
!(opcode == JUMP_BACKWARD_NO_INTERRUPT || opcode == JUMP_BACKWARD || opcode == JUMP_BACKWARD_JIT) &&
611-
!(opcode == POP_JUMP_IF_TRUE || opcode == POP_JUMP_IF_FALSE || opcode == POP_JUMP_IF_NONE || opcode == POP_JUMP_IF_NOT_NONE);
606+
bool needs_guard_ip = _PyOpcode_NeedsGuardIp[opcode];
612607

613608
// Strange control-flow, unsupported opcode, etc.
614609
if (jump_taken ||
615610
// This happens when a recursive call happens that we can't trace. Such as Python -> C -> Python calls
616611
// If we haven't guarded the IP, then it's untraceable.
617612
(frame != tstate->interp->jit_state.jit_tracer_current_frame && !needs_guard_ip) ||
618613
(oparg > 0xFFFF) ||
619-
// TODO (gh-140277): The constituent use one extra stack slot. So we need to check for heaedroom.
614+
// TODO (gh-140277): The constituent use one extra stack slot. So we need to check for headroom.
620615
(opcode == BINARY_OP_SUBSCR_GETITEM && old_stack_level + 1 > old_code->co_stacksize)||
621616
// Exception stuff, could be handled in the future maybe?
622617
opcode == WITH_EXCEPT_START || opcode == RERAISE || opcode == CLEANUP_THROW || opcode == PUSH_EXC_INFO ||
@@ -1155,7 +1150,6 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil
11551150
_PyExitData *exit = &executor->exits[next_exit];
11561151
exit->target = buffer[i].target;
11571152
dest->operand0 = (uint64_t)exit;
1158-
exit->is_dynamic = (char)(opcode == _DYNAMIC_EXIT);
11591153
next_exit--;
11601154
}
11611155
}
@@ -1561,20 +1555,15 @@ _Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is
15611555
}
15621556

15631557
void
1564-
_Py_JITTracer_InvalidateDependency(PyThreadState *old_tstate, void *obj)
1558+
_Py_JITTracer_InvalidateDependency(PyThreadState *tstate, void *obj)
15651559
{
15661560
_PyBloomFilter obj_filter;
15671561
_Py_BloomFilter_Init(&obj_filter);
15681562
_Py_BloomFilter_Add(&obj_filter, obj);
15691563

1570-
PyInterpreterState *interp = old_tstate->interp;
1571-
1572-
_Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
1573-
if (bloom_filter_may_contain(&tstate->interp->jit_state.jit_tracer_dependencies, &obj_filter))
1574-
{
1575-
tstate->interp->jit_state.jit_tracer_dependencies_still_valid = false;
1576-
}
1577-
1564+
if (bloom_filter_may_contain(&tstate->interp->jit_state.jit_tracer_dependencies, &obj_filter))
1565+
{
1566+
tstate->interp->jit_state.jit_tracer_dependencies_still_valid = false;
15781567
}
15791568
}
15801569
/* Invalidate all executors */

Python/optimizer_analysis.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,14 @@ _Py_uop_analyze_and_optimize(
528528
{
529529
OPT_STAT_INC(optimizer_attempts);
530530

531-
optimize_uops(
531+
length = optimize_uops(
532532
initial_func, buffer,
533533
length, curr_stacklen, dependencies);
534534

535+
if (length == 0) {
536+
return length;
537+
}
538+
535539
assert(length > 0);
536540

537541
length = remove_unneeded_uops(buffer, length);

0 commit comments

Comments
 (0)