Skip to content

Commit

Permalink
Standardize a few names
Browse files Browse the repository at this point in the history
"ci" might be a CthulhuInterpreter, CodeInfo, or CallInfo.
Use `interp` consistently for CthulhuInterpreter.

Also incorporates the test from #134.
  • Loading branch information
timholy committed Mar 10, 2021
1 parent 9938126 commit e11f9a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Shortcut for [`descend_code_typed`](@ref).
"""
const descend = descend_code_typed

descend(ci::CthulhuInterpreter, mi::MethodInstance; kwargs...) = _descend(ci, mi; iswarn=false, interruptexc=false, kwargs...)
descend(interp::CthulhuInterpreter, mi::MethodInstance; kwargs...) = _descend(interp, mi; iswarn=false, interruptexc=false, kwargs...)

function codeinst_rt(code::CodeInstance)
rettype = code.rettype
Expand Down Expand Up @@ -350,17 +350,17 @@ function do_typeinf!(interp, mi)
end

function mkinterp(@nospecialize(F), @nospecialize(TT))
ci = CthulhuInterpreter()
interp = CthulhuInterpreter()
sigt = Base.signature_type(F, TT)
match = Base._which(sigt)
mi = Core.Compiler.specialize_method(match)
do_typeinf!(ci, mi)
(ci, mi)
do_typeinf!(interp, mi)
(interp, mi)
end

function _descend(@nospecialize(F), @nospecialize(TT); params=current_params(), kwargs...)
(ci, mi) = mkinterp(F, TT)
_descend(ci, mi; params=params, kwargs...)
(interp, mi) = mkinterp(F, TT)
_descend(interp, mi; params=params, kwargs...)
end

descend_code_typed(b::Bookmark; kw...) =
Expand Down
32 changes: 16 additions & 16 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@ import Core.Compiler: InferenceParams, OptimizationParams, get_world_counter,
get_inference_cache, code_cache,
WorldView, lock_mi_inference, unlock_mi_inference, InferenceState

InferenceParams(ei::CthulhuInterpreter) = InferenceParams(ei.native)
OptimizationParams(ei::CthulhuInterpreter) = OptimizationParams(ei.native)
get_world_counter(ei::CthulhuInterpreter) = get_world_counter(ei.native)
get_inference_cache(ei::CthulhuInterpreter) = get_inference_cache(ei.native)
InferenceParams(interp::CthulhuInterpreter) = InferenceParams(interp.native)
OptimizationParams(interp::CthulhuInterpreter) = OptimizationParams(interp.native)
get_world_counter(interp::CthulhuInterpreter) = get_world_counter(interp.native)
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
lock_mi_inference(ei::CthulhuInterpreter, mi::MethodInstance) = nothing
unlock_mi_inference(ei::CthulhuInterpreter, mi::MethodInstance) = nothing
lock_mi_inference(interp::CthulhuInterpreter, mi::MethodInstance) = nothing
unlock_mi_inference(interp::CthulhuInterpreter, mi::MethodInstance) = nothing

code_cache(ei::CthulhuInterpreter) = ei.opt
code_cache(interp::CthulhuInterpreter) = interp.opt
Core.Compiler.get(a::Dict, b, c) = Base.get(a,b,c)
Core.Compiler.get(a::WorldView{<:Dict}, b, c) = Base.get(a.cache,b,c)
Core.Compiler.haskey(a::Dict, b) = Base.haskey(a, b)
Core.Compiler.haskey(a::WorldView{<:Dict}, b) =
Core.Compiler.haskey(a.cache, b)
Core.Compiler.setindex!(a::Dict, b, c) = setindex!(a, b, c)
Core.Compiler.may_optimize(ei::CthulhuInterpreter) = true
Core.Compiler.may_compress(ei::CthulhuInterpreter) = false
Core.Compiler.may_discard_trees(ei::CthulhuInterpreter) = false
Core.Compiler.verbose_stmt_info(ei::CthulhuInterpreter) = true
Core.Compiler.may_optimize(interp::CthulhuInterpreter) = true
Core.Compiler.may_compress(interp::CthulhuInterpreter) = false
Core.Compiler.may_discard_trees(interp::CthulhuInterpreter) = false
Core.Compiler.verbose_stmt_info(interp::CthulhuInterpreter) = true

function Core.Compiler.add_remark!(ei::CthulhuInterpreter, sv::InferenceState, msg)
push!(get!(ei.msgs, sv.linfo, Tuple{Int, String}[]),
function Core.Compiler.add_remark!(interp::CthulhuInterpreter, sv::InferenceState, msg)
push!(get!(interp.msgs, sv.linfo, Tuple{Int, String}[]),
sv.currpc => msg)
end

function Core.Compiler.finish(state::InferenceState, ei::CthulhuInterpreter)
r = invoke(Core.Compiler.finish, Tuple{InferenceState, AbstractInterpreter}, state, ei)
ei.unopt[Core.Compiler.any(state.result.overridden_by_const) ? state.result : state.linfo] = InferredSource(
function Core.Compiler.finish(state::InferenceState, interp::CthulhuInterpreter)
r = invoke(Core.Compiler.finish, Tuple{InferenceState, AbstractInterpreter}, state, interp)
interp.unopt[Core.Compiler.any(state.result.overridden_by_const) ? state.result : state.linfo] = InferredSource(
copy(isa(state.src, OptimizationState) ?
state.src.src : state.src),
copy(state.stmt_info),
Expand Down
11 changes: 6 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ mi = firstassigned(first(methods(unspecva)).specializations)
root = Cthulhu.treelist(mi)
@test occursin("Vararg", root.data.callstr)

# issue #132
f132(w, dim) = [i == dim ? w[i]/2 : w[i]/1 for i in eachindex(w)]
interp, mi = Cthulhu.mkinterp(f132, (Vector{Int}, Int))
@test isa(mi, Core.MethodInstance) # just check that the above succeeded

# treelist for stacktraces
fst1(x) = backtrace()
@inline fst2(x) = fst1(x)
Expand Down Expand Up @@ -316,11 +321,7 @@ end
###
### Printer test:
###
function foo()
T = rand() > 0.5 ? Int64 : Float64
sum(rand(T, 100))
end
ci, infos, mi, rt, slottypes = process(foo, Tuple{});
ci, infos, mi, rt, slottypes = process(test, Tuple{});
io = IOBuffer()
Cthulhu.cthulu_typed(io, :none, ci, rt, mi, true, false)
str = String(take!(io))
Expand Down

0 comments on commit e11f9a4

Please sign in to comment.