When looking at the @code_warntype
output of the following function
using Distributions
const priors = [ Normal(0, 1), Beta(2, 5)]
const pars = [2.0, 0.9]
function prioreval_test(priors, pars::Vector{Float64})
if all(insupport.(priors, pars))
log_priorval = sum(logpdf.(priors, pars))
alarm = false
else
log_priorval = -Inf
alarm = true
end
return log_priorval, alarm
end
@code_warntype prioreval_test(priors, pars)
I find that it is not type stable
Body::Tuple{Any,Bool}
3 1 ββ %1 = (Base.arraysize)(priors, 1)::Int64 ββ»β·β·β·β·β· materialize
β %2 = (Base.slt_int)(%1, 0)::Bool βββ»β·β·β· instantiate
β %3 = (Base.ifelse)(%2, 0, %1)::Int64 ββββββββββ combine_axes
β %4 = %new(Base.OneTo{Int64}, %3)::Base.OneTo{Int64} βββββββββ broadcast_axes
β %5 = (Base.arraysize)(pars, 1)::Int64 ββββββ»β·β· broadcast_axes
β %6 = (Base.slt_int)(%5, 0)::Bool βββββββ»β·β·β· axes
β %7 = (Base.ifelse)(%6, 0, %5)::Int64 βββββββββββ map
β %8 = %new(Base.OneTo{Int64}, %7)::Base.OneTo{Int64} ββββββββββ Type
β %9 = (%7 === %3)::Bool ββββββ»β·β·β·β· _bcs
β %10 = (Base.and_int)(true, %9)::Bool βββββββ» _bcs1
ββββ goto #3 if not %10 ββββββββ _bcsm
2 ββ goto #4 ββββββββ
3 ββ %13 = (%3 === 1)::Bool βββββββββ» ==
ββββ goto #4 ββββββββ
4 ββ %15 = Ο (#2 => %10, #3 => %13)::Bool βββββββ
ββββ goto #6 if not %15 βββββββ
5 ββ goto #12 βββββββ
6 ββ %18 = (%3 === %7)::Bool βββββββββ»β· ==
β %19 = (Base.and_int)(true, %18)::Bool ββββββββββ» &
ββββ goto #8 if not %19 ββββββββ
7 ββ goto #9 ββββββββ
8 ββ %22 = (%7 === 1)::Bool βββββββββ» ==
ββββ goto #9 ββββββββ
9 ββ %24 = Ο (#7 => %19, #8 => %22)::Bool βββββββ
ββββ goto #11 if not %24 βββββββ
10 β goto #12 βββββββ
11 β %27 = %new(Base.DimensionMismatch, "arrays could not be broadcast to a common size")::DimensionMismatch ββββββββ» Type
β (Base.Broadcast.throw)(%27) βββββββ
ββββ $(Expr(:unreachable)) βββββββ
12 β %30 = Ο (#5 => %7, #10 => %3)::Int64 ββββββ
β %31 = Ο (#5 => %7, #10 => %3)::Int64 ββββββ
β %32 = Ο (#5 => %7, #10 => %3)::Int64 ββββββ
β %33 = Ο (#5 => %7, #10 => %3)::Int64 ββββββ
β %34 = Ο (#5 => %8, #10 => %4)::Base.OneTo{Int64} ββββββ
β %35 = (Core.tuple)(%34)::Tuple{Base.OneTo{Int64}} ββββββ
ββββ goto #13 ββββββ
13 β goto #14 βββββ
14 β goto #15 ββββ
15 β goto #16 βββ» instantiate
16 β %40 = (Base.arraysize)(priors, 1)::Int64 ββββ»β·β·β·β·β·β·β· preprocess
β %41 = (Base.slt_int)(%40, 0)::Bool βββββ»β·β·β· preprocess_args
β %42 = (Base.ifelse)(%41, 0, %40)::Int64 ββββββββββββββ preprocess
β %43 = (%42 === 1)::Bool βββββββ»β·β· extrude
β %44 = (Base.not_int)(%43)::Bool ββββββββ» newindexer
β %45 = (Core.tuple)(%44)::Tuple{Bool} ββββββββββ shapeindexer
β %46 = (Core.tuple)(1)::Tuple{Int64} ββββββββββ _newindexer
β %47 = %new(Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}}, priors, %45, %46)::Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}}
β %48 = (Base.arraysize)(pars, 1)::Int64 βββββββ»β·β·β·β·β· preprocess
β %49 = (Base.slt_int)(%48, 0)::Bool ββββββββ»β·β·β· extrude
β %50 = (Base.ifelse)(%49, 0, %48)::Int64 βββββββββββββββ newindexer
β %51 = (%50 === 1)::Bool ββββββββββ»β·β· shapeindexer
β %52 = (Base.not_int)(%51)::Bool βββββββββββ» _newindexer
β %53 = (Core.tuple)(%52)::Tuple{Bool} βββββββββββ
β %54 = (Core.tuple)(1)::Tuple{Int64} βββββββββββ
β %55 = %new(Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}, pars, %53, %54)::Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}
β %56 = (Core.tuple)(%47, %55)::Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}
β %57 = %new(Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(insupport),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}, Distributions.insupport, %56, %35)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(insupport),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}
β %58 = (Base.slt_int)(%30, 1)::Bool ββββ»β·β·β· iterate
ββββ goto #18 if not %58 ββββ
17 β goto #19 ββββ
18 β goto #19 ββββ
19 β %62 = Ο (#17 => true, #18 => false)::Bool βββ
β %63 = Ο (#18 => 1)::Int64 βββ
β %64 = Ο (#18 => 1)::Int64 βββ
ββββ goto #21 if not %62 βββ
20 β %66 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), :(:ccall), 2, Array{Any,1}, :(%31), :(%31)))::Array{Any,1}lar
ββββ goto #31 βββ
21 β goto #26 if not false ββββ» getindex
22 β %69 = (Base.sle_int)(1, %63)::Bool βββββ»β·β· checkbounds
β %70 = (Base.sle_int)(%63, %32)::Bool ββββββββ checkbounds_indices
β %71 = (Base.and_int)(%69, %70)::Bool βββββββ» checkindex
β %72 = (Base.and_int)(%71, true)::Bool βββββββ» &
ββββ goto #24 if not %72 βββββ
23 β goto #25 βββββ
24 β %75 = Base.throw_boundserror::typeof(Base.throw_boundserror) βββββ
β %76 = (Core.tuple)(%63)::Tuple{Int64} βββββ
β invoke %75(%57::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(insupport),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}, %76::Tuple{Int64})
ββββ $(Expr(:unreachable)) βββββ
25 β nothing β
26 β %80 = (Base.Broadcast.ifelse)(%44, %63, 1)::Int64 βββββ»β·β·β· _broadcast_getindex
β %81 = (Base.arrayref)(false, priors, %80)::Distribution{Univariate,Continuous} ββββββ» _getindex
β %82 = (Base.Broadcast.ifelse)(%52, %63, 1)::Int64 βββββββ»β· _getindex
β %83 = (Base.arrayref)(false, pars, %82)::Float64 ββββββββ» _broadcast_getindex
β %84 = (Distributions.insupport)(%81, %83)::Any ββββββ» _broadcast_getindex_evalf
ββββ goto #27 ββββ
27 β %86 = (Base.Broadcast.typeof)(%84)::DataType βββ» copy
β %87 = (isa)(%86, Type{Bool})::Bool βββ
ββββ goto #29 if not %87 βββ
28 β %89 = invoke BitArray{1}($(QuoteNode(array initializer with undefined values))::UndefInitializer, %33::Int64)::BitArray{1}β·β· similar
ββββ goto #30 βββ
29 β %91 = (Base.Broadcast.similar)(%57, %86)::Any βββ
ββββ goto #30 βββ
30 β %93 = Ο (#28 => %89, #29 => %91)::Any βββ
β (Base.setindex!)(%93, %84, %63) βββ
β %95 = (Base.Broadcast.copyto_nonleaf!)(%93, %57, %34, %64, 1)::Any βββ
ββββ goto #31 βββ
31 β %97 = Ο (#20 => %66, #30 => %95)::Any ββ
ββββ goto #32 ββ
32 β %99 = (Main.all)(%97)::Any β
ββββ goto #65 if not %99 β
4 33 β %101 = (Base.arraysize)(priors, 1)::Int64 ββ»β·β·β·β·β· materialize
β %102 = (Base.slt_int)(%101, 0)::Bool βββ»β·β·β· instantiate
β %103 = (Base.ifelse)(%102, 0, %101)::Int64 ββββββββββ combine_axes
β %104 = %new(Base.OneTo{Int64}, %103)::Base.OneTo{Int64} βββββββββ broadcast_axes
β %105 = (Base.arraysize)(pars, 1)::Int64 ββββββ»β·β· broadcast_axes
β %106 = (Base.slt_int)(%105, 0)::Bool βββββββ»β·β·β· axes
β %107 = (Base.ifelse)(%106, 0, %105)::Int64 βββββββββββ map
β %108 = %new(Base.OneTo{Int64}, %107)::Base.OneTo{Int64} ββββββββββ Type
β %109 = (%107 === %103)::Bool ββββββ»β·β·β·β· _bcs
β %110 = (Base.and_int)(true, %109)::Bool βββββββ» _bcs1
ββββ goto #35 if not %110 ββββββββ _bcsm
34 β goto #36 ββββββββ
35 β %113 = (%103 === 1)::Bool βββββββββ» ==
ββββ goto #36 ββββββββ
36 β %115 = Ο (#34 => %110, #35 => %113)::Bool βββββββ
ββββ goto #38 if not %115 βββββββ
37 β goto #44 βββββββ
38 β %118 = (%103 === %107)::Bool βββββββββ»β· ==
β %119 = (Base.and_int)(true, %118)::Bool ββββββββββ» &
ββββ goto #40 if not %119 ββββββββ
39 β goto #41 ββββββββ
40 β %122 = (%107 === 1)::Bool βββββββββ» ==
ββββ goto #41 ββββββββ
41 β %124 = Ο (#39 => %119, #40 => %122)::Bool βββββββ
ββββ goto #43 if not %124 βββββββ
42 β goto #44 βββββββ
43 β %127 = %new(Base.DimensionMismatch, "arrays could not be broadcast to a common size")::DimensionMismatch ββββββββ» Type
β (Base.Broadcast.throw)(%127) βββββββ
ββββ $(Expr(:unreachable)) βββββββ
...
%new(Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}}, priors, %145, %146)::Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}}
β %148 = (Base.arraysize)(pars, 1)::Int64 βββββββ»β·β·β·β·β· preprocess
β %149 = (Base.slt_int)(%148, 0)::Bool ββββββββ»β·β·β· extrude
β %150 = (Base.ifelse)(%149, 0, %148)::Int64 βββββββββββββββ newindexer
β %151 = (%150 === 1)::Bool ββββββββββ»β·β· shapeindexer
β %152 = (Base.not_int)(%151)::Bool βββββββββββ» _newindexer
β %153 = (Core.tuple)(%152)::Tuple{Bool} βββββββββββ
β %154 = (Core.tuple)(1)::Tuple{Int64} βββββββββββ
β %155 = %new(Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}, pars, %153, %154)::Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}
β %156 = (Core.tuple)(%147, %155)::Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}
β %157 = %new(Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(logpdf),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}, Distributions.logpdf, %156, %135)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(logpdf),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}
β %158 = (Base.slt_int)(%130, 1)::Bool ββββ»β·β·β· iterate
ββββ goto #50 if not %158 ββββ
49 β goto #51 ββββ
50 β goto #51 ββββ
51 β %162 = Ο (#49 => true, #50 => false)::Bool βββ
β %163 = Ο (#50 => 1)::Int64 βββ
β %164 = Ο (#50 => 1)::Int64 βββ
ββββ goto #53 if not %162 βββ
52 β %166 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), :(:ccall), 2, Array{Any,1}, :(%131), :(%131)))::Array{Any,1}r
ββββ goto #63 βββ
53 β goto #58 if not false ββββ» getindex
54 β %169 = (Base.sle_int)(1, %163)::Bool βββββ»β·β· checkbounds
β %170 = (Base.sle_int)(%163, %132)::Bool ββββββββ checkbounds_indices
β %171 = (Base.and_int)(%169, %170)::Bool βββββββ» checkindex
β %172 = (Base.and_int)(%171, true)::Bool βββββββ» &
ββββ goto #56 if not %172 βββββ
55 β goto #57 βββββ
56 β %175 = Base.throw_boundserror::typeof(Base.throw_boundserror) βββββ
β %176 = (Core.tuple)(%163)::Tuple{Int64} βββββ
β invoke %175(%157::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(logpdf),Tuple{Base.Broadcast.Extruded{Array{Distribution{Univariate,Continuous},1},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{Array{Float64,1},Tuple{Bool},Tuple{Int64}}}}, %176::Tuple{Int64})
ββββ $(Expr(:unreachable)) βββββ
57 β nothing β
58 β %180 = (Base.Broadcast.ifelse)(%144, %163, 1)::Int64 βββββ»β·β·β· _broadcast_getindex
β %181 = (Base.arrayref)(false, priors, %180)::Distribution{Univariate,Continuous} ββββββ» _getindex
β %182 = (Base.Broadcast.ifelse)(%152, %163, 1)::Int64 βββββββ»β· _getindex
β %183 = (Base.arrayref)(false, pars, %182)::Float64 ββββββββ» _broadcast_getindex
β %184 = (Distributions.logpdf)(%181, %183)::Any ββββββ» _broadcast_getindex_evalf
ββββ goto #59 ββββ
59 β %186 = (Base.Broadcast.typeof)(%184)::DataType βββ» copy
β %187 = (isa)(%186, Type{Bool})::Bool βββ
ββββ goto #61 if not %187 βββ
60 β %189 = invoke BitArray{1}($(QuoteNode(array initializer with undefined values))::UndefInitializer, %133::Int64)::BitArray{1}β· similar
ββββ goto #62 βββ
61 β %191 = (Base.Broadcast.similar)(%157, %186)::Any βββ
ββββ goto #62 βββ
62 β %193 = Ο (#60 => %189, #61 => %191)::Any βββ
β (Base.setindex!)(%193, %184, %163) βββ
β %195 = (Base.Broadcast.copyto_nonleaf!)(%193, %157, %134, %164, 1)::Any βββ
ββββ goto #63 βββ
63 β %197 = Ο (#52 => %166, #62 => %195)::Any ββ
ββββ goto #64 ββ
64 β %199 = (Main.sum)(%197)::Any β
5 ββββ goto #66 β
65 β nothing β
11 66 β %202 = Ο (#64 => %199, #65 => -Inf)::Any β
β %203 = Ο (#64 => false, #65 => true)::Bool β
β %204 = (Core.tuple)(%202, %203)::Tuple{Any,Bool} β
ββββ return %204
I can force the output to Float64 by setting the last line to return log_priorval::Float64, alarm
but I would like to understand where the type instability is coming from. It has to be the if/else statement but I donβt see why as typeof(Inf)
is also Float64
.
Thanks for your help.