I have some code that traces and collects the def-use chain of an operation up to the block arguments of its scope. In other words, given an operation P with Operands o1…oN; I add the operations that define o1…oN to my list and then recurse on the operands of o1…oN, etc. Stopping after we reach a block arg or constant value.
I want to insert a clone of this chain into a modified copy of the block (a different module).
How can I go about doing this?
I currently have something that inserts the cloned operation chain - except that when I clone each operation I think I lose the dependencies between them (results in <<UNKNOWN SSA VALUE>>, as the new op has no reference to it’s cloned operands)
I notice I can maybe remap the operands of an operation using an IRMapping as an argument to the clone method, or use something like cloneInto…
Assuming that the region has SSA dominance, I would clone the ops on the chain top-to-bottom, one-by-one and after cloning each op, map the old results to the new results in the IRMapping object (same for block if necessary). I am not aware of a helper function to automate this.
How are you inserting the cloned Op? It needs to be inserted in a place where the remapped operands are actually defined. I usually use an IRRewriter (if it’s not within a Pattern), do rewriter.setInsertionPointToStart(...) to the block I want the cloned Ops in and then go through the loop described my Matthias, i.e. rewriter.clone() and mapping.map()
Ah, yes in that case it looks like you will need a remapping pass after the cloning. I would just iterate over the cloned ops, iterate over their operands and check if they have been remapped in the IRMapping, if so, you can change the operand in-place.
Ahh yes that is a good solution! I am testing a method where I push the indices of recursive args of an operation to a map : DenseMap<Operation *, SmallVector<unsigned>> , while climbing the tree and then lookup the args to replace with itself after the call to clone… Possibly a bit over complicated.
Also, do you mean haven’t been remapped ? Not sure if I have fully understood