This is an archive of the discontinued LLVM Phabricator instance.

[mlir][SCF] Add utility method to add new yield values to a loop.
ClosedPublic

Authored by mravishankar on May 6 2022, 5:04 PM.

Details

Summary

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.

Diff Detail

Event Timeline

mravishankar created this revision.May 6 2022, 5:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2022, 5:04 PM
mravishankar requested review of this revision.May 6 2022, 5:04 PM

Fix erroneous edit.

nicolasvasilache accepted this revision.May 10 2022, 5:35 AM

Generally looks good, thanks for the improvement!
It is unfortunate however to not retire the old one as this is almost purely a 1-1 replacement with a slight variation and a perf improvement.
So I went ahead and debugged to a successful outcome: https://reviews.llvm.org/D125309.
Feel free to just incorporate in your current PR and land the whole thing.

mlir/include/mlir/Dialect/SCF/Utils/Utils.h
81

If the endgoal is to allow reuse with rewrite infra, consider OpBuilder -> RewriterBase

This revision is now accepted and ready to land.May 10 2022, 5:35 AM
So I went ahead and debugged to a successful outcome: https://reviews.llvm.org/D125309.

Thats awesome. Ill patch in your change and check.

mlir/include/mlir/Dialect/SCF/Utils/Utils.h
81

That was what I was going for in the end, but just making the loops no-ops and replacing all uses just makes the loop go away with CSE. So just leaving it to use OpBuilder for now cause its probably more broadly applicable. Also based on how I needed to use, changing this to use RewriterBase would require changing tileConsumerAndFuseProducer to also use RewriterBase. Avoiding that for now.

This revision was landed with ongoing or failed builds.May 10 2022, 11:44 AM
This revision was automatically updated to reflect the committed changes.