From 40a9f6cfff62a1fcb2a5dd37049190015759acb8 Mon Sep 17 00:00:00 2001 From: Pete Vilter Date: Fri, 18 Nov 2022 00:18:21 -0500 Subject: [PATCH 1/3] Adapt SnoopPrecompile to work for new julia API. Starting in https://github.com/JuliaLang/julia/pull/47615, we are changing the API for snoopi_deep, to allow for thread safe snooping in one thread while compiling in another. This commit changes SnoopPrecompile to work with the new API if it's available. Co-Authored-By: Pete Vilter --- SnoopPrecompile/src/SnoopPrecompile.jl | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/SnoopPrecompile/src/SnoopPrecompile.jl b/SnoopPrecompile/src/SnoopPrecompile.jl index d0dcd6b3..eaf2d38b 100644 --- a/SnoopPrecompile/src/SnoopPrecompile.jl +++ b/SnoopPrecompile/src/SnoopPrecompile.jl @@ -5,6 +5,7 @@ export @precompile_all_calls, @precompile_setup const verbose = Ref(false) # if true, prints all the precompiles const have_inference_tracking = isdefined(Core.Compiler, :__set_measure_typeinf) const have_force_compile = isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("#@force_compile")) +const have_clear_and_fetch_timings = isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) function precompile_roots(roots) @assert have_inference_tracking @@ -59,16 +60,29 @@ macro precompile_all_calls(ex::Expr) end end if have_inference_tracking - ex = quote - Core.Compiler.Timings.reset_timings() - Core.Compiler.__set_measure_typeinf(true) - try - $ex - finally - Core.Compiler.__set_measure_typeinf(false) - Core.Compiler.Timings.close_current_timer() + if have_clear_and_fetch_timings + # use new thread-safe timings API if it's available in this version of Julia + ex = quote + Core.Compiler.__set_measure_typeinf(true) + try + $ex + finally + Core.Compiler.__set_measure_typeinf(false) + end + $SnoopPrecompile.precompile_roots(Core.Compiler.Timings.clear_and_fetch_timings()) + end + else + ex = quote + Core.Compiler.Timings.reset_timings() + Core.Compiler.__set_measure_typeinf(true) + try + $ex + finally + Core.Compiler.__set_measure_typeinf(false) + Core.Compiler.Timings.close_current_timer() + end + $SnoopPrecompile.precompile_roots(Core.Compiler.Timings._timings[1].children) end - $SnoopPrecompile.precompile_roots(Core.Compiler.Timings._timings[1].children) end end return esc(quote From ec14f3534c1a1ac76b55c39fe9b9900c0a767676 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Mon, 21 Nov 2022 18:15:58 -0700 Subject: [PATCH 2/3] fixup --- SnoopPrecompile/src/SnoopPrecompile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SnoopPrecompile/src/SnoopPrecompile.jl b/SnoopPrecompile/src/SnoopPrecompile.jl index eaf2d38b..3f128287 100644 --- a/SnoopPrecompile/src/SnoopPrecompile.jl +++ b/SnoopPrecompile/src/SnoopPrecompile.jl @@ -5,7 +5,7 @@ export @precompile_all_calls, @precompile_setup const verbose = Ref(false) # if true, prints all the precompiles const have_inference_tracking = isdefined(Core.Compiler, :__set_measure_typeinf) const have_force_compile = isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("#@force_compile")) -const have_clear_and_fetch_timings = isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) +const have_clear_and_fetch_timings = isdefined(Core.Compiler, :Timings) && isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) function precompile_roots(roots) @assert have_inference_tracking From 876e80838bb4afa1de8ceadcadd6887ff1fb971a Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Fri, 30 Dec 2022 11:50:22 -0700 Subject: [PATCH 3/3] Adapt snoopi_deep for backwards compatibility with old API Create a dummy ROOT node, and return the tree with that node. --- SnoopCompileCore/src/snoopi_deep.jl | 43 +++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/SnoopCompileCore/src/snoopi_deep.jl b/SnoopCompileCore/src/snoopi_deep.jl index 3719cf84..563b9c9f 100644 --- a/SnoopCompileCore/src/snoopi_deep.jl +++ b/SnoopCompileCore/src/snoopi_deep.jl @@ -1,3 +1,7 @@ +const have_clear_and_fetch_timings = isdefined(Core.Compiler, :Timings) && isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) + +using Core.Compiler.Timings: ROOT, ROOTmi, InferenceFrameInfo, Timing + struct InferenceTiming mi_info::Core.Compiler.Timings.InferenceFrameInfo inclusive_time::Float64 @@ -70,17 +74,34 @@ function addchildren!(parent::InferenceTimingNode, t::Core.Compiler.Timings.Timi end end -function start_deep_timing() - Core.Compiler.Timings.reset_timings() - Core.Compiler.__set_measure_typeinf(true) -end -function stop_deep_timing() - Core.Compiler.__set_measure_typeinf(false) - Core.Compiler.Timings.close_current_timer() -end - -function finish_snoopi_deep() - return InferenceTimingNode(Core.Compiler.Timings._timings[1]) +if have_clear_and_fetch_timings + function start_deep_timing() + Core.Compiler.__set_measure_typeinf(true) + end + function stop_deep_timing() + Core.Compiler.__set_measure_typeinf(false) + end + function finish_snoopi_deep() + # Construct a dummy Timing for ROOT(), for backwards compatibility with the old API. + root = Timing( + InferenceFrameInfo(ROOTmi, 0x0, Any[], Any[Core.Const(ROOT)], 1), + 0x0) + root.children = Core.Compiler.Timings.clear_and_fetch_timings() + + return InferenceTimingNode(root) + end +else + function start_deep_timing() + Core.Compiler.Timings.reset_timings() + Core.Compiler.__set_measure_typeinf(true) + end + function stop_deep_timing() + Core.Compiler.__set_measure_typeinf(false) + Core.Compiler.Timings.close_current_timer() + end + function finish_snoopi_deep() + return InferenceTimingNode(Core.Compiler.Timings._timings[1]) + end end function _snoopi_deep(cmd::Expr)