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

@code_warntype shows the following code is type unstable #49970

Open
atbug opened this issue May 27, 2023 · 2 comments
Open

@code_warntype shows the following code is type unstable #49970

atbug opened this issue May 27, 2023 · 2 comments
Labels
compiler:inference Type inference

Comments

@atbug
Copy link

atbug commented May 27, 2023

Running the following code with julia test.jl shows type instability, but I believe the code is type stable.

test.jl

using LinearAlgebra
using InteractiveUtils

function hey(q)
    A = kron([1 1; 1 1], [1 1; 1 1])
    B = kron([1 1; 1 1], [1 0; 0 1])
    if norm(q) > 1.0
        return A
    else
        return A - B
    end
end

function foo(f)
    return f.([[1.0, 1.0], [1.0, 0.0]])
end

# hey([1.0, 0.0])

@code_warntype foo(hey)

The output is

MethodInstance for foo(::typeof(hey))
  from foo(f) @ Main /path/to/test.jl:14
Arguments
  #self#::Core.Const(foo)
  f::Core.Const(hey)
Body::Any
1 ─ %1 = Base.vect(1.0, 1.0)::Vector{Float64}
│   %2 = Base.vect(1.0, 0.0)::Vector{Float64}
│   %3 = Base.vect(%1, %2)::Vector{Vector{Float64}}
│   %4 = Base.broadcasted(f, %3)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(hey), Tuple{Vector{Vector{Float64}}}}
│   %5 = Base.materialize(%4)::Any
└──      return %5

However, uncomment the line hey([1.0, 0.0]) fix the issue.

This has been tested on 1.9.0, 1.8.5 and 1.6.7.

@atbug atbug changed the title @code_warntype show the following code as type unstable @code_warntype shows the following code as type unstable May 28, 2023
@atbug atbug changed the title @code_warntype shows the following code as type unstable @code_warntype shows the following code is type unstable May 28, 2023
@inkydragon
Copy link
Sponsor Member

The interesting thing is that if you call hey first, foo becomes type stable.
v1.9.0

using LinearAlgebra

function hey(q)
    A = kron([1 1; 1 1], [1 1; 1 1])
    B = kron([1 1; 1 1], [1 0; 0 1])
    if norm(q) > 1.0
        return A
    else
        return A - B
    end
end
hey([1.0, 1.0]);
# @code_warntype hey([1.0, 1.0])
# @code_warntype hey([1.0, 0.0])

function foo(f)
    return f.([[1.0, 1.0], [1.0, 0.0]])
end
@code_warntype foo(hey)
julia> @code_warntype foo(hey)
MethodInstance for foo(::typeof(hey))
  from foo(f) @ Main REPL[6]:1
Arguments
  #self#::Core.Const(foo)
  f::Core.Const(hey)
Body::Vector{Matrix{Int64}}
1%1 = Base.vect(1.0, 1.0)::Vector{Float64}%2 = Base.vect(1.0, 0.0)::Vector{Float64}%3 = Base.vect(%1, %2)::Vector{Vector{Float64}}%4 = Base.broadcasted(f, %3)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(hey), Tuple{Vector{Vector{Float64}}}}
│   %5 = Base.materialize(%4)::Vector{Matrix{Int64}}
└──      return %5

Or add a type assert

function hey(q) :: Matrix{Int64}
...

@inkydragon inkydragon added the compiler:inference Type inference label May 29, 2023
@KristofferC
Copy link
Sponsor Member

Probably similar to #35800

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

No branches or pull requests

3 participants