Page MenuHomePhabricator

[mlir][Pattern] Create a new IRRewriter class to enable sharing code with pattern rewrites
AcceptedPublic

Authored by rriddle on Wed, Jan 13, 2:52 PM.

Details

Summary

This revision adds two new classes, IRRewriterImpl and IRRewriter. IRRewriterImpl is a new shared base class between IRRewriter and PatternRewriter. PatternRewriter will continue to be the base class used to perform rewrites within a rewrite pattern. IRRewriter on the other hand, is a new class that allows for tracking IR rewrites from outside of a rewrite pattern. In this revision all of the old API from PatternRewriter is moved to IRRewriterImpl, but the distinction between IRRewriter and PatternRewriter is kept on the chance that a necessary API divergence happens in the future.

This revision will enable the creation of utilities that can be invoked from in rewrite patterns and normal transformation code.

Depends On D94632

Diff Detail

Event Timeline

rriddle created this revision.Wed, Jan 13, 2:52 PM
rriddle requested review of this revision.Wed, Jan 13, 2:52 PM

I went with IRRewriterImpl as this seems close to a SmallVectorImpl situation, but happy to rename to something else if there are suggestions.

bollu added a subscriber: bollu.Fri, Jan 15, 7:38 PM

Out of curiosity: may I please have some context for what this base class is being created for? Ie, what consumers other than the existing PatternRewriter will soon exist? :)

mlir/lib/IR/PatternMatch.cpp
315

Nit: spuriously deleted line?

rriddle added a comment.EditedFri, Jan 15, 8:25 PM

Out of curiosity: may I please have some context for what this base class is being created for? Ie, what consumers other than the existing PatternRewriter will soon exist? :)

There are existing use cases, i.e. if you want to have some utility that transforms a piece of IR and share it between pattern and non-pattern code you currently have to duplicate it. What you would be able to do after this revision is share it by using an IRRewriterImpl as the driver for the mutation:

void someSharedUtility(IRRewriterImpl &rewriter, ...) {
  // Some interesting IR mutation here.
}

// Some RewritePattern
LogicalResult MyPattern::matchAndRewrite(Operation *op, PatternRewriter &rewriter) {
  ...
  someSharedUtility(rewriter, ...);
  ...
}

// Some Pass
void MyPass::runOnOperation() {
  ...
  IRRewriter rewriter(...);
  someSharedUtility(rewriter, ...);
}
mehdi_amini accepted this revision.Sat, Jan 23, 12:06 PM
This revision is now accepted and ready to land.Sat, Jan 23, 12:06 PM