Skip to content

Commit

Permalink
wip: use InternalCodeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Feb 12, 2024
1 parent 284f4c8 commit 7ec7d51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
23 changes: 16 additions & 7 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end

const InferenceKey = Union{MethodInstance,InferenceResult}
const InferenceDict{T} = IdDict{InferenceKey, T}
const OptimizationDict = IdDict{MethodInstance, CodeInstance}
const OptimizationDict = @static VERSION v"1.11.0-DEV.1552" ? Nothing : IdDict{MethodInstance, CodeInstance}
const PC2Remarks = Vector{Pair{Int, String}}
const PC2Effects = Dict{Int, Effects}
const PC2Excts = Dict{Int, Any}
Expand Down Expand Up @@ -65,26 +65,35 @@ end
#=CC.=#get_inference_world(interp::CthulhuInterpreter) = get_inference_world(interp.native)
CC.get_inference_cache(interp::CthulhuInterpreter) = get_inference_cache(interp.native)

# No need to do any locking since we're not putting our results into the runtime cache
CC.lock_mi_inference(interp::CthulhuInterpreter, mi::MethodInstance) = nothing
CC.unlock_mi_inference(interp::CthulhuInterpreter, mi::MethodInstance) = nothing
CC.method_table(interp::CthulhuInterpreter) = method_table(interp.native)

if isdefined(CC, :cache_owner)
@static if VERSION v"1.11.0-DEV.1552"
struct CthulhuCacheToken
token
end
CC.cache_owner(interp::CthulhuInterpreter) = CthulhuCacheToken(CC.cache_owner(interp.native))
struct CodeCacheView{CodeCache}
code_cache::CodeCache
end

CodeCacheView(interp::CthulhuInterpreter) = CodeCacheView(CC.code_cache(interp))
function Base.getproperty(interp::CthulhuInterpreter, name::Symbol)
if name === :opt
return CodeCacheView(interp)
end
return getfield(interp, name)
end
Base.get(view::CodeCacheView, mi::MethodInstance, default) = CC.get(view.code_cache, mi, default)
Base.getindex(view::CodeCacheView, mi::MethodInstance) = CC.getindex(view.code_cache, mi)
Base.haskey(view::CodeCacheView, mi::MethodInstance) = CC.haskey(view.code_cache, mi)
else
struct CthulhuCache
cache::OptimizationDict
end

CC.code_cache(interp::CthulhuInterpreter) = WorldView(CthulhuCache(interp.opt), WorldRange(get_inference_world(interp)))
CC.get(wvc::WorldView{CthulhuCache}, mi::MethodInstance, default) = get(wvc.cache.cache, mi, default)
CC.haskey(wvc::WorldView{CthulhuCache}, mi::MethodInstance) = haskey(wvc.cache.cache, mi)
CC.setindex!(wvc::WorldView{CthulhuCache}, ci::CodeInstance, mi::MethodInstance) = setindex!(wvc.cache.cache, ci, mi)
end

CC.may_optimize(interp::CthulhuInterpreter) = true
CC.may_compress(interp::CthulhuInterpreter) = false
Expand Down
13 changes: 8 additions & 5 deletions test/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
using ..VSCodeServer
end

function cthulhu_info(@nospecialize(f), @nospecialize(TT=()); optimize=true)
(interp, mi) = Cthulhu.mkinterp(Core.Compiler.NativeInterpreter(), f, TT)
(; src, rt, exct, infos, slottypes, effects) = Cthulhu.lookup(interp, mi, optimize; allow_no_src=true)
function cthulhu_info(@nospecialize(f), @nospecialize(tt=());
optimize=true, interp=Core.Compiler.NativeInterpreter())
(interp, mi) = Cthulhu.mkinterp(f, tt; interp)
(; src, rt, exct, infos, slottypes, effects) =
Cthulhu.lookup(interp, mi, optimize; allow_no_src=true)
if src !== nothing
src = Cthulhu.preprocess_ci!(src, mi, optimize, Cthulhu.CthulhuConfig(dead_code_elimination=true))
config = Cthulhu.CthulhuConfig(; dead_code_elimination=true)
src = Cthulhu.preprocess_ci!(src, mi, optimize, config)
end
(; interp, src, infos, mi, rt, exct, slottypes, effects)
return (; interp, src, infos, mi, rt, exct, slottypes, effects)
end

function find_callsites_by_ftt(@nospecialize(f), @nospecialize(TT=Tuple{}); optimize=true)
Expand Down
12 changes: 3 additions & 9 deletions test/test_Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ end
end
t[1]
end
@test length(callsites) == 1 # getindex(::Union{Vector{Any}, Const(tuple(1,nothing))}, ::Const(1))
@test length(callsites) == 1 # getindex(::Union{Vector{Any}, Const(tuple(1,nothing))}, ::Const(1))
callinfo = callsites[1].info
@test isa(callinfo, Cthulhu.MultiCallInfo)
callinfos = callinfo.callinfos
@test length(callinfos) == 2
@test count(ci->isa(ci, Cthulhu.MICallInfo), callinfos) == 1 # getindex(::Vector{Any}, ::Const(1))
@test count(ci->isa(ci, Cthulhu.MICallInfo), callinfos) == 1 # getindex(::Vector{Any}, ::Const(1))
@test count(ci->isa(ci, Cthulhu.ConstPropCallInfo) || isa(ci, Cthulhu.SemiConcreteCallInfo), callinfos) == 1 # getindex(::Const(tuple(1,nothing)), ::Const(1))
end

Expand Down Expand Up @@ -1015,13 +1015,7 @@ end
let (interp, mi) = Cthulhu.mkinterp((Int,)) do var::Int
countvars50037(1, var)
end
key = nothing
for (mi, codeinst) in interp.opt
if mi.def.name === :countvars50037
key = mi
break
end
end
key = only(Base.specializations(only(methods(countvars50037))))
codeinst = interp.opt[key]
inferred = @atomic :monotonic codeinst.inferred
@test length(inferred.ir.cfg.blocks) == 1
Expand Down

0 comments on commit 7ec7d51

Please sign in to comment.