Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Feb 8, 2024
1 parent 5411430 commit 16e55e2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
9 changes: 8 additions & 1 deletion src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
69 changes: 43 additions & 26 deletions test/test_AbstractInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 16e55e2

Please sign in to comment.