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

Support tagged CodeInstance #520

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 6 additions & 2 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,14 @@ get_effects(result::CC.SemiConcreteResult) = result.effects
end

function lookup_optimized(interp::CthulhuInterpreter, mi::MethodInstance, allow_no_src::Bool=false)
codeinst = interp.opt[mi]
# codeinst = interp.opt[mi]
codeinst = CC.getindex(CC.code_cache(interp), mi)
rt = cached_return_type(codeinst)
exct = cached_exception_type(codeinst)
opt = codeinst.inferred
opt = get(interp.opt, codeinst, nothing)
if opt === nothing
opt = codeinst.inferred
end
if opt !== nothing
opt = opt::OptimizedSource
src = CC.copy(opt.ir)
Expand Down
33 changes: 28 additions & 5 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ end

const InferenceKey = Union{MethodInstance,InferenceResult}
const InferenceDict{T} = IdDict{InferenceKey, T}
const OptimizationDict = IdDict{MethodInstance, CodeInstance}

# const OptimizationDict = IdDict{MethodInstance, CodeInstance}
const OptimizationDict = IdDict{CodeInstance, OptimizedSource}
const PC2Remarks = Vector{Pair{Int, String}}
const PC2Effects = Dict{Int, Effects}
const PC2Excts = Dict{Int, Any}
Expand Down Expand Up @@ -65,6 +67,13 @@ end
#=CC.=#get_inference_world(interp::CthulhuInterpreter) = get_inference_world(interp.native)
CC.get_inference_cache(interp::CthulhuInterpreter) = get_inference_cache(interp.native)

struct CthulhuCacheToken
native::Any
end
@static if isdefined(CC, :cache_owner)
CC.cache_owner(interp::CthulhuInterpreter) = CthulhuCacheToken(CC.cache_owner(interp.native))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could in theory also use the cache of the AbsInt we are wrapping, but that seems dangerous.

end

# 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
Expand All @@ -73,7 +82,9 @@ struct CthulhuCache
cache::OptimizationDict
end

CC.code_cache(interp::CthulhuInterpreter) = WorldView(CthulhuCache(interp.opt), WorldRange(get_inference_world(interp)))
@static if !isdefined(CC, :cache_owner)
CC.code_cache(interp::CthulhuInterpreter) = WorldView(CthulhuCache(interp.opt), WorldRange(get_inference_world(interp)))
end
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 Expand Up @@ -169,9 +180,21 @@ function create_cthulhu_source(@nospecialize(opt), effects::Effects)
return OptimizedSource(ir, opt.src, opt.src.inlineable, effects)
end

function CC.transform_result_for_cache(interp::CthulhuInterpreter,
linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult)
return create_cthulhu_source(result.src, result.ipo_effects)
# function CC.transform_result_for_cache(interp::CthulhuInterpreter,
# linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult)
# return create_cthulhu_source(result.src, result.ipo_effects)
# end



function CC.CodeInstance(interp::CthulhuInterpreter, result::InferenceResult,
valid_worlds::WorldRange)
opt_src = create_cthulhu_source(result.src, result.ipo_effects)
ci = @invoke CC.CodeInstance(interp::AbstractInterpreter, result, valid_worlds)
if opt_src isa OptimizedSource
interp.opt[ci] = opt_src
end
return ci
end

@static if VERSION ≥ v"1.11.0-DEV.879"
Expand Down
Loading