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

improve inferrabilities by telling the compiler relational invariants #40594

Merged
merged 1 commit into from
Apr 26, 2021

Conversation

aviatesk
Copy link
Sponsor Member

Our compiler doesn't understand these relations automatically yet.

Copy link
Member

@simeonschaub simeonschaub left a comment

Choose a reason for hiding this comment

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

Could you perhaps add a short comment to each of these functions explaining that this is because we don't do relational analysis yet, just because the code does look a bit odd at first glance? Also, is there any way to test this by checking the inferred return types?

Comment on lines 132 to 133
@assert isa(f2, Tuple{})
_isless(newret, f1, f2)
Copy link
Member

Choose a reason for hiding this comment

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

Is @assert the right tool for these kind of hints? We have been talking about eliminating them unless in debug mode, so perhaps something like

Suggested change
@assert isa(f2, Tuple{})
_isless(newret, f1, f2)
_isless(newret, f1, f2::Tuple{})

would be better?

Copy link
Sponsor Member Author

@aviatesk aviatesk Apr 25, 2021

Choose a reason for hiding this comment

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

Thanks for your comment.
My original motivation to use @assert instead of typeassert was that otherwise JET will report false positive errors on the typeassertions for some complicated cases where inference is unsure about the type of one tuple but sure about that of the other, e.g.:

example false positive reports

under the definition of _isless below

    @inline function _isless(ret, I1::NTuple{N,Int}, I2::NTuple{N,Int}) where N
        newret = ifelse(ret==0, icmp(I1[N], I2[N]), ret)
        f1, f2 = Base.front(I1), Base.front(I2)
        if isa(f1, Tuple{})
            return _isless(newret, f1, f2::Tuple{}) # L133
        else
            return _isless(newret, f1, f2::Tuple{Int,Vararg{Int}}) # L135
        end
    end
julia> report_call((Int, Tuple{Vararg{Int}}, Tuple{Int,Int,Vararg{Int}})) do ret, t1, t2
           Base.IteratorsMD._isless(ret, t1, t2)
       end
═════ 2 possible errors found ═════
┌ @ none:2 Base.getproperty(Base.IteratorsMD, :_isless)(ret, t1, t2)
│┌ @ /Users/aviatesk/julia/julia/base/multidimensional.jl:133 Core.typeassert(f2, Core.apply_type(Base.IteratorsMD.Tuple))
││ invalid builtin function call: Core.typeassert(f2::Union{Tuple{Int64}, Tuple{Int64, Int64, Vararg{Int64}}}, Core.apply_type(Base.IteratorsMD.Tuple)::Type{Tuple{}})
│└────────────────────────────────────────────────────────────
│┌ @ /Users/aviatesk/julia/julia/base/multidimensional.jl:135 Base.IteratorsMD._isless(newret, f1, Core.typeassert(f2, Core.apply_type(Base.IteratorsMD.Tuple, Base.IteratorsMD.Int, Core.apply_type(Base.IteratorsMD.Vararg, Base.IteratorsMD.Int))))
││┌ @ /Users/aviatesk/julia/julia/base/multidimensional.jl:135 Core.typeassert(f2, Core.apply_type(Base.IteratorsMD.Tuple, Base.IteratorsMD.Int, Core.apply_type(Base.IteratorsMD.Vararg, Base.IteratorsMD.Int)))
│││ invalid builtin function call: Core.typeassert(f2::Tuple{}, Core.apply_type(Base.IteratorsMD.Tuple, Base.IteratorsMD.Int, Core.apply_type(Base.IteratorsMD.Vararg, Base.IteratorsMD.Int)::Core.TypeofVararg)::Type{Tuple{Int64, Vararg{Int64}}})
││└────────────────────────────────────────────────────────────

and with @assert JET can forcibly exclude those cases (but yes, this is just because JET's bug report criteria is somewhat looser on manually thrown exceptions than type failures...)


Anyway, I totally agree that @assert is very unnatural to be here, and there is no strong reason to make changes just for the sake of JET.
In the latest commit (bb0bff9) I focused on eliminating dynamic dispatches by adding type annotations (and also exclude some possibilities for JET to report false positives).
Hopefully in this direction we can agree with taking these changes in ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, your version looks reasonable to me.

@aviatesk aviatesk force-pushed the relational branch 5 times, most recently from ca13b0d to bb0bff9 Compare April 25, 2021 13:07
Our compiler doesn't understand these relations automatically yet.
Copy link
Member

@simeonschaub simeonschaub left a comment

Choose a reason for hiding this comment

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

LGTM.

On a related note: Did you ever think about running some of JET's analysis on base as part of CI? Making it a required check is probably difficult, since JET would always have to be updated with internal base changes right away, but perhaps it could run as a scheduled Job, similar to PkgEval and BaseBenchmarks? I am a bit worried that now that we are getting quite careful when it comes to inference and invalidations in base, it will also be really easy to accidentally introduce regressions in that area. @timholy might have some thoughts on this as well.

@simeonschaub simeonschaub added the compiler:inference Type inference label Apr 26, 2021
@JeffBezanson JeffBezanson merged commit a4e1082 into JuliaLang:master Apr 26, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
…JuliaLang#40594)

Our compiler doesn't understand these relations automatically yet.
jarlebring pushed a commit to jarlebring/julia that referenced this pull request May 4, 2021
…JuliaLang#40594)

Our compiler doesn't understand these relations automatically yet.
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
…JuliaLang#40594)

Our compiler doesn't understand these relations automatically yet.
@aviatesk aviatesk deleted the relational branch May 31, 2021 11:12
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
…JuliaLang#40594)

Our compiler doesn't understand these relations automatically yet.
martinholters added a commit that referenced this pull request Oct 19, 2021
...by removing unnecessary type checks/assertions. This basically
restores the function bodies to what they looked like prior to #40594
and only keeps the changed signatures (with the additional changes to
circumvent #39088..
KristofferC pushed a commit that referenced this pull request Oct 19, 2021
...by removing unnecessary type checks/assertions. This basically
restores the function bodies to what they looked like prior to #40594
and only keeps the changed signatures (with the additional changes to
circumvent #39088.
KristofferC pushed a commit that referenced this pull request Oct 19, 2021
...by removing unnecessary type checks/assertions. This basically
restores the function bodies to what they looked like prior to #40594
and only keeps the changed signatures (with the additional changes to
circumvent #39088.

(cherry picked from commit df81a0d)
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
...by removing unnecessary type checks/assertions. This basically
restores the function bodies to what they looked like prior to JuliaLang#40594
and only keeps the changed signatures (with the additional changes to
circumvent JuliaLang#39088.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
...by removing unnecessary type checks/assertions. This basically
restores the function bodies to what they looked like prior to JuliaLang#40594
and only keeps the changed signatures (with the additional changes to
circumvent JuliaLang#39088.
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.

3 participants