Hi, I just found that selectdim
does not infer the concrete SubArray
:
julia> using InteractiveUtils
julia> a = rand(5, 5);
julia> @code_warntype selectdim(a, 2, 1)
Body::SubArray
1 ─ %1 = (Base.arraysize)(A, 1)::Int64
│ %2 = (Base.arraysize)(A, 2)::Int64
│ %3 = (Base.slt_int)(%1, 0)::Bool
│ %4 = (Base.ifelse)(%3, 0, %1)::Int64
│ %5 = %new(Base.OneTo{Int64}, %4)::Base.OneTo{Int64}
│ %6 = (Base.slt_int)(%2, 0)::Bool
│ %7 = (Base.ifelse)(%6, 0, %2)::Int64
│ %8 = %new(Base.OneTo{Int64}, %7)::Base.OneTo{Int64}
│ %9 = %new(Base.Slice{Base.OneTo{Int64}}, %5)::Base.Slice{Base.OneTo{Int64}}
│ %10 = %new(Base.Slice{Base.OneTo{Int64}}, %8)::Base.Slice{Base.OneTo{Int64}}
│ %11 = (d === 1)::Bool
│ %12 = (Base.ifelse)(%11, i, %9)::Union{Slice{OneTo{Int64}}, Int64}
│ %13 = (Base.sub_int)(d, 1)::Int64
│ %14 = (%13 === 1)::Bool
│ %15 = (Base.ifelse)(%14, i, %10)::Union{Slice{OneTo{Int64}}, Int64}
│ %16 = (Core.tuple)(%12, %15)::Tuple{Union{Slice{OneTo{Int64}}, Int64},Union{Slice{OneTo{Int64}}, Int64}}
│ %17 = (Base._selectdim)(A, d, i, %16)::SubArray
└── return %17
The view
works well:
julia> @code_warntype view(a, :, 1)
Body::SubArray{Float64,1,Array{Float64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}
1 ─ %1 = (Base.arraysize)(A, 1)::Int64
│ (Base.arraysize)(A, 2)
│ %3 = (Base.slt_int)(%1, 0)::Bool
│ %4 = (Base.ifelse)(%3, 0, %1)::Int64
│ %5 = %new(Base.OneTo{Int64}, %4)::Base.OneTo{Int64}
│ %6 = %new(Base.Slice{Base.OneTo{Int64}}, %5)::Base.Slice{Base.OneTo{Int64}}
│ %7 = (getfield)(I, 2)::Int64
└── goto #6 if not $(Expr(:boundscheck))
2 ─ %9 = (Core.tuple)(%6, %7)::Tuple{Base.Slice{Base.OneTo{Int64}},Int64}
│ (Base.arraysize)(A, 1)
│ %11 = (Base.arraysize)(A, 2)::Int64
│ %12 = (Base.slt_int)(%11, 0)::Bool
│ %13 = (Base.ifelse)(%12, 0, %11)::Int64
│ %14 = (Base.sle_int)(1, %7)::Bool
│ %15 = (Base.sle_int)(%7, %13)::Bool
│ %16 = (Base.and_int)(%14, %15)::Bool
│ %17 = (Base.and_int)(%16, true)::Bool
│ %18 = (Base.and_int)(true, %17)::Bool
└── goto #4 if not %18
3 ─ goto #5
4 ─ invoke Base.throw_boundserror(_2::Array{Float64,2}, %9::Tuple{Base.Slice{Base.OneTo{Int64}},Int64})
└── $(Expr(:unreachable))
5 ┄ nothing
6 ┄ %24 = (Core.tuple)(%6, %7)::Tuple{Base.Slice{Base.OneTo{Int64}},Int64}
│ (Base.arraysize)(A, 1)
│ (Base.arraysize)(A, 2)
│ %27 = (Base.arraysize)(A, 1)::Int64
│ (Base.arraysize)(A, 2)
│ %29 = (Base.slt_int)(%27, 0)::Bool
│ %30 = (Base.ifelse)(%29, 0, %27)::Int64
│ %31 = (Base.sub_int)(%30, 0)::Int64
│ %32 = (Base.mul_int)(1, %31)::Int64
│ %33 = (Base.sub_int)(%7, 1)::Int64
│ %34 = (Base.mul_int)(%33, %32)::Int64
│ %35 = (Base.add_int)(1, %34)::Int64
│ (Base.arraysize)(A, 1)
│ (Base.arraysize)(A, 2)
│ %38 = (Base.sub_int)(%35, 1)::Int64
│ %39 = %new(SubArray{Float64,1,Array{Float64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}, A, %24, %38, 1)::SubArray{Float64,1,Array{Float64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}
└── return %39
I check that it arise from later part of this line. I am not sure how it causes this. Should I worry about this type instability, say if I am solving an ODE with a core derivative function having those selectdim
?
Thanks!