@@ -802,15 +802,17 @@ _PyJit_translate_single_bytecode_to_trace(
802802 // Fitness-based trace quality check (before reserving space for this instruction)
803803 _PyJitTracerTranslatorState * ts = & tracer -> translator_state ;
804804 int32_t eq = compute_exit_quality (target_instr , opcode , tracer );
805+ DPRINTF (3 , "Fitness check: %s(%d) fitness=%d, exit_quality=%d, depth=%d\n" ,
806+ _PyOpcode_OpName [opcode ], oparg , ts -> fitness , eq , ts -> frame_depth );
805807
806808 // Check if fitness is depleted — should we stop the trace?
807809 if (ts -> fitness < eq ) {
808810 // This is a tracer heuristic rather than normal program control flow,
809811 // so leave operand1 clear and let the resulting side exit increase chain_depth.
810812 ADD_TO_TRACE (_EXIT_TRACE , 0 , 0 , target );
811813 OPT_STAT_INC (fitness_terminated_traces );
812- DPRINTF (2 , "Fitness terminated: fitness=%d < exit_quality=%d\n" ,
813- ts -> fitness , eq );
814+ DPRINTF (2 , "Fitness terminated: %s(%d) fitness=%d < exit_quality=%d\n" ,
815+ _PyOpcode_OpName [ opcode ], oparg , ts -> fitness , eq );
814816 goto done ;
815817 }
816818
@@ -867,8 +869,12 @@ _PyJit_translate_single_bytecode_to_trace(
867869 assert (jump_happened ? (next_instr == computed_jump_instr ) : (next_instr == computed_next_instr ));
868870 uint32_t uopcode = BRANCH_TO_GUARD [opcode - POP_JUMP_IF_FALSE ][jump_happened ];
869871 ADD_TO_TRACE (uopcode , 0 , 0 , INSTR_IP (jump_happened ? computed_next_instr : computed_jump_instr , old_code ));
870- tracer -> translator_state .fitness -= compute_branch_penalty (
871- target_instr [1 ].cache , jump_happened );
872+ int bp = compute_branch_penalty (target_instr [1 ].cache , jump_happened );
873+ tracer -> translator_state .fitness -= bp ;
874+ DPRINTF (3 , " branch penalty: -%d (history=0x%04x, taken=%d) -> fitness=%d\n" ,
875+ bp , target_instr [1 ].cache , jump_happened ,
876+ tracer -> translator_state .fitness );
877+
872878 break ;
873879 }
874880 case JUMP_BACKWARD_JIT :
@@ -877,6 +883,8 @@ _PyJit_translate_single_bytecode_to_trace(
877883 case JUMP_BACKWARD :
878884 ADD_TO_TRACE (_CHECK_PERIODIC , 0 , 0 , target );
879885 tracer -> translator_state .fitness -= FITNESS_BACKWARD_EDGE ;
886+ DPRINTF (3 , " backward edge penalty: -%d -> fitness=%d\n" ,
887+ FITNESS_BACKWARD_EDGE , tracer -> translator_state .fitness );
880888 _Py_FALLTHROUGH ;
881889 case JUMP_BACKWARD_NO_INTERRUPT :
882890 {
@@ -1010,18 +1018,29 @@ _PyJit_translate_single_bytecode_to_trace(
10101018 goto unsupported ;
10111019 }
10121020 int32_t frame_penalty = compute_frame_penalty (& tstate -> interp -> opt_config );
1013- ts_depth -> fitness -= frame_penalty * ts_depth -> frame_depth ;
1021+ int32_t cost = frame_penalty * ts_depth -> frame_depth ;
1022+ ts_depth -> fitness -= cost ;
1023+ DPRINTF (3 , " _PUSH_FRAME: depth=%d, penalty=-%d (per_frame=%d) -> fitness=%d\n" ,
1024+ ts_depth -> frame_depth , cost , frame_penalty ,
1025+ ts_depth -> fitness );
10141026 }
10151027 else if (uop == _RETURN_VALUE || uop == _RETURN_GENERATOR || uop == _YIELD_VALUE ) {
10161028 _PyJitTracerTranslatorState * ts_depth = & tracer -> translator_state ;
10171029 int32_t frame_penalty = compute_frame_penalty (& tstate -> interp -> opt_config );
10181030 if (ts_depth -> frame_depth <= 0 ) {
10191031 // Underflow: returning from a frame we didn't enter
10201032 ts_depth -> fitness -= frame_penalty * 2 ;
1033+ DPRINTF (3 , " %s: underflow penalty=-%d -> fitness=%d\n" ,
1034+ _PyOpcode_uop_name [uop ], frame_penalty * 2 ,
1035+ ts_depth -> fitness );
10211036 }
10221037 else {
10231038 // Reward returning: small inlined calls should be encouraged
10241039 ts_depth -> fitness += frame_penalty ;
1040+ DPRINTF (3 , " %s: return reward=+%d, depth=%d -> fitness=%d\n" ,
1041+ _PyOpcode_uop_name [uop ], frame_penalty ,
1042+ ts_depth -> frame_depth - 1 ,
1043+ ts_depth -> fitness );
10251044 }
10261045 ts_depth -> frame_depth = ts_depth -> frame_depth <= 0 ? 0 : ts_depth -> frame_depth - 1 ;
10271046 }
@@ -1070,6 +1089,8 @@ _PyJit_translate_single_bytecode_to_trace(
10701089 // This ensures the next iteration's fitness check reflects the cost of
10711090 // all instructions translated so far.
10721091 tracer -> translator_state .fitness -= FITNESS_PER_INSTRUCTION ;
1092+ DPRINTF (3 , " per-insn cost: -%d -> fitness=%d\n" ,
1093+ FITNESS_PER_INSTRUCTION , tracer -> translator_state .fitness );
10731094 DPRINTF (2 , "Trace continuing (fitness=%d)\n" , tracer -> translator_state .fitness );
10741095 return 1 ;
10751096done :
@@ -1161,6 +1182,8 @@ _PyJit_TryInitializeTracing(
11611182 ? (int32_t )cfg -> fitness_initial_side
11621183 : (int32_t )cfg -> fitness_initial ;
11631184 ts -> frame_depth = 0 ;
1185+ DPRINTF (3 , "Fitness init: %s trace, fitness=%d\n" ,
1186+ is_side_trace ? "side" : "root" , ts -> fitness );
11641187
11651188 tracer -> is_tracing = true;
11661189 return 1 ;
0 commit comments