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

Get better type info from partially generated functions #31025

Merged
merged 2 commits into from
Feb 13, 2019

Conversation

Keno
Copy link
Member

@Keno Keno commented Feb 10, 2019

Consider the following function:

julia> function foo(a, b)
           ntuple(i->(a+b; i), Val(4))
       end
foo (generic function with 1 method)

(In particular note that the return type of the closure does not depend on the types
of a and b`). Unfortunately, prior to this change, inference was unable to determine
the return type in this situation:

julia> code_typed(foo, Tuple{Any, Any}, trace=true)
Refused to call generated function with non-concrete argument types ntuple(::getfield(Main, Symbol("##15#16")){_A,_B} where _B where _A, ::Val{4}) [GeneratedNotConcrete]

1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = Main.:(##15#16)::Const(##15#16, false)
│   %2 = Core.typeof(a)::DataType
│   %3 = Core.typeof(b)::DataType
│   %4 = Core.apply_type(%1, %2, %3)::Type{##15#16{_A,_B}} where _B where _A
│   %5 = %new(%4, a, b)::##15#16{_A,_B} where _B where _A
│   %6 = Main.ntuple(%5, $(QuoteNode(Val{4}())))::Any
└──      return %6
) => Any

Looking at the definition of ntuple

julia/base/ntuple.jl

Lines 45 to 56 in abb09f8

@inline function ntuple(f::F, ::Val{N}) where {F,N}
N::Int
(N >= 0) || throw(ArgumentError(string("tuple length should be ≥0, got ", N)))
if @generated
quote
@nexprs $N i -> t_i = f(i)
@ncall $N tuple t
end
else
Tuple(f(i) for i = 1:N)
end
end

we see that it is a generated function an inference thus refuses to invoke it,
unless it can prove the concrete type of all arguments to the function. As
the above example illustrates, this restriction is more stringent than necessary.
It is true that we cannot invoke generated functions on arbitrary abstract
signatures (because we neither want to the user to have to be able to nor
do we trust that users are able to preverse monotonicity - i.e. that the return
type of the generated code will always be a subtype of the return type of a more
abstract signature).

However, if some piece of information is not used (the type of the passed function
in this case), there is no problem with calling the generated function (since
information that is unnused cannot possibly affect monotnicity).

This PR allows us to recognize pieces of information that are syntactically unused,
and call the generated functions, even if we do not have those pieces of information.

As a result, we are now able to infer the return type of the above function:

julia> code_typed(foo, Tuple{Any, Any})
1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = Main.:(##3#4)::Const(##3#4, false)
│   %2 = Core.typeof(a)::DataType
│   %3 = Core.typeof(b)::DataType
│   %4 = Core.apply_type(%1, %2, %3)::Type{##3#4{_A,_B}} where _B where _A
│   %5 = %new(%4, a, b)::##3#4{_A,_B} where _B where _A
│   %6 = Main.ntuple(%5, $(QuoteNode(Val{4}())))::NTuple{4,Int64}
└──      return %6
) => NTuple{4,Int64}

In particular, we use the new frontent used flags from the previous commit.
One additional complication is that we want to accesss these flags without
uncompressing the generator source, so we change the compression scheme to
place the flags at a known location.

Fixes #31004

@JeffBezanson
Copy link
Member

I found a crash here --- jl_code_for_staged assumes linfo->specTypes is a tuple type, but now it can be a UnionAll type.

@Keno
Copy link
Member Author

Keno commented Feb 11, 2019 via email

Consider the following function:
```
julia> function foo(a, b)
           ntuple(i->(a+b; i), Val(4))
       end
foo (generic function with 1 method)
```

(In particular note that the return type of the closure does not depend on the types
of `a` and b`). Unfortunately, prior to this change, inference was unable to determine
the return type in this situation:

```
julia> code_typed(foo, Tuple{Any, Any}, trace=true)
Refused to call generated function with non-concrete argument types ntuple(::getfield(Main, Symbol("##15#16")){_A,_B} where _B where _A, ::Val{4}) [GeneratedNotConcrete]

1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = Main.:(##15#16)::Const(##15#16, false)
│   %2 = Core.typeof(a)::DataType
│   %3 = Core.typeof(b)::DataType
│   %4 = Core.apply_type(%1, %2, %3)::Type{##15#16{_A,_B}} where _B where _A
│   %5 = %new(%4, a, b)::##15#16{_A,_B} where _B where _A
│   %6 = Main.ntuple(%5, $(QuoteNode(Val{4}())))::Any
└──      return %6
) => Any
```

