Skip to content

Commit 709c0a1

Browse files
committed
address review
1 parent 2f9438a commit 709c0a1

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ extern "C" {
1717

1818
/* Default fitness configuration values for trace quality control.
1919
* These can be overridden via PYTHON_JIT_FITNESS_* environment variables. */
20-
#define FITNESS_INITIAL 1000
21-
#define FITNESS_INITIAL_SIDE 800
2220
#define FITNESS_PER_INSTRUCTION 2
21+
#define FITNESS_INITIAL (UOP_MAX_TRACE_LENGTH * FITNESS_PER_INSTRUCTION)
22+
#define FITNESS_INITIAL_SIDE 800
2323
#define FITNESS_BRANCH_BIASED 5
2424
#define FITNESS_BRANCH_UNBIASED 25
2525
#define FITNESS_BACKWARD_EDGE 80

Python/optimizer.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -808,21 +808,6 @@ _PyJit_translate_single_bytecode_to_trace(
808808
goto unsupported;
809809
}
810810

811-
// Track frame depth changes for fitness (only for supported frame transitions)
812-
if (frame != tracer->prev_state.instr_frame) {
813-
_PyJitTracerTranslatorState *ts_depth = &tracer->translator_state;
814-
if (frame->previous == tracer->prev_state.instr_frame) {
815-
ts_depth->frame_depth++;
816-
// Penalty scales with depth: shallow inlining is cheap,
817-
// deep inlining gets progressively more expensive.
818-
int32_t penalty = (int32_t)tstate->interp->opt_config.fitness_frame_entry
819-
* ts_depth->frame_depth;
820-
ts_depth->fitness -= penalty;
821-
} else if (ts_depth->frame_depth > 0) {
822-
ts_depth->frame_depth--;
823-
}
824-
}
825-
826811
if (oparg > 0xFFFF) {
827812
DPRINTF(2, "Unsupported: oparg too large\n");
828813
unsupported:
@@ -1089,6 +1074,28 @@ _PyJit_translate_single_bytecode_to_trace(
10891074
assert(next->op.code == STORE_FAST);
10901075
operand = next->op.arg;
10911076
}
1077+
else if (uop == _PUSH_FRAME) {
1078+
_PyJitTracerTranslatorState *ts_depth = &tracer->translator_state;
1079+
ts_depth->frame_depth++;
1080+
if (ts_depth->frame_depth >= MAX_ABSTRACT_FRAME_DEPTH) {
1081+
// The optimizer can't handle frames this deep,
1082+
// so there's no point continuing the trace.
1083+
DPRINTF(2, "Unsupported: frame depth %d >= MAX_ABSTRACT_FRAME_DEPTH\n",
1084+
ts_depth->frame_depth);
1085+
goto unsupported;
1086+
}
1087+
int32_t penalty = (int32_t)tstate->interp->opt_config.fitness_frame_entry
1088+
* ts_depth->frame_depth;
1089+
ts_depth->fitness -= penalty;
1090+
}
1091+
else if (uop == _RETURN_VALUE || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE) {
1092+
_PyJitTracerTranslatorState *ts_depth = &tracer->translator_state;
1093+
if (ts_depth->frame_depth <= 0) {
1094+
// Underflow
1095+
ts_depth->fitness -= (int32_t)tstate->interp->opt_config.fitness_frame_entry * 2;
1096+
}
1097+
ts_depth->frame_depth = ts_depth->frame_depth <= 0 ? 0 : ts_depth->frame_depth - 1;
1098+
}
10921099
else if (_PyUop_Flags[uop] & HAS_RECORDS_VALUE_FLAG) {
10931100
PyObject *recorded_value = tracer->prev_state.recorded_value;
10941101
tracer->prev_state.recorded_value = NULL;

0 commit comments

Comments
 (0)