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

Standardize a few names #135

Merged
merged 1 commit into from
Mar 11, 2021
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
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
64 changes: 34 additions & 30 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,38 +239,46 @@ end
@test occursin("x\e[91m\e[1m::Any\e[22m\e[39m", str)
end

##
# backedges & treelist
##
# 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

## Functions for "backedges & treelist"
# The printing changes when the functions are defined inside the testset
fbackedge1() = 1
fbackedge2(x) = x > 0 ? fbackedge1() : -fbackedge1()
@test fbackedge2(0.2) == 1
@test fbackedge2(-0.2) == -1
mspec = @which(fbackedge1()).specializations
mi = isa(mspec, Core.SimpleVector) ? mspec[1] : mspec.func
root = Cthulhu.treelist(mi)
@test Cthulhu.count_open_leaves(root) == 2
@test root.data.callstr == "fbackedge1()"
@test root.children[1].data.callstr == " fbackedge2(::Float64)"

# issue #114
unspecva(@nospecialize(i::Int...)) = 1
@test unspecva(1, 2) == 1
mi = firstassigned(first(methods(unspecva)).specializations)
root = Cthulhu.treelist(mi)
@test occursin("Vararg", root.data.callstr)

# treelist for stacktraces
fst1(x) = backtrace()
@inline fst2(x) = fst1(x)
@noinline fst3(x) = fst2(x)
@inline fst4(x) = fst3(x)
fst5(x) = fst4(x)
tree = Cthulhu.treelist(fst5(1.0))
@test match(r"fst1 at .*:\d+ => fst2 at .*:\d+ => fst3\(::Float64\) at .*:\d+", tree.data.callstr) !== nothing
@test length(tree.children) == 1
child = tree.children[1]
@test match(r" fst4 at .*:\d+ => fst5\(::Float64\) at .*:\d+", child.data.callstr) !== nothing

@testset "backedges and treelist" begin
@test fbackedge2(0.2) == 1
@test fbackedge2(-0.2) == -1
mspec = @which(fbackedge1()).specializations
mi = isa(mspec, Core.SimpleVector) ? mspec[1] : mspec.func
root = Cthulhu.treelist(mi)
@test Cthulhu.count_open_leaves(root) == 2
@test root.data.callstr == "fbackedge1()"
@test root.children[1].data.callstr == " fbackedge2(::Float64)"

# issue #114
unspecva(@nospecialize(i::Int...)) = 1
@test unspecva(1, 2) == 1
mi = firstassigned(first(methods(unspecva)).specializations)
root = Cthulhu.treelist(mi)
@test occursin("Vararg", root.data.callstr)

# treelist for stacktraces
tree = Cthulhu.treelist(fst5(1.0))
@test match(r"fst1 at .*:\d+ => fst2 at .*:\d+ => fst3\(::Float64\) at .*:\d+", tree.data.callstr) !== nothing
@test length(tree.children) == 1
child = tree.children[1]
@test match(r" fst4 at .*:\d+ => fst5\(::Float64\) at .*:\d+", child.data.callstr) !== nothing
end


##
# Cthulhu config test
Expand Down Expand Up @@ -316,11 +324,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