FluxML / IRTools.jl

Mike's Little Intermediate Representation
MIT License
111 stars 36 forks source link

pushfirst! missing for Pipe #16

Closed phipsgabler closed 5 years ago

phipsgabler commented 5 years ago

There should be an equivalent to the method of IR, pushing a statement at the front of the pipe. Currently I use

function pushfirst!(p::IRTools.Pipe, x)
    first_v, first_stmt = first(p)
    IRTools.insert!(p, first_v, x)
end

Is the following modification of push! correct, or is there even something simpler?

function pushfirst!(p::IRTools.Pipe, x)
    tmp = IRTools.var!(p)
    IRTools.substitute!(p, tmp, IRTools.pushfirst!(p.to, prewalk(IRTools.substitute(p), x)))
    return tmp
end
MikeInnes commented 5 years ago

Note that this wouldn't be more efficient on pipes, but it doesn't do any harm to add it. That looks right to me so would be great to have as a PR.