diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 46b98bdc..5954af91 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -20,6 +20,13 @@ const mapany = Base.mapany const ArgTypes = Vector{Any} +@static if VERSION ≥ v"1.11.0-DEV.1498" + import .CC: get_inference_world + using Base: get_world_counter +else + import .CC: get_world_counter, get_world_counter as get_inference_world +end + Base.@kwdef mutable struct CthulhuConfig enable_highlighter::Bool = false highlighter::Cmd = `pygmentize -l` @@ -728,7 +735,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs display_CI = true elseif toggle === :ast || toggle === :llvm || toggle === :native view_cmd = CODEVIEWS[toggle] - world = get_world_counter(interp) + world = get_inference_world(interp) println(iostream) view_cmd(iostream, mi, optimize, debuginfo, world, CONFIG) display_CI = false diff --git a/src/codeview.jl b/src/codeview.jl index a1ccd4dc..b7c987a7 100644 --- a/src/codeview.jl +++ b/src/codeview.jl @@ -447,7 +447,7 @@ const BOOKMARKS = Bookmark[] function Base.show( io::IO, ::MIME"text/plain", b::Bookmark; optimize::Bool=false, debuginfo::AnyDebugInfo=:none, iswarn::Bool=false, hide_type_stable::Bool=false) - world = get_world_counter(b.interp) + world = get_inference_world(b.interp) CI, rt = InteractiveUtils.code_typed(b; optimize) (; interp, mi) = b (; effects) = lookup(interp, mi, optimize) @@ -505,7 +505,7 @@ InteractiveUtils.code_llvm( b.mi, optimize, debuginfo === :source, - get_world_counter(b.interp), + get_inference_world(b.interp), config, dump_module, raw, @@ -526,7 +526,7 @@ InteractiveUtils.code_native( b.mi, optimize, debuginfo === :source, - get_world_counter(b.interp), + get_inference_world(b.interp), config, dump_module, raw, diff --git a/src/interpreter.jl b/src/interpreter.jl index 620fb619..8e93250c 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -50,7 +50,7 @@ function CthulhuInterpreter(interp::AbstractInterpreter=NativeInterpreter()) InferenceDict{PC2Excts}()) end -import .CC: InferenceParams, OptimizationParams, get_world_counter, +import .CC: InferenceParams, OptimizationParams, #=get_inference_world,=# get_inference_cache, code_cache, lock_mi_inference, unlock_mi_inference, method_table, inlining_policy using Base: @invoke @@ -62,7 +62,7 @@ CC.OptimizationParams(interp::CthulhuInterpreter) = else CC.OptimizationParams(interp::CthulhuInterpreter) = OptimizationParams(interp.native) end -CC.get_world_counter(interp::CthulhuInterpreter) = get_world_counter(interp.native) +#=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 @@ -73,7 +73,7 @@ struct CthulhuCache cache::OptimizationDict end -CC.code_cache(interp::CthulhuInterpreter) = WorldView(CthulhuCache(interp.opt), WorldRange(get_world_counter(interp))) +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) diff --git a/src/reflection.jl b/src/reflection.jl index eee04004..b498b549 100644 --- a/src/reflection.jl +++ b/src/reflection.jl @@ -87,7 +87,7 @@ function find_callsites(interp::AbstractInterpreter, CI::Union{Core.CodeInfo, IR func = args[6] ftype = widenconst(argextype(func, CI, sptypes, slottypes)) sig = Tuple{ftype} - callsite = Callsite(id, TaskCallInfo(callinfo(sig, nothing; world=get_world_counter(interp))), head) + callsite = Callsite(id, TaskCallInfo(callinfo(sig, nothing; world=get_inference_world(interp))), head) end end end @@ -99,7 +99,7 @@ function find_callsites(interp::AbstractInterpreter, CI::Union{Core.CodeInfo, IR mi = get_mi(info) meth = mi.def if isa(meth, Method) && nameof(meth.module) === :CUDAnative && meth.name === :cufunction - callsite = transform(Val(:CuFunction), callsite, c, CI, mi, slottypes; world=get_world_counter(interp)) + callsite = transform(Val(:CuFunction), callsite, c, CI, mi, slottypes; world=get_inference_world(interp)) end end diff --git a/test/test_AbstractInterpreter.jl b/test/test_AbstractInterpreter.jl index 04024942..12b17a6e 100644 --- a/test/test_AbstractInterpreter.jl +++ b/test/test_AbstractInterpreter.jl @@ -6,35 +6,52 @@ if isdefined(parentmodule(@__MODULE__), :VSCodeServer) end const CC = Core.Compiler -import Core: MethodInstance, CodeInstance -import .CC: WorldRange, WorldView, NativeInterpreter - -# define new `AbstractInterpreter` that satisfies the minimum interface requirements -# while managing its cache independently -macro newinterp(name) - cachename = gensym(string(name, "Cache")) - name = esc(name) + +""" + @newinterp NewInterpreter + +Defines new `NewInterpreter <: AbstractInterpreter` whose cache is separated +from the native code cache, satisfying the minimum interface requirements. +""" +macro newinterp(InterpName) + InterpCacheName = esc(Symbol(string(InterpName, "Cache"))) + InterpName = esc(InterpName) + C = Core + CC = Core.Compiler quote - struct $cachename - dict::IdDict{MethodInstance,CodeInstance} + struct $InterpCacheName + dict::IdDict{$C.MethodInstance,$C.CodeInstance} + end + $InterpCacheName() = $InterpCacheName(IdDict{$C.MethodInstance,$C.CodeInstance}()) + struct $InterpName <: $CC.AbstractInterpreter + meta # additional information + world::UInt + inf_params::$CC.InferenceParams + opt_params::$CC.OptimizationParams + inf_cache::Vector{$CC.InferenceResult} + code_cache::$InterpCacheName + function $InterpName(meta = nothing; + world::UInt = Base.get_world_counter(), + inf_params::$CC.InferenceParams = $CC.InferenceParams(), + opt_params::$CC.OptimizationParams = $CC.OptimizationParams(), + inf_cache::Vector{$CC.InferenceResult} = $CC.InferenceResult[], + code_cache::$InterpCacheName = $InterpCacheName()) + return new(meta, world, inf_params, opt_params, inf_cache, code_cache) + end end - struct $name <: CC.AbstractInterpreter - interp::CC.NativeInterpreter - cache::$cachename - $name(world = Base.get_world_counter(); - interp = CC.NativeInterpreter(world), - cache = $cachename(IdDict{MethodInstance,CodeInstance}()) - ) = new(interp, cache) + $CC.InferenceParams(interp::$InterpName) = interp.inf_params + $CC.OptimizationParams(interp::$InterpName) = interp.opt_params + @static if VERSION ≥ v"1.11.0-DEV.1498" + CC.get_inference_world(interp::$InterpName) = interp.world + else + CC.get_world_counter(interp::$InterpName) = interp.world end - CC.InferenceParams(interp::$name) = CC.InferenceParams(interp.interp) - CC.OptimizationParams(interp::$name) = CC.OptimizationParams(interp.interp) - CC.get_world_counter(interp::$name) = CC.get_world_counter(interp.interp) - CC.get_inference_cache(interp::$name) = CC.get_inference_cache(interp.interp) - CC.code_cache(interp::$name) = WorldView(interp.cache, WorldRange(CC.get_world_counter(interp))) - CC.get(wvc::WorldView{<:$cachename}, mi::MethodInstance, default) = get(wvc.cache.dict, mi, default) - CC.getindex(wvc::WorldView{<:$cachename}, mi::MethodInstance) = getindex(wvc.cache.dict, mi) - CC.haskey(wvc::WorldView{<:$cachename}, mi::MethodInstance) = haskey(wvc.cache.dict, mi) - CC.setindex!(wvc::WorldView{<:$cachename}, ci::CodeInstance, mi::MethodInstance) = setindex!(wvc.cache.dict, ci, mi) + $CC.get_inference_cache(interp::$InterpName) = interp.inf_cache + $CC.code_cache(interp::$InterpName) = $CC.WorldView(interp.code_cache, $CC.WorldRange(interp.world)) + $CC.get(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance, default) = get(wvc.cache.dict, mi, default) + $CC.getindex(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance) = getindex(wvc.cache.dict, mi) + $CC.haskey(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance) = haskey(wvc.cache.dict, mi) + $CC.setindex!(wvc::$CC.WorldView{$InterpCacheName}, ci::$C.CodeInstance, mi::$C.MethodInstance) = setindex!(wvc.cache.dict, ci, mi) end end