Skip to content

Commit

Permalink
keep the external code cache design
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Feb 13, 2024
1 parent 7ec7d51 commit d4fb503
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
31 changes: 11 additions & 20 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 = @static VERSION v"1.11.0-DEV.1552" ? Nothing : IdDict{MethodInstance, CodeInstance}
const OptimizationDict = IdDict{MethodInstance, CodeInstance}
const PC2Remarks = Vector{Pair{Int, String}}
const PC2Effects = Dict{Int, Effects}
const PC2Excts = Dict{Int, Any}
Expand Down Expand Up @@ -65,40 +65,31 @@ end
#=CC.=#get_inference_world(interp::CthulhuInterpreter) = get_inference_world(interp.native)
CC.get_inference_cache(interp::CthulhuInterpreter) = get_inference_cache(interp.native)

CC.may_optimize(interp::CthulhuInterpreter) = true
CC.may_compress(interp::CthulhuInterpreter) = false
CC.may_discard_trees(interp::CthulhuInterpreter) = false
CC.verbose_stmt_info(interp::CthulhuInterpreter) = true

CC.method_table(interp::CthulhuInterpreter) = method_table(interp.native)

@static if VERSION v"1.11.0-DEV.1552"
# Since the cache for `CthulhuInterpreter` is volatile and does not involve with the
# internal code cache, technically, there's no requirement to supply `cache_owner` as an
# identifier for the internal code cache. However, the definition of `cache_owner` is
# necessary for utilizing the default `CodeInstance` constructor, define the overload here.
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
CC.may_discard_trees(interp::CthulhuInterpreter) = false
CC.verbose_stmt_info(interp::CthulhuInterpreter) = true

function CC.add_remark!(interp::CthulhuInterpreter, sv::InferenceState, msg)
key = CC.any(sv.result.overridden_by_const) ? sv.result : sv.linfo
Expand Down
4 changes: 2 additions & 2 deletions test/test_AbstractInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

const CC = Core.Compiler

if isdefined(CC, :cache_owner)
@static if VERSION v"1.11.0-DEV.1552"
macro newinterp(InterpName)
InterpCacheName = QuoteNode(Symbol(string(InterpName, "Cache")))
InterpName = esc(InterpName)
Expand Down Expand Up @@ -77,7 +77,7 @@ macro newinterp(InterpName)
$CC.setindex!(wvc::$CC.WorldView{$InterpCacheName}, ci::$C.CodeInstance, mi::$C.MethodInstance) = setindex!(wvc.cache.dict, ci, mi)
end
end
end # isdefined(CC, :cache_owner)
end # if VERSION ≥ v"1.11.0-DEV.1552"

@doc """
@newinterp NewInterpreter
Expand Down
4 changes: 4 additions & 0 deletions test/test_Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,11 @@ end
let (interp, mi) = Cthulhu.mkinterp((Int,)) do var::Int
countvars50037(1, var)
end
@static if VERSION v"1.10"
key = only(Base.specializations(only(methods(countvars50037))))
else
key = only(methods(countvars50037)).specializations[1]
end
codeinst = interp.opt[key]
inferred = @atomic :monotonic codeinst.inferred
@test length(inferred.ir.cfg.blocks) == 1
Expand Down

0 comments on commit d4fb503

Please sign in to comment.