Skip to content

Improve type-inference of in-place broadcast by returning the destination #50794

Open
@jishnub

Description

Discussed on discourse

Currently, an in-place broadcast operation is lowered such that Base.materialize! is the last step, whose result is returned. The proposal is to add an explicit return dest after this, so that the return type of the operation may be inferrable even if that of Base.materialize! is not.

Currently

julia> @descend (Y -> Y .+= Any[zeros(size(Y))][1])(ones(2))
(::var"#5#6")(Y) @ Main REPL[2]:1
┌ Warning: couldn't retrieve source of (::var"#5#6")(Y) @ Main REPL[2]:1
└ @ TypedSyntax ~/.julia/packages/TypedSyntax/vbZHi/src/node.jl:31
Variables
  #self#::Core.Const(var"#5#6"())
  Y::Vector{Float64}

%0 = invoke #5(::Vector{Float64})::Any
    @ REPL[2]:1 within `#5`
1%1 = Main.:+::Core.Const(+)
│   %2 = Main.Any::Core.Const(Any)
│   %3 = Main.size(Y)::Tuple{Int64}%4 = Main.zeros(%3)::Vector{Float64}%5 = Base.getindex(%2, %4)::Vector{Any}%6 = Base.getindex(%5, 1)::Any%7 = Base.broadcasted(%1, Y, %6)::Any%8 = Base.materialize!(Y, %7)::Any
└──      return %8
Select a call to descend into or  to ascend. [q]uit. [b]ookmark.
Toggles: [w]arn, [h]ide type-stable statements, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
 • %3 = size(::Vector{Float64})::Tuple{Int64}
   %4 = zeros(::Tuple{Int64})::Vector{Float64}
   %5 = getindex(::Type{Any},::Vector{Float64})::Vector{Any}
   %6 = getindex(::Vector{Any},::Int64)::Any
   %7 = broadcasted(::typeof(+),::Vector{Float64},::Any)::Any
   %8 = call  materialize!(::Vector{Float64},::Any)::Any
   

Changing the return %8 to return Y would resolve this issue.

It appears that broadcast! does not suffer from this.

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]broadcastApplying a function over a collection

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions