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

Move code_typed and return_types out of inference #11600

Merged
merged 1 commit into from
Jun 23, 2015
Merged
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
Move code_typed and return_types out of inference. Fix #11590
yuyichao committed Jun 21, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 336282aa33e2f943d0140961c28e85db843531ba
38 changes: 0 additions & 38 deletions base/inference.jl
Original file line number Diff line number Diff line change
@@ -3269,44 +3269,6 @@ function replace_getfield!(ast, e::ANY, tupname, vals, sv, i0)
end
end

function code_typed(f, types::ANY; optimize=true)
types = to_tuple_type(types)
code_typed(call, Tuple{isa(f,Type)?Type{f}:typeof(f), types.parameters...}, optimize=optimize)
end
function code_typed(f::Function, types::ANY; optimize=true)
types = to_tuple_type(types)
asts = []
for x in _methods(f,types,-1)
linfo = func_for_method(x[3],types,x[2])
if optimize
(tree, ty) = typeinf(linfo, x[1], x[2], linfo, true, true)
else
(tree, ty) = typeinf_uncached(linfo, x[1], x[2], optimize=false)
end
if !isa(tree,Expr)
push!(asts, ccall(:jl_uncompress_ast, Any, (Any,Any), linfo, tree))
else
push!(asts, tree)
end
end
asts
end

function return_types(f, types::ANY)
types = to_tuple_type(types)
return_types(call, Tuple{isa(f,Type)?Type{f}:typeof(f), types.parameters...})
end
function return_types(f::Function, types::ANY)
types = to_tuple_type(types)
rt = []
for x in _methods(f,types,-1)
linfo = func_for_method(x[3],types,x[2])
(tree, ty) = typeinf(linfo, x[1], x[2])
push!(rt, ty)
end
rt
end

#tfunc(f,t) = methods(f,t)[1].func.code.tfunc

ccall(:jl_set_typeinf_func, Void, (Any,), typeinf_ext)
Copy link
Member

Choose a reason for hiding this comment

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

It was using the wrong "call" function here. @JeffBezanson is it possible to express the notion of "the call function that would have been visible to the callee" in this function? (cough#11452cough)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, yeah. This is exactly why I felt so strange when I saw #11452 .....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although hopefully this is not blocking the PR. Since this is also the old behavior.

Copy link
Member

Choose a reason for hiding this comment

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

no, although it means you could also fix this by doing a one-liner change from call to Main.Base.call here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vtjnash Will that do it? This is in the Base module and the call should already be Base.call. No?

Copy link
Member

Choose a reason for hiding this comment

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

no, and that's the current behavior. This call refers to Core.Inference.call because of the file that it is in, not Main.Base.call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vtjnash Ahh. Maybe that's why it was broken before then. This used to be only defined in Core.Inference with a wrapper in Base. After moving it out of Inference, the call is pointing to the correct function now. (Note that reflection.jl is also included in sysimg.jl)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vtjnash Somehow I always thought you what you meant is that the code in this PR is having problem getting the correct call.

So should I do the one-liner (ok maybe two since there's also return_types) and experimenting with moving these out of Inference in another PR?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine to move these out with this PR, I just wanted to point out why this worked (more than just "seems to fix it") and see if @JeffBezanson could recommend any other improvement

Copy link
Member

Choose a reason for hiding this comment

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

One thing that can be done is to have the macros generate an escaped call symbol, in order to pass the call function from the calling context.

The general problem is a case like methods(call, Tuple{Any, ..., where you obviously can't look at the first argument to see what module call is from.

42 changes: 39 additions & 3 deletions base/reflection.jl
Original file line number Diff line number Diff line change
@@ -225,9 +225,45 @@ code_native(f::ANY, types::ANY) = code_native(STDOUT, f, types)
code_native(io::IO, f::ANY, t::ANY) =
code_native(io, call, tt_cons(isa(f, Type) ? Type{f} : typeof(f), t))

if isdefined(Core, :Inference) && not_int(is(current_module(), Core.Inference))
code_typed(args...;kwargs...) = Core.Inference.code_typed(args...;kwargs...)
return_types(args...;kwargs...) = Core.Inference.return_types(args...;kwargs...)
function code_typed(f::Function, types::ANY; optimize=true)
types = to_tuple_type(types)
asts = []
for x in _methods(f,types,-1)
linfo = Core.Inference.func_for_method(x[3],types,x[2])
if optimize
(tree, ty) = Core.Inference.typeinf(linfo, x[1], x[2], linfo,
true, true)
else
(tree, ty) = Core.Inference.typeinf_uncached(linfo, x[1], x[2],
optimize=false)
end
if !isa(tree, Expr)
push!(asts, ccall(:jl_uncompress_ast, Any, (Any,Any), linfo, tree))
else
push!(asts, tree)
end
end
asts
end

function code_typed(f, t::ANY; optimize=true)
code_typed(call, tt_cons(isa(f, Type) ? Type{f} : typeof(f), t),
optimize=optimize)
end

function return_types(f::Function, types::ANY)
types = to_tuple_type(types)
rt = []
for x in _methods(f,types,-1)
linfo = Core.Inference.func_for_method(x[3],types,x[2])
(tree, ty) = Core.Inference.typeinf(linfo, x[1], x[2])
push!(rt, ty)
end
rt
end

function return_types(f, t::ANY)
return_types(call, tt_cons(isa(f, Type) ? Type{f} : typeof(f), t))
end

function which(f::ANY, t::ANY)