2018-10-16 13:59:31 +08:00
|
|
|
//===- PatternMatch.cpp - Base classes for pattern match ------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
2018-10-26 07:44:04 +08:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-03-27 05:45:38 +08:00
|
|
|
#include "mlir/IR/Operation.h"
|
2018-12-28 06:35:10 +08:00
|
|
|
#include "mlir/IR/Value.h"
|
2018-10-16 13:59:31 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
PatternBenefit::PatternBenefit(unsigned benefit) : representation(benefit) {
|
|
|
|
assert(representation == benefit && benefit != ImpossibleToMatchSentinel &&
|
|
|
|
"This pattern match benefit is too large to represent");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short PatternBenefit::getBenefit() const {
|
|
|
|
assert(representation != ImpossibleToMatchSentinel &&
|
|
|
|
"Pattern doesn't match");
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pattern implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-12 07:56:49 +08:00
|
|
|
Pattern::Pattern(StringRef rootName, PatternBenefit benefit,
|
|
|
|
MLIRContext *context)
|
|
|
|
: rootKind(OperationName(rootName, context)), benefit(benefit) {}
|
2018-10-16 13:59:31 +08:00
|
|
|
|
2018-11-29 07:09:39 +08:00
|
|
|
// Out-of-line vtable anchor.
|
|
|
|
void Pattern::anchor() {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RewritePattern and PatternRewriter implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void RewritePattern::rewrite(Operation *op, std::unique_ptr<PatternState> state,
|
2018-11-29 07:09:39 +08:00
|
|
|
PatternRewriter &rewriter) const {
|
2018-10-22 10:03:29 +08:00
|
|
|
rewrite(op, rewriter);
|
2018-10-16 13:59:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void RewritePattern::rewrite(Operation *op, PatternRewriter &rewriter) const {
|
2019-03-26 02:53:03 +08:00
|
|
|
llvm_unreachable("need to implement either matchAndRewrite or one of the "
|
|
|
|
"rewrite functions!");
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
PatternMatchResult RewritePattern::match(Operation *op) const {
|
2019-03-26 02:53:03 +08:00
|
|
|
llvm_unreachable("need to implement either match or matchAndRewrite!");
|
2018-10-16 13:59:31 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:35:23 +08:00
|
|
|
/// Patterns must specify the root operation name they match against, and can
|
|
|
|
/// also specify the benefit of the pattern matching. They can also specify the
|
|
|
|
/// names of operations that may be generated during a successful rewrite.
|
|
|
|
RewritePattern::RewritePattern(StringRef rootName,
|
|
|
|
ArrayRef<StringRef> generatedNames,
|
|
|
|
PatternBenefit benefit, MLIRContext *context)
|
|
|
|
: Pattern(rootName, benefit, context) {
|
|
|
|
generatedOps.reserve(generatedNames.size());
|
|
|
|
std::transform(generatedNames.begin(), generatedNames.end(),
|
|
|
|
std::back_inserter(generatedOps), [context](StringRef name) {
|
|
|
|
return OperationName(name, context);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
PatternRewriter::~PatternRewriter() {
|
|
|
|
// Out of line to provide a vtable anchor for the class.
|
|
|
|
}
|
|
|
|
|
2018-11-23 17:10:26 +08:00
|
|
|
/// This method performs the final replacement for a pattern, where the
|
|
|
|
/// results of the operation are updated to use the specified list of SSA
|
|
|
|
/// values. In addition to replacing and removing the specified operation,
|
|
|
|
/// clients can specify a list of other nodes that this replacement may make
|
|
|
|
/// (perhaps transitively) dead. If any of those ops are dead, this will
|
|
|
|
/// remove them as well.
|
2019-03-27 08:05:09 +08:00
|
|
|
void PatternRewriter::replaceOp(Operation *op, ArrayRef<Value *> newValues,
|
2018-12-28 06:35:10 +08:00
|
|
|
ArrayRef<Value *> valuesToRemoveIfDead) {
|
2018-11-23 17:10:26 +08:00
|
|
|
// Notify the rewriter subclass that we're about to replace this root.
|
|
|
|
notifyRootReplaced(op);
|
|
|
|
|
|
|
|
assert(op->getNumResults() == newValues.size() &&
|
|
|
|
"incorrect # of replacement values");
|
|
|
|
for (unsigned i = 0, e = newValues.size(); i != e; ++i)
|
|
|
|
op->getResult(i)->replaceAllUsesWith(newValues[i]);
|
|
|
|
|
|
|
|
notifyOperationRemoved(op);
|
|
|
|
op->erase();
|
|
|
|
|
2018-11-24 23:40:55 +08:00
|
|
|
// TODO: Process the valuesToRemoveIfDead list, removing things and calling
|
|
|
|
// the notifyOperationRemoved hook in the process.
|
2018-11-23 17:10:26 +08:00
|
|
|
}
|
|
|
|
|
2018-11-24 23:40:55 +08:00
|
|
|
/// op and newOp are known to have the same number of results, replace the
|
|
|
|
/// uses of op with uses of newOp
|
|
|
|
void PatternRewriter::replaceOpWithResultsOfAnotherOp(
|
2019-03-27 08:05:09 +08:00
|
|
|
Operation *op, Operation *newOp, ArrayRef<Value *> valuesToRemoveIfDead) {
|
2018-11-24 23:40:55 +08:00
|
|
|
assert(op->getNumResults() == newOp->getNumResults() &&
|
|
|
|
"replacement op doesn't match results of original op");
|
|
|
|
if (op->getNumResults() == 1)
|
|
|
|
return replaceOp(op, newOp->getResult(0), valuesToRemoveIfDead);
|
|
|
|
|
2018-12-28 06:35:10 +08:00
|
|
|
SmallVector<Value *, 8> newResults(newOp->getResults().begin(),
|
|
|
|
newOp->getResults().end());
|
2018-11-24 23:40:55 +08:00
|
|
|
return replaceOp(op, newResults, valuesToRemoveIfDead);
|
2018-10-24 01:12:00 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:33:18 +08:00
|
|
|
/// Move the blocks that belong to "region" before the given position in
|
|
|
|
/// another region. The two regions must be different. The caller is in
|
|
|
|
/// charge to update create the operation transferring the control flow to the
|
|
|
|
/// region and pass it the correct block arguments.
|
2019-06-21 18:26:39 +08:00
|
|
|
void PatternRewriter::inlineRegionBefore(Region ®ion, Region &parent,
|
2019-06-11 23:33:18 +08:00
|
|
|
Region::iterator before) {
|
2019-06-21 18:26:39 +08:00
|
|
|
parent.getBlocks().splice(before, region.getBlocks());
|
|
|
|
}
|
|
|
|
void PatternRewriter::inlineRegionBefore(Region ®ion, Block *before) {
|
|
|
|
inlineRegionBefore(region, *before->getParent(), before->getIterator());
|
2019-06-11 23:33:18 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:12:00 +08:00
|
|
|
/// This method is used as the final notification hook for patterns that end
|
|
|
|
/// up modifying the pattern root in place, by changing its operands. This is
|
2019-03-27 08:05:09 +08:00
|
|
|
/// a minor efficiency win (it avoids creating a new operation and removing
|
2018-10-24 01:12:00 +08:00
|
|
|
/// the old one) but also often allows simpler code in the client.
|
|
|
|
///
|
|
|
|
/// The opsToRemoveIfDead list is an optional list of nodes that the rewriter
|
|
|
|
/// should remove if they are dead at this point.
|
|
|
|
///
|
|
|
|
void PatternRewriter::updatedRootInPlace(
|
2019-03-27 08:05:09 +08:00
|
|
|
Operation *op, ArrayRef<Value *> valuesToRemoveIfDead) {
|
2018-10-24 01:12:00 +08:00
|
|
|
// Notify the rewriter subclass that we're about to replace this root.
|
|
|
|
notifyRootUpdated(op);
|
|
|
|
|
2018-11-24 23:40:55 +08:00
|
|
|
// TODO: Process the valuesToRemoveIfDead list, removing things and calling
|
|
|
|
// the notifyOperationRemoved hook in the process.
|
2018-10-16 13:59:31 +08:00
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternMatcher implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-26 02:53:03 +08:00
|
|
|
RewritePatternMatcher::RewritePatternMatcher(
|
2019-05-18 13:21:13 +08:00
|
|
|
OwningRewritePatternList &&patterns)
|
|
|
|
: patterns(std::move(patterns)) {
|
2019-03-26 02:53:03 +08:00
|
|
|
// Sort the patterns by benefit to simplify the matching logic.
|
|
|
|
std::stable_sort(this->patterns.begin(), this->patterns.end(),
|
|
|
|
[](const std::unique_ptr<RewritePattern> &l,
|
|
|
|
const std::unique_ptr<RewritePattern> &r) {
|
|
|
|
return r->getBenefit() < l->getBenefit();
|
|
|
|
});
|
|
|
|
}
|
2018-10-16 13:59:31 +08:00
|
|
|
|
2019-03-26 02:53:03 +08:00
|
|
|
/// Try to match the given operation to a pattern and rewrite it.
|
2019-05-18 13:21:13 +08:00
|
|
|
bool RewritePatternMatcher::matchAndRewrite(Operation *op,
|
|
|
|
PatternRewriter &rewriter) {
|
2018-10-26 04:11:06 +08:00
|
|
|
for (auto &pattern : patterns) {
|
2019-03-26 02:53:03 +08:00
|
|
|
// Ignore patterns that are for the wrong root or are impossible to match.
|
|
|
|
if (pattern->getRootKind() != op->getName() ||
|
|
|
|
pattern->getBenefit().isImpossibleToMatch())
|
2018-10-16 13:59:31 +08:00
|
|
|
continue;
|
|
|
|
|
2019-03-26 02:53:03 +08:00
|
|
|
// Try to match and rewrite this pattern. The patterns are sorted by
|
|
|
|
// benefit, so if we match we can immediately rewrite and return.
|
|
|
|
if (pattern->matchAndRewrite(op, rewriter))
|
2019-04-23 07:06:09 +08:00
|
|
|
return true;
|
2018-10-16 13:59:31 +08:00
|
|
|
}
|
2019-04-23 07:06:09 +08:00
|
|
|
return false;
|
2018-10-16 13:59:31 +08:00
|
|
|
}
|