Skip to content

Commit

Permalink
WIP: Adjust to julia#53219
Browse files Browse the repository at this point in the history
These are the changes I was using locally when developing that branch
to have basic Cthulhu functionality. It's likely additional changes
are required to have Cthulhu fully functional.
  • Loading branch information
Keno committed Feb 19, 2024
1 parent 864002c commit c4edb90
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
18 changes: 9 additions & 9 deletions TypedSyntax/src/node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) =
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TypedSyntax/test/exhaustive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions src/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit c4edb90

Please sign in to comment.