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

Sneaky bait and switch in lowered code #21281

Closed
KristofferC opened this issue Apr 5, 2017 · 2 comments
Closed

Sneaky bait and switch in lowered code #21281

KristofferC opened this issue Apr 5, 2017 · 2 comments
Labels
regression Regression in behavior compared to a previous version

Comments

@KristofferC
Copy link
Sponsor Member

KristofferC commented Apr 5, 2017

Consider:

type Q{T}
    x::T
end

Q() = Q(0)

function f(qs)
    for i in 1:5
        q = qs[i]
        q.x = i
    end
end

qss = [Q() for i in 1:5, j in 1:5]
@code_warntype f(view(qss, :, 1))

Looking at the result we see:

Variables:
  #self#::#f
  qs::SubArray{Q{Int64},1,Array{Q{Int64},2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}
  i::Int64
  q::Any
  #temp#::Int64
  r::Q{Int64}

This leads us to believe that q is inferrred to Any and that we have a type instability. However, looking more carefully, we find that q is actually not used in the function body. Instead a new variable, r is introduced which takes the place of q. This one is correctly inferred to be of type Q{Int64}.

On 0.5 the result is instead:

Variables:
  #self#
::#f
  qs::SubArray{Q{Int64},1,Array{Q{Int64},2},Tuple{Colon,Int64},true}
  #temp#::Int64
  i::Int64
  q::Q{Int64}
  r::Q{Int64}

which infers q correctly.

This probably does not have an impact on performance but makes @code_warntype much harder to use since you can't trust your variable names anymore.

@KristofferC KristofferC added the regression Regression in behavior compared to a previous version label Apr 5, 2017
@KristofferC KristofferC changed the title Sneaky bait and switch by @code_warntype Sneaky bait and switch in lowered code Apr 5, 2017
@KristofferC
Copy link
Sponsor Member Author

KristofferC commented Apr 5, 2017

Probably related (below, unused y is inferred as Any, on 0.5 inferred as Array{Int64, 1}):

julia> z{T}(x::T, y::Vector{T}) = x
z (generic function with 1 method)

julia> @code_warntype z(3, [1,2])
Variables:
  #self#::#z
  x::Int64
  y::Any

Body:
  begin 
      return x::Int64
  end::Int64

@yuyichao
Copy link
Contributor

yuyichao commented Apr 5, 2017

Dup of #20801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Regression in behavior compared to a previous version
Projects
None yet
Development

No branches or pull requests

2 participants