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

WIP: Warn if @code_typed, @code_warntype, etc. are showing inaccurate specialization #33142

Closed
wants to merge 1 commit into from
Closed

WIP: Warn if @code_typed, @code_warntype, etc. are showing inaccurate specialization #33142

wants to merge 1 commit into from

Conversation

DilumAluthge
Copy link
Member

This is an experiment in addressing the problem described in:

@DilumAluthge
Copy link
Member Author

DilumAluthge commented Sep 3, 2019

Here are some examples:

julia> f1(x) = Base.typemax(x)
f1 (generic function with 1 method)

julia> @code_lowered f1(Int)
WARNING: I am lying to you.
CodeInfo(
1%1 = Base.typemax
│   %2 = (%1)(x)
└──      return %2
)

julia> @code_typed f1(Int)
WARNING: I am lying to you.
CodeInfo(
1return 9223372036854775807
) => Int64

julia> @code_warntype f1(Int)
WARNING: I am lying to you.
Variables
  #self#::Core.Compiler.Const(f1, false)
  x::Core.Compiler.Const(Int64, false)

Body::Int64
1%1 = Base.typemax::Core.Compiler.Const(typemax, false)
│   %2 = (%1)(x)::Core.Compiler.Const(9223372036854775807, false)
└──      return %2

julia> g(x) = x + x
g (generic function with 1 method)

julia> @code_lowered g(1)
CodeInfo(
1%1 = x + x
└──      return %1
)

julia> @code_typed g(1)
CodeInfo(
1%1 = Base.add_int(x, x)::Int64
└──      return %1
) => Int64

julia> @code_warntype g(1)
Variables
  #self#::Core.Compiler.Const(g, false)
  x::Int64

Body::Int64
1%1 = (x + x)::Int64
└──      return %1

julia> foo(t::Type) = t
foo (generic function with 1 method)

julia> @code_lowered foo(Int)
WARNING: I am lying to you.
CodeInfo(
1return t
)

julia> @code_typed foo(Int)
WARNING: I am lying to you.
CodeInfo(
1return t
) => Type{Int64}

julia> @code_warntype foo(Int)
WARNING: I am lying to you.
Variables
  #self#::Core.Compiler.Const(foo, false)
  t::Core.Compiler.Const(Int64, false)

Body::Type{Int64}
1return t

julia> function foo2(t::Type)
           x = ones(t, 10)
           return sum(map(sin, x))
       end
foo2 (generic function with 1 method)

julia> @code_lowered foo2(Int)
WARNING: I am lying to you.
CodeInfo(
1 ─      x = Main.ones(t, 10)
│   %2 = Main.map(Main.sin, x)
│   %3 = Main.sum(%2)
└──      return %3
)

julia> @code_typed foo2(Int)
WARNING: I am lying to you.
CodeInfo(
1 ── %1  = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Int64,1}, svec(Any, Int64), 0, :(:ccall), Array{Int64,1}, 10, 10))::Array{Int64,1}
│          invoke Base.fill!(%1::Array{Int64,1}, 1::Int64)::Any%3  = %new(Base.Generator{Array{Int64,1},typeof(sin)}, sin, %1)::Base.Generator{Array{Int64,1},typeof(sin)}%4  = invoke Base._collect(%1::Array{Int64,1}, %3::Base.Generator{Array{Int64,1},typeof(sin)}, $(QuoteNode(Base.EltypeUnknown()))::Base.EltypeUnknown, $(QuoteNode(Base.HasShape{1}()))::Base.HasShape{1})::Array{Float64,1}%5  = Base.identity::typeof(identity)
│    %6  = Base.add_sum::typeof(Base.add_sum)
│    %7  = Base.arraysize(%4, 1)::Int64%8  = Base.slt_int(%7, 0)::Bool%9  = Base.ifelse(%8, 0, %7)::Int64%10 = Base.sub_int(%9, 0)::Int64%11 = (%10 === 0)::Bool
└───       goto #3 if not %11
2 ──       goto #11
3 ── %14 = (%10 === 1)::Bool
└───       goto #5 if not %14
4 ── %16 = Base.arrayref(false, %4, 1)::Float64
└───       goto #11
5 ── %18 = Base.slt_int(%10, 16)::Bool
└───       goto #10 if not %18
6 ── %20 = Base.arrayref(false, %4, 1)::Float64%21 = Base.arrayref(false, %4, 2)::Float64
└─── %22 = Base.add_float(%20, %21)::Float64
7 ┄─ %23 = φ (#6 => 2, #8 => %29)::Int64%24 = φ (#6 => %22, #8 => %31)::Float64%25 = Base.slt_int(%9, 0)::Bool%26 = Base.ifelse(%25, 0, %9)::Int64%27 = Base.slt_int(%23, %26)::Bool
└───       goto #9 if not %27
8 ── %29 = Base.add_int(%23, 1)::Int64%30 = Base.arrayref(false, %4, %29)::Float64%31 = Base.add_float(%24, %30)::Float64
└───       goto #7
9 ──       goto #11
10%34 = Base.slt_int(%9, 0)::Bool%35 = Base.ifelse(%34, 0, %9)::Int64%36 = invoke Base.mapreduce_impl(%5::typeof(identity), %6::typeof(Base.add_sum), %4::Array{Float64,1}, 1::Int64, %35::Int64, 1024::Int64)::Float64
└───       goto #11
11%38 = φ (#2 => 0.0, #4 => %16, #9 => %24, #10 => %36)::Float64
└───       goto #12
12 ─       goto #13
13 ─       goto #14
14 ─       goto #15
15 ─       goto #16
16 ─       goto #17
17 ─       goto #18
18return %38
) => Float64

julia> @code_warntype foo2(Int)
WARNING: I am lying to you.
Variables
  #self#::Core.Compiler.Const(foo2, false)
  t::Core.Compiler.Const(Int64, false)
  x::Array{Int64,1}

Body::Float64
1 ─      (x = Main.ones(t, 10))
│   %2 = Main.map(Main.sin, x)::Array{Float64,1}%3 = Main.sum(%2)::Float64
└──      return %3

@DilumAluthge DilumAluthge changed the title WIP: Warn if @code_typed/@code_warntype/etc is showing inaccurate specialization WIP: Warn if @code_typed, @code_warntype, etc. are showing inaccurate specialization Sep 3, 2019
@DilumAluthge DilumAluthge changed the title WIP: Warn if @code_typed, @code_warntype, etc. are showing inaccurate specialization WIP: Warn if @code_typed, @code_warntype, etc. are showing inaccurate specialization Sep 3, 2019
@DilumAluthge DilumAluthge reopened this Sep 4, 2019
@DilumAluthge DilumAluthge reopened this Sep 5, 2019
@DilumAluthge DilumAluthge reopened this Sep 21, 2019
@mbauman
Copy link
Member

mbauman commented Sep 23, 2019

I'd love to see this. I don't think we need to make it an option, but let's also make the warning a little less passive-aggressive. :)

@DilumAluthge
Copy link
Member Author

This is just a note to myself:

This pull request only deals with positional arguments. We will need to also find a way to deal with inaccurate specialization on keyword arguments.

@DilumAluthge DilumAluthge deleted the da/being-honest-about-specialization branch November 14, 2019 05:21
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

Successfully merging this pull request may close these issues.

2 participants