The current implementation of cloneWithNewYields has a few issues
- It clones the loop body of the original loop to create a new loop. This is very expensive.
- It performs erase operations which are incompatible when this method is called from within a pattern rewrite. All erases need to go through PatternRewriter.
To address these a new utility method replaceLoopWithNewYields is added
which
- moves the operations from the original loop into the new loop.
- replaces all uses of the original loop with the corresponding results of the new loop
- use a call back to allow caller to generate the new yield values.
- the original loop is modified to just yield the basic block arguments corresponding to the iter_args of the loop. This represents a no-op loop. The loop itself is dead (since all its uses are replaced), but is not removed. The caller is expected to erase the op. Consequently, this method can be called from within a matchAndRewrite method of a PatternRewriter.
The cloneWithNewYields could be replaces with
replaceLoopWithNewYields, but that seems to trigger a failure during
walks, potentially due to the operations being moved. That is left as
a TODO.
If the endgoal is to allow reuse with rewrite infra, consider OpBuilder -> RewriterBase