2018-10-16 13:59:31 +08:00
|
|
|
//===- PatternMatch.cpp - Base classes for pattern match ------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-16 13:59:31 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-16 13:59:31 +08:00
|
|
|
|
2018-10-26 07:44:04 +08:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-10-09 06:44:34 +08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.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"
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
|
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, PatternRewriter &rewriter) const {
|
2019-03-26 02:53:03 +08:00
|
|
|
llvm_unreachable("need to implement either matchAndRewrite or one of the "
|
|
|
|
"rewrite functions!");
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:07:55 +08:00
|
|
|
LogicalResult 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
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
/// values.
|
|
|
|
void PatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
|
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");
|
2019-08-08 04:48:19 +08:00
|
|
|
op->replaceAllUsesWith(newValues);
|
2018-11-23 17:10:26 +08:00
|
|
|
|
|
|
|
notifyOperationRemoved(op);
|
|
|
|
op->erase();
|
|
|
|
}
|
|
|
|
|
2019-10-17 00:50:28 +08:00
|
|
|
/// This method erases an operation that is known to have no uses. The uses of
|
|
|
|
/// the given operation *must* be known to be dead.
|
|
|
|
void PatternRewriter::eraseOp(Operation *op) {
|
|
|
|
assert(op->use_empty() && "expected 'op' to have no uses");
|
|
|
|
notifyOperationRemoved(op);
|
|
|
|
op->erase();
|
|
|
|
}
|
|
|
|
|
2020-03-31 03:45:40 +08:00
|
|
|
void PatternRewriter::eraseBlock(Block *block) {
|
|
|
|
for (auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) {
|
|
|
|
assert(op.use_empty() && "expected 'op' to have no uses");
|
|
|
|
eraseOp(&op);
|
|
|
|
}
|
|
|
|
block->erase();
|
|
|
|
}
|
|
|
|
|
2019-11-06 03:57:03 +08:00
|
|
|
/// Merge the operations of block 'source' into the end of block 'dest'.
|
|
|
|
/// 'source's predecessors must be empty or only contain 'dest`.
|
|
|
|
/// 'argValues' is used to replace the block arguments of 'source' after
|
|
|
|
/// merging.
|
|
|
|
void PatternRewriter::mergeBlocks(Block *source, Block *dest,
|
2019-12-07 12:06:48 +08:00
|
|
|
ValueRange argValues) {
|
2019-11-06 03:57:03 +08:00
|
|
|
assert(llvm::all_of(source->getPredecessors(),
|
|
|
|
[dest](Block *succ) { return succ == dest; }) &&
|
|
|
|
"expected 'source' to have no predecessors or only 'dest'");
|
|
|
|
assert(argValues.size() == source->getNumArguments() &&
|
|
|
|
"incorrect # of argument replacement values");
|
|
|
|
|
|
|
|
// Replace all of the successor arguments with the provided values.
|
|
|
|
for (auto it : llvm::zip(source->getArguments(), argValues))
|
2020-01-12 00:54:04 +08:00
|
|
|
std::get<0>(it).replaceAllUsesWith(std::get<1>(it));
|
2019-11-06 03:57:03 +08:00
|
|
|
|
|
|
|
// Splice the operations of the 'source' block into the 'dest' block and erase
|
|
|
|
// it.
|
|
|
|
dest->getOperations().splice(dest->end(), source->getOperations());
|
|
|
|
source->dropAllUses();
|
|
|
|
source->erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split the operations starting at "before" (inclusive) out of the given
|
|
|
|
/// block into a new block, and return it.
|
|
|
|
Block *PatternRewriter::splitBlock(Block *block, Block::iterator before) {
|
|
|
|
return block->splitBlock(before);
|
|
|
|
}
|
|
|
|
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
/// 'op' and 'newOp' are known to have the same number of results, replace the
|
2018-11-24 23:40:55 +08:00
|
|
|
/// uses of op with uses of newOp
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
void PatternRewriter::replaceOpWithResultsOfAnotherOp(Operation *op,
|
|
|
|
Operation *newOp) {
|
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)
|
[mlir] Remove 'valuesToRemoveIfDead' from PatternRewriter API
Summary:
Remove 'valuesToRemoveIfDead' from PatternRewriter API. The removal
functionality wasn't implemented and we decided [1] not to implement it in
favor of having more powerful DCE approaches.
[1] https://github.com/tensorflow/mlir/pull/212
Reviewers: rriddle, bondhugula
Reviewed By: rriddle
Subscribers: liufengdb, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72545
2020-01-28 05:13:20 +08:00
|
|
|
return replaceOp(op, newOp->getResult(0));
|
|
|
|
return replaceOp(op, newOp->getResults());
|
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
|
|
|
}
|
|
|
|
|
2019-10-09 06:44:34 +08:00
|
|
|
/// Clone the blocks that belong to "region" before the given position in
|
|
|
|
/// another region "parent". The two regions must be different. The caller is
|
|
|
|
/// responsible for creating or updating the operation transferring flow of
|
|
|
|
/// control to the region and passing it the correct block arguments.
|
|
|
|
void PatternRewriter::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before,
|
|
|
|
BlockAndValueMapping &mapping) {
|
|
|
|
region.cloneInto(&parent, before, mapping);
|
|
|
|
}
|
|
|
|
void PatternRewriter::cloneRegionBefore(Region ®ion, Region &parent,
|
|
|
|
Region::iterator before) {
|
|
|
|
BlockAndValueMapping mapping;
|
|
|
|
cloneRegionBefore(region, parent, before, mapping);
|
|
|
|
}
|
|
|
|
void PatternRewriter::cloneRegionBefore(Region ®ion, Block *before) {
|
|
|
|
cloneRegionBefore(region, *before->getParent(), before->getIterator());
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternMatcher implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-26 02:53:03 +08:00
|
|
|
RewritePatternMatcher::RewritePatternMatcher(
|
2019-08-12 09:33:42 +08:00
|
|
|
const OwningRewritePatternList &patterns) {
|
2019-08-06 09:37:56 +08:00
|
|
|
for (auto &pattern : patterns)
|
|
|
|
this->patterns.push_back(pattern.get());
|
|
|
|
|
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(),
|
2019-08-06 09:37:56 +08:00
|
|
|
[](RewritePattern *l, RewritePattern *r) {
|
2019-03-26 02:53:03 +08:00
|
|
|
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) {
|
2019-08-06 09:37:56 +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.
|
2020-03-17 08:55:32 +08:00
|
|
|
if (succeeded(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
|
|
|
}
|