diff --git a/Compiler/src/typeinfer.jl b/Compiler/src/typeinfer.jl
index 3eb159cfa1bc0..3084370b26aba 100644
--- a/Compiler/src/typeinfer.jl
+++ b/Compiler/src/typeinfer.jl
@@ -1410,9 +1410,15 @@ function add_codeinsts_to_jit!(interp::AbstractInterpreter, ci, source_mode::UIn
return ci
end
+const collect_dispatch_backtrace = fill(false)
+const dispatch_backtrace = IdDict{CodeInstance,Any}()
+
function typeinf_ext_toplevel(interp::AbstractInterpreter, mi::MethodInstance, source_mode::UInt8)
ci = typeinf_ext(interp, mi, source_mode)
ci = add_codeinsts_to_jit!(interp, ci, source_mode)
+ if collect_dispatch_backtrace[]
+ dispatch_backtrace[ci] = backtrace()
+ end
return ci
end
@@ -1432,6 +1438,7 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_m
inspected = IdSet{CodeInstance}()
tocompile = Vector{CodeInstance}()
codeinfos = []
+ bt = collect_dispatch_backtrace[] ? backtrace() : nothing
# first compute the ABIs of everything
latest = true # whether this_world == world_counter()
for this_world in reverse(sort!(worlds))
@@ -1469,6 +1476,9 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_m
end
while !isempty(tocompile)
callee = pop!(tocompile)
+ if collect_dispatch_backtrace[]
+ dispatch_backtrace[callee] = bt
+ end
callee in inspected && continue
# now make sure everything has source code, if desired
mi = get_ci_mi(callee)