diff --git a/TypedSyntax/src/node.jl b/TypedSyntax/src/node.jl index 0756e5aa..0d7db70f 100644 --- a/TypedSyntax/src/node.jl +++ b/TypedSyntax/src/node.jl @@ -30,16 +30,18 @@ function tsn_and_mappings(@nospecialize(f), @nospecialize(t); kwargs...) tsn_and_mappings(m, src, rt; kwargs...) end -function tsn_and_mappings(m::Method, src::CodeInfo, @nospecialize(rt); warn::Bool=true, strip_macros::Bool=false, kwargs...) +function tsn_and_mappings(mi::MethodInstance, src::CodeInfo, @nospecialize(rt); warn::Bool=true, strip_macros::Bool=false, kwargs...) + m = mi.def::Method def = definition(String, m) if isnothing(def) warn && @warn "couldn't retrieve source of $m" return nothing, nothing end - return tsn_and_mappings(m, src, rt, def...; warn, strip_macros, kwargs...) + return tsn_and_mappings(mi, src, rt, def...; warn, strip_macros, kwargs...) end -function tsn_and_mappings(m::Method, src::CodeInfo, @nospecialize(rt), sourcetext::AbstractString, lineno::Integer; warn::Bool=true, strip_macros::Bool=false, kwargs...) +function tsn_and_mappings(mi::MethodInstance, src::CodeInfo, @nospecialize(rt), sourcetext::AbstractString, lineno::Integer; warn::Bool=true, strip_macros::Bool=false, kwargs...) + m = mi.def::Method filename = isnothing(functionloc(m)[1]) ? string(m.file) : functionloc(m)[1] rootnode = JuliaSyntax.parsestmt(SyntaxNode, sourcetext; filename=filename, first_line=lineno, kwargs...) if strip_macros @@ -50,7 +52,7 @@ function tsn_and_mappings(m::Method, src::CodeInfo, @nospecialize(rt), sourcetex end end Δline = lineno - m.line # offset from original line number (Revise) - mappings, symtyps = map_ssas_to_source(src, rootnode, Δline) + mappings, symtyps = map_ssas_to_source(src, mi, rootnode, Δline) node = TypedSyntaxNode(rootnode, src, mappings, symtyps) node.typ = rt return node, mappings @@ -59,9 +61,8 @@ end TypedSyntaxNode(@nospecialize(f), @nospecialize(t); kwargs...) = tsn_and_mappings(f, t; kwargs...)[1] function TypedSyntaxNode(mi::MethodInstance; kwargs...) - m = mi.def::Method src, rt = getsrc(mi) - tsn_and_mappings(m, src, rt; kwargs...)[1] + tsn_and_mappings(mi, src, rt; kwargs...)[1] end TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, Δline::Integer=0) = @@ -397,8 +398,7 @@ end # Main logic for mapping `src.code[i]` to node(s) in the SyntaxNode tree # Success: when we map it to a unique node # Δline is the (Revise) offset of the line number -function map_ssas_to_source(src::CodeInfo, rootnode::SyntaxNode, Δline::Int) - mi = src.parent::MethodInstance +function map_ssas_to_source(src::CodeInfo, mi::MethodInstance, rootnode::SyntaxNode, Δline::Int) slottypes = src.slottypes::Union{Nothing, Vector{Any}} have_slottypes = slottypes !== nothing ssavaluetypes = src.ssavaluetypes::Vector{Any} @@ -736,7 +736,7 @@ function map_ssas_to_source(src::CodeInfo, rootnode::SyntaxNode, Δline::Int) end return mappings, symtyps end -map_ssas_to_source(src::CodeInfo, rootnode::SyntaxNode, Δline::Integer) = map_ssas_to_source(src, rootnode, Int(Δline)) +map_ssas_to_source(src::CodeInfo, mi::MethodInstance, rootnode::SyntaxNode, Δline::Integer) = map_ssas_to_source(src, mi, rootnode, Int(Δline)) function follow_back(src, arg) # Follow SSAValue backward to see if it maps back to a slot diff --git a/TypedSyntax/test/exhaustive.jl b/TypedSyntax/test/exhaustive.jl index f81c8c3d..dfbc7a6e 100644 --- a/TypedSyntax/test/exhaustive.jl +++ b/TypedSyntax/test/exhaustive.jl @@ -26,7 +26,7 @@ const goodmis = Core.MethodInstance[] continue end try - tsn, _ = TypedSyntax.tsn_and_mappings(m, src, rt, ret...; warn=false) + tsn, _ = TypedSyntax.tsn_and_mappings(mi, src, rt, ret...; warn=false) @test isa(tsn, TypedSyntaxNode) push!(goodmis, mi) catch diff --git a/src/codeview.jl b/src/codeview.jl index 45644694..03ffb008 100644 --- a/src/codeview.jl +++ b/src/codeview.jl @@ -182,7 +182,6 @@ function cthulhu_typed(io::IO, debuginfo::Symbol, # we're working on pre-optimization state, need to ignore `LimitedAccuracy` src = copy(src) src.ssavaluetypes = mapany(ignorelimited, src.ssavaluetypes::Vector{Any}) - src.rettype = ignorelimited(src.rettype) if src.slotnames !== nothing slotnames = Base.sourceinfo_slotnames(src) diff --git a/src/interpreter.jl b/src/interpreter.jl index efa27aac..b6b796f2 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -126,10 +126,17 @@ function create_cthulhu_source(@nospecialize(opt), effects::Effects) return OptimizedSource(ir, opt.src, opt.src.inlineable, effects) end +@static if VERSION ≥ v"1.12.0-DEV.15" +function CC.transform_result_for_cache(interp::CthulhuInterpreter, + linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult, can_discard_trees::Bool=false) + return create_cthulhu_source(result.src, result.ipo_effects) +end +else function CC.transform_result_for_cache(interp::CthulhuInterpreter, linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult) return create_cthulhu_source(result.src, result.ipo_effects) end +end @static if VERSION ≥ v"1.11.0-DEV.879" function CC.inlining_policy(interp::CthulhuInterpreter, @@ -139,6 +146,7 @@ function CC.inlining_policy(interp::CthulhuInterpreter, return src.ir end else + @show typeof(src) @assert src isa CC.IRCode || src === nothing "invalid Cthulhu code cache" # the default inlining policy may try additional effor to find the source in a local cache return @invoke CC.inlining_policy(interp::AbstractInterpreter, @@ -176,7 +184,7 @@ function CC.IRInterpretationState(interp::CthulhuInterpreter, src = inferred.src method_info = CC.MethodInfo(src) return CC.IRInterpretationState(interp, method_info, ir, mi, argtypes, world, - src.min_world, src.max_world) + code.min_world, code.max_world) end @static if VERSION ≥ v"1.11.0-DEV.737" diff --git a/src/reflection.jl b/src/reflection.jl index 16ca9b62..33d40fc9 100644 --- a/src/reflection.jl +++ b/src/reflection.jl @@ -350,9 +350,8 @@ function add_sourceline!(locs, CI, stmtidx::Int) end function get_typed_sourcetext(mi::MethodInstance, src::CodeInfo, @nospecialize(rt); warn::Bool=true) - meth = mi.def::Method - tsn, mappings = TypedSyntax.tsn_and_mappings(meth, src, rt; warn, strip_macros=true) - return truncate_if_defaultargs!(tsn, mappings, meth) + tsn, mappings = TypedSyntax.tsn_and_mappings(mi, src, rt; warn, strip_macros=true) + return truncate_if_defaultargs!(tsn, mappings, mi.def::Method) end function get_typed_sourcetext(mi::MethodInstance, ::IRCode, @nospecialize(rt); kwargs...)