@@ -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