@@ -17,7 +17,7 @@ use crate::gc::append_gc_offsets;
1717use crate :: payload:: { IseqCodePtrs , IseqStatus , IseqVersion , IseqVersionRef , JITFrame , get_or_create_iseq_payload} ;
1818use crate :: state:: ZJITState ;
1919use crate :: stats:: { CompileError , exit_counter_for_compile_error, exit_counter_for_unhandled_hir_insn, incr_counter, incr_counter_by, send_fallback_counter, send_fallback_counter_for_method_type, send_fallback_counter_for_super_method_type, send_fallback_counter_ptr_for_opcode, send_without_block_fallback_counter_for_method_type, send_without_block_fallback_counter_for_optimized_method_type} ;
20- use crate :: stats:: { counter_ptr, with_time_stat, Counter , Counter :: { compile_time_ns, exit_compile_error} } ;
20+ use crate :: stats:: { counter_ptr, with_time_stat, trace_compile_phase , Counter , Counter :: { compile_time_ns, exit_compile_error} } ;
2121use crate :: { asm:: CodeBlock , cruby:: * , options:: debug, virtualmem:: CodePtr } ;
2222use crate :: backend:: lir:: { self , Assembler , C_ARG_OPNDS , C_RET_OPND , CFP , EC , NATIVE_STACK_PTR , Opnd , SP , SideExit , SideExitRecompile , Target , asm_ccall, asm_comment} ;
2323use crate :: hir:: { iseq_to_hir, BlockId , Invariant , RangeType , SideExitReason :: { self , * } , SpecialBackrefSymbol , SpecialObjectType } ;
@@ -200,17 +200,20 @@ fn gen_iseq_entry_point(cb: &mut CodeBlock, iseq: IseqPtr, jit_exception: bool)
200200 return Err ( CompileError :: ExceptionHandler ) ;
201201 }
202202
203- // Compile ISEQ into High-level IR
204- let function = crate :: stats:: with_time_stat ( Counter :: compile_hir_time_ns, || compile_iseq ( iseq) . inspect_err ( |_| {
205- incr_counter ! ( failed_iseq_count) ;
206- } ) ) ?;
203+ let iseq_name = iseq_get_location ( iseq, 0 ) ;
204+ trace_compile_phase ( & iseq_name, || {
205+ // Compile ISEQ into High-level IR
206+ let function = crate :: stats:: with_time_stat ( Counter :: compile_hir_time_ns, || compile_iseq ( iseq) . inspect_err ( |_| {
207+ incr_counter ! ( failed_iseq_count) ;
208+ } ) ) ?;
207209
208- // Compile the High-level IR
209- let IseqCodePtrs { start_ptr, .. } = gen_iseq ( cb, iseq, Some ( & function) ) . inspect_err ( |err| {
210- debug ! ( "{err:?}: gen_iseq failed: {}" , iseq_get_location( iseq, 0 ) ) ;
211- } ) ?;
210+ // Compile the High-level IR
211+ let IseqCodePtrs { start_ptr, .. } = gen_iseq ( cb, iseq, Some ( & function) ) . inspect_err ( |err| {
212+ debug ! ( "{err:?}: gen_iseq failed: {}" , iseq_get_location( iseq, 0 ) ) ;
213+ } ) ?;
212214
213- Ok ( start_ptr)
215+ Ok ( start_ptr)
216+ } )
214217}
215218
216219/// Invalidate an ISEQ version and allow it to be recompiled on the next call.
@@ -312,9 +315,14 @@ fn gen_iseq(cb: &mut CodeBlock, iseq: IseqPtr, function: Option<&Function>) -> R
312315 return Err ( CompileError :: IseqVersionLimitReached ) ;
313316 }
314317
315- // Compile the ISEQ
318+ // Compile the ISEQ. When function is None, this is a lazy compile
319+ // from a stub hit — wrap in a trace event covering the full compile.
316320 let mut version = IseqVersion :: new ( iseq) ;
317- let code_ptrs = gen_iseq_body ( cb, iseq, version, function) ;
321+ let code_ptrs = if function. is_none ( ) {
322+ trace_compile_phase ( & iseq_get_location ( iseq, 0 ) , || gen_iseq_body ( cb, iseq, version, function) )
323+ } else {
324+ gen_iseq_body ( cb, iseq, version, function)
325+ } ;
318326 match & code_ptrs {
319327 Ok ( code_ptrs) => {
320328 unsafe { version. as_mut ( ) } . status = IseqStatus :: Compiled ( code_ptrs. clone ( ) ) ;
@@ -344,7 +352,9 @@ fn gen_iseq_body(cb: &mut CodeBlock, iseq: IseqPtr, mut version: IseqVersionRef,
344352
345353 // Compile the High-level IR
346354 let ( iseq_code_ptrs, gc_offsets, iseq_calls) =
347- crate :: stats:: with_time_stat ( Counter :: compile_lir_time_ns, || gen_function ( cb, iseq, version, function) ) ?;
355+ trace_compile_phase ( "codegen" , ||
356+ crate :: stats:: with_time_stat ( Counter :: compile_lir_time_ns, || gen_function ( cb, iseq, version, function) )
357+ ) ?;
348358
349359 // Stub callee ISEQs for JIT-to-JIT calls
350360 for iseq_call in iseq_calls. iter ( ) {
@@ -2955,7 +2965,9 @@ fn compile_iseq(iseq: IseqPtr) -> Result<Function, CompileError> {
29552965 return Err ( CompileError :: IseqStackTooLarge ) ;
29562966 }
29572967
2958- let hir = crate :: stats:: with_time_stat ( Counter :: compile_hir_build_time_ns, || iseq_to_hir ( iseq) ) ;
2968+ let hir = trace_compile_phase ( "build_hir" , ||
2969+ crate :: stats:: with_time_stat ( Counter :: compile_hir_build_time_ns, || iseq_to_hir ( iseq) )
2970+ ) ;
29592971 let mut function = match hir {
29602972 Ok ( function) => function,
29612973 Err ( err) => {
@@ -2964,7 +2976,7 @@ fn compile_iseq(iseq: IseqPtr) -> Result<Function, CompileError> {
29642976 }
29652977 } ;
29662978 if !get_option ! ( disable_hir_opt) {
2967- function. optimize ( ) ;
2979+ trace_compile_phase ( "optimize" , || function. optimize ( ) ) ;
29682980 }
29692981 function. dump_hir ( ) ;
29702982 Ok ( function)
0 commit comments