Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust to JuliaLang/julia#53088 #539

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
74 changes: 46 additions & 28 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 All @@ -43,8 +60,9 @@ end
import Base.Experimental: @MethodTable, @overlay

@newinterp MTOverlayInterp
@MethodTable(OverlayedMT)
CC.method_table(interp::MTOverlayInterp) = CC.OverlayMethodTable(CC.get_world_counter(interp), OverlayedMT)
@MethodTable OverlayedMT
CC.method_table(interp::MTOverlayInterp) =
CC.OverlayMethodTable(Cthulhu.get_inference_world(interp), OverlayedMT)
@overlay OverlayedMT sin(x::Float64) = 1

@testset "OverlayMethodTable integration" begin
Expand Down
2 changes: 1 addition & 1 deletion test/test_codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Revise.track(TestCodeViewSandbox, normpath(@__DIR__, "TestCodeViewSandbox.jl"))
config = Cthulhu.CONFIG

io = IOBuffer()
codeview(io, mi, optimize, debuginfo, Cthulhu.get_world_counter(interp), config)
codeview(io, mi, optimize, debuginfo, Cthulhu.get_inference_world(interp), config)
@test !isempty(String(take!(io))) # just check it works
end
end
Expand Down
Loading