Looking at the definition of ntuple

https://github.com/JuliaLang/julia/blob/abb09f88804c4e74c752a66157e767c9b0f8945d/base/ntuple.jl#L45-L56

we see that it is a generated function an inference thus refuses to invoke it,
unless it can prove the concrete type of *all* arguments to the function. As
the above example illustrates, this restriction is more stringent than necessary.
It is true that we cannot invoke generated functions on arbitrary abstract
signatures (because we neither want to the user to have to be able to nor
do we trust that users are able to preverse monotonicity - i.e. that the return
type of the generated code will always be a subtype of the return type of a more
abstract signature).

However, if some piece of information is not used (the type of the passed function
in this case), there is no problem with calling the generated function (since
information that is unnused cannot possibly affect monotnicity).

This PR allows us to recognize pieces of information that are *syntactically* unused,
and call the generated functions, even if we do not have those pieces of information.

As a result, we are now able to infer the return type of the above function:
```
julia> code_typed(foo, Tuple{Any, Any})
1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = Main.:(##3#4)::Const(##3#4, false)
│   %2 = Core.typeof(a)::DataType
│   %3 = Core.typeof(b)::DataType
│   %4 = Core.apply_type(%1, %2, %3)::Type{##3#4{_A,_B}} where _B where _A
│   %5 = %new(%4, a, b)::##3#4{_A,_B} where _B where _A
│   %6 = Main.ntuple(%5, $(QuoteNode(Val{4}())))::NTuple{4,Int64}
└──      return %6
) => NTuple{4,Int64}
```

In particular, we use the new frontent `used` flags from the previous commit.
One additional complication is that we want to accesss these flags without
uncompressing the generator source, so we change the compression scheme to
place the flags at a known location.

Fixes #31004
@Keno Keno requested a review from vtjnash February 11, 2019 23:57
@Keno Keno merged commit 38d6247 into master Feb 13, 2019
@mbauman mbauman deleted the kf/partialgenerator branch February 13, 2019 21:30
generator_mt = typeof(method.generator.gen).name.mt
length(generator_mt) == 1 || return false

generator_method = first(MethodList(generator_mt))
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it's introducing a bit of excess design debt, making it harder to fix #14919. The mt field only exists as a implementation detail, and you shouldn't depend on it being meaning for anything. Also first(MethodList(generator_mt)) seems like a clear design smell...

Copy link
Member Author

Choose a reason for hiding this comment

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

So just use methods from reflection.jl?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was concerned that that would try to do a method lookup that would be more expensive, than just doing this, which is O(1).

Copy link
Member

Choose a reason for hiding this comment

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

Let the runtime worry about that.

Copy link
Member Author

Choose a reason for hiding this comment

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

All right, will do.

KristofferC added a commit to KristofferC/ResumableFunctions.jl that referenced this pull request May 16, 2019
This package was noted to fail on the 1.2 branch of Julia. The usage of `func_for_method_checked` was changed in JuliaLang/julia#31025 to also take an `sparams` argument. The old usage was deprecated but the function `depwarn` is not available in `Core.Compiler`. Using the `Base` version of this function will show the deprecation message instead of hard errorring.
Keno added a commit that referenced this pull request Nov 29, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12
Keno added a commit that referenced this pull request Nov 29, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12
Keno added a commit that referenced this pull request Nov 29, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12
KristofferC pushed a commit that referenced this pull request Dec 8, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Dec 14, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Dec 14, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Dec 21, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Dec 21, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Dec 21, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
KristofferC pushed a commit that referenced this pull request Oct 10, 2023
This code was introduced by me back in #31025 to speed up evaluation
of generated functions that didn't make use of all of their arguments to
make generation decisions. However, it neglected to take into account the
possibility that the generator could be varargs. As a result, an unfortunate
coincidence of an unused slot in the correct position could have allowed
expansion of generators that were not supposed to be expandable. This can
cause incorrect inference with all the usual consequences. However, fortunately
this coincidence appears to be pretty rare.

Fixes JuliaDebug/CassetteOverlay.jl#12

(cherry picked from commit 328dd57)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants