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

inference: improve TypeVar/Vararg handling #42583

Merged
merged 2 commits into from
Oct 16, 2021
Merged

Conversation

aviatesk
Copy link
Sponsor Member

@aviatesk aviatesk commented Oct 10, 2021

During working on the incoming lattice overhaul, I found it's quite
confusing that TypeVar and Vararg can appear in the same context
as valid Type objects as well as extended lattice elements.
Since it usually needs special cases to operate on TypeVar and Vararg
(e.g. they can not be used in subtyping), I believe it would be helpful
to avoid bugs and catch logic errors in the future development if we
separate contexts where they can appear from ones where Type
objects and extended lattice elements are expected.

So this PR:

  • tries to separate their context, e.g. now TypeVar and Vararg should
    not be used in _limit_type_size, which is supposed to return Type,
    but they should be handled by its helper function __limit_type_size
  • makes sure tfuncs don't return TypeVars and TypeVar never spills
    into the abstract state
  • makes sure widenconst are not called on TypeVar and Vararg,
    and now widenconst is ensured to return Type always
  • and other general refactors

@aviatesk aviatesk requested review from Keno and vtjnash October 10, 2021 18:40
@aviatesk aviatesk added the compiler:inference Type inference label Oct 10, 2021
@aviatesk aviatesk force-pushed the avi/validwidenconst branch 4 times, most recently from d058fe6 to 777b4b0 Compare October 11, 2021 05:47
@aviatesk aviatesk changed the title inference: make widenconst always return Type inference: improve handlings of TypeVar and Vararg Oct 11, 2021
Base automatically changed from avi/infrefactor to master October 11, 2021 15:38
@aviatesk aviatesk force-pushed the avi/validwidenconst branch 2 times, most recently from 874dd2d to d850968 Compare October 12, 2021 17:51
aviatesk added a commit that referenced this pull request Oct 12, 2021
base/compiler/tfuncs.jl Outdated Show resolved Hide resolved
base/compiler/typeinfer.jl Outdated Show resolved Hide resolved
base/compiler/typeutils.jl Outdated Show resolved Hide resolved
base/essentials.jl Outdated Show resolved Hide resolved
aviatesk added a commit that referenced this pull request Oct 13, 2021
aviatesk added a commit that referenced this pull request Oct 13, 2021
@aviatesk aviatesk closed this Oct 13, 2021
@aviatesk aviatesk reopened this Oct 13, 2021
@aviatesk
Copy link
Sponsor Member Author

Alright, so I think this is good to go ?
I may have missed some edge cases, but they should apprear as explicit errors and I'll try to be responsible to fix them.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Oct 13, 2021

yeah, just keep an eye on the next nightly pkgeval

@vtjnash vtjnash added the status:merge me PR is reviewed. Merge when all tests are passing label Oct 13, 2021
@KristofferC
Copy link
Sponsor Member

Or run PkgEval first?

@aviatesk
Copy link
Sponsor Member Author

@nanosoldier runtests(ALL, vs = ":master")

aviatesk added a commit to JuliaDebug/Cthulhu.jl that referenced this pull request Oct 15, 2021
After <JuliaLang/julia#42583> is merged,
`widenconst` doesn't accept non-valid Julia types (i.e. `TypeVar`/`Vararg`),
and we will handle them explicitly.
Type{Bound}, # DataType => Type{Bound} where Bound<:Integer
Type{unbound}, # DataType => Type
Vector{Some{Bound}}, # DataType => Vector{Some{Bound}} where Bound<:Integer
Vector{Some{unbound}}, # DataType => Array
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

You really went overboard here! Most of these cannot happen at runtime, since this would not declare a valid object (for example). Only wrap_Type (Type{T}) can contain an invalid parameter.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

So conceptually we want to replace invalid non-Types with Bottom ? It feels a bit weird to keep inference going when we know something never happens at runtime.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

No, it just shouldn't occur, so it is a lot of code and tests to handle a situation that can only be user-constructed as arguments to code_typed. Conceptually it only happens in the runtime for Type{T}::DataType, so the replacement could be just:
has_free_typevars(t) ? (isType(t) ? Type : Any) : t

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Okay, I got it.
Since I saw some unbound TypeVars during sysimg creation, I thought inference sometimes seemed to forward unbound TypeVars, but it shouldn't and so it might also be bad entry arguments given at somewhere (maybe return_type ?).
Anyway, I believe this PR is solid now so I'm going to merge this once CI gets passed.

@nanosoldier
Copy link
Collaborator

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

During working on the incoming lattice overhaul, I found it's quite
confusing that `TypeVar` and `Vararg` can appear in the same context
as valid `Type` objects as well as extended lattice elements.
Since it usually needs special cases to operate on `TypeVar` and `Vararg`
(e.g. they can not be used in subtyping as an obvious example), I believe
it would be great avoid bugs and catch logic errors in the future development
if we separate contexts where they can appear from ones where `Type`
objects and extended lattice elements are expected.

So this commit:
- tries to separate their context, e.g. now `TypeVar` and `Vararg` should
  not be used in `_limit_type_size`, which is supposed to return `Type`,
  but they should be handled its helper function `__limit_type_size`
- makes sure `tfunc`s don't return `TypeVar`s and `TypeVar` never spills
  into the abstract state
- makes sure `widenconst` are not called on `TypeVar` and `Vararg`,
  and now `widenconst` is ensured to return `Type` always
- and does other general refactors
@aviatesk aviatesk force-pushed the avi/validwidenconst branch 4 times, most recently from a62c695 to dd036ef Compare October 16, 2021 17:09
This commit eliminates unbound `TypeVar`s from `argtypes` in order to
make the analysis much easier down the road.

At runtime, only `Type{...}::DataType` can contain invalid type parameters,
but we may have arbitrary malformed user-constructed type arguments given
at inference entries.
So we will replace only the malformed `Type{...}::DataType` with `Type`
and simply replace other possibilities with `Any`.
The replacement might not be that accurate, but an unbound `TypeVar` is
really invalid in anyway.

Like the change added on `arrayref_tfunc`, now we may be able to remove
some `isa(x, TypeVar)`/`has_free_typevars` predicates used in various
places, but it is left as an exercise for the reader.
@aviatesk aviatesk merged commit 0ea61fa into master Oct 16, 2021
@aviatesk aviatesk deleted the avi/validwidenconst branch October 16, 2021 17:17
@DilumAluthge DilumAluthge removed the status:merge me PR is reviewed. Merge when all tests are passing label Oct 19, 2021
ianatol pushed a commit to ianatol/julia that referenced this pull request Nov 5, 2021
aviatesk added a commit that referenced this pull request Nov 6, 2021
Follows #42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RationalAI's test suite. I added the other two cases by
some code review.
aviatesk added a commit that referenced this pull request Nov 7, 2021
Follows #42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RationalAI's test suite. I added the other two cases by
some code review.
aviatesk added a commit that referenced this pull request Nov 7, 2021
#42971)

Follows #42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RerationalAI's test suite. I added the other two cases by
some code review.
KristofferC pushed a commit that referenced this pull request Nov 8, 2021
#42971)

Follows #42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RerationalAI's test suite. I added the other two cases by
some code review.

(cherry picked from commit 71d57d9)
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
JuliaLang#42971)

Follows JuliaLang#42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RerationalAI's test suite. I added the other two cases by
some code review.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
JuliaLang#42971)

Follows JuliaLang#42583.
The missing case within `const_prop_function_heuristic` was originally
reported from RerationalAI's test suite. I added the other two cases by
some code review.
aviatesk added a commit that referenced this pull request Sep 20, 2023
… computed by `abstract_apply` (#51393)

This commit adds special handling for `Vararg` types that may appear at
the end of `argtypes`, as computed by `abstract_apply`. Even though PR
#42583 has ensured that `Vararg` and `TypeVar` should never appear
within the abstract state, they can still be part of `argtypes`. As a
result, this kind of special handling is still necessary. It remains an
open question whether we can refactor `abstract_apply` to prevent
`Vararg`s from appearing in `argtypes` in the first place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants