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

fix inference of split_rest #47329

Merged
merged 2 commits into from
Oct 26, 2022
Merged

fix inference of split_rest #47329

merged 2 commits into from
Oct 26, 2022

Conversation

simeonschaub
Copy link
Member

fixes #47326

@simeonschaub simeonschaub added the compiler:inference Type inference label Oct 26, 2022
@jakobnissen
Copy link
Contributor

Just to copy a comment over from the other PR: I wonder if this aggressive constprop might be better to have directly in the lowering, if possible, such that it applies to all cases of Base.split_rest created using slurping. I don't know if it's feasible to do at the callsite though.

Copy link
Sponsor Member

@aviatesk aviatesk left a comment

Choose a reason for hiding this comment

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

Good catch!

@aviatesk
Copy link
Sponsor Member

Just to copy a comment over from the other PR: I wonder if this aggressive constprop might be better to have directly in the lowering, if possible, such that it applies to all cases of Base.split_rest created using slurping. I don't know if it's feasible to do at the callsite though.

We really can't generally transform Base.split_rest into code with some fixed length, as Julia's lowering pass doesn't known type information of object being slurped.

@jakobnissen
Copy link
Contributor

What I mean is - since it already lowers like below, would it be possible to somehow say "constprop the split_rest call, regardless of the type of args"? This requires no type information, I think?

julia> Meta.@lower function fun1(args...)
           head..., tail, t2 = args
           head
       end
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─      $(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─     return $(Expr(:method, :fun1))
)))
│        $(Expr(:method, :fun1))
│   %3 = Core.Typeof(fun1)
│   %4 = Core.apply_type(Vararg, Core.Any)
│   %5 = Core.svec(%3, %4)
│   %6 = Core.svec()
│   %7 = Core.svec(%5, %6, $(QuoteNode(:(#= REPL[15]:1 =#))))
│        $(Expr(:method, :fun1, :(%7), CodeInfo(
    @ REPL[15]:2 within `none`
1 ─ %1 = Base.split_rest(args, 2)
│        head = Core.getfield(%1, 1)
│   %3 = Core.getfield(%1, 2)
│   %4 = Base.indexed_iterate(%3, 1)
│        tail = Core.getfield(%4, 1)
│        @_3 = Core.getfield(%4, 2)
│   %7 = Base.indexed_iterate(%3, 2, @_3)
│        t2 = Core.getfield(%7, 1)
│   @ REPL[15]:3 within `none`
└──      return head
)))
└──      return fun1
))))

@aviatesk
Copy link
Sponsor Member

would it be possible to somehow say "constprop the split_rest call, regardless of the type of args"

So you basically want to annotate @constprop :aggressive fun1 automatically since it contains slurping, or force const-prop' for Base.split_rest calls whenever they appear? The second option might be reasonable, but we will need to implement callsite @constprop annotation support first.

@jakobnissen
Copy link
Contributor

I had the latter in mind, but don't know if it's technically feasible.

@simeonschaub
Copy link
Member Author

I'm not sure we actually want that, since I'd expect aggressive constant prop to only really be useful in the case of statically sized iterators. Seems better to only annotate this for specific methods where we know it's profitable.

What I would've hoped though is that inference's constant prop heuristic were able to deduct profitability of constant prop here. Perhaps the issue is that tuple slicing has a bunch of hard-coded if-statements, causing the code to be considered too complicated for this?

@JeffBezanson JeffBezanson added the status:merge me PR is reviewed. Merge when all tests are passing label Oct 26, 2022
@aviatesk aviatesk merged commit 19d06df into master Oct 26, 2022
@aviatesk aviatesk deleted the sds/constprop_split_rest branch October 26, 2022 22:44
@giordano giordano removed the status:merge me PR is reviewed. Merge when all tests are passing label Oct 27, 2022
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.

Type instability in front slurping of tuples.
5 participants