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

Type inference oddity reported by @code_warntype #54344

Closed
shamazmazum opened this issue May 3, 2024 · 3 comments
Closed

Type inference oddity reported by @code_warntype #54344

shamazmazum opened this issue May 3, 2024 · 3 comments

Comments

@shamazmazum
Copy link

shamazmazum commented May 3, 2024

My versioninfo():

Julia Version 1.10.3
Commit 0b4590a550 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: FreeBSD (x86_64-unknown-freebsd13.2)
  CPU: 16 × AMD Ryzen 7 5800X 8-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)

Consider this function:

function foo(array, op, ps1)
    function cc(:: Any)
        op(array)
    end

    return cc.(ps1)
end

Now run @code_warntype as follows:

julia> data = rand(Bool, (100, 100, 100));

julia> @code_warntype foo(data, x -> sum(2x), [])
MethodInstance for foo(::Array{Bool, 3}, ::var"#2#3", ::Vector{Any})
  from foo(array, op, ps1) @ Main REPL[1]:1
Arguments
  #self#::Core.Const(foo)
  array::Array{Bool, 3}
  op::Core.Const(var"#2#3"())
  ps1::Vector{Any}
Locals
  cc::var"#cc#1"{Array{Bool, 3}, var"#2#3"}
Body::Any
1 ─ %1 = Main.:(var"#cc#1")::Core.Const(var"#cc#1")
│   %2 = Core.typeof(array)::Core.Const(Array{Bool, 3})
│   %3 = Core.typeof(op)::Core.Const(var"#2#3")
│   %4 = Core.apply_type(%1, %2, %3)::Core.Const(var"#cc#1"{Array{Bool, 3}, var"#2#3"})
│        (cc = %new(%4, array, op))
│   %6 = Base.broadcasted(cc, ps1)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, var"#cc#1"{Array{Bool, 3}, var"#2#3"}, Tuple{Vector{Any}}}
│   %7 = Base.materialize(%6)::Any
└──      return %7

The Body::Any part is red and I have read that this is very bad for performance. Since there is no rules on what I should do to avoid red marks, it's hard to tell if this is a bug or not. IMHO, multiplication of a value of type Bool by two should return an Int, summing Ints results in Int, so the result must be Vector{Int}. Am I not right? Why cannot julia infer the type?

UPD:
Trying @code_warntype foo(data, sum, []) correctly returns Vector{Int} for the result type.

@vchuravy
Copy link
Sponsor Member

vchuravy commented May 3, 2024

Please use https://discourse.julialang.org for usage questions.

Note that [] is Vector{Any}

@vchuravy vchuravy closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2024
@shamazmazum
Copy link
Author

@vchuravy [2] works as well, but whatever...

@arhik
Copy link
Sponsor

arhik commented May 8, 2024

@shamazmazum I just checked it. It is not fixed yet. I have the same output as yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants