Open mikmoore opened 2 years ago
Our broadcast
support const-propagation. If you look-into @code_typed
.
You will find:
11 ┄ %38 = Base.Broadcast.ifelse(%10, %21, 1)::Int64
│ %39 = Base.arrayref(false, x, %38)::Tuple{Int64, Float64, Rational{Int64}}
│ %40 = Base.getfield(%39, 2, true)::Float64
The problem is that during broadcast
, we call copy
, which checks the return type of bc::Broadcasted
first, if it's concrete then falls-back to copyto!
.
IIUC, the current combine_eltypes
is pure-type based, and of-cource it fails to return Float64
.
It would be nice if constant propagation worked through broadcasting.
This fails because
getindex(::Tuple{A,B,C},i::Int) where {A,B,C}
is unstable unlessA==B==C
ori
gets constant-folded.One can work around this via the
get2
solution above or a similarbroadcast(...) do
block (and obviouslysum(get2,x)
is the best solution in this MWE), but having broadcast natively recognize and propagate scalar constants would be convenient and prevent people from unknowingly falling into this trap. The primary cases where I've gotten into trouble with this are broadcasts overgetindex
andgetproperty
.