IRTools.jl icon indicating copy to clipboard operation
IRTools.jl copied to clipboard

support finding dependencies of a variable

Open Roger-luo opened this issue 5 years ago • 7 comments

I think we discuss this a long time ago in slack @MikeInnes , by dispatching pullbacks or other contextual calls to only variables dependent on the result, it will at least eliminate some annoying errors for functions that does not involve global references etc.

but I'm not sure this is the right way to implement it. I guess you might have some comments. especially, I'm not sure this is right for branches.

Roger-luo avatar May 29 '20 22:05 Roger-luo

This seems like a reasonable approach; it won't yet work for branches because ir[v] doesn't exist for block arguments. You instead have to enumerate each branch to the block and include all passed argument variables as dependencies.

It may be better to rework this to use a dataflow-based approach, which is basically like doing type inference. You walk forward over all variables keeping track of the set of dependencies (which is easy to calculate as the union of dependencies of variables in the Expr). When you get to the end of the block, you check each branch to see if its dependencies have changed, and if so add the target block to a work queue. That will correctly handle things like loops where dependencies can be complex.

It might be useful to have a little tutorial on doing data flow analyses in the docs, since this is a good approach to almost every non-trivial task on IR.

MikeInnes avatar Jun 01 '20 13:06 MikeInnes

Thanks! @MikeInnes by dataflow-based approach do you mean something like Mjolnir? Or should I PR this feature into Mjolnir instead of IRTools?

Roger-luo avatar Jun 01 '20 19:06 Roger-luo

No, "data flow analysis" is just a general approach to writing algorithms on IR like this; see for example https://en.wikipedia.org/wiki/Data-flow_analysis. The clearest example in IRTools is probably this function which turns all implicit inter-block variable usage into explicit block arguments. So I'm suggesting that you rework the structure of your algorithm to do a dataflow analysis in the way that I described above.

MikeInnes avatar Jun 02 '20 12:06 MikeInnes

@MikeInnes I just rework this, should be ready for a review.

Roger-luo avatar Jun 08 '20 01:06 Roger-luo

err... Not sure why this fails on 1.0

Roger-luo avatar Jun 08 '20 15:06 Roger-luo

I think this is because the IR on 1.0 is different from later Julia versions due to different implementation of @info. So I mark that as broken.

@MikeInnes any chance to review this?

Roger-luo avatar Jun 13 '20 19:06 Roger-luo

@MikeInnes bump

Roger-luo avatar Jul 03 '20 04:07 Roger-luo