I just noticed a type-instability with selectdim
. Consider the function
f(A) = selectdim(A, 1, 1)
For a matrix, we have that the return type of f
is a 1D SubArray
(as expected):
julia> A = rand(2,2);
julia> @code_warntype f(A)
Variables
#self#::Core.Compiler.Const(f, false)
A::Array{Float64,2}
Body::SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},true}
1 ─ %1 = Main.selectdim(A, 1, 1)::SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},true}
└── return %1
However, if we call f
on an array of dimension 3
, the return type of f
is a union of a 1D SubArray
and a 2D SubArray
:
julia> A = rand(2,2,2);
julia> @code_warntype f(A)
Variables
#self#::Core.Compiler.Const(f, false)
A::Array{Float64,3}
Body::Union{SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}}},true}, SubArray{Float64,1,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},Int64},true}}
1 ─ %1 = Main.selectdim(A, 1, 1)::Union{SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}}},true}, SubArray{Float64,1,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},Int64},true}}
└── return %1
I expected the return type of f
to b a 2D SubArray
(hence be type stable) which would match the behaviour of @views A[1,:,:]
no?
Am I missing something here? when would f
ever be a 1D SubArray
?