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"
|
[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;
|
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternBenefit
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-16 13:59:31 +08:00
|
|
|
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 {
|
2020-06-19 04:58:17 +08:00
|
|
|
assert(!isImpossibleToMatch() && "Pattern doesn't match");
|
2018-10-16 13:59:31 +08:00
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-27 08:23:41 +08:00
|
|
|
// Pattern
|
2018-10-22 10:03:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-12 07:56:49 +08:00
|
|
|
Pattern::Pattern(StringRef rootName, PatternBenefit benefit,
|
|
|
|
MLIRContext *context)
|
|
|
|
: rootKind(OperationName(rootName, context)), benefit(benefit) {}
|
2020-10-27 08:23:41 +08:00
|
|
|
Pattern::Pattern(PatternBenefit benefit, MatchAnyOpTypeTag tag)
|
2020-06-19 04:58:25 +08:00
|
|
|
: benefit(benefit) {}
|
2020-10-27 08:23:41 +08:00
|
|
|
Pattern::Pattern(StringRef rootName, ArrayRef<StringRef> generatedNames,
|
|
|
|
PatternBenefit benefit, MLIRContext *context)
|
2019-05-25 10:35:23 +08:00
|
|
|
: 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);
|
|
|
|
});
|
|
|
|
}
|
2020-10-27 08:23:41 +08:00
|
|
|
Pattern::Pattern(ArrayRef<StringRef> generatedNames, PatternBenefit benefit,
|
|
|
|
MLIRContext *context, MatchAnyOpTypeTag tag)
|
2020-06-19 04:58:25 +08:00
|
|
|
: Pattern(benefit, tag) {
|
|
|
|
generatedOps.reserve(generatedNames.size());
|
|
|
|
std::transform(generatedNames.begin(), generatedNames.end(),
|
|
|
|
std::back_inserter(generatedOps), [context](StringRef name) {
|
|
|
|
return OperationName(name, context);
|
|
|
|
});
|
|
|
|
}
|
2019-05-25 10:35:23 +08:00
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RewritePattern
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void RewritePattern::rewrite(Operation *op, PatternRewriter &rewriter) const {
|
|
|
|
llvm_unreachable("need to implement either matchAndRewrite or one of the "
|
|
|
|
"rewrite functions!");
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult RewritePattern::match(Operation *op) const {
|
|
|
|
llvm_unreachable("need to implement either match or matchAndRewrite!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Out-of-line vtable anchor.
|
|
|
|
void RewritePattern::anchor() {}
|
|
|
|
|
[mlir][PDL] Add support for PDL bytecode and expose PDL support to OwningRewritePatternList
PDL patterns are now supported via a new `PDLPatternModule` class. This class contains a ModuleOp with the pdl::PatternOp operations representing the patterns, as well as a collection of registered C++ functions for native constraints/creations/rewrites/etc. that may be invoked via the pdl patterns. Instances of this class are added to an OwningRewritePatternList in the same fashion as C++ RewritePatterns, i.e. via the `insert` method.
The PDL bytecode is an in-memory representation of the PDL interpreter dialect that can be efficiently interpreted/executed. The representation of the bytecode boils down to a code array(for opcodes/memory locations/etc) and a memory buffer(for storing attributes/operations/values/any other data necessary). The bytecode operations are effectively a 1-1 mapping to the PDLInterp dialect operations, with a few exceptions in cases where the in-memory representation of the bytecode can be more efficient than the MLIR representation. For example, a generic `AreEqual` bytecode op can be used to represent AreEqualOp, CheckAttributeOp, and CheckTypeOp.
The execution of the bytecode is split into two phases: matching and rewriting. When matching, all of the matched patterns are collected to avoid the overhead of re-running parts of the matcher. These matched patterns are then considered alongside the native C++ patterns, which rewrite immediately in-place via `RewritePattern::matchAndRewrite`, for the given root operation. When a PDL pattern is matched and has the highest benefit, it is passed back to the bytecode to execute its rewriter.
Differential Revision: https://reviews.llvm.org/D89107
2020-12-02 06:30:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PDLValue
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PDLValue::print(raw_ostream &os) {
|
|
|
|
if (!impl) {
|
|
|
|
os << "<Null-PDLValue>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Value val = impl.dyn_cast<Value>()) {
|
|
|
|
os << val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AttrOpTypeImplT aotImpl = impl.get<AttrOpTypeImplT>();
|
|
|
|
if (Attribute attr = aotImpl.dyn_cast<Attribute>())
|
|
|
|
os << attr;
|
|
|
|
else if (Operation *op = aotImpl.dyn_cast<Operation *>())
|
|
|
|
os << *op;
|
|
|
|
else
|
|
|
|
os << aotImpl.get<Type>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PDLPatternModule
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void PDLPatternModule::mergeIn(PDLPatternModule &&other) {
|
|
|
|
// Ignore the other module if it has no patterns.
|
|
|
|
if (!other.pdlModule)
|
|
|
|
return;
|
|
|
|
// Steal the other state if we have no patterns.
|
|
|
|
if (!pdlModule) {
|
|
|
|
constraintFunctions = std::move(other.constraintFunctions);
|
|
|
|
createFunctions = std::move(other.createFunctions);
|
|
|
|
rewriteFunctions = std::move(other.rewriteFunctions);
|
|
|
|
pdlModule = std::move(other.pdlModule);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Steal the functions of the other module.
|
|
|
|
for (auto &it : constraintFunctions)
|
|
|
|
registerConstraintFunction(it.first(), std::move(it.second));
|
|
|
|
for (auto &it : createFunctions)
|
|
|
|
registerCreateFunction(it.first(), std::move(it.second));
|
|
|
|
for (auto &it : rewriteFunctions)
|
|
|
|
registerRewriteFunction(it.first(), std::move(it.second));
|
|
|
|
|
|
|
|
// Merge the pattern operations from the other module into this one.
|
|
|
|
Block *block = pdlModule->getBody();
|
|
|
|
block->getTerminator()->erase();
|
|
|
|
block->getOperations().splice(block->end(),
|
|
|
|
other.pdlModule->getBody()->getOperations());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Function Registry
|
|
|
|
|
|
|
|
void PDLPatternModule::registerConstraintFunction(
|
|
|
|
StringRef name, PDLConstraintFunction constraintFn) {
|
|
|
|
auto it = constraintFunctions.try_emplace(name, std::move(constraintFn));
|
|
|
|
(void)it;
|
|
|
|
assert(it.second &&
|
|
|
|
"constraint with the given name has already been registered");
|
|
|
|
}
|
|
|
|
void PDLPatternModule::registerCreateFunction(StringRef name,
|
|
|
|
PDLCreateFunction createFn) {
|
|
|
|
auto it = createFunctions.try_emplace(name, std::move(createFn));
|
|
|
|
(void)it;
|
|
|
|
assert(it.second && "native create function with the given name has "
|
|
|
|
"already been registered");
|
|
|
|
}
|
|
|
|
void PDLPatternModule::registerRewriteFunction(StringRef name,
|
|
|
|
PDLRewriteFunction rewriteFn) {
|
|
|
|
auto it = rewriteFunctions.try_emplace(name, std::move(rewriteFn));
|
|
|
|
(void)it;
|
|
|
|
assert(it.second && "native rewrite function with the given name has "
|
|
|
|
"already been registered");
|
|
|
|
}
|
|
|
|
|
2020-10-27 08:23:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternRewriter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-20 07:07:42 +08:00
|
|
|
// Merge the operations of block 'source' before the operation 'op'. Source
|
|
|
|
// block should not have existing predecessors or successors.
|
|
|
|
void PatternRewriter::mergeBlockBefore(Block *source, Operation *op,
|
|
|
|
ValueRange argValues) {
|
|
|
|
assert(source->hasNoPredecessors() &&
|
|
|
|
"expected 'source' to have no predecessors");
|
|
|
|
assert(source->hasNoSuccessors() &&
|
|
|
|
"expected 'source' to have no successors");
|
|
|
|
|
2020-10-29 03:03:15 +08:00
|
|
|
// Split the block containing 'op' into two, one containing all operations
|
2020-08-20 07:07:42 +08:00
|
|
|
// before 'op' (prologue) and another (epilogue) containing 'op' and all
|
|
|
|
// operations after it.
|
|
|
|
Block *prologue = op->getBlock();
|
|
|
|
Block *epilogue = splitBlock(prologue, op->getIterator());
|
|
|
|
|
|
|
|
// Merge the source block at the end of the prologue.
|
|
|
|
mergeBlocks(source, prologue, argValues);
|
|
|
|
|
|
|
|
// Merge the epilogue at the end the prologue.
|
|
|
|
mergeBlocks(epilogue, prologue);
|
|
|
|
}
|
|
|
|
|
2019-11-06 03:57:03 +08:00
|
|
|
/// 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());
|
|
|
|
}
|
|
|
|
